Wooden Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 11627    Accepted Submission(s): 4807
Problem 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 woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to
prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:



(a) The setup time for the first wooden stick is 1 minute.

(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l' and weight w' if l<=l' and w<=w'. Otherwise, it will need 1 minute for setup.



You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is
a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).
 
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden
sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, ..., ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one
or more spaces.
 
Output
The output should contain the minimum setup time in minutes, one per line.
 
Sample Input
3
5
4 9 5 2 2 1 3 5 1 4
3
2 2 1 1 2 2
3
1 3 2 2 3 1
 
Sample Output
2
1
3

非常典型的贪心题。

题意:能够简化成给定n个物品,每一个物品有两项參数a,b。求这n个物品能组成的序列的最小个数使得每一个序列的每两项參数都是非减的。

题解:先依照递增的顺序给物品排序。然后从前往后推断每一个物品有多少个能与它组成合法序列,最后输出序列个数即是。

这题和南阳上那道矩形嵌套有些像,但矩形嵌套是求最长的序列,不能用贪心。得用DP。

#include <stdio.h>
#include <string.h>
#include <algorithm> #define maxn 5010 struct Node {
int x, y;
} arr[maxn];
bool vis[maxn]; bool cmp(Node a, Node b) {
return a.x < b.x;
} int main() {
int t, n, i, j, k, ans;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(i = 0; i < n; ++i)
scanf("%d%d", &arr[i].x, &arr[i].y);
std::sort(arr, arr + n, cmp);
memset(vis, 0, sizeof(bool) * n);
ans = 0;
for(i = 0; i < n; ++i) {
if(vis[i]) continue;
++ans; k = i; vis[i] = 1;
for(j = i + 1; j < n; ++j) {
if(vis[j]) continue;
if(arr[j].x >= arr[k].x && arr[j].y >= arr[k].y)
vis[k = j] = 1;
}
}
printf("%d\n", ans);
}
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU1051 Wooden Sticks 【贪婪】的更多相关文章

  1. Hdu1051 Wooden Sticks 2017-03-11 23:30 62人阅读 评论(0) 收藏

    Wooden Sticks Problem Description There is a pile of n wooden sticks. The length and weight of each ...

  2. hdu1051 Wooden Sticks(贪心+排序,逻辑)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu1051 Wooden Sticks

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1051 大意:求最少升序序列的个数. #include <cstdio> #include &l ...

  4. HDU1051:Wooden Sticks

    Problem Description There is a pile of n wooden sticks. The length and weight of each stick are know ...

  5. Wooden Sticks(hdu1051)

    Wooden Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  6. HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)

    嘤嘤嘤,实习半年多的小蒟蒻的第一篇博客(题解) 英文的: There is a pile of n wooden sticks. The length and weight of each stick ...

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

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16262 Accepted: 6748 Descri ...

  9. HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. 全文检索引擎Solr 指南

    全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T

  2. 采用Eclipse中间Maven构建Web项目错误(一)

    采用Eclipse中间Maven构建Web工程 1.在进行上述操作时.pom.xml一直报错 <project xmlns="http://maven.apache.org/POM/4 ...

  3. Xamarin.Android 入门实例(1)之获取与解析JSON

    1.Main.axml 视图界面 2.视图代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  4. android传感器;摇抽奖功能

    package com.kane.sensortest; import java.util.Random; import android.hardware.Sensor; import android ...

  5. MongoDB最新版本3.2.9下载地址

    https://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-3.2.9.zip?_ga=1.22538 ...

  6. Codeforces 12D Ball 树形阵列模拟3排序元素

    主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  7. 【转】Directx11 HelloWorld之HLSL的Effect框架的使用

    最近尝试用了下Directx下的Effect框架,作为一初学者初学者,说下为什么我们要使用Effect框架及其好处吧. 首先Effect最大好处的就是简单,使得编写Shader绘制的程序工作量大大下降 ...

  8. cocos2d-x 3.1.1 学习笔记[17] 关于这些活动功能

    供cocos2d-x通常使用的方法,我有一个好脸色.这项研究真的奖励. 向导首先,定义,实施一系列连续动作. 对于我们的行动能回调函数,我们必须申报并加以实施. void callBack(); vo ...

  9. HDU 2063:过山车(偶匹配,匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. java.lang.VerifyError

    public class VerifyErrorextends LinkageError Thrown when the "verifier" detects that a cla ...