zoj3258 DP 01背包

将自己历史的AC共享

zoj3258 DP题

对第一种石头01背包,对第二种石头按单位价值从大到小贪心,ans=max(ans,v1[i]+v2[c-i]);

//2003839 2009-09-27 18:27:42 Accepted  3258 C++ 370 964 greentea@FireStar 
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <ctime>
#include <utility>
#include <stdexcept>

using namespace std;

#define inf (1<<30)
#define PB push_back
#define mset(x,a) memset(x,(a),sizeof(x))
#define SIZE(X) ((int)X.size())
#define dbg(x) cerr<<#x<<" : "<<x<<endl
#define rg(x,y) (x=x>y?x:y)
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef long long LL;
typedef unsigned long long uLL;
#define twoL(X) (((LL)(1))<<(X))
const double PI=acos(-1.0);
const double eps=1e-11;
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T a, T b) {if(a<0) return (gcd(-a,b)); if(b<0) return (gcd(a,-b)); return (b==0)?a:gcd(b,a%b);}
template <class T> T lcm(T a, T b) {return a*b/gcd(a,b);}
LL toLL(string s) { istringstream sin(s); LL t; sin>>t; return t;}
int toInt(string s) {istringstream sin(s); int t; sin>>t; return t;}
string toString(LL v) {ostringstream sout; sout<<v; return sout.str();}
string toString(int v) {ostringstream sout; sout<<v; return sout.str();}
#define FOREACH(it, a) for(typeof((a).begin()) it = (a).begin(); it!=(a).end(); ++it)
#define ALL(x) ((x).begin(), (x).end())
#define cross(a, b, c)  ((c).x-(a).x)*((b).y-(a).y)-((b).x-(a).x)*((c).y-(a).y)
#define sq_dist(p, q) ((p).x-(q).x)*((p).x-(q).x)+((p).y-(q).y)*((p).y-(q).y)

int n, c;

struct point {
    double w;
    int num;
};
vector<point> p;

double v[50005];
double v1[50005];

bool cmp(point p1, point p2) {
    return p1.w > p2.w;
}

int main()
{
    while ( scanf("%d%d", &n, &c) != EOF ) {
        for ( int i = 0; i <= c; ++i )
            v[i] = 0, v1[i] = 0;
        for ( int i = 0; i < n; ++i ) {
            int t1, t2, f;
            scanf("%d%d%d", &t1, &t2, &f);
            if ( f == 0 ) {
                for ( int j = c; j >= 0; --j ) {
                    if ( j+t1 > c )    continue;
                    if ( v[j] == 0 && j != 0 )    continue;
                    v[j+t1] = max(v[j+t1], v[j]+t2);
                }
            }
            else {
                point t;
                t.w = t2*1.0/t1;
                t.num = t1;
                p.PB(t);
            }
        }
        sort(p.begin(), p.end(), cmp);
        int cnd = 1;
        for ( int i = 0; i < p.size(); ++i ) {
            while ( p[i].num ) {
                v1[cnd] = v1[cnd-1]+p[i].w;
                cnd++;
                p[i].num--;
            }
        }
        for ( int i = 1; i <= c; ++i ) {
            if ( v[i] == 0 )    v[i] = v[i-1];
            if ( v1[i] == 0 )    v1[i] = v1[i-1];
        }
        double ans = 0.0;
        for ( int i = 0; i <= c; ++i )
            if ( v[i]+v1[c-i] > ans )    ans = v[i]+v1[c-i];
        printf("%.2lf\n", ans);
    }
    return 0;
}

如觉得还有问题 可参考其他相关文章
zoj3258 其他1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值