In order to make the game more interesting, GTW, the leader of those GTs, would emit energy for m times, of which the i−th 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 n−th second.
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.(1≤n,m≤50000)
In the following n lines, the i−th line contains two integers ai and bi, which indicate the group of the i−th GT and his value of ability, respectively. (0≤ai≤1,1≤bi≤106)
In the following m lines, the i−th line contains an integer ci, which indicates the time of emitting energy for i−th time.
The i−th line should contain exactly an integer, which indicates the number of GTs who survive.
題解:
此題注意不要被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;
}