题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4972
题目大意:
有n项(1~3000)工程,总耗时T,每项工程耗时ti,收益为si*Tleft(Tleft:剩余时间)。(1<= ti , si <=500)
题目思路:
假设可以满足T>=sum(ti),这题可以贪心求解。
设两项工程i,j
假设i先做那么收益为si*t+sj*(t-ti)
假设j先做那么收益为sj*t+si*(t-tj)
显然收益大的先做更优,而且两式相减后可以发现与剩余时间Tleft无关。
但是该题有一个限制条件,就是可能出现Tleft-ti<0。
这样其实就是转化为了一个01背包问题问题了,就是在T时间里取得最大收益。
当然必须按最优顺序选择工程。
代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll __int64
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define eps (1e-8)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#pragma comment(linker, "/STACK:102400000,102400000")
template <class T> T _max(T x,T y){return x>y? x:y;}
template <class T> T _min(T x,T y){return x<y? x:y;}
template <class T> T _abs(T x){return (x < 0)? -x:x;}
template <class T> T _mod(T x,T y){return (x > 0)? x%y:((x%y)+y)%y;}
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
template <class T> void getmax(T& x,T y){x=(y > x)? y:x;}
template <class T> void getmin(T& x,T y){x=(x<0 || y<x)? y:x;}
int TS,cas=1;
const int M=3000+5;
int n,t;
struct node{
int t,s;
void read(){scanf("%d%d",&t,&s);}
bool operator < (const node& p)const{
return s*p.t > p.s*t;
}
}task[M];
int dp[M<<2];
void run(){
int i,j;
for(i=0;i<n;i++) task[i].read();
sort(task,task+n);
int ret=0;
clr_all(dp,0);
for(i=0;i<n;i++)
for(j=t;j>=task[i].t;j--)
ret=_max(ret,dp[j]=_max(dp[j],dp[j-task[i].t]+(t-j+task[i].t)*task[i].s));
printf("%d\n",ret);
}
void preSof(){
}
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
preSof();
//run();
while((~scanf("%d%d",&n,&t))) run();
//for(scanf("%d",&TS);cas<=TS;cas++) run();
return 0;
}