链接:https://ac.nowcoder.com/acm/contest/315/D
来源:牛客网
题目描述
妞妞参加完Google Girl Hackathon之后,打车回到了牛家庄。
妞妞需要支付给出租车司机车费s元。妞妞身上一共有n个硬币,第i个硬币价值为p[i]元。
妞妞想选择尽量多的硬币,使其总价值足以支付s元车费(即大于等于s)。
但是如果从妞妞支付的这些硬币中移除一个或者多个硬币,剩下的硬币总价值还是足以支付车费的话,出租车司机是不会接受的。例如: 妞妞使用价值为2,5,7的硬币去支付s=11的车费,出租车司机是不会接受的,因为价值为2这个硬币是可以移除的。
妞妞希望能选取最大数量的硬币,使其总价值足以支付车费并且出租车司机能接受。
妞妞希望你能帮她计算最多可以支付多少个硬币。
输入描述:
输入包括两行, 第一行包括两个正整数n和s(1 <= n <= 10, 1 <= s <= 1000), 表示妞妞的硬币个数和需要支付的车费。
第二行包括n个正整数p[i] (1 <= p[i] <= 100),表示第i个硬币的价值。
保证妞妞的n个硬币价值总和是大于等于s。
输出描述:
输出一个整数, 表示妞妞最多可以支付的硬币个数。
示例1
输入
5 9 4 1 3 5 4
输出
3
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 200; 4 typedef long long ll; 5 6 int main() 7 { 8 ios::sync_with_stdio(0); 9 int n,s,p[N],sum = 0,ans = 0,cnt; 10 cin>>n>>s; 11 for(int i =1;i <=n;++i) cin>>p[i]; 12 sort(p+1,p+n+1); 13 int maxs = 1<<(n+1); 14 for(int i = 1;i <maxs;++i){ 15 sum = cnt = 0; 16 for(int j = 1;j <= n;++j){ 17 if(i&(1<<j))sum+=p[j],cnt++; 18 } 19 if(sum < s)continue; 20 else{ 21 for(int j = 1;j <= n;++j){ 22 if(i&(1<<j)){ 23 sum-=p[j]; 24 break; 25 } 26 } 27 if(sum < s)ans = max(ans,cnt); 28 } 29 } 30 cout<<ans<<endl; 31 return 0; 32 }