zoukankan      html  css  js  c++  java
  • hihocoder-Week184-满减优惠

    hihocoder-Week184-满减优惠

    题目1 : 满减优惠

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    最近天气炎热,小Ho天天宅在家里叫外卖。他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元。并且如果消费总计满X元,还能享受优惠。小Ho是一个不薅羊毛不舒服斯基的人,他希望选择若干道不同的菜品,使得总价在不低于X元的同时尽量低。

    你能算出这一餐小Ho最少消费多少元吗?

    输入

    第一行包含两个整数N和X,(1 <= N <= 20, 1 <= X <= 100)

    第二行包含N个整数A1, A2, ..., AN。(1 <= Ai <= 100)

    输出

    输出最少的消费。如果小Ho把N道菜都买了还不能达到X元的优惠标准,输出-1。

    样例输入
    10 50
    9 9 9 9 9 9 9 9 9 8
    样例输出
    53

    题解:

      简单的一重背包问题。

    #include <cstdio> 
    #include <cstring> 
    const int MAXN = 2000 + 10; 
    
    int n, x, num[MAXN], dp[MAXN]; 
    
    int main(){
    	freopen("in.txt", "r", stdin);    
    
    	int sum, ans; 
    	while(scanf("%d %d", &n, &x) != EOF){
    		sum = 0; 
    		for(int i=0; i<n; ++i){
    			scanf("%d", &num[i]); 
    			sum += num[i]; 
    		}
    		ans = -1; 
    		if(sum >= x){
    			memset(dp, 0, sizeof(dp)); 
    			dp[0] = 1; 
    			for(int i=0; i<n; ++i){
    				for(int j=sum; j>=num[i]; --j){
    					if( dp[j - num[i]] ){
    						dp[j] = 1; 
    					}
    				}
    			} 
    			for(int i=x; i<=sum; ++i){
    				if( dp[i] ){
    					ans = i; 
    					break; 
    				}
    			}
    		}
    		printf("%d
    ", ans );  
    	}
    	return 0; 
    }
    

      

  • 相关阅读:
    Javascript FP-ramdajs
    微信小程序开发
    SPA for HTML5
    One Liners to Impress Your Friends
    Sass (Syntactically Awesome StyleSheets)
    iOS App Icon Template 5.0
    React Native Life Cycle and Communication
    Meteor framework
    RESTful Mongodb
    Server-sent Events
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/8260616.html
Copyright © 2011-2022 走看看