Page 1 of 2

957 - Popes

Posted: Thu Jan 25, 2007 12:54 pm
by aha2007
It's seems to me the problem of maximum sum of continuous sub sequences of length m.
I know a linear algorithm when length m is not specified , but don't know how to extend it for
specified length m. Can any one kind enough to give me a linear time algorithm for this problem , or any link describing that algorithm.

Posted: Sat Feb 17, 2007 11:09 am
by sclo
It is even easier when m is fixed.
Let a be the number of popes elected in the year i.
Let f[0]=a[0], and f = f[i-1]+a for i>0, so f is a cumulative sum.
Consider the value of f[i+m-1]-f[i-1] for all i.

As for the algorithm for maximum consecutive sum, it is trivial to solve when all the elements of the array is non-negative.

Posted: Thu Jun 28, 2007 5:36 pm
by helloneo
Hello..~
Can anybody tell me what is wrong with my code..?
I'm getting WAs.. but I have no idea..

check is the number of occurences of 'i' and..
sum is the cumulative sum up to check

Code: Select all

code removed
Thanks.. :-)

Posted: Thu Jun 28, 2007 6:01 pm
by Jan
Try the cases.

Input:

Code: Select all

11
20
20 numbers given in sample :)
12
20
20 numbers given in sample :)
17
20
20 numbers given in sample :)
Output:

Code: Select all

11 12 21
11 12 21
13 6 21
Hope the weird input set helps. :wink: .

Posted: Thu Jun 28, 2007 6:26 pm
by helloneo
Thanks a lot..~
I got AC.. :-)


PS. very funny input.. :-) It took some time to get the point.. :cry:

957-Popes

Posted: Sat Sep 18, 2010 9:29 pm
by Symon
I can't realize why I'm getting WA.
Please give me some input output.

Code: Select all

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    long int y,p,i,j,a[100005],cnt[1][3],max,increase;
    while(scanf("%ld%ld",&y,&p)!=EOF)
    {
        max=-1;
        increase=-1;
        for(i=0;i<p;i++)
        {
            scanf("%ld",&a[i]);
            if(i!=0&&a[i]<a[i-1])
            {
                increase=0;
            }
        }
        if(increase==0)
        {
            sort(a,a+p);
        }
        for(i=0;i<p;i++)
        {
            cnt[0][2]=0;
            for(j=i;j<p&&a[j]<=a[i]+(y-1);j++)
            {
                cnt[0][2]++;
            }
            if(max<cnt[0][2])
            {
                max=cnt[0][2];
                cnt[0][0]=a[i];
                cnt[0][1]=a[j-1];
            }
        }        
        printf("%ld %ld %ld\n\n",max,cnt[0][0],cnt[0][1]);
    }
    return 0;
}]
thanks...

Re: 957 - Popes

Posted: Fri Sep 13, 2013 9:00 am
by Hammer Engineer
Why RE ? Please help

Code: Select all

#include <iostream>
#include<vector>
#include<cstdio>
using namespace std;

int main()
{
    freopen("input.txt", "r", stdin);

    int span;

    //scanf("%d",&span);
     while ( scanf("%d",&span) != EOF ) {
          vector<int> a(100001);
          vector<int> F(100001);

          int Y , year;
          a[0] = F[0] = 0;



        scanf("%d",&Y);

        span -=1;
        for(int i=1; i<=Y; i++)
        {
            scanf("%d",&year);
            a[year] +=1;
        }

        for(int i=1;i<a.size();i++)
        {
            F[i] = (a[i] + F[i-1]);
        }

        int max=0,from , sum;
        for(int i=1;i<F.size();i++)
        {

            if(i+span < F.size())
                sum = F[i+span] - F[i-1];
            else
                sum = F[F.size()-1] - F[i-1];

            if(sum > max)
            {
                max=sum;
                from = i;
            }
        }



        printf("%d %d %d\n", max , from , from+span);

     }
    return 0;
}

Re: 957 - Popes

Posted: Fri Sep 13, 2013 9:23 pm
by brianfry713
Don't read from a file.

Re: 957 - Popes

Posted: Mon Sep 16, 2013 1:06 am
by Hammer Engineer
@brainfry713 When i'm submitting code i'm removing freopen() [ sorry I did't mension that in code].
Is there any other problem that can end with a RE ?

Re: 957 - Popes

Posted: Tue Sep 17, 2013 10:04 pm
by brianfry713
year L <= 1,000,000

Re: 957 - Popes

Posted: Sun Oct 05, 2014 11:07 pm
by omar.fmc
Hello all,
I'm getting RE and I can't find out the reason of that.
Here's my code:

Code: Select all

Accepted 
Appreciate your help :-)


EDIT:
For those who are getting RE notice that P<=100000 in the judge's input (not 5000 as in the problem statement).

Re: 957 - Popes

Posted: Tue Oct 07, 2014 10:50 pm
by brianfry713
There is a case in the judge's input where P > 5000, but not one where P > 100000.
http://online-judge.uva.es/board/viewto ... &hilit=957

Re: 957 - Popes

Posted: Tue Oct 07, 2014 10:58 pm
by omar.fmc
brianfry713 wrote:There is a case in the judge's input where P > 5000, but not one where P > 100000.
http://online-judge.uva.es/board/viewto ... &hilit=957
Thanks a lot. Got Accepted :-)

Re:

Posted: Sat Oct 18, 2014 7:28 pm
by morkoh
Jan wrote:Try the cases.

Input:

Code: Select all

11
20
20 numbers given in sample :)
12
20
20 numbers given in sample :)
17
20
20 numbers given in sample :)
Output:

Code: Select all

11 12 21
11 12 21
13 6 21
Hope the weird input set helps. :wink: .
Could someone tell me how is this correct?
From years 11-21 we had 12 popes.
There was a pope elected in year 8 and he was alive up until year 12 when a new one was elected.
pope 5 was from 8-11, pope 6 in 12, pope 7 and 8 | 13-14, pope 9 15, pope 10 16, pope 11 in 17, in year 21 pope 16.
So, there were popes from 5 to 16 on a period of 11 years. Pope 5 being in year 11, and pope 16 in year 21.
21 - 11 + 1 = 11 years...
Shouldn't the answer be 12 8 21, instead of 11 12 21?

It is obvious that the text of the task is completely unclear. There's no limit between difference of election years of first and last pope.
And somehow all of the answers automatically assume that last-first+1 <= Y.

Re: 957 - Popes

Posted: Tue Oct 21, 2014 8:13 pm
by brianfry713
Input:

Code: Select all

11
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31

12
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31

17
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
Correct output:

Code: Select all

11 12 21
11 12 21
13 6 21
From years 12 to 21 there were 11 popes elected. There was no pope elected in year 11. We are only interested in the years of election, not the number of popes in office during the time frame. The problem statement is misleading.