BC66 GTW likes gt

10 篇文章 0 订阅
10 篇文章 0 订阅

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.


题解:

此题注意不要被c[i]没有顺序坑了,其实c[i]的作用就是

将1-c[i]都加1,保存到数组里面统计次数就行了,

O(n)的做法可以先直接算出加上所有c[i]的结果后

(因为如果第i秒的能力值比前面不是同一组的能力值大,

那么就会一直比前面那个数大,因为i秒后,他们加的次数是一样的),

在统计不需要被删除的个数。

#include <stdio.h>
#include <string.h>
const int maxn = 55555;
int add[maxn], sum[maxn], mx[2][maxn], a[maxn], b[maxn];
void print ( int n )
{
    for ( int i = 1; i <= n; i ++ )
        printf ( "%d ", b[i] );
    printf ( "\n" );
}
/*
此题可以先算出答案再计算:
因为后面的数变大时,前面的数也会变大,如果第i秒有个数比前面不是同一组的数
大,那么它就会一直比前面那个数大
*/
inline int Max ( int a, int b )
{
    return a > b ? a : b;
}
int main ( )
{
    int T, n, m, time, ans;
    scanf ( "%d", &T );
    while ( T -- )
    {
        memset ( add, 0, sizeof ( add ) );
        memset ( sum, 0, sizeof ( sum ) );
        memset ( mx, 0, sizeof ( mx ) );
        scanf ( "%d%d", &n, &m );
        for ( int i = 1; i <= n; i ++ )
            scanf ( "%d%d", &a[i], &b[i] );
        for ( int i = 0; i < m; i ++ )
        {
            scanf ( "%d", &time );
            add[ time ] ++; //前time个需要加
        }
        for ( int i = n; i >= 1; i -- ) //注意逆序
        {
            sum[i] = sum[i+1]+add[i];
            b[i] = b[i]+sum[i]; //将最后的值算出来
        }
        //print ( n );
        for ( int i = n; i >= 1; i -- )
        {
            mx[ a[i] ][i] = Max ( b[i], mx[ a[i] ][i+1] );  //保存01组i-n的最大值
            mx[ a[i]^1 ][i] = mx[ a[i]^1 ][i+1];    //开始少了这个
        }
        ans = 0;
        for ( int i = 1; i <= n; i ++ )
            if ( b[i] >= mx[ a[i]^1 ][i+1] )    //比不是同一组的最大值还大就不会被删除
                ans ++;
        printf ( "%d\n", ans );
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值