Page 3 of 3

Re: 10374 - Election

Posted: Wed Apr 24, 2013 11:46 am
by kier.guevara
My code now got AC. I changed the cin with gets and scanf. I don't understand how it became AC. Can you explain it?

Re: 10374 - Election

Posted: Wed Apr 24, 2013 11:45 pm
by brianfry713
I didn't look at your code before you deleted it, were you using getline()?.

uva 10374

Posted: Wed May 29, 2013 11:37 pm
by mahbub2111
getting wa please help
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k,l,n,m,o,p,d[1000],h,ma,s;
char r[1000][1000];
char b[1000][1000];
char c[1000],f[1000];
scanf("%d",&m);

for(o=1; o<=m; o++)
{printf("\n");
scanf("%d",&p);
while(getchar()!='\n');
for(n=0; n<p; n++)
{
gets(r[n]);
gets(b[n]);
}
for(j=0; j<p; j++)
{
d[j]=0;

}



scanf("%d",&l);
while(getchar()!='\n');
for(k=0; k<l; k++)
{
gets(c);
for(j=0; j<p; j++)
{
if(!strcmp(c,r[j]))
{
d[j]++;
}

}
}
ma=0;
s=0;
for(i=0; i<p; i++)
{
if(ma<d)
{
ma=d;
h=i;
}



}
for(i=0; i<p; i++)
{
if(d==ma)s++;

}
if(o>1&&o<=m)printf("\n");
if(s>1)printf("tie\n");
else puts(b[h]);
if(o<m)
printf("\n");
}







return 0;
}

Re: uva 10374

Posted: Wed May 29, 2013 11:51 pm
by brianfry713
The outputs of two consecutive cases will be separated by a blank line.

Re: 10374 - Election

Posted: Sun Jul 21, 2013 3:44 pm
by Loading...
Please help me . getting WA . I know it has no tricky cases . but still getting WA

Code: Select all


#include <stdio.h>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

#define pf printf
#define sf scanf
#define maxm 10000
#define eps 1e-10
#define mem(a,b) memset(a,b,sizeof(a))

char st_can[30][100],st_party[30][100],str[100];
int res[50],a,b;

void search(){
    int i,j,k;
    for(i=0;i<a;i++){
        if(strcmp(str,st_can[i])==0){
            res[i]++;
            return;
        }
    }
}

int main(){

    int i,j,k,n,t,kase=0; char ch;
    sf("%d",&t);
    for(k=1;k<=t;k++){
        getchar();
        sf("%d",&a);
        getchar();
        for(i=0;i<a;i++){
            gets(st_can[i]);
            gets(st_party[i]);
        }

        sf("%d",&b);
        getchar();
        for(i=0;i<b;i++){
            gets(str);
            search();
        }

        int tag=0,max=-1,win;
        for(i=0;i<a;i++){
            if(res[i]>max){
                max=res[i];
                win=i;
                tag=0;
            }
            else if(res[i]==max){
                tag=1;
                max=-1;
            }
        }

        if(tag==1) pf("tie\n");
        else{
            pf("%s\n",st_party[win]);
        }

        mem(st_party,0);
        mem(st_can,0);
        mem(res,0);

        if(k!=t) pf("\n");

    }



    return 0;

}



Re: uva 10374

Posted: Sun Jul 21, 2013 3:47 pm
by shuvokr
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
First cheak this two things carefully .
Then your code don't give the correct answer, for understand...

Sample input

Code: Select all

4

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

For this input your code give the wrong answer
Ac output

Code: Select all

Rhinoceros

Rhinoceros

Rhinoceros

Rhinoceros


Re: uva 10374

Posted: Sun Jul 21, 2013 5:30 pm
by shuvokr
Munna your code give the wrong answer for this kind of kag
Sample input:

Code: Select all

1

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Jane Doe
The output should be

Code: Select all

tie
:D

Re: 10374 - Election

Posted: Tue Jul 23, 2013 1:05 am
by brianfry713
Don't use a single getchar() and count on it to be a newline.

Re: 10374 - Election

Posted: Sun Aug 11, 2013 6:21 pm
by brianfry713
Input:

Code: Select all

1

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
1
asdf
Output should be tie

Re: 10374 - Election

Posted: Thu Mar 20, 2014 1:07 pm
by uDebug
After struggling with this problem for a good few hours, I'd like to share my findings.

