10324 - Zeros and Ones

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Faithkeeper_Rangwan
New poster
Posts: 12
Joined: Sun Jul 07, 2013 7:32 pm

Re: 10324 - Zeros and Ones

Post by Faithkeeper_Rangwan »

I got TLE with this one

Code: Select all

[color=#40FF80]AC'd[/color]
does it is because the algorithm is too slow or the improper input handling (and the program stuck in the loop)?
Last edited by Faithkeeper_Rangwan on Fri May 23, 2014 1:50 pm, edited 1 time in total.
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10324 - Zeros and Ones

Post by uDebug »

Faithkeeper_Rangwan wrote:I got TLE with this one does it is because the algorithm is too slow or the improper input handling (and the program stuck in the loop)?
It might be because of all the "cin"s you have. Try replacing them with "scanf"s instead.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
Blief.S
New poster
Posts: 16
Joined: Wed Jun 18, 2014 9:03 pm

Re: 10324 - Zeros and Ones WHy WA

Post by Blief.S »

Getting WA...... Every i/o is correct.... Why WA plz help......

Code: Select all

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int n,i,j,cas=0,flag,mini,macs,k,l,siz;
    char str[1000001];
    char c[100];
    while(scanf("%s",&str)!=EOF){
            gets(c);
        if(c[0]=='\n')
                return 0;
        scanf("%d",&n);
        int flag[1000]={0};
        cas++;
        l=n;
        while(n--){

            scanf("%d %d",&i,&j);
            if(i>j){
                mini=j;
                macs=i;
            }
            else if(i<j){
                mini=i;
                macs=j;
            }
            else {
                flag[n]=1;
                continue;
            }
            for(k=mini;k<macs;k++){
                    if(str[k]==str[k+1]) flag[n]=1;
                    else {
                            flag[n]=0;
                            break;
                    }
                }
            }
            printf("Case %d:\n",cas);
            while(l--){
                if(flag[l]==1) printf("Yes\n");
            else printf("No\n");
            }

        }
    return 0;
}




I think i got WA for "new line" fact. plz help me to overcome.....i have tried many ways..........
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10324 - Zeros and Ones

Post by uDebug »

Blief.S wrote:Getting WA...... Every i/o is correct.... Why WA plz help......I think i got WA for "new line" fact. plz help me to overcome.....i have tried many ways..........
Change Line #9 to

Code: Select all

while(scanf("%s",str)!=EOF){
It's what you probably mean to do.

Also, you have both a variable and an array called "flag". Get rid of the variable since you're not using it. Incidentally, you're not using the variable named "siz", also.

Finally, it's probably a good idea to give some leeway on the array sizes. I know the problem states that the string of characters will 1,000,000 but you might want to make it 1,000,010 - just to be completely safe, you know.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10324 - Zeros and Ones

Post by brianfry713 »

Maybe there are more than 1000 queries.
Check input and AC output for thousands of problems on uDebug!
rafid059
New poster
Posts: 13
Joined: Thu Feb 27, 2014 6:35 pm

Re: 10324 - Zeros and Ones

Post by rafid059 »

I'm getting WA. I took the string as character by character and checked if there is any "change" in the string. I saved the index of the "change" and later compared with the pair values of i and j. All the sample i/o are correct but i'm getting WA.

One more thing. Will there be any i or j which will be greater than the length of the main string? Thanks in advance :)

Code: Select all

Got Accepted!  :D
Last edited by rafid059 on Tue Aug 05, 2014 1:42 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10324 - Zeros and Ones

Post by brianfry713 »

Instead of:
i = fmin(i, j);
j = fmax(i, j);
Try:
if(i > j)
swap(i, j);
Check input and AC output for thousands of problems on uDebug!
rafid059
New poster
Posts: 13
Joined: Thu Feb 27, 2014 6:35 pm

Re: 10324 - Zeros and Ones

Post by rafid059 »

brianfry713 wrote:Instead of:
i = fmin(i, j);
j = fmax(i, j);
Try:
if(i > j)
swap(i, j);
Thanks again, Guru! :D i'm new to c++. Can you please explain the difference between the two codes?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10324 - Zeros and Ones

Post by brianfry713 »

For one thing I would use min and max instead of fmin and fmax when working with ints.

if i <= j then this code would have no effect:
i = min(i, j);
j = max(i, j);

However if i > j then:
i = min(i, j);
//now i = j
j = max(i, j);
//Instead of j getting i's previous value which is overwritten by j, now both i and j equal j's previous value.
Check input and AC output for thousands of problems on uDebug!
gautamzero
New poster
Posts: 32
Joined: Fri Oct 10, 2014 1:10 pm
Location: Sylhet, Bangladesh

Re: 10324 - Zeros and Ones

Post by gautamzero »

why WA???
:(
this is my code:

Code: Select all

erased
Last edited by gautamzero on Wed Jan 07, 2015 1:13 pm, edited 3 times in total.
None but a fool is always right..
so don't be upset when u r wrong..
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10324 - Zeros and Ones

Post by lighted »

Check input in uDebug. http://www.udebug.com/UVa/10324
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
gautamzero
New poster
Posts: 32
Joined: Fri Oct 10, 2014 1:10 pm
Location: Sylhet, Bangladesh

Re: 10324 - Zeros and Ones

Post by gautamzero »

i have tested the I/O s given in the random inputs at udebug...
but still WA :(
None but a fool is always right..
so don't be upset when u r wrong..
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10324 - Zeros and Ones

Post by brianfry713 »

change line 21 to:
a = b;
Check input and AC output for thousands of problems on uDebug!
gautamzero
New poster
Posts: 32
Joined: Fri Oct 10, 2014 1:10 pm
Location: Sylhet, Bangladesh

Re: 10324 - Zeros and Ones

Post by gautamzero »

thnx..again :D
i'm just making silly mistake again&again... :oops:
None but a fool is always right..
so don't be upset when u r wrong..
mano129
New poster
Posts: 1
Joined: Mon Dec 14, 2015 6:39 am

Re: 10324 - Zeros and Ones

Post by mano129 »

I am getting wrong answer.pls help

Code: Select all


//10324 Zeros and Ones

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <sstream>
#include <set>
#include <climits>
#include <cstdio>
#include <string>
#include <fstream> 
using namespace std;

int main(void){

  	// //std::ios_base::sync_with_stdio(false);
  	// freopen("1.txt","r",stdin);
  	// freopen("2.txt","w",stdout);

	//string str;
	char strarray[1000010];
	int num;
	int i = 0;	

	while(scanf("%s",strarray) != EOF){
	cin >> num;
	cout << "Case " << ++i <<":"<<endl;
		// char strarray[1000010];
		// strcpy(strarray, str.c_str());
		int min,max;
		int a=0;
		char temp = 0;

		for (int i=0; i<num; i++){
			scanf("%d %d",&min,&max);
			//cin >> min >> max;
			if(min == max){
				cout << "Yes"<<endl;
			}
			else{
				int state=1;
				temp = strarray[min];
                                //If max position value greater than minimum
				if(max > min){
					for(a=min; a<=max; a++){
						if(strarray[a] != temp){
							cout << "No" << endl;
							state = 0;
							break;
						}
						else if(a==max && state!=0){
							cout << "Yes"<<endl;
							break;
						}
					}
				}
				else{
					for(a=max; a<=min; a++){
						if(strarray[a] != temp){
							cout << "No" << endl;
							state = 0;
							break;
						}
						
						else if(a==max && state!=0){
							cout << "Yes"<<endl;
							break;
						}
					}

				}
			}
			
		}
	}
	return 0;

 }
Post Reply

Return to “Volume 103 (10300-10399)”