Title Link: http://poj.org/problem?id=3273
Because of the number of days in a row so greedy before and after the naked two points
Features of the two-part theme:
1. Very easy to write
2. It's easy to write wrong
So pay attention to:
Is the value of the upper and lower bounds too large or too small to add up will explode
Can we finally jump out of the loop
How to adjust the lb and UB after determining the median value
Note that the LB place should be assigned a value of mid+1 otherwise it will be t to die
#include <cstdio>#include<cstdlib>#include<ctime>#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<stack>#include<Set>#include<queue>#include<vector>using namespaceStd;typedefLong Longll;Const intMAXN =100010;Const DoubleEPS = 1e-7;intA[MAXN];intMain () {//freopen ("In.txt", "R", stdin); intN, M; scanf ("%d%d", &n, &m); intMaxnum =0; for(inti =0; I < n; i++) {scanf ("%d", &A[i]); Maxnum=Max (Maxnum, A[i]); } intUB =0x3fffffff; intLB =Maxnum; while(UB >lb) { intMID = (UB + lb)/2; intCNT =0; intsum =0; for(inti =0; I < n; i++) { if(sum + a[i] >mid) {CNT++; Sum=A[i]; Continue; } Sum+=A[i]; } CNT++; if(CNT <=m) UB=mid; Elselb= Mid +1; } printf ("%d\n", UB); return 0;}
POJ 3273 Monthly Expense two points