您的位置:首页 > 其它

BZOJ 2794: [Poi2012]Cloakroom 背包DP

2017-10-07 15:09 288 查看
Time Limit: 20 Sec Memory Limit: 128 MB

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

5

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

Sample Output

TAK

NIE

TAK

TAK

NIE

HINT

Source

鸣谢Oimaster

dalao友链

显然想到离线,然后顺序处理,因为离线之后排序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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: