题目大意:给你两种物品,每种物品有一个价值和花费,花费只有两种,一种花费为 , 一种花费为2.、
给你一个背包容量为v, 求当前容量下所能达到的最大价值。
==========================================================
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const LL INF = 0xffffff;
const int maxn = ;
const LL MOD = 1e9+;
struct node
{
int cost;///花费
int value;///价值
int index;///下标
bool friend operator < (node A, node B)
{
return A.value > B.value;///按照单价进行排序
}
}One[maxn], Tow[maxn], P;
int sum[maxn] = {}, ans[maxn];
int main()
{
int n, Cost;
int num1 = , num2 = ;///Last 最后一个价值为 1 物品的下标。
scanf("%d %d", &n, &Cost);
for(int i=; i<=n; i++)
{
scanf("%d %d",&P.cost, &P.value);
P.index = i;
if(P.cost == )
One[num1 ++] = P;
else
Tow[num2 ++] = P;
}
sort(One+, One + num1+);
sort(Tow+, Tow + num2+);
Tow[].cost = , Tow[].value = ;
for(int i=; i< num1; i++)
sum[i] += sum[i-] + One[i].value;
int Index = , Max = , Temp, CurValue = , CurCost = ;
for(int i=; i< num2; i++)
{
CurValue += Tow[i].value;
CurCost += Tow[i].cost;
if(CurCost > Cost)
break;
if(Cost - CurCost >= num1-)
Temp = CurValue + sum[num1-];
else
Temp = CurValue + sum[Cost - CurCost];
if(Temp > Max)
{
Max = Temp;
Index = i;
}
}
printf("%d\n", Max);
int k = ;
for(int i=; i<=Index; i++)
ans[k ++] = Tow[i].index;
for(int i=; i<=(Cost-Index*)&& i<num1; i++)
ans[k ++] = One[i].index;
for(int i=; i<k- ;i++)
printf("%d ", ans[i]);
if(k != )
printf("%d\n", ans[k-]);
return ;
}
/*
10 10
1 14
2 15
2 11
2 12
2 9
1 14
2 15
1 9
2 11
2 6
*/