1880: 蛤玮点菜
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 321 Solved: 32
Submit Status Web Board
Description
在我们下饭店的时候蛤玮经常负责点菜,今天饭店搞活动,当总价格大于等于X时可以减去Y的优惠,注意如果总价是2X也仅减去一倍Y.蛤玮非常了解菜品也了解大家,他知道每个菜品有一个饱食度,只有菜品饱食度的和不小于K时大家才会吃的开心.请问蛤玮如何点菜才能在让大家吃的开心的前提下花尽量少的钱,输出最后需要付的钱.注意蛤玮是个有追求的人,所以他不会点重复的菜.
Input
T(1<=T<=40),表示数据组数.
每组数据第一行n(1<=n<=30),K,X,Y(1<=K,X,Y<=1e9, X>=Y),表示一共有n种菜,X,Y,K如题目中描述.
接下来n行每行两个数ai,bi(1<=ai,bi<=1e8),分别表示第i个菜的价格和饱食度.
Output
每组数据输出一个数,表示总价.如果无解则输出"go die".
Sample Input
1
2 2 20 12
10 2
10 2
2 2 20 12
10 2
10 2
Sample Output
8
HINT
不可以通过白给店家钱而不买任何东西来得到减价优惠.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
const LL inf = 9999999999;
LL a[N], b[N];
struct node
{
LL price, f;
bool operator<(const node X)
{
return f<X.f;
}
}p1[(1<<16)+10], p2[(1<<16)+10];
int cmp(node A,node B)
{
return A.f<B.f;
}
set<LL>s;
set<LL>::iterator it;
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
LL k, x, y;
scanf("%d %lld %lld %lld", &n, &k, &x, &y);
LL ans=0;
for(int i=0;i<n;i++)
{
scanf("%lld %lld", &a[i], &b[i]);
ans+=b[i];
}
if(ans<k)
{
printf("go die\n");
continue;
}
int n1=n/2;
int n2=n-n1;
for(int i=0;i<(1<<n1);i++)
{
LL s1=0, s2=0;
for(int j=0;j<n1;j++)
{
if(i&(1<<j))
{
s1+=a[j], s2+=b[j];
}
}
p1[i].price=s1, p1[i].f=s2;
}
for(int i=0;i<(1<<n2);i++)
{
LL s1=0, s2=0;
for(int j=0;j<n2;j++)
{
if(i&(1<<j))
{
s1+=a[n1+j], s2+=b[n1+j];
}
}
p2[i].price=s1, p2[i].f=s2;
}
sort(p1,p1+(1<<n1)-1,cmp);
sort(p2,p2+(1<<n2)-1,cmp);
s.clear();
ans=inf;
int r=(1<<n2)-1;
for(int i=0;i<(1<<n1);i++)
{
while(r>=0&&p2[r].f+p1[i].f>=k)
{
s.insert(p2[r].price);
r--;
}
if(s.empty()) continue;
if(p1[i].price+(*s.begin())<x)
{
ans=min(ans,p1[i].price+(*s.begin()));
LL ax=x-p1[i].price;
it=s.lower_bound(ax);
if(it!=s.end()) ans=min(ans,(*it)+p1[i].price-y);
}
else
{
ans=min(ans,p1[i].price+(*s.begin())-y);
}
}
printf("%lld\n",ans);
}
return 0;
}