Some cows need to be cleaned. There are n (n <= 10000) cows willing to clean, from time s to time t, it takes some money. Ask how much it will cost if you want to clean cows all the time.
Idea: The data volume of 1 W is not very large. In addition, the time limit is 5 s, so we will return with N ^ 2.
Sort the ox by the start of the time period.
Set f [I] to the minimum time spent on cleaning the first ox.
Then f [I] = min (F [I], F [J] + cost [I]) (F [I]. st <= f [J]. ED + 1)
The last is the initial value and the answer. As the question says that there are cows cleaning every moment, the initial value of F is the maximum value. The initial value of the ox started before the requirement is the cost of the ox.
Update the answer at the time of return. If the ox is still working at the end of the request, use the ox to update the answer. Finally, output the answer.
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 10010#define INF 0x3f3f3f3fusing namespace std;struct Complex{ int x,y; int cost; bool operator <(const Complex &a)const { if(x != a.x) return x < a.x; return y < a.y; } void Read() { scanf("%d%d%d",&x,&y,&cost); x++,y++; }}src[MAX];int cnt,st,ed;int f[MAX];int main(){ cin >> cnt >> st >> ed; st++,ed++; for(int i = 1;i <= cnt; ++i) src[i].Read(); sort(src + 1,src + cnt + 1); memset(f,0x3f,sizeof(f)); int ans = INF; for(int i = 1;i <= cnt; ++i) if(src[i].x <= st) f[i] = src[i].cost; for(int i = 1;i <= cnt; ++i) for(int j = 0;j < i; ++j) if(src[i].x <= src[j].y + 1) { f[i] = min(f[i],f[j] + src[i].cost); if(src[i].y >= ed) ans = min(ans,f[i]); } if(ans == INF) ans = -1; cout << ans << endl; return 0;}
Bzoj 1672 usaco 2005 Dec cleaning shifts dynamic plan for clearing Cowshed