题目链接:

  http://acm.hdu.edu.cn/listproblem.php?vol=53

A题:

2018年湘潭邀请赛

题意:

  总共有sum(a[i])篇文章,文章含有i条引用的文章数是ai,求最大的h使得最少有h篇文章含有至少h条引用。

思路:

  二分,不过一开始把i和ai的含义读反wa了几发。

代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <deque>
 4 #include <queue>
 5 #include <stack>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <bitset>
 9 #include <cstdio>
10 #include <string>
11 #include <vector>
12 #include <cstdlib>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 typedef long long LL;
19 typedef pair<LL, LL> pLL;
20 typedef pair<LL, int> pLi;
21 typedef pair<int, LL> pil;;
22 typedef pair<int, int> pii;
23 typedef unsigned long long uLL;
24 
25 #define lson rt<<1
26 #define rson rt<<1|1
27 #define lowbit(x) x&(-x)
28 #define name2str(name) (#name)
29 #define bug printf("*********\n")
30 #define debug(x) cout<<#x"=["<<x<<"]" <<endl
31 #define FIN freopen("D://code//in.txt","r",stdin)
32 #define IO ios::sync_with_stdio(false),cin.tie(0)
33 
34 const double eps = 1e-8;
35 const int mod = 1000000007;
36 const int maxn = 2e5 + 7;
37 const double pi = acos(-1);
38 const int inf = 0x3f3f3f3f;
39 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
40 
41 int n;
42 int a[maxn];
43 
44 bool check(LL x) {
45     LL sum = 0;
46     for(int i = 0; i <= n; i++) {
47         if(i >= x) sum += a[i];
48     }
49     return sum >= x;
50 }
51 
52 int main(){
53     while(~scanf("%d", &n)) {
54         for(int i = 0; i <= n; i++) {
55             scanf("%d", &a[i]);
56         }
57         LL ub = INF, lb = 0, mid, ans = 0;
58         while(ub >= lb) {
59             mid = (ub + lb) >> 1;
60             if(check(mid)) {
61                 lb = mid + 1;
62                 ans = mid;
63             } else {
64                 ub = mid - 1;
65             }
66         }
67         printf("%lld\n", ans);
68     }
69     return 0;
70 }
View Code

相关文章: