HDU 3486 Interviewe
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3486
题目写的清清楚楚,认真读就能 AC 了,讨论区那些人都是没认真读题,模棱两可按照自己的意愿在写,写错了怪谁呢?
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; const int maxn = 2e5 + 10; int N, K; int a[maxn], maxx[maxn][20]; void RMQ(int num) { for(int i = 1; i <= N; i ++) maxx[i][0] = a[i]; for(int j = 1; j < 23; j ++) { for(int i = 1; i <= num; i ++) { if(i + (1 << j) - 1<= num) maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << (j - 1))][j - 1]); } } } int QueryMax(int l, int r) { int k = (int)(log(r - l + 1) / log(2.0)); int ans = max(maxx[l][k], maxx[r - (1 << k) + 1][k]); return ans; } int step(int p) { int q = 0, cnt = 0; for(int i = 1; i <= N; i += p) { q += QueryMax(i, i + p - 1), cnt ++; if(q > K) break; } return cnt; } int main() { while(~scanf("%d%d", &N, &K)) { if(N == -1 && K == -1) break; memset(maxx, 0, sizeof(maxx)); memset(a, 0, sizeof(a)); for(int i = 1; i <= N; i ++) { scanf("%d", &a[i]); } RMQ(N); int people = N + 1; for(int i = 1; i <= N; i ++) { int st = 1, en = i; int sum = 0; int p = 0; while(en <= N && st <= N && sum <= K) { sum += QueryMax(st, en); st += i; en += i; p ++; } if(sum <= K) { continue; } if(p < people) { people = p; } } if(people == N + 1) people = -1; printf("%d\n", people); } return 0; }