zoukankan      html  css  js  c++  java
  • [ZOJ 3631] Watashi's BG

    Watashi's BG

    Time Limit: 3 Seconds      Memory Limit: 65536 KB

    Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer training camp, students are divided into several groups and each day one of the groups will design some problems to hold a contest. Today students of Group C are required to design the problems, and they spent the whole night to check to test data which made them very tired. Watashi decides to give some money as a reward to group C so that they can buy the lunch for free.

    There are N days in the training schedule, and all students have booked their lunch for N days so we know how much money they will spend in each day. Now the leader of group C needs to decide how to use Watashi's money. Since the money is limited, it may not be possible that they can have free lunch every day. So each day the leader can choose to pay for the whole group's lunch by themselves or use Watashi's money. Of course, the leader wants to spend Watashi's money as much as possible, but he is too busy to write a program to calculate the maximum money he can spend from Watashi's reward. Can you help him?

    Input

    The input contains multiple test cases ( no more than 50 test cases ). In each test case, first there are two integer, N ( 1 <= N <=30 ) , which is the number of training days, M ( 0 <= M <=10000000 ) , which is the reward money from Watashi. Then there is a line containing N positive integers with the ith integer indicating the money group C need to pay for the lunch of the ith day. All these integers are no more than 10000000 and integers are seperated by a space.

    Output

    For each test case, output one line with an integer which is the maximum money group C can spend from Watashi's reward

    Sample Input

    3 10
    8 4 5
    

    Sample Output

    9
    
    剪枝很重要!!!
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 33
    
    int n,m;
    int ans;
    int a[N];
    int sum[N];
    
    bool cmp(int a,int b){ return a>b;}
    
    void DFS(int nowx,int nows)
    {
        if(nows>m) return;
        if(nowx>=n)
        {
            ans=max(nows,ans);
            return;
        }
        if(nows+sum[n-1]-sum[nowx-1]<ans) return;
        DFS(nowx+1,nows+a[nowx]);
        DFS(nowx+1,nows);
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            sort(a,a+n,cmp);
            for(int i=0;i<n;i++)
            {
                if(i==0) sum[i]=0;
                else sum[i]=sum[i-1]+a[i];
            }
            ans=0;
            DFS(0,0);
            printf("%d
    ",ans);
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    2014.3.3 图像旋转方法
    2014.2.23 datagridview显示图片的方法
    2016.10.8 文件读取和两种模式写入
    2016.8.11 DataTable合并及排除重复方法
    2016.8.17服务器端数据库用户导入导出方法 expdp和impdp
    2016.8.11 禁用360进程防护功能
    2016.7.27 VS搜索正则表达式,在UltraEdit中可选用Perl正则引擎,按C#语法搜索
    2016.6.18主窗体、子窗体InitializeComponent()事件、Load事件发生顺序以及SeleChanged事件的发生
    delphi之猥琐的webserver实现
    HTTP协议中GET、POST和HEAD的介绍
  • 原文地址:https://www.cnblogs.com/hate13/p/4140008.html
Copyright © 2011-2022 走看看