1. 程式人生 > >SDUTOJ 3307 (01揹包求第K大)

SDUTOJ 3307 (01揹包求第K大)

a

Time Limit: 1000MS Memory limit: 65536K

題目描述

有一個奇怪的超市,超市中有m種商品,每個商品都有一個都價格pi,超市的特殊在於:如果顧客買第i件商品,就會贈送ci個歡樂豆,一種商品對一個顧客只會贈送一次。現在有兩個好朋友來超市買東西,他們都帶了n元錢,如果最終兩個人得到的歡樂豆不一樣,他們的歡樂豆的和的最大值是多少?

輸入

 首先輸入一個正整數T (T<10),測試資料有T組。 對於每組測試資料的第一行有兩個整數n,m ( 1 <= n,m <= 1000)。之後有m行,每行兩個數字p c,代表第i件商品的價格是p,贈送的歡樂豆的數量c(1 <= p,c <= 1000).

輸出

 輸出一個整數,表示兩個人獲得的歡樂豆的和的最大值。

示例輸入

1
10 3
2 3
3 5
5 8

示例輸出

29

提示

 第一個人可以買第1,2,3種商品,得到的歡樂豆是16,第二個人也可以買1,2,3這三種商品,但是不能和第一個人得到的歡樂豆一樣,所以最多可以得到13個歡樂豆。

來源

windream

示例程式

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int p[1005],w[1005];
int n,v,dp[1005][31] = {0},a[5],b[5];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        int k = 2;
        scanf("%d%d",&v,&n);
        memset(dp,0,sizeof(dp));
        for(int i = 0; i<n; i++) {
            scanf("%d%d",&p[i],&w[i]);
        }
        for(int i = 0; i<n; i++) {
            for(int j = v; j>=p[i]; j--) {
                int cnt = 0,d;
                for(d = 1; d<=k; d++) {
                    a[d] = dp[j-p[i]][d]+w[i];
                    b[d] = dp[j][d];
                }
                int x,y,z;
                x = y = z = 1;
                a[d] = b[d] = -1;
                while(z<=k && (x<=k || y<=k)) {
                    if(a[x] > b[y]) {
                        dp[j][z] = a[x];
                        x++;
                    } else {
                        dp[j][z] = b[y];
                        y++;
                    }
                    if(dp[j][z]!=dp[j][z-1])
                        z++;
                }
            }
        }
        if(dp[v][1] == dp[v][2]) {
            printf("%d\n",dp[v][1]);
        } else {
            printf("%d\n",dp[v][1] + dp[v][2]);
        }
    }

    return 0;
}


相關推薦

SDUTOJ 3307 01揹包K

a Time Limit: 1000MS Memory limit: 65536K 題目描述 有一個奇怪的超市,超市中有m種商品,每個商品都有一個都價格pi,超市的特殊在於:如果顧客

hdu263901揹包k優解

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3803    Acce

BestCoder Round #65 C. ZYB's Premutation線段樹k

題意: 給定[1,i]前綴逆序對數,還原原始序列 分析: 先把前綴和還原成原來的,考慮倒著來,每個就相當於前面有多少個比自己大,線段樹求第k大就好了,BIT什麼的也可以 程式碼:

[poj2828]Buy Tickets樹狀陣列k

傳送門 題意: i個人排隊,對於第i個人,我們知道他排在第pi個人後面(可以插隊)且他有一個權值val,我們需要輸出排好之後每個人的權值。 首先我想的是連結串列來模擬,但是發現由於p[i]指的是位置而不是編號,所以連結串列是隻能O(n^2)的。 那麼怎麼辦呢

2014西安網路預選賽1002字尾陣列K的子串hdu5008

The input consists of multiple test cases.Please process till EOF.  Each test case begins with a line containing a string s(|s| ≤ 105) with only lowercase

2639骨頭問題2(01揹包k值)

Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must h

洛谷P3960 列隊【noip2017D2T3】多顆線段樹k

題目描述 Sylvia 是一個熱愛學習的女♂孩子。 前段時間,Sylvia 參加了學校的軍訓。眾所周知,軍訓的時候需要站方陣。 Sylvia 所在的方陣中有n \times mn×m 名學生,方陣的行數為 nn ,列數為 mm 。 為了便於管理,教官在訓

陣列中第二大數後續K

package cn.edu.nwsuaf.cie.qhs; import java.util.Scanner; public class GetGreater {     private int initArray[];     public int[] getInitArray() {      

hdu3949 XOR線性基【k

題目連結 分析: 求第k大:把k二進位制拆分,如果k的第i位上是1,ans^=b[i] 這是什麼道理呢? 異或消元最後得到的是一組基 給出n個數能夠異或出來的值,都是這些基線性組合形成的數

spoj 7258 SUBLEXk字串

top stdin add algo can find size esp con   其實對sam的拓撲排序我似懂非懂但是會用一點了。    /** @xigua */ #include <stdio.h> #include <cmath> #in

hdu 5919--Sequence II主席樹--區間不同數個數+區間k

positions minus -s ima date rst itl 主席樹 技術 題目鏈接 Problem Description Mr. Frog has an integer sequence of length n, which can be denot

LeetCode703 Kth Largest Element in a StreamK元素

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth dis

POJ2104主席樹區間K

模板題,模板來自B站UESTCACM #include <iostream> #include <algorithm> #include <queue> #in

POJ 題目2985 The k-th Largest Group線段樹單點更新k值,並查集

The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7869 Accepted: 2534 Description Newman likes play

poj1442(treapk)

syn div gcd += ace str while pre clu ac代碼: #include<bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i <=

無序數組K/K小的數

std name 根據 ide ace mat algorithm cout ios 方法一:quicksort 根據快排思想,從後往前找比基準數小的,交換位置。 從前往後找比基準數大的,交換位置。 最後安放基準數。 保證 l到p 是大數,若 p-l+1==k 那麽p就是第

hdoj2602:Bone Collector01揹包問題-dp-模版題

Bone Collector Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 65 &nb

POJ - 2184 Cow Exhibition 01揹包 中 負數的處理

"Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons  The cows want

HDU2852 KiKi's K-Number (權值線段樹k)

題意:三種操作,0 e 表示插入一個數字e,1 e 表示刪除一個數字e,2 e k 表示查詢比e大的第k個數,刪除和查詢均可能沒有 目標。 思路:建一棵權值線段樹,維護每個數字區間中數字的數量。 查詢時,先查出1到e的數字數量n,然後查詢第k+n大。 #include<cs

區間k黑匣子_NOI導刊2010提高06

https://www.luogu.org/problemnew/show/P1801 可以用multiset水過,發現有超多資料結構可以處理這道題。== 線段樹:https://blog.csdn.net/weishengmingerfendou/article/details/47144