ZOJ 3932 Handshakes

来源:互联网 发布:毒药超女网络海选 编辑:程序博客网 时间:2024/06/05 04:33

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3932


题面:

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 a1a2, ..., 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 <iostream>#include <string> #include <cstdio>#include <cstring>#include <map>#include <set>#include <algorithm>#include <cmath>using namespace std;int a[100005],sum[100005];int main(){  int t,n,maxx;  scanf("%d",&t);  while(t--)  {  maxx=0;  scanf("%d",&n);      for(int i=1;i<=n;i++)  {  scanf("%d",&a[i]);  }  a[n+1]=0;  sum[n+1]=0;  for(int i=n;i>=1;i--)  {  if(a[i+1])  {  sum[i]=sum[i+1]+1;  }  else  sum[i]=sum[i+1];  if(a[i]+sum[i]>maxx)  maxx=a[i]+sum[i];  }  printf("%d\n",maxx);  }  return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 6个钱包买房说明了什么 贷款买房的人们后悔了 买房交了定金能退吗没签合同 买房了怎么低调发朋友圈 陵水清水湾买房后悔了 买房交了5万定金后悔了 买房下了定金可以退吗 买房签了合同能退房吗 买房给了定金能退吗 买房后悔了 2018在鲅鱼圈投资买房的人亏了 买房要了解什么 为什么现在买房 贷款买房划算吗 中国人买房忌讳13楼吗 婚前贷款买房属于共同财产吗 买房首付可以贷款吗 二手房按揭买房难办吗 北京买房能落户吗 夫妻买房可以一个人贷款吗 低首付买房靠谱吗 贷款买房能拿到房产证吗 7天内买房定金能退吗 2019年汉南买房有必要吗 贷款买房有房产证吗 买房17楼有什么忌讳吗 现在是买房的好时机吗 公积金能异地买房吗 公积金可以跨省买房吗 公积金可以异地买房吗 住房公积金可以异地买房吗 有贷款还能贷款买房吗 银行有贷款还能按揭买房吗 深圳公积金可以异地买房吗 外地公积金可以在本地买房吗 2018年公积金可以跨省买房吗 名下有贷款还可以贷款买房吗 2018网贷没还清可以贷款买房吗 买新房注意什么 买房买几层好 刚买的二手房可以卖吗