ZOJ 3932 Handshakes
来源:互联网 发布:毒药超女网络海选 编辑:程序博客网 时间:2024/06/05 04:33
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3932
题面:
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 <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;}
- ZOJ 3932 Handshakes
- ZOJ 3932 Handshakes
- zoj--3932--Handshakes(水题)
- ZOJ 3932——Handshakes
- ZOJ - 3932 Handshakes (技巧)握手
- ZOJ 3932Handshakes(翻译水题)
- Handshakes
- Codeforces534D:Handshakes
- CFD. Handshakes
- codeforces #298 D. Handshakes
- 【CodeForces 534D】Handshakes
- Codeforces 534D Handshakes(贪心)
- #298 (div.2) D. Handshakes
- URAL 1194. Handshakes (乱搞)
- 【Codeforces 534D】【贪心】Handshakes
- Codeforces 534D Handshakes【贪心】
- zoj 3932
- 534D.Handshakes(Codeforces Round #298(div2))
- gdutProblem E: 积木积水
- SMTP协议错误代码及原因
- js获取元素相对于父元素的位移,获取元素本身的宽高
- 程序员眼里的高迸发
- js 学习总结,可利用其与原生代码交互
- ZOJ 3932 Handshakes
- 抽屉效果
- Java多线程
- Android常用系统广播
- 鼠标拉近旋转拖拽模型
- html5+css(3)
- 第三次上机作业
- 快速幂算法初尝试
- 2016-4-4至2016-4-10