HDU 6276 Easy h-index(思维+二分/前缀和)

题意字面理解:求最大的h,使得至少有h篇文章被发表并且每篇文章都至少有h个引用

进一步翻译:给定一个数组,求$h_{max}$,使得$\sum_{i=index}^{n}a_{i}\geq h_{max}$

第一种思路,也是我一开始的思路(Onlgn):

二分答案,求$\sum_{i=mid}^{n}a_{i}$,记为sum,记录mid,若满足sum>=h,向右区间继续二分;反之向左区间二分。

两种二分写法都可:

int get(int a[], int n)//[l, r)
{int ans=0, l=0, r=n+1;
    while(l<r){
        int mid=(r+l)>>1;
        if(check(a, mid)){
            ans=mid;l=mid+1;
        }
        else r=mid;
    }
    return ans;
}
int get(int a[], int n)//[l, r]
{
    int ans=0, l=0, r=n;
    while(l<=r){
        int mid=(r+l)>>1;
        if(check(a, mid)){
            ans=mid;l=mid+1;
        }
        else r=mid-1;
    }
    return ans;
}

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
int n, a[200005];

bool check(int a[], int h)
{
    ll sum=0;
    for(int i=h;i<=n;++i)
        sum+=a[i];
    return sum>=h;
}
int get(int a[], int n)
{
    int ans=0, l=0, r=n;
    while(l<=r){
        int mid=(r+l)>>1;
        if(check(a, mid)){
            ans=mid;l=mid+1;
        }
        else r=mid-1;
    }
    return ans;
}
int main()
{
    while(cin>>n){
        for(int i=0;i<=n;++i)
            cin>>a[i];
        cout<<get(a, n)<<endl;
    }
    return 0;
}
/*
1
1 2
2
1 2 3
3
0 0 0 0
*/

第二种思路,也是最简单的解法(On):

进一步翻译题目:求数组最大的下标index,使得$\sum_{i=index}^{n}a_{i}\geq h$,所以数组从后向前扫描,求一个前缀和即可(很简单的一个模型)

代码:

#include <bits/stdc++.h>
using namespace std;

int n, a[200005];
int main()
{
    while(cin>>n){
        long long int temp=0;
        int ans=0;
        for(int i=0;i<=n;++i) cin>>a[i];
        for(int i=n;i>=0;--i){
            temp+=a[i];
            if(temp>=i) {ans=i;break;}
        }
        cout<<ans<<endl;
    }
    return 0;
}

最后,记得开ll,不开ll见祖宗

原文地址:https://www.cnblogs.com/ChenyangXu/p/10765919.html

时间: 2024-11-09 03:47:00

HDU 6276 Easy h-index(思维+二分/前缀和)的相关文章

HDU 4004 The Frog&#39;s Games(二分+小思维+用到了lower_bound)

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9678    Accepted Submission(s): 4428 The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triat

HDU 4004 The Frog&#39;s Games 二分 贪心

戳这里:HDU 4004 //思路:二分经典入门题...贪心判方案是否可行 1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角线,问至少要画几条. 解法:如果一个斜着的矩形长宽分别是 a,b,那么它的面积是 2ab.最优解肯定是离 sqrt(n/2)很近的位置.想想 n=5 时答案为什么是7 然后在那个小范围内枚举一下就好了.我给一张做题时画的图 #include <bits/stdc++.h> using namesp

hdu So Easy!

So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2026    Accepted Submission(s): 624 Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the cei

HDU - 1845 Jimmy’s Assignment (二分匹配)

Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the gr

HDU 2289 Cup【高精度,二分】

Cup Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8942    Accepted Submission(s): 2744 Problem Description The WHU ACM Team has a big cup, with which every member drinks water. Now, we know th

HDU 1498 50 years, 50 colors(二分最大匹配之最小点覆盖)

题目地址:HDU 1498 晕啊...三个人同时做的这个题,结果全都理解错意思了..而且每个人理解错的地方还都不一样..但是样例还都能过,...简直炫酷... 题意:n*n的矩阵放置不同的颜色(不同的数字代表不同的颜色),你有k次选择,每一次只能选择某一行或某一列,可以消除该行(列)的所有颜色,问有哪几种颜色,无论怎样经过k次选择后依然无法完全抹去. 这个题的思路就是分别求出每种颜色的最少操作次数.然后只要大于k次的就是不符合要求的.然后输出就行了. #include <iostream> #

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

POJ3061 Subsequence(二分前缀和法+尺取法)

二分+前缀和法 满足条件的子序列长度在(0,n)之间,sum[x+i]-sum[i]为从从第i个元素开始序列长度为x的元素的和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #