# P1083 [NOIP2012 提高组] 二分 + 差分

##### 题解

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int N, M, R[maxn], D[maxn], S[maxn], T[maxn], df[maxn];
ll sum[maxn];

{
int x = 0;
char c = 0;
for (; c < '0' || c > '9'; c = getchar())
;
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 1) + (x << 3) + c - '0';
return x;
}

bool judge(int p)
{
for (int i = 1; i <= N; ++i)
sum[i] = df[i];
for (int i = 1; i <= p; ++i)
sum[S[i]] -= D[i], sum[T[i] + 1] += D[i];
for (int i = 1; i <= N; ++i)
if ((sum[i] += sum[i - 1]) < 0)
return 0;
return 1;
}

int main()
{
for (int i = 1; i <= N; ++i)
for (int i = 1; i <= M; ++i)
df[1] = R[1];
for (int i = 2; i <= N; ++i)
df[i] = R[i] - R[i - 1];
int lb = 0, ub = N + 1;
while (ub - lb > 1)
{
int mid = (lb + ub) >> 1;
if (judge(mid))
lb = mid;
else
ub = mid;
}
if (ub == N + 1)
puts("0");
else
printf("-1\n%d\n", ub);
return 0;
}


11-02 5078

09-21 634
02-17 534
11-16 759
09-04 111
08-06 367
07-21 399
02-01 1287
11-13 346
11-20 66
10-23 177
05-11 21