BZOJ 2794: [Poi2012]Cloakroom 背包DP
2017-10-07 15:09
288 查看
Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 319 Solved: 202
6 2 7
5 4 9
1 2 4
2 5 8
1 3 9
5
2 7 1
2 7 2
3 2 0
5 7 2
4 1 5
NIE
TAK
TAK
NIE
Submit: 319 Solved: 202
Description
有n件物品,每件物品有三个属性a[i], b[i], c[i] (a[i]Input
第一行一个正整数n (n<=1,000),接下来n行每行三个正整数,分别表示c[i], a[i], b[i] (c[i]<=1,000, 1<=a[i]Output
输出q行,每行为TAK (yes)或NIE (no),第i行对应第i此询问的答案。Sample Input
56 2 7
5 4 9
1 2 4
2 5 8
1 3 9
5
2 7 1
2 7 2
3 2 0
5 7 2
4 1 5
Sample Output
TAKNIE
TAK
TAK
NIE
HINT
Source
鸣谢Oimasterdalao友链
显然想到离线,然后顺序处理,因为离线之后排序a和询问的m之后两者均具有单调性,故可以线性决策,因此我们用f[i]表示当前用这些已经可以用的物品组成i的使得b的最小值最大的那个数,然后我们每次去判断是否满足这个b大于询问的m+s就可以了,时间复杂度非常优秀(至少比暴力不知道高到哪里去了)#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int N = 1010; const int M = 1000010; const int inf = 1e9 + 7; struct Data { int a, b, c; } w[M]; bool cmp(const Data &A, const Data &B) { return A.a < B.a; } struct Dota{ int m, k, s, id; } q[M]; bool cmpp(const Dota &A, const Dota &B) { return A.m < B.m; } int n, Q, f[M], ans[M]; int main( ) { scanf( "%d", &n ); for( register int i = 1; i <= n; i++ ) scanf( "%d%d%d", &w[i].c, &w[i].a, &w[i].b ); sort( w + 1, w + n + 1, cmp ); scanf( "%d", &Q ); for( register int i = 1; i <= Q; i++ ) { scanf( "%d%d%d", &q[i].m, &q[i].k, &q[i].s ); q[i].id = i; } sort( q + 1, q + Q + 1, cmpp ); int now = 1, top = 100000; f[0] = inf; for( register int i = 1; i <= Q; i++ ) { while( now <= n && w[now].a <= q[i].m ) { for( register int j = top; j >= w[now].c; j-- ) f[j] = max( f[j], min( f[ j - w[now].c ], w[now].b ) ); now++; } if( f[q[i].k] > q[i].m + q[i].s ) ans[q[i].id] = 1; } for( register int i = 1; i <= Q; i++ ) if( ans[i] ) printf( "TAK\n" ); else printf( "NIE\n" ); return 0; }
相关文章推荐
- BZOJ 2794: [Poi2012]Cloakroom 询问离线 背包dp
- bzoj2794 [Poi2012]Cloakroom ( 背包DP+离线 )
- bzoj 2794 [Poi2012]Cloakroom 离线+背包
- BZOJ2794 : [Poi2012]Cloakroom
- 【bzoj2748】 HAOI2012音量调节 背包dp
- bzoj 2792 [Poi2012]Well 二分+dp+two_pointer
- [BZOJ][HAOI2012][背包DP]音量调节
- bzoj 2798 [Poi2012]Bidding 博弈论 dp
- 【BZOJ2794】[Poi2012]Cloakroom 离线+背包
- 2794: [Poi2012]Cloakroom
- bzoj 2794 [Poi2012]Cloakroom dp
- [bzoj2748][HAOI2012] 音量调节 背包dp
- [POI 2012]Cloakroom(DP)
- 【bzoj3522】[Poi2014]Hotel 树形dp
- BZOJ 2802: [Poi2012]Warehouse Store
- [BZOJ 2801]POI2012 Minimalist Security
- BZOJ 2748: [HAOI2012]音量调节【二维dp,枚举】
- 【BZOJ 2803】【POI 2012】Prefixuffix
- 【bzoj2789】[Poi2012]Letters
- bzoj 1296: [SCOI2009]粉刷匠【dp+背包dp】