【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维)

205 篇文章 4 订阅
98 篇文章 1 订阅

题干:

The Problem

Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched to form Yertle's throne. Each of the five thousand, six hundred and seven turtles ordered by Yertle has a different weight and strength. Your task is to build the largest stack of turtles possible.

Input

Standard input consists of several lines, each containing a pair of integers separated by one or more space characters, specifying the weight and strength of a turtle. The weight of the turtle is in grams. The strength, also in grams, is the turtle's overall carrying capacity, including its own weight. That is, a turtle weighing 300g with a strength of 1000g could carry 700g of turtles on its back. There are at most 5,607 turtles.

Output

Your output is a single integer indicating the maximum number of turtles that can be stacked without exceeding the strength of any one.

Sample Input

300 1000
1000 1200
200 600
100 101

Sample Output

3

 

题目大意:

给出n只乌龟(n<=6000)的体重和可以承受的重量。 求可以叠起来的最高的乌龟是多少。

解题报告:

  类似一个0-1背包,只不过他把真正的权值放在了第二维上,反而把原本的第二维放在了权值上,这样我们需要权值的时候就倒着判断一遍就好了。或者直接记录可以到达的最大值然后输出就行了。

 

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node {
	int w,p;//w体重,p承重 
} node[MAX];
bool cmp(Node a,Node b) {
	if(a.p!=b.p) return a.p<b.p;
	else return a.w < b.w; //貌似可以不加
}
int tot;
int dp[MAX];
int main()
{
	int q,qq;
	while(~scanf("%d%d",&q,&qq)) {
		node[++tot].w=q;
		node[tot].p=qq;
	}
	sort(node+1,node+tot+1,cmp);
	memset(dp,INF,sizeof dp);
	dp[0]=0;
	int maxx = 0;
	for(int i = 1; i<=tot; i++) {
		for(int j = maxx+1;j>=0; j--) {
			if(node[i].p > dp[j-1]+node[i].w) {
				dp[j] = min(dp[j],dp[j-1]+node[i].w);
				maxx = max(maxx,j);
			}
		}
	}
	printf("%d\n",maxx);
	return 0 ;
 }

二维去写:

一篇

另一篇

大概贴一个代码过来、

#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 1000000000;
const int maxn = 10000 + 5;
 
struct Node{
    int first, second;
    Node(int f=0, int s=0){
        this -> first = f;
        this -> second = s;
    }
}t[maxn];
 
bool cmp(Node a, Node b){
    return a.first+a.second < b.first+b.second;
}
 
int dp[maxn][maxn];
 
int main(){
    int w, c;
    int n = 0;
    while(scanf("%d%d", &w, &c) != EOF){
        t[n++] = Node(w, c-w);
    }
    sort(t, t+n, cmp);
    for(int i = 0;i < n;i++){
        fill(dp[i], dp[i]+maxn, INF);
        dp[i][0] = 0;
    }
    dp[0][1] = t[0].first;
    for(int i = 1;i < n;i++){
        for(int j = 1;j <= n;j++){
            dp[i][j] = dp[i-1][j];
            if(t[i].second >= dp[i-1][j-1]){
                dp[i][j] = min(dp[i][j], dp[i-1][j-1]+t[i].first);
            }
        }
    }
    int ans = 0;
    for(int i = n;i >= 0;i--){
        if(dp[n-1][i] != INF){
            ans = i;
            break;
        }
    }
    printf("%d\n", ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值