[11.4] Market Test

File the INPUT: market.In
the Output File: market.out
Time limit: 1 seconds The
Memory limit: 128 megabytes
in a total of n bits town shops, followed by numbers 1 to n. Each store will only sell an article, the first of which i store items priced at ci, the value of vi, and the store opened in time ti.
Byteasar planned shopping m times, where the i-th shopping time Ti, budget Mi. Every time when shopping, Byteasar will purchase up to an item in each store, of course, he can not choose what to buy. If shopping earlier than the time the store opened, then obviously he can not be shopping in this store. Now Byteasar want to know, for each plan, he bought up to the total value of the number of items. Please write a program that helps Byteasar arrange shopping plan.
Note: Each spent amount shall not exceed the budget, the budget must be spent not finished, and the budget can not be left to other programs to use.
Input
The first line contains two positive integers n; m, indicates the number of the total number of stores and shopping plans.
Next n lines of three positive integers ci; vi; ti, respectively, each store's price, value and open time.
Next m lines of two positive integers Ti; Mi, respectively, time and budget for each shopping plan.
Output
output m lines, each an integer, the maximum possible output value for each plan and.
Examples

market.in market.out
5 2
5 5 4
1 3 1
3 4 3
6 2 2
4 3 2
3 8
5 9
10
12

The first plan can be purchased at each store 2,3,5 an item, total cost of 1 + 3 + 4 = 8, the total value of 3 + 4 + 3 = 10.
The second plan can be purchased at each store 1,2,3 an item, the total cost for the 5 + 1 + 3 = 9, with a total value of 5 + 3 + 4 = 12.

Notes
to 100% of the data, 1 ≤ ti; Ti ≤ n .

Test point number n m c and ; M and v in t i ; T and
1 = 10 = 5 ≤ 10 ≤ 10 ≤ 10
2 = 20 = 10 ≤ 100 ≤ 100 ≤ 20
3 = 100 = 1 ≤ 100 ≤ 100 = 1
4 = 200 = 1 ≤ 200 ≤ 200 = 1
5 = 150 = 100000 ≤ 150 ≤ 150 ≤ 150
6 = 300 = 100000 ≤ 300 ≤ 300 ≤ 300
7 = 20 = 100000 ≤ 109 ≤ 300 ≤ 20
8 = 200 = 100000 ≤ 109 ≤ 200 ≤ 200
9 = 300 = 100000 ≤ 109 ≤ 300 ≤ 300
10 = 300 = 100000 ≤ 109 ≤ 300 ≤ 300

 

 

Solution: This question is 01 backpack optimization, pay attention to two points (because he has a monotonic ah qwq)

         Amway about, the solution of this problem special bar https://blog.csdn.net/wu_tongtong/article/details/75305310

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=501;
int n,m;
struct node{
    int c,v,t;
}e[N];
int T,M,mx=0;
ll f[N*N];  
struct data{
    int t,m,id,ed;
}q[1000010 ];
 int ANS [ 1000010 ]; 

int CMP (Node A, Node B) {
     IF (AT = BT!) Return AT < BT;
     the else  return AC> BC; 
} 
int CMP2 (Data A, Data B) { 
     IF ( ! A. Ed = b.ed) return A. Ed < b.ed;
     the else  return AM> BM; 
} 

int fathest ( int X) { // record at each interrogation which can store most away for later 
    int L = . 1 , R & lt = n-;
     int ANS = 0;        
     The while (L <= R & lt) { 
         int MID = (L + R & lt) >> . 1 ;
         IF (E [MID] .T <= X) = max ANS (ANS, MID), L = MID + . 1 ;
         the else R & lt = mid- . 1 ; 
    } 
    return ANS; 
} 

void ido_ycll () {
     int K = . 1 ;
     the while (Q [K] .ed && K <= m!) K ++;   // stores can not get to this inquiry is skipped 
    memset (F, 0x3F , the sizeof (F)); 
    F [ 0 ] = 0 ;
     for ( int I =1;i<=n;i++){
        for(int j=mx;j>=e[i].v;j--)  
            f[j]=min(f[j],f[j-e[i].v]+(ll)e[i].c);
        int last=mx;
        while(q[k].ed==i && k<=m){ 
            for (int s=last;s>=1;s--)
                if (f[s]<=q[k].m)   
                   { ans[q[k].id]=s; last=s; break; }
            k++;
        } 
    }
}

int main()
{
    freopen("market.in","r",stdin);  
    freopen("market.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++){
        scanf("%d %d %d",&e[i].c,&e[i].v,&e[i].t);
        mx+=e[i].v;
    }
    sort(e+1,e+1+n,cmp);
    for (int i=1;i<=m;i++){
        scanf("%d %d",&q[i].t,&q[i].m);
        q[i].id=i;
    }
    for (int i=1;i<=m;i++)
        q[i].ed=fathest(q[i].t);//最远能去的那个商店的编号 
    sort(q+1,q+1+m,cmp2);
    ido_ycll();
    for (int i=1;i<=m;i++)
        printf("%d\n",ans[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11792952.html