BZOJ 2794: [Poi2012]Cloakroom 背包DP

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;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值