• Handshakes


    Description

    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
    
    题意:每个人进屋子里坐下,给出每个人和多少人握过手,让你去求一个人可能最多的握手次数;
    
    解题思路:从后往前遍历,a[i]表示第i个人最多握多少次手,然后输出最多的那个,和学姐一起想这个题0.0,好有成就感0.0;
    
    感悟:比赛真是太刺激了~
    
    代码:
    
    #include
    #include
    #include
    using namespace std;
    #define maxn 100010
    int a[maxn];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            memset(a,0,sizeof(a));
            int n;
            scanf("%d",&n);
            for(int i=0;i
            {
                scanf("%d",&a[i]);
            }
            int ans=0,sum=0;
            for(int i=n-1;i>=0;i--)
            {
                if(a[i])sum++;
                a[i]+=(sum-1);
                ans=max(ans,a[i]);
            }
            printf("%d ",ans);
        }
        return 0;
    }

  • 相关阅读:
    CSS 圣杯/双飞翼布局
    java 面向对象编程-- 第15章 集合框架
    java 面向对象编程--第十四章 多线程编程
    java 面向对象编程-- 第十三章 反射、类加载与垃圾回收
    java 面向对象编程 --第十二章 JDK常用类
    java 面向对象编程--第十章 接口
    java面向对象编程--第十一章 异常处理
    java面向对象编程--第九章 多态和抽象
    java面向对象编程——第八章 类的高级概念
    java面向对象编程— —第七章 继承
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5781559.html
Copyright © 2020-2023  润新知