博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C. Come to a spring outing
阅读量:5122 次
发布时间:2019-06-13

本文共 1931 字,大约阅读时间需要 6 分钟。

 

C. Come to a spring outing

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 65536KB
64-bit integer IO format: 
%lld      Java class name: 
Main
Submit  Status  PID: 29358
Font Size: 
+ 
-

    Spring is coming,WHU ACM team plans to organize a spring outing because of the good weather. They bought a lot of things to eat and use (collectively referred to the item), each item has a volume. In order to take them away, they also bought 3 backpacks, each backpack can be fitted to a certain volume of things, and the question is there: Can they take all the items away?

Input

    Input contains several test cases. The first line is an integer T indicates the number of test cases. For each test case, the first line is two integer n and m(1<=n<=30,1<=m<=400), indicate the number of items and the capacity of each backpack. The second line contains n integers, the i-th integer wi(1<=wi<=400) shows the volume of the i-th item.

Output

    For each set of data, output the case number first, and then output a string, “Yes” means they can take all things away, and “No” means they cannot take all things away by three backpack.

Sample Input

2
4 3
1 2 3 3
4 3
2 2 2 2

Sample Output

Case 1: Yes
Case 2: No
Submit  Status  PID: 29358
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int w[33];
int n,v;
bool cmp(int a,int b)
{
    return a>b;
}
bool OK=false;
void dfs(int i,int v1,int v2,int v3)
{
    if(i==n){ OK=true;  return; }
    if(OK==true) return;
    if(v1+w
<=v)
        dfs(i+1,v1+w
,v2,v3);
    if(v2+w
<=v)
        dfs(i+1,v1,v2+w
,v3);
    if(v3+w
<=v)
        dfs(i+1,v1,v2,v3+w
);
}
int main()
{
    int T;
    cin>>T;
    int cas=0;
while(T--)
{
    int sum=0;
    cin>>n>>v;
    for(int i=0;i<n;i++)
    {
        cin>>w
;  sum+=w;
    }
    if(sum>3*v)
    {
        printf("Case %d: No\n",++cas);
        continue;
    }
    OK=false;
    sort(w,w+n,cmp);
    dfs(0,0,0,0);
    if(OK)
        printf("Case %d: Yes\n",++cas);
    else
        printf("Case %d: No\n",++cas);
}
    return 0;
}

转载于:https://www.cnblogs.com/CKboss/p/3350915.html

你可能感兴趣的文章
Jmeter学习之旅(二)——Jmeter功能概要
查看>>
jquery源码笔记(五): jquery.extend() 扩展一些工具方法
查看>>
JSON.stringify 语法实例讲解
查看>>
Python6 模块
查看>>
P3377 【模板】左偏树(可并堆)
查看>>
Djang 用户登录
查看>>
Java同步锁——lock与synchronized 的区别【转】
查看>>
洛谷-校门外的树-数组
查看>>
Python--网络编程-----文件传输简单版本
查看>>
3 使用模块
查看>>
解决前端页面运行出现乱码的现象
查看>>
CF 208E. Blood Cousins [dsu on tree 倍增]
查看>>
趣谈面试(一)
查看>>
Quart2D setNeedsDisplay
查看>>
设计模式之策略设计模式
查看>>
SQL2005 安装时 “性能监视器计数器要求(错误)” 解决方案
查看>>
杂-lowbit
查看>>
724#寻找数组的中心索引
查看>>
用Python做一个飞机大战游戏
查看>>
登录注册
查看>>