sicily8034. F7 Championship

来源:互联网 发布:java简单的小游戏 编辑:程序博客网 时间:2024/04/27 16:19
Description

The fictional World Championship of Formula 7 Drivers 2013 was characterized by exciting races and frequent shifts of driver positions on the leaderboard. Antun has missed most of it because he was training for SYSUCPC. Now his only consolation are his medals and being the main character in this task. He has a simple question for you SYSUCPC contestants: "How many drivers participating in this Championship still had a chance to become Formula 7 World Champion at the start of the final race?” The World Champion is, of course, the driver with the largest point total at the end (after the final race).
There are N drivers participating in the Championship. They are all assigned points after each race, including the final one. The winner of the race is awarded N points, the runner-up gets N - 1 points, and so on until the last driver, who gets 1 point. Two drivers cannot finish a race in the same spot.
Write a program to calculate, based on the total number of points that each driver has earned before the final race, how many drivers still have a chance to have the largest total after the final race and thus win the Championship. If more than one driver has the same maximum point total, they are all awarded the World Champion title.

Input

The first line of input contains the positive integer N (3 ≤ N ≤ 300 000), the number of drivers participating in the Championship.
Each of the following N lines contains a single integer Bi (0 ≤ Bi ≤ 2 000 000, i = 1, ..., N), the number of points that a driver has before the final race.

Output

The first and only line of output should contain the requested number of drivers that can still win.  


   这道题目,需要一代优化一下算法才不会超时。主要是因为数据量确实有点大,然后测试案例也比较多。所以,需要优化。然后,我的算法不是很好,但是经过优化后,通过时间也是相当快的。


#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;const int T = 300009;int main(){    int n;    int a[T];    while (scanf("%d", &n) != EOF)    {        for (int i = 0; i < n; ++ i)        {            scanf("%d", &a[i]);        }        sort(a, a + n);        int tem = n;        int size = 0;        int cms = 0;        for (int i = 0; i < n; ++ i)        {            int ant = a[i] + n;            bool ch = 1;            if (cms <= i)            {                cms = i;            }            if (i >= 1 && a[i] == a[i - 1])            {                ++ size;                continue;            }            for (int j = cms; j < n; ++ j)            {                if (ant < a[j] + tem - j)                {                    ++ size;                    ch = 0;                    cms = j;                    break;                }            }            if (ch == 1)            {                break;            }        }        printf("%d\n", n - size);    }}