这道题目网上有几个题解,均有问题。其实就是简单的贪心+排序,没必要做的那么复杂。
一旦tot+curv > v时,显然curv==2, 有三种可能:
(1)取出最小的curv==1的pp,装入当前的p;
(2)取出后续最大的curv==1的p,并且装入;
(3)当前已经是最优的(即后续不存在curv==1的类型),同时前一个的pp比当前的p更优。(这种情况不需要特判)

 /* 3B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct node_t {
int t, p, id;
friend bool operator< (const node_t& a, const node_t& b) {
if (a.p == b.p)
return a.t < b.t;
return a.p > b.p;
}
} node_t; const int maxn = 1e5+;
node_t nd[maxn]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, v; scanf("%d %d", &n, &v);
rep(i, , n+) {
scanf("%d %d", &nd[i].t, &nd[i].p);
nd[i].id = i;
if (nd[i].t == )
nd[i].p += nd[i].p;
} sort(nd+, nd++n); int i, j, k, p, pp = , pid, tot = ;
int ans = , tmp;
vi vc; i = ;
while (i <= n) {
if (nd[i].t == ) {
k = ;
p = nd[i].p>>;
pp = p;
pid = nd[i].id;
} else {
k = ;
p = nd[i].p;
} if (tot+k <= v) {
tot += k;
ans += p;
vc.pb(nd[i].id);
} else if (pp) {
// tot+k > v
// must be k == 2
// and pre has a 1
j = i+;
while (j<=n && nd[j].t!=)
++j; // no type 1 but may be pp < p (becase p is half if the nd[pid].p).
if (j > n) {
tmp = ;
} else {
tmp = nd[j].t>>;
}
// two way to solve
if (p-pp > tmp) {
// erase pid and add new id
for (vi::iterator iter=vc.begin(); iter!=vc.end(); ++iter) {
if (*iter == pid) {
vc.erase(iter);
break;
}
}
vc.pb(nd[i].id);
ans += p-pp;
} else if (tmp) {
// add nd[j].id;
vc.pb(nd[j].id);
ans += tmp;
} break;
} if (tot == v)
break;
++i;
} printf("%d\n", ans);
rep(i, , SZ(vc)) {
printf("%d ", vc[i]);
}
putchar('\n'); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
04-18 10:24