【ZOJ3932 The 16th Zhejiang University Programming ContestF】【水题】Handshakes 每人与之前认识的人握手 最大可能认识人数

来源:互联网 发布:和上瘾类似的网络剧 编辑:程序博客网 时间:2024/05/21 20:29
Handshakes

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Last week, n students participated in the annual programming contest of Marjar University. Students are labeled from 1 to n. They came to the competition area one by one, one after another in the increasing order of their label. Each of them went in, and before sitting down at his desk, was greeted by his/her friends who were present in the room by shaking hands.

For each student, you are given the number of students who he/she shook hands with when he/she came in the area. For each student, you need to find the maximum number of friends he/she could possibly have. For the sake of simplicity, you just need to print the maximum value of the n numbers described in the previous line.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1 ≤ n ≤ 100000) -- the number of students. The next line contains n integers a1, a2, ..., an (0 ≤ ai < i), where ai is the number of students who the i-th student shook hands with when he/she came in the area.

Output

For each test case, output an integer denoting the answer.

Sample Input

230 1 150 0 1 1 1

Sample Output

23

Author: LIN, Xi
Source: The 16th Zhejiang University Programming Contest


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int casenum, casei;int n;int a[N], sum[N];int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d", &n);for (int i = 1; i <= n; ++i)scanf("%d", &a[i]);sum[n + 1] = 0;for (int i = n; i >= 1; i--)sum[i] = sum[i + 1] + (a[i]>0);int ans = 0;for (int i = 1; i <= n; ++i)gmax(ans, a[i] + sum[i + 1]);printf("%d\n", ans);}return 0;}/*【题意】n(1e5)个人依次进房间,每个人会与之前在房间的所有人中认识的握手。告诉你每个人与多少人握了手,让你输出哪个人可能认识的人最多,输出这个最多认识人数。【类型】水题【分析】直接统计后缀非0数的个数gamx(ans,a[i]+后缀非0数个数)【时间复杂度&&优化】O(n)*/


0 0