UVA957 Popes【水题】

On the occasion of Pope John Paul II death, the American weekly magazine Time observed the largest number of Popes to be selected in a 100-year period was 28, from 867 (Adrian II) to 965 (John XIII). This is a very interesting piece of trivia, but it would be much better to have a program to compute that number for a period of any length, not necessarily 100 years. Furthermore, the Catholic Church being an eternal institution, as far as we can predict it, we want to make sure that our program will remain valid per omnia secula seculorum.
    Write a program that given the list of years in which each Pope was elected and a positive number Y , computes the largest number of Popes that were in office in a Y -year period, and the year of election for the first and last Popes in that period. Note that, given a year N, the Y -year period that starts in year N is the time interval from the first day of year N to the last day of year N + Y − 1. In case of a tie, i.e., if there is more that one Y -year period with the same largest number of Popes, your program should report only the most ancient one.
Input
The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
    The first line of the input contains a positive integer Y , the number of years of the period we are interested in. The second line contains another positive integer, the number of Popes, P. Each of the remaining P lines contains the year of election of a Pope, in chronological order. We know that P ≤ 100000 and also that the last year L in the file is such L ≤ 1000000, and that Y ≤ L.
Output
For each test case, write to the output contains a single line with three integers, separated by spaces: the largest number of Popes in a Y -year period, the year of election of the first Pope in that period and the year of election of the last Pope in the period.
Sample Input
5
20
1
2
3
6
8
12
13
13
15
16
17
18
19
20
20
21
25
26
30
31
Sample Output
6 16 20

问题链接UVA957 Popes
问题简述:给定任期Y,以及每个人当选的时间,问哪个时间段在职的人最多。
问题分析:水题不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA957 Popes */

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5;
int a[N];

int main()
{
    int y, p, cnt, maxc, lo, s, e;
    while(~scanf("%d%d", &y, &p)) {
        cnt = maxc = lo = 0;
        for(int i = 0; i < p; i++) {
            scanf("%d", &a[i]);
            cnt++;
            if(i) {
                while(a[i] - a[lo] >= y)
                    lo++, cnt--;
                if(cnt > maxc) {
                    maxc = cnt;
                    s = a[lo];
                    e = a[i];
                }
            }
        }

        printf("%d %d %d\n", maxc, s, e);
    }

    return 0;
}
发布了2289 篇原创文章 · 获赞 2373 · 访问量 265万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/105566512