宁波工程学院[1372] Do What n个数中取出某些数使得和大于T且和最小
来源:互联网 发布:MySQL删除重复记录 编辑:程序博客网 时间:2024/04/30 14:36
[1372] Do What
There are n numbers of business, different business will cost you different times. But you need not to finish all of them, just it is ok when you finish some of them that you spend more than T minutes(do not include T minutes).
So, can you find the way to cost the minimum time when pass T times?
So, can you find the way to cost the minimum time when pass T times?
Input until EOF.
First line will contain a positive integer n(1 <= n <= 100) means the number of businesses.
Next line follows n positive integers ti(1 <= ti <= 10), means the time of each business you will cost.
Last line is a integer T(1 <= T <= 1000).
First line will contain a positive integer n(1 <= n <= 100) means the number of businesses.
Next line follows n positive integers ti(1 <= ti <= 10), means the time of each business you will cost.
Last line is a integer T(1 <= T <= 1000).
The minimum time of you cost, if there is no answer, print '-1'.
31 2 31031 4 76
-17
无
Hungar
题意: 从n个数中取出任意个 数字 使得这些数字的和大于T且和最小
思路:
01背包 背包过程中取离T最近的值
详细看代码
#include<stdio.h>#include<string.h>int n,t,ans;int val[105];int dp[10000];void _01bag(int v){int i,j;memset(dp,0,sizeof(dp));for(i=0;i<n;i++)for(j=v;j>=val[i];j--)if(dp[j]<dp[j-val[i]]+val[i]){dp[j]=dp[j-val[i]]+val[i];if(dp[j]>t&&ans>dp[j]-t) ans=dp[j]-t;}printf("%d\n",ans+t);}int main(){int i;while(scanf("%d",&n)!=EOF){ans=999999999;int sum=0;for(i=0;i<n;i++){scanf("%d",&val[i]);sum+=val[i];} scanf("%d",&t);if(sum>t) _01bag(sum);else printf("-1\n");}return 0;}
- 宁波工程学院[1372] Do What n个数中取出某些数使得和大于T且和最小
- 宁波工程学院 OJ [1282] A Bouquet of Flowers 最大的k个数的和
- 宁波工程学院 OJ [1292] Jokes 一堆数中对每个数与前k个重复的数字的个数
- 统计一个二进制数据中1的个数和大于等于当前数的最小的一个二的N次幂
- 求一堆数中取出2个异或使得异或值大于某个数
- N个数中找三个数,使其和的绝对值最小--
- 数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0<x<100,元素个数也是大于0, 小于100 。
- CSU 1323 求一堆数中取出2个异或使得异或值大于某个数
- 如何使用位操作得到大于N且为2的次方的最小的数
- 给定一个数t,以及n个整数,在这n个数中找到加和为t的所有组合
- (使用STL自带的排序功能进行排序7.3.12)UVA 11057 Exact Sum(在一组数据中找到这么两个数,使得他们的和等于指定数&&且这两个数之间的差值最小)
- Poj2769找最小数m使得n个数都不同余
- 给一个正整数n,找出个数最小的平方数让他们的和等于n
- 返回给定的n个数的序列中的最大数和最小数
- n个数中取出r个数组合,逆序输出 组合数
- java:n个数中取出m个数,计算之和为S,求这几个数
- 【HDU 3486】【二分+RMQ】 Interviewe 【最小化区间个数m,使得每个区间最大值和大于k】
- 给一个正整数 n, 找到若干个完全平方数(比如1, 4, 9, ... )使得他们的和等于 n。你需要让平方数的个数最少。
- 招聘移动开发,老板必问的10个问题
- Linux 的基础命令操作图解
- Android一个工程引用另一个工程
- Costing Methods
- 互联网上的常用端口
- 宁波工程学院[1372] Do What n个数中取出某些数使得和大于T且和最小
- how to view printf output in win32 app on visual studio 2010?
- hdu3343
- 使用Spread.NET创建Y Plot图表
- android应用安全——组件通信安全(Intent)
- jQuery实现ajax
- web开发平台的演变
- spring map使用annotation泛型注入问题分析
- android开机速度优化 | 全志A10