XMU1460 最高得分 &&124 - ZOJ Monthly, March 2013 - D 01背包巧妙处理

1460.最高得分
Time Limit: 1000 MS         Memory Limit: 65536 K
Total Submissions: 181 (36 users)         Accepted: 40 (28 users)
[ My Solution ]

Description
在很多算法比赛的网站上, 我们常常能看到一些和ACM比赛不太相同的比赛规则。在这里, 我们以codeforces网站上的比赛为例, 在通常情况下, 每道题目开始的时候都有一定的分值, 每道题目的分值会随着比赛时间和提交次数的增多而递减。为了简化问题, 假设初始的时候, 每道题目有个固定的分值, 每经过一分钟, 每道题目的分值递减该题初始分值的1/T (T为比赛总时长, 以分钟为单位) , 每道题目的最终得分为你首次成功通过该题后该题剩余的分值, 例如, 在一场历时100分钟的比赛中, 一道初始1000分的题目, 每经过一分钟就递减10分, 假设你在第20分钟的时候成功通过了该道题目, 则这道题目你的得分为800分。现在给你一场比赛的总时长T和题目的列表, 并且, 你预估了成功通过每道题所需要花费的时间, 假设每道题目你都能一次就通过了, 那么, 怎样安排做题顺序才能使你的最终得分最高呢?

Input
输入的第一行有2个整数n, T (1 <= n, T <= 1,000)分别代表了比赛的题目数量和比赛的时长, 接下来有n行, 每行2个整数t, s (1 <= t, s <= 1,000)分别代表通过每道题目需要的时间和可以获得的分值。(T与t的时间单位均为分钟)

Output
输出一个四舍五入到小数点后8位有效数字的数, 代表可能获得的最高得分。

Sample Input
3 10
7 40
5 30
3 20

Sample Output
20.00000000

Hint
先解出第3道题目, 再解出第2道题目, 这样的顺序得分最高。

Source
doraemon @ xmu

http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1460



思路:先按价值/时间的大小排序,把大的放到前面。因为单位时间获得价值多的肯定要先选。再用一次01背包。就能得出结果了。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct st
{
	int w;
	double v;
}a[1111];
double dp[1111];
bool cmp(st a,st b)
{
	if(a.v/a.w==b.v/b.w)
		return a.w>b.w;
	else
		return a.v/a.w>b.v/b.w;
}
int main()
{
	int i,j,m,n;
	double t;
	double max=0;
	while(scanf("%d%lf",&n,&t)!=EOF)
	{
		max=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%lf",&a[i].w,&a[i].v);
		}
		memset(dp,0,sizeof(dp));
		sort(a,a+n,cmp);
		for(i=0;i<n;i++)
		{
			for(j=(int)t;j>=a[i].w;j--)
			{
				if(dp[j]<dp[j-a[i].w]+a[i].v*(1-1/t*j))
					dp[j]=dp[j-a[i].w]+a[i].v*(1-1/t*j);
				if(dp[j]>max)
					max=dp[j];
			}
		}
		printf("%.8lf\n",max);
	}
	return 0;
}


124 - ZOJ Monthly, March 2013 - D
Digging

Time Limit: 2 Seconds      Memory Limit: 65536 KB

When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It's not difficult to understand why we choose to believe the prophecy (or we just assume it is true to entertain ourselves) if you know the other prophecies appeared in the Maya Calendar. For instance, it has accurately predicted a solar eclipse on July 22, 2009.

The ancient civilization, such as Old Babylonianhas, Ancient Egypt and etc, some features in common. One of them is the tomb because of the influence of the religion. At that time, the symbol of the tomb is the pyramid. Many of these structures featured a top platform upon which a smaller dedicatory building was constructed, associated with a particular Maya deity. Maya pyramid-like structures were also erected to serve as a place of interment for powerful rulers.

Now there are N coffin chambers in the pyramid waiting for building and the ruler has recruited some workers to work for T days. It takes ti days to complete the ith coffin chamber. The size of the ith coffin chamber is si. They use a very special method to calculate the reward for workers. If starting to build the ith coffin chamber when there are t days left, they can get t*si units of gold. If they have finished a coffin chamber, then they can choose another coffin chamber to build (if they decide to build the ith coffin chamber at the time t, then they can decide next coffin chamber at the time t-ti).

At the beginning, there are T days left. If they start the last work at the time t and the finishing time t-ti < 0, they will not get the last pay.

Input

There are few test cases.

The first line contains N, T (1 ≤ N ≤ 3000,1 ≤ T ≤ 10000), indicating there are N coffin chambers to be built, and there are T days for workers working. Next N lines contains ti, si (1 ≤ ti, si ≤ 500).

All numbers are integers and the answer will not exceed 2^31-1.

Output

For each test case, output an integer in a single line indicating the maxminal units of gold the workers will get.

Sample Input
3 10
3 4
1 2
2 1
Sample Output
62
Hint

Start the second task at the time 10
Start the first task at the time 9
Start the third task at the time 6
The answer is 10*2+9*4+6*1=62

http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=4962


题意:

3 10
3 4
1 2
2 1
输入 n m  n个题目 ,总的时间m

每个题目的 a b  a表示做这道题需要的时间  4是做这道题的单位得分(总得分为单位得分乘以做这道题的时候的剩余时间)  没道题都确信能够做出切一次性ac  问能得到的最高分

注意本题是开始做的时候就给分

第 10秒开始做第二道哦题目  得到10*2分
第9秒的时候做第一道得到9*4、

第6秒的时候做第三道得到6*1分

一共得到
The answer is 10*2+9*4+6*1=62


思路和上面的差不多 做了下小修改  




#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct st
{
	int w;
	double v;
}a[3111];
double dp[11111];
bool cmp(st a,st b)
{
	if(fabs((a.v/a.w-b.v/b.w))<0.0000001)
	  //if(a.v/a.w==b.v/b.w)
		return a.w>b.w;
	else
		return a.v/a.w>b.v/b.w;
}
int main()
{
	int i,j,m,n;
	int t;
	double max=0;
	while(scanf("%d%d",&n,&t)!=EOF)
	{
		max=0;
		for(i=0;i<n;i++)
		{
			scanf("%d%lf",&a[i].w,&a[i].v);
		}
		memset(dp,0,sizeof(dp));
		sort(a,a+n,cmp);
		for(i=0;i<n;i++)
		{
			for(j=(int)t;j>=a[i].w;j--)
			{
				if(dp[j]<dp[j-a[i].w]+a[i].v*(t-j+a[i].w))
					  dp[j]=dp[j-a[i].w]+a[i].v*(t-j+a[i].w);
				if(dp[j]>max)
					max=dp[j];
			}
		}
	//	for(i=0;i<=t;i++)
		//	printf("%lf ",dp[i]);
		printf("%.0lf\n",max);
	}
	return 0;
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值