题目大概是给一些折线,问安排多少个保安才能监控全部折点。
预处理出能否看到进行区间dp即可,转移用前缀和优化
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define db double
const int INF = 0x3f3f3f3f;
const int maxn = 5010;
int f[maxn][maxn];
int ok[maxn][maxn];
int H[maxn];
int n;
inline db slope(int a,int b) {
return (db)(H[a] - H[b])/(a-b);
}
int mx;
int l,r;
int ans;
int main () {
scanf("%d",&n);
mx = INF;
for(int i = 1;i <= n; ++i) {
scanf("%d",&H[i]);
ok[i][i] = ok[i][i - 1] = 1;
db k = slope(i - 1,i);
for(int j = i - 2;j >= 1; --j) {
while(j and slope(j,i) >= k) {
ok[i][j --] = 0;
}
k = slope(j,i);
ok[i][j] = 1;
}
}
ans = 0;
for(r = 1;r <= n; ++r) {
int head = 1;
int tail = 0;
for(l = r;l; --l) {
if(ok[r][l]) {
if(!ok[r][l - 1]) tail = l - 1;
f[l][r] = head;
}
else {
f[l][r] = head + min(f[l][tail],f[l][tail + 1]);
if (ok[r][l - 1]) {
head += min(f[l][tail],f[l][tail + 1]);
}
}
ans ^= f[l][r];
}
}
cout<<ans<<endl;
return 0;
}
原文:https://www.cnblogs.com/akoasm/p/10111406.html