SDAU课程练习--problemB(1001)
题目描述
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代码
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct woods
{
int l;
int w;
}a[5000];
bool cmp(woods a, woods b)
{
if (a.l < b.l)
return 1;
else if (a.l == b.l)
{
if (a.w < b.w)
return 1;
}
return 0;
}
int main()
{
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int N,m,sum,flag,count;
cin >> N;
woods tem;
for (int i = 0; i < N; i++)
{
sum = 0;
flag = 1;
cin >> m;
for (int j = 0; j < m; j++)
{
cin >> a[j].l >>a[j].w;
}
sort(a, a+m, cmp);
/*for (int l = 0; l < m; l++)
{
cout << a[l].l << " " << a[l].w << endl;
}*/
for (int k = 0; k < m ; k++)
{
//count = 0;
tem.l = a[k].l;
tem.w = a[k].w;
if (a[k].l != -1)
{
sum++;
for (int l = k; l < m; l++)
{
if (a[l].l >= tem.l&&a[l].w >= tem.w)
{
tem.l = a[l].l;
tem.w = a[l].w;
a[l].l = -1;
a[l].w = -1;
//count++;
}
}
}
}
cout << sum<<endl;
}
}
SDAU课程练习--problemB(1001)的更多相关文章
- SDAU课程练习--problemQ(1016)
题目描述 FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'med ...
- SDAU课程练习--problemG(1006)
题目描述 Problem Description The highest building in our city has only one elevator. A request list is m ...
- SDAU课程练习--problemO(1014)
题目描述 Before bridges were common, ferries were used to transport cars across rivers. River ferries, u ...
- SDAU课程练习--problemA(1000)
题目描述 The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape i ...
- SDAU课程练习--problemC
题目描述 Here is a famous story in Chinese history. "That was about 2300 years ago. General Tian Ji ...
- SDAU课程练习--problemE
problemE 题目描述 "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@ ...
- 4、json-server的使用
json-server 详解 转载于https://www.cnblogs.com/fly_dragon/p/9150732.html JSON-Server 是一个 Node 模块,运行 Expre ...
- 一分钟内搭建全web的API接口神器json-server详解
JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...
- json-server 详解
JSON-Server 是一个 Node 模块,运行 Express 服务器,你可以指定一个 json 文件作为 api 的数据源. 安装json-server npm install -g json ...
随机推荐
- VC MFC工具栏(CToolBar)控件
一.工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创 ...
- ubuntu如何实现访问实际网络中windows共享文件夹
方法一: 首先在建立一个挂载目录. sudo mkdir /mnt/share 然后就把共享目录持载进去. 服务器:192.168.6.84 共享名:gg 用户名:administrator 密 码: ...
- js 第一天
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- CSS3秘笈:第二章
1.一个样式由两个元素组成:浏览器对其设置格式的网页元素(选择器,selector)和实际的格式化指令(声明块,declaration block). 2.简单的样式也包含了以下几个元素: (1)Se ...
- hdu_5705_Clock("巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5705 题意:给你一个时间和一个角度,问你下一个时针和分针形成给出的角度是什么时候 题解:我们可以将这个 ...
- php中的PHP_EOL换行符
看手册时发现PHP_EOL这个变量,查了下资料,原来是相当于换行符 在unix系列用 \n 在windows系列用 \r\n 在mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代 ...
- 安装你自己的perl modules
来源: http://www.cnblogs.com/itech/archive/2012/12/17/2822044.html 安装你自己的perl modules.当没有root权限的时候,需要安 ...
- profile和bash
24down votefavorite 12 I am reading about basic shell scripting from Linux Command Line and Shell Sc ...
- zf-关于更换页面,的各种问题。
问题1:找不到common 这个变量(集合)与layer这个js文件. 这里的common 就是一个方法集合,声明var common; common.abc = function(参数1,参数2, ...
- cin详解(cin.get()、cin.getline()、cin.clear()、cin.sync())
在C中,输入输出用scanf和printf,在输入数据的同时还需说明数据的类型,如果输入数据较多,那就很麻烦,而C++中也有相似的东西cin和cout,它们来自C++的一个名叫" iostr ...