ZOJ 1025 Wooden Sticks(贪心 基础题)

题目链接

题意:

机器加工n个木条,每个木条有一个长度和重量。加工第一根木条需要1分钟的准备时间,接下来如果后一根木条的长度和质量都大于等于前一根木条,则不需要准备时间,否则需要1分钟的准备时间,求加工完所有木条最少时间。

比如有5根木条,长度和重量分别是(4,9),
(5,2), (2,1), (3,5), (1,4),则需要2分钟就可加工第1分钟加工(1,4), (3,5), (4,9);第2分钟加工 (2,1), (5,2);

思路:将木条按长度从小到大排序,dp[i]记录第i根木条是在什么时刻被加工的(初始化为1);

如果第i根木条比同一时间加工的木条长度都要短,质量都要轻的话,那么它的dp就不变,否则加1.

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5005;
typedef long long ll;
int n;
int dp[N];

struct node
{
	int l, w;
	bool operator< (const node &rhs) const{
		if(l != rhs.l) return l < rhs.l;
		return w <= rhs.w;
	}
}stick[N];

int main()
{
	int t;
	scanf("%d", &t);
    while(t--)
    {
    	scanf("%d", &n);
    	for(int i = 0; i < n; i++)
    	{
    		scanf("%d%d", &stick[i].l, &stick[i].w);
    	}
    	sort(stick, stick + n);
    	dp[0] = 1;
    	int ans = 0;
    	for(int i = 1; i < n; i++)
    	{
    		dp[i] = 1;
    		for(int j = 0; j < i; j++)
    		{
    			if(stick[j].w > stick[i].w && dp[i] == dp[j])
    			{
    				dp[i] = dp[j] + 1;
    			}
    		}
    	}
    	for(int i = 0; i < n; i++)
    	{
    		ans = max(ans, dp[i]);
    	}
    	printf("%d\n", ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-07 17:52:29

ZOJ 1025 Wooden Sticks(贪心 基础题)的相关文章

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

zoj 1025 - Wooden Sticks

题目:求一个序列中最大不上升子序列的个数. 分析:dp,LIS.一个序列中的不上升子序列的最小个数,是他的最大上升子序列长度. 证明:首先求串的最大上升子序列,那么每个元素一定属于一个不同的不下降串: 如果,取第一个最大上升子序列,那么每个元素一定是集合中的最大值: 这些最大值可以分别确定一个不上子串,对应一个集合,所以下界是|LIS|: 然后,假设存在一个集合P,最大值p不在lis中,他不属于上面的集合: 他自己确定一个不上升序列,设他前面的集合的最大值为lis(i): 则有lis(i)>=

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworkin

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDU Wooden Sticks (贪心)

Wooden Sticks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 19   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description There is a pile of n woo

hdu 1009 贪心基础题

B - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse contai