CF 贪心+dp(动态规划) 01背包(做与不做)

7 篇文章 1 订阅
2 篇文章 0 订阅

CF

Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description

LYD loves codeforces since there are many Russian contests. In an contest lasting for T minutes there are n problems, and for the ith problem you can get aiditi points, where ai indicates the initial points, di indicates the points decreased per minute (count from the beginning of the contest), and ti stands for the passed minutes when you solved the problem (count from the begining of the contest).
Now you know LYD can solve the ith problem in ci minutes. He can't perform as a multi-core processor, so he can think of only one problem at a moment. Can you help him get as many points as he can?

Input

The first line contains two integers n,T(0≤n≤2000,0≤T≤5000).
The second line contains n integers a1,a2,..,an(0<ai≤6000).
The third line contains n integers d1,d2,..,dn(0<di≤50).
The forth line contains n integers c1,c2,..,cn(0<ci≤400).

Output

Output an integer in a single line, indicating the maximum points LYD can get.

Sample Input
3 10
100 200 250
5 6 7
2 4 10
Sample Output
254
Hint
Source
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

题意:在总时间T时间内,有n道题目,ai是第i道题目的分值,di是第i道题目,在单位时间内分数减少di,ci是第i道题目在ci时间内做完,题目让求的是在给定时间内,能够得到最多的分数(这就用到贪心的思想),最大的分数就是ai-di*ci(这是能够得到的最大分数),为了能够得到最大的分数,我们就要先做分数消耗最快并且解题需要时间最短的。例如:(两个题目A和B,如果先做A,那么丢失的分数就是ca*da+(ca+cb)*db,如果先做B,那么丢失的分数就是cb*db+(cb+ca)*da ,所以要丢失最少的分数,就要找di最大,并且ci最小的,那么这时就需要求di/ci的比值,如果比值大的话,就先做(证明di大ci小,其比值才会最大),如果比值小的话,就后做,这时就需要排序  )

思路:用结构体来存放a,d,c,f(比值),然后将按照从大到小的顺序进行排序,

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
#define maxn  100010
long long int dp[maxn];
struct node{
	int a;
	int d;
	int c;
	double f;
}num[maxn];
bool compare(node a,node b)
{
	return a.f>b.f;
} 
int main()
{
	int n,t;
	int i,j,k;
	scanf("%d%d",&n,&t);
	memset(dp,0,sizeof(dp));
	for(i=0;i<n;i++)
	{
		scanf("%d",&num[i].a);
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&num[i].d);
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&num[i].c);
		num[i].f=1.0*num[i].d/num[i].c;
	}
   sort(num,num+n,compare);
   for(i=0;i<n;i++)
   {
   	for(j=t;j>=num[i].c;j--)
   	{
   	   dp[j]=max(dp[j],dp[j-num[i].c]+num[i].a-j*num[i].d);
                          // 做这个题目之前做的题目的分数加上这道题目的得分,然后与当前的分数比较,取最大的
            //cout<<"j = "<<j<<" dp[j] = "<<dp[j]<<endl;
          //      cout<<" dp[j-num[i].c] = "<<j-num[i].c<<endl; 在做这个题目之前做的题目,用现在的时间减去现在做的题目的时间,就是之前做的题目
           //     cout<<" j * num[i].d = "<<j * num[i].d<<endl; 丢失的分数	
	}
   }	
  long long  int result=0;//找最大的分数
   for(i=0;i<=t;i++)
   {
   	 result=max(result,dp[i]);
   }
   printf("%lld\n",result);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值