Codeforces Beta Round #3 B. Lorry
程序员文章站
2022-05-09 17:41:52
...
http://codeforces.com/contest/3/problem/B
题意:给n个物品 和 体积为v的背包
物品包括物品体积(只有1 和 2 )和价值
求能得到的最大价值
贪心 按照p/t单位价值排序 价值相同优先2体积
假如背包没有装满
可以是 2体积置换背包内1体积的物品 需要判断
ACcode:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
struct node
{
int t,p,id;
}veh[maxn];
bool cmp(node a,node b)
{
if(a.p * b.t == b.p * a.t)
return a.t > b.t;
return (a.p * b.t) > (b.p * a.t);
}
int main()
{
int n,v;scanf("%d%d",&n,&v);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&veh[i].t,&veh[i].p);
veh[i].id = i;
}
sort(veh+1,veh+1+n,cmp);
// for(int i=1;i<=n;i++)
// printf("! %d %d %d\n",veh[i].t,veh[i].p,veh[i].id);
vector<int>pos;
vector<int>id;
for(int i=1;i<=n;i++)
{
if(v == 0) break;
if(v-veh[i].t >= 0)
{
v-=veh[i].t;
pos.push_back(i);
id.push_back(veh[i].id);
}
else
{
int op1=-1,op2=-1;
for(int j=i;j>=1;j--)
if(veh[j].t == 1) {op1=j;break;}
for(int j=i+1;j<=n;j++)
if(veh[j].t == 1) {op2=j;break;}
if(op1 != -1 && op2 != -1)
{
if(veh[op1].p + veh[op2].p <= veh[i].p)
{
pos[op1-1]=i;
id[op1-1]=veh[i].id;
}
else
{
pos.push_back(op2);
id.push_back(veh[op2].id);
}
}
else if(op1 != -1 && op2 == -1)
{
if(veh[op1].p <= veh[i].p)
{
pos[op1-1]=i;
id[op1-1]=veh[i].id;
}
else
{
pos.push_back(i);
id.push_back(veh[i].id);
}
}
else if(op1 == -1 && op2 != -1)
{
pos.push_back(op2);
id.push_back(veh[op2].id);
}
break;
}
}
int cnt = 0;
for(int i = 0; i < pos.size(); i++)
cnt += veh[pos[i]].p;
printf("%d\n", cnt);
for(int i = 0; i < id.size(); i++)
{
if(i != 0) printf(" ");
printf("%d", id[i]);
}
printf("\n");
return 0;
}
上一篇: Codeforces Beta Round #3 B. Lorry(贪心)
下一篇: 【Codeforces Round #519 by Botan Investments D】Mysterious Crime
推荐阅读
-
Codeforces Round #595 (Div. 3)D1D2 贪心 STL
-
Codeforces Round #656 (Div. 3)D. a-Good String(递归+dfs)
-
CodeForces 1324 - Codeforces Round #627 (Div. 3)
-
Codeforces Round #649 (Div. 2)-B. Most socially-distanced subsequence(思维)
-
Codeforces Round #650 (Div. 3) B. Even Array
-
Codeforces Round #686 (Div. 3) A. Special Permutation
-
A. Add Odd or Subtract Even(思维题) Codeforces Round #624 (Div. 3)
-
Codeforces Round #461 (Div. 2) B. Magic Forest(异或的性质)
-
Codeforces Round 604 B. Beautiful Numbers
-
[几何] Codeforces 772B VK Cup 2017 - Round 2 B. Volatile Kite