题目描述

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 Output

2
1
3

题目大意

将n根木头放入一台机器加工,若要放的木头的长度与宽度都分别大于或等于上一根的长度与宽度,则需要调整机器。第一根放之前也需要调整机器。求需要调整几次

大致思路

贪心问题。贪心标准为一次调整加工的木头尽可能的多。因此需要第一根木头尽可能的小,然后找出所有与它相容的木头。所求调整次数即为最优解。

下面的代码使用了数组(使用容器会方便一些,当时有些偷懒。。。。)先由小到大sort,从第一个开始,每当找到上一个相容的即从数组中除去(重新赋值为-1,使数据无效)寻找到最末,然后从下一个有效数据开始重复以上过程,知道数组中全为无效数据,执行此过程的次数即为解。

AC代码

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. using namespace std;
  5. struct woods
  6. {
  7. int l;
  8. int w;
  9. }a[5000];
  10. bool cmp(woods a, woods b)
  11. {
  12. if (a.l < b.l)
  13. return 1;
  14. else if (a.l == b.l)
  15. {
  16. if (a.w < b.w)
  17. return 1;
  18. }
  19. return 0;
  20. }
  21. int main()
  22. {
  23. //freopen("date.in", "r", stdin);
  24. //freopen("date.out", "w", stdout);
  25. int N,m,sum,flag,count;
  26. cin >> N;
  27. woods tem;
  28. for (int i = 0; i < N; i++)
  29. {
  30. sum = 0;
  31. flag = 1;
  32. cin >> m;
  33. for (int j = 0; j < m; j++)
  34. {
  35. cin >> a[j].l >>a[j].w;
  36. }
  37. sort(a, a+m, cmp);
  38. /*for (int l = 0; l < m; l++)
  39. {
  40. cout << a[l].l << " " << a[l].w << endl;
  41. }*/
  42. for (int k = 0; k < m ; k++)
  43. {
  44. //count = 0;
  45. tem.l = a[k].l;
  46. tem.w = a[k].w;
  47. if (a[k].l != -1)
  48. {
  49. sum++;
  50. for (int l = k; l < m; l++)
  51. {
  52. if (a[l].l >= tem.l&&a[l].w >= tem.w)
  53. {
  54. tem.l = a[l].l;
  55. tem.w = a[l].w;
  56. a[l].l = -1;
  57. a[l].w = -1;
  58. //count++;
  59. }
  60. }
  61. }
  62. }
  63. cout << sum<<endl;
  64. }
  65. }

SDAU课程练习--problemB(1001)的更多相关文章

  1. SDAU课程练习--problemQ(1016)

    题目描述 FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'med ...

  2. SDAU课程练习--problemG(1006)

    题目描述 Problem Description The highest building in our city has only one elevator. A request list is m ...

  3. SDAU课程练习--problemO(1014)

    题目描述 Before bridges were common, ferries were used to transport cars across rivers. River ferries, u ...

  4. SDAU课程练习--problemA(1000)

    题目描述 The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape i ...

  5. SDAU课程练习--problemC

    题目描述 Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji ...

  6. SDAU课程练习--problemE

    problemE 题目描述 "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@ ...

  7. 4、json-server的使用

    json-server 详解 转载于https://www.cnblogs.com/fly_dragon/p/9150732.html JSON-Server 是一个 Node 模块,运行 Expre ...

  8. 一分钟内搭建全web的API接口神器json-server详解

    JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...

  9. json-server 详解

    JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...

随机推荐

  1. ERROR 1406 : Data too long for column 解决办法

    解决办法: 在my.ini里找到 sql-mode=”STRICT_TRANS_TABLES,NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION” 把其中的STRIC ...

  2. H5的新应用-获取用户当前的地理坐标

    ------------------------------ <script type="text/javascript">                       ...

  3. akka 入门

    http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...

  4. 十一章:用CSS进行布局

    本章重点:盒模型与元素浮动. 盒模型: 1.CSS处理网页时,它认为每个元素都包含在一个不可见的盒子里.这就是众所周知的盒模型,这里的盒子由内容区域.内容区域周围的空间.内边距和外边缘和边框外面将元素 ...

  5. git(工作区,暂存区,管理修改,撤销修改,删除文件)

    工作区和暂存区 984次阅读 Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的l ...

  6. LeetCode OJ 219.Contains Duplicate 2

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  7. LeetCode OJ 217.Contains Duplicate

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  8. Django:之安全、国际化和session

    Django 安全 以下是关于Django安全的一些特征,它包括如何使基于Django的网站的一些建议. 关于安全的官方文档:https://docs.djangoproject.com/en/dev ...

  9. 路由器wan口连接不上的问题

    路由器:tp-link:系统:win8.1:网络类型:PPPoE 克隆“当前管理PC的MAC地址”后,无法连接,将网线直接连到电脑上可以连接,所以问题一定出在路由上,经过不断探索发现问题所在,通过命令 ...

  10. Oracle中清除BIN$开头的垃圾表的解决办法

    10g的新特性flashback闪回区 在10g中bin开头表示已经删除的放在回收站的表,oracle在删除表时并没有彻底的删除,而是把表放入回收站!purge recyclebin清空回收站即可. ...