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 大意:
  

描述

C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于

第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?

输入

第一行是一个整数T,表示输入数据一共有T组。

每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示

木棒的长度和质量。

输出

处理这些木棒的最短时间。

先将木头按长度从小到大排列,然后假设从第一个木头开始,查询第2~n个木头,把不用时间的全部标记,再找到下一个未标记的木头为开始,时间加一,再将这块木头后面不用时间的全部标记,以此类推。

 #include<cstdio>
#include<algorithm>
using namespace std;
struct stu
{
int l,w;
}st[];
bool cmp(stu a,stu b)
{
if(a.l != b.l) //将木头按长度从小到大排列,长度一样按质量从小到大排列
return a.l<b.l;
else
return a.w<b.w;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,d[];
int n;
scanf("%d",&n);
for(i = ; i < n ; i++)
{
scanf("%d %d",&st[i].l,&st[i].w);
d[i]=; //标记查询过的木头,0为未标记
}
int ans=;
sort(st,st+n,cmp);
for(i = ; i < n ; i++)
{
if(!d[i])
{
d[i]=;
ans++; //ans表示时间
int w=st[i].w;
for(j = i+ ; j < n ; j++) //查询i以后的木头
{
if(!d[j] && st[j].w >= w) //因为木头的长度是从小到大排列,所以这只判断重量
{
d[j]=; //标记在i后处理不用时间的木头
w=st[j].w;
} }
}
}
printf("%d\n",ans);
}
}

杭电 1051 Wooden Sticks的更多相关文章

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

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

  2. HDU 1051 Wooden Sticks (贪心)

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

  3. HDOJ.1051 Wooden Sticks (贪心)

    Wooden Sticks 点我挑战题目 题意分析 给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w.第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍 ...

  4. 1051 Wooden Sticks

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

  5. HDU 1051 Wooden Sticks 贪心||DP

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

  6. HDU - 1051 Wooden Sticks 贪心 动态规划

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

  7. hdu 1051:Wooden Sticks(水题,贪心)

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

  8. HDOJ 1051. Wooden Sticks

    题目 There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The ...

  9. HDU 1051 Wooden Sticks

    题意: 有 n 根木棒,长度和质量都已经知道,需要一个机器一根一根地处理这些木棒. 该机器在加工过程中需要一定的准备时间,是用于清洗机器,调整工具和模板的. 机器需要的准备时间如下: 1.第一根需要1 ...

随机推荐

  1. D.出题人的手环

    链接:https://ac.nowcoder.com/acm/contest/358/D 题意: 出题人的妹子送了出题人一个手环,这个手环上有 n 个珠子,每个珠子上有一个数. 有一天,出题人和妹子分 ...

  2. Windows如何利用输入法简单的打出 ‘↑’ ‘↓’ ‘↖’等箭头

    ‘↑’  shang ‘↓’ xia ‘←’ zuo ‘→’ you ‘↖’ zuoshang ‘↙’  zuoxia ‘↗’  youshang ‘↘’  youxia

  3. vi编辑器查找

    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 你在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然后输入你要查找的关键字敲回车就可以了. 如 ...

  4. apache配置优化 - 解决apache环境下网站访问速度慢的问题(重点参考)

    如果apche访问量过大,将会导致页面打开迟缓,下载速度也降低,如果由于经费和环境问题,集群方案没有得以应用.可以通过对Apache2增加模块MPM来进行优化, 这里我选择线程型MPM加以优化:  开 ...

  5. 如何连接MDB数据,并且获取相关的数据

    不说了直接上代码: try{ String str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + address + & ...

  6. 解决“程序包管理器控制台”输入命令找不到Nuget包问题

    问题: 问题原因: Nuget源的地址上不去 解决办法: 1.将Nuget源更新为可以国内使用的官方Nuget源. 1)打开VS2013:工具-->Nuget程序包管理器-->程序包管理器 ...

  7. 一文带你读懂 Mysql 和 InnoDB存储引擎

    作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...

  8. hdu6376 度度熊剪纸条

    思路: 01背包.有些细节需要注意一下,比如k = 0的情况. 实现: #include <bits/stdc++.h> using namespace std; typedef pair ...

  9. 【extjs6学习笔记】0.1 准备:基础概念 (01)

    1. Ext.application 应用程序入口点 2. Ext.onReady() 页面加载完成后触发动作 3. Ext.define() 4. Ext.data.proxy.Proxy 5. E ...

  10. openssl安装介绍

    #因CentOS7默认安装了openssl1.0版本,需要删除该版本,才能安装openssl.1.0.2l版本yum remove -y openssl openssl-devel cd /usr/l ...