hdu 5596__GTW likes gt

GTW likes gt

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 727    Accepted Submission(s): 264


Problem Description
Long long ago, there were n adorkable GT. Divided into two groups, they were playing games together, forming a column. The ith GT would randomly get a value of ability bi. At the ith second, the ith GT would annihilate GTs who are in front of him, whose group differs from his, and whose value of ability is less than his.

In order to make the game more interesting, GTW, the leader of those GTs, would emit energy for m times, of which the ith time of emitting energy is ci. After the ci second, b1,b2,...,bci would all be added 1.

GTW wanted to know how many GTs would survive after the nth second.
 

Input
The first line of the input file contains an integer T(5), which indicates the number of test cases.

For each test case, there are n+m+1 lines in the input file.

The first line of each test case contains 2 integers n and m, which indicate the number of GTs and the number of emitting energy, respectively.(1n,m50000)

In the following n lines, the ith line contains two integers ai and bi, which indicate the group of the ith GT and his value of ability, respectively. (0ai1,1bi106)

In the following m lines, the ith line contains an integer ci, which indicates the time of emitting energy for ith time.
 

Output
There should be exactly T lines in the output file.

The ith line should contain exactly an integer, which indicates the number of GTs who survive.
 

Sample Input
1 4 3 0 3 1 2 0 3 1 1 1 3 4
 

Sample Output
3
Hint
After the first seconds,$b_1=4,b_2=2,b_3=3,b_4=1$ After the second seconds,$b_1=4,b_2=2,b_3=3,b_4=1$ After the third seconds,$b_1=5,b_2=3,b_3=4,b_4=1$,and the second GT is annihilated by the third one. After the fourth seconds,$b_1=6,b_2=4,b_3=5,b_4=2$ $c_i$ is unordered.
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5599 5598 5597 5595 5594 
 
看了zx大神的代碼,根據思路寫了一遍,結果一直wa,跟ac代碼的區別就在於MAX數組定義爲了局部變量,而全局變量是自動賦初值0的。
後來發現發現訪問的MAX數組的下標可能到n,因此其實如果數據不那麼水的話,除非每次都初始化,否則還是會wa的。代碼如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50000+10;
struct node
{
    int a,b;
}f[maxn];
int main()
{
    int _,vis[maxn],n,m,c;
    scanf("%d",&_);
    while(_--) {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) {
            scanf("%d%d",&f[i].a,&f[i].b);
        }
        int MAX[maxn][3];
        memset(MAX,0,sizeof(MAX));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++) {
            scanf("%d",&c);
            vis[c-1]++;
        }
        int sum=0;
        for(int i=n-1;i>=0;i--) {
            sum+=vis[i];
            f[i].b+=sum;
        }
        int MAX0=-1,MAX1=-1;
        for(int i=n-1;i>=0;i--) {
            if(f[i].a) {
                MAX1=max(MAX1,f[i].b);
                MAX[i][1]=MAX1;
                MAX[i][0]=MAX0;
            }
            else {
                MAX0=max(MAX0,f[i].b);
                MAX[i][1]=MAX1;
                MAX[i][0]=MAX0;
            }
        }
        int ans=0;
        for(int i=0;i<n;i++) {
            if(f[i].a) {
                ans+=f[i].b<MAX[i+1][0];
            }
            else {
                ans+=f[i].b<MAX[i+1][1];
            }
        }
        printf("%d\n",n-ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章