First, off, these comments, in my mind, are not really at the crux of the problem and are, actually, a bit "distracting"
Probably there is something weird about the judge input. I took all inputs with gets,removed trailing and leading spaces,and finally got ac. ""No lines contain leading or trailing blanks. "" is not correct.
F*** this stupid problem. Not only the input contains leading and trailing spaces, there can be "futile" blank lines throughout the judge data which must be ignored in order to get Accepted. The judge data is SHIT...I request the admins to fix this. This kind of problem teaches you nothing but only piss you off/
While there may be leading / trailing spaces in the lines that contain numbers (which, I agree, is not what the problem states), there doesn't seem to be anything weird or atypical in regards to spaces on the balance of the lines (the ones that contain the names). In other words, the following outline code, worked perfectly in reading the input

Code: Select all

#include<bits/stdc++.h>

using namespace std;

int main() {

	/* Variables used in the program */
	int candidates, i, j, names, testCases;

   string candidate, party;
	
	/* Read in the number of test cases */
	scanf("%d\n", &testCases);
	
	/* Process the test cases */
	for(i = 0; i < testCases; i++) {
		
		/* Print a newline between cases */
		if(i != 0) {
			printf("\n");
		}
		
		/* Read in the number of candidates */
		scanf("%d\n", &candidates);
		
		/* Process the candidates */
		for(j = 0; j < candidates; j++) {
			
          /* Read in the candidate and the party they belong to */ 
          getline(cin, candidate); getline(cin, party);
	
			// Do stuff	
				
		}
		
		/* Read in the number of names */
		scanf("%d\n", &names);
		
		/* Process the names */
		for(j = 0; j < names; j++) {

          /* Get the candidate name */		
          getline(cin, candidate);

			// Do stuff
		
		}
	}
	return 0;
}
There's nothing fancy in regards to reading the input.

However, the line in the problem statement that was not necessarily clear to me is the following
No candidate name is repeated and no party name is repeated in the input.
What's being said here is that

No pair of candidate / party names is repeated.

So for example, this is probably not in the judge's input when the candiate / party names are being listed

Code: Select all

Sara Shahida
Jaguar
Sara Shahida
Jaguar
Because the candidate / party name pair, Sara Shahida / Jaguar, is repeated.

However, there's indeed a case in the input where something like this exists

Code: Select all

Sara Shahida
Jaguar
Jacob Holger
Jaguar
Note that both Sara Shahida and Jacob Holger belong to the Jaguar party (and that's fine since no candidate party / name pair is repeated). So, it's important, your code be able to handle this test case. Think of your approach in terms of individual votes - rather than party votes.

Apart from the great test cases already listed previously on this thread, I'd like to add the following ones. I got an AC only after my code could successfully handle these.

Input:

Code: Select all

2

2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
3
Jacob Holger
Sara Shahida
Jacob Holger

2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
2
Jacob Holger
Sara Shahida
AC Output:

Code: Select all

Jaguar

tie

Re: 10374 - Election

Posted: Sun Apr 20, 2014 6:12 pm
by mamun4122
why runtime error :-?

Code: Select all

//got AC

Re: 10374 - Election

Posted: Mon Apr 21, 2014 6:54 am
by uDebug
mamun4122 wrote:
why runtime error :-?
You know, the very least you could do before you ask for help is to run your code on the test cases provided on the thread. I mean they are there for a reason.

Try running your code in the post above yours. What happens?

Re: 10374 - Election

Posted: Thu Aug 21, 2014 10:16 am
by rafid059
I got a lot of runtime errors only to know how judge's input works in this problem. This is a really easy problem but the judge makes it difficult. To all those people who are getting runtime error, there are a few tricks.
There ARE leading and trailing spaces in "number"(integer n and m.. see the problem description) inputs
There ARE extra blank lines between "CONSECUTIVE" cases. It means that before the input of "integer n" there can be one or more than one blanks in the input.
There are no extra blanks before or after the "integer m"
If you handle these and if your logics are correct, then you will surely get Accepted. If you have other opinions, then please do share. :)

Re: 10374 - Election

Posted: Fri Mar 06, 2015 11:15 am
by moudud99
Can anyone help me finding my bug? I am getting RE.
Thanks in advance.

Code: Select all

#include<bits/stdc++.h>
#define FRU freopen("out.txt","w",stdout)
#define FRO freopen("in.txt","r",stdin)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(ara,n) memset(ara,n,sizeof ara)
#define loop(i,j,n) for(i=j;i<n;i++)
#define rloop(i,j,n) for(i=n;i>=j;i--)
#define INF 2147483647
//const int row[]={-1, -1, -1,  0,  0,  1,  1,  1};  // Kings Move
//const int col[]={-1,  0,  1, -1,  1, -1,  0,  1};  // Kings Move
//const int row[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int col[]={-1,  1, -2,  2, -2,  2, -1,  1};  // Knights Move
//const int row[]={-1,0,0,1,0};
//const int col[]={0,-1,1,0,0};
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b)
{
    return ((a*b)/gcd(a,b));
}

using namespace std;

int main()
{
    //FRO;
//FRU;
//std::ios_base::sync_with_stdio(false);
    int a,b,c,i,j,k,tc,t=0;
    int n,m,cnt=0;
    map<string,string>mmp1;
    map<string,int>mmp2;
    string s,s1;

    vector<string>vec;
    scanf("%d",&tc);
    //getchar();
    while(tc--)
    {
        mmp1.clear();
        mmp2.clear();
        vec.clear();
        pair<int,string>pp[1001];
        s.clear();
        s1.clear();
        if(t)printf("\n");
        t++;
        scanf("%d",&n);
        //getchar();
        for(i=0; i<n; i++)
        {
            getline(cin,s);
            if(s[0]=='\0'|| s[0]==' ')
            {
                i--;
                continue;
            }
            getline(cin,s1);
            mmp1[s]=s1;
        }
        scanf("%d",&m);
        i=m;
        //getchar();
        while(i--)
        {
            getline(cin,s);
            if(s[0]=='\0'|| s[0]==' ')
            {
                //cout<<s<<endl;
                i++;
                continue;
            }
            if(mmp1.find(s)!=mmp1.end())
            {
                if(mmp2.find(s)==mmp2.end())vec.pb(s);
                mmp2[s]++;
            }
        }
        for(i=0; i<vec.size(); i++)pp[i]=mp(mmp2[vec[i]],vec[i]);
        int len=vec.size();
        sort(pp,pp+len);
        //printf("%d %d\n",pp[len-1].ff,pp[len-2].ff);
        if(len==1|| (m>1&& pp[len-1].ff!= pp[len-2].ff))cout<<mmp1[pp[len-1].ss]<<endl;
        else printf("tie\n");

    }
    return 0;
}

Re: 10374 - Election

Posted: Wed Feb 17, 2016 6:32 pm
by TryCatchMe
I got WA the first 3 submissions. The judges input is fine and uDebug even explains in an earlier post how to handle reading the i/o. Here is some input I created that pointed out some cases I overlooked. Once I solved this i/o correctly, I got Accepted with NO fancy tricks. In my Accepted program I read i/o using the following. (maybe not the perfect way to do it but it works and is plenty fast (I got Accepted in 0.000s) )

Code: Select all

#include <iostream>

using namespace std;

int main() {
	int ncases, np, nv, N = 0;
	string line, candidate, party;

	cin >> ncases;
	while (ncases--) {

        cin >> np; getline(cin , line);  /* drain newline after reading int */
		
        for (int i = 0; i < np; i++) {
            getline(cin , candidate); getline(cin , party);
            //process candidate and party
        }
        cin >> nv; getline(cin , line);  /* drain newline after reading int */
		
        for (int i = 0; i < nv; i++) {
            getline(cin , candidate);
			//process candidate
        }
		
		//process data
		//string theWinningCandidate = ...
		//bool thereIsATie = ...

        if (N++)
            cout << endl;
        if (thereIsATie)
            cout << "tie" << endl;
        else
            cout << theWinningCandidate << endl;
	}
	
    return 0;
}
Input

Code: Select all

9

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
John Smith
Marilyn Manson
Marilyn Manson
Jane Doe
John Smith
Marilyn Manson

3
Marilyn Manson
Rhinoceros
Jane Doe
Family Coalition
John Smith
independent
6
Brian Fry
Brian Fry
Brian Fry
Brian Fry
Brian Fry
Brian Fry

3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
6
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel

2
Jan
Jaguar
Brian Fry
Jaguar
2
Brian Fry
Jan

2
Sara Shahida
Jaguar
Jacob Holger
Jaguar
1
Brian Fry

3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
John Smith

3
stanley toles
Rhinoceros
jan guru
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
John Smith

3
Brian Fry
Rhinoceros
Sohel
Family Coalition
John Smith
independent
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
Brian Fry

3
Brian Fry
Rhinoceros
Sohel
Rhinoceros
John Smith
Rhinoceros
7
Brian Fry
Brian Fry
Brian Fry
Sohel
Sohel
Sohel
Sohel
Accepted Output

Code: Select all

Rhinoceros

tie

tie

tie

tie

tie

independent

Rhinoceros

Rhinoceros