当前位置:编程学习 > 网站相关 >>

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

 

 


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

 

 

[cpp]
#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; 

#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 num

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,