先check是不是完全满足,不是再二分订单查找不满足的订单
一开始写得像个弱智一样,check函数最后累加时for循坏竟写成
...
#define f(i, a, b) for (int i = a; i <= b; i++)
...
int check(int md){
f(i, 1, md)
...
}
而实际上边界值应该是n
#include <stdio.h>
#include <stdlib.h>
#define scan(x) scanf("%d", &x)
#define f(i, a, b) for (int i = a; i <= b; i++)
#define pn(x) printf("%def\n", x)
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define max 1000086
int n, m, rent[max], mark = 0, d[max], s[max], e[max], diff[max], wa = 1;
int check(int md)
{
int sum = 0;
if (md > mark)
f(i, mark + 1, md)
{
diff[s[i]] -= d[i];
diff[e[i] + 1] += d[i];
}
else
f(i, md + 1, mark)
{
diff[s[i]] += d[i];
diff[e[i] + 1] -= d[i];
}
mark = md;
f(i, 1, n)
{
sum += diff[i];
if (sum < 0)
return 0;
}
return 1;
}
int main()
{
//IN;
//OUT;
scan(n);
scan(m);
f(i, 1, n)
scan(rent[i]);
f(i, 1, m)
{
scan(d[i]);
scan(s[i]);
scan(e[i]);
}
diff[1] = rent[1];
f(i, 2, n)
diff[i] = rent[i] - rent[i - 1];
if (check(m))
{
printf("0");
return 0;
}
int left = 1, right = m, mid; //二分
while (left <= right)
{
mid = (left + right) / 2;
if (check(mid))
left = mid + 1;
else
right = mid - 1;
}
printf("-1\n%d", right + 1);
return 0;
}