前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ZOJ 3932 Handshakes

ZOJ 3932 Handshakes

作者头像
ShenduCC
发布2018-04-26 16:08:05
3770
发布2018-04-26 16:08:05
举报
文章被收录于专栏:算法修养算法修养

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

2 3 0 1 1 5 0 0 1 1 1 Sample Output

2 3

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>

using namespace std;
#define MAX 100000
long long int a[MAX+5];
long long int s[MAX+5];
long long int b[MAX+5];
int t;
int n;
long long int sum;
int main()
{
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%d",&n);
            sum=0;s[0]=0;
            //memset(b,0,sizeof(b));
            for(int i=1;i<=n;i++)
            {

                 scanf("%lld",&a[i]);
                 if(a[i]>=1)
                     b[i]=1;
                 else
                     b[i]=0;
                 s[i]=s[i-1]+b[i];

            }
            for(int i=1;i<=n;i++)
            {
                sum=max(sum,s[n]-s[i]+a[i]);
            }
            printf("%lld\n",sum);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档