poj_1065_Wooden Sticks
来源:互联网 发布:java 获取jar包中资源 编辑:程序博客网 时间:2024/06/06 00:20
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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).
(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
213
按照棍子长度从小到大排序,之后从前向后遍历,找到每一个都比前一个l,w都大的位置,把它们相应的位置信息都置为1。然后再从前到后扫描,看没有置为1的w加和。
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node
{
int l,w;
};
int vis[5002]={0};
node a[5002];
bool cmp(node a,node b)
{
return a.l<b.l||(a.l==b.l&&a.w<b.w);
}
void solve(int n)
{
int cnt=0;
for(int i=0;i<n;i++)
{
if(!vis[i])
{
vis[i] = 1;
cnt++;
int w = a[i].w;
for(int j = i+1; j < n; j++)
{
if(!vis[j] && a[j].w >= w)
{
vis[j] = 1;
w = a[j].w;
}
}
}
}
printf("%d\n",cnt);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++) scanf("%d%d",&a[i].l,&a[i].w);
sort(a,a+n,cmp);
solve(n);
}
return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node
{
int l,w;
};
int vis[5002]={0};
node a[5002];
bool cmp(node a,node b)
{
return a.l<b.l||(a.l==b.l&&a.w<b.w);
}
void solve(int n)
{
int cnt=0;
for(int i=0;i<n;i++)
{
if(!vis[i])
{
vis[i] = 1;
cnt++;
int w = a[i].w;
for(int j = i+1; j < n; j++)
{
if(!vis[j] && a[j].w >= w)
{
vis[j] = 1;
w = a[j].w;
}
}
}
}
printf("%d\n",cnt);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++) scanf("%d%d",&a[i].l,&a[i].w);
sort(a,a+n,cmp);
solve(n);
}
return 0;
}
0 0
- poj_1065_Wooden Sticks
- Sticks
- sticks
- sticks
- sticks
- Sticks
- sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- Sticks
- sticks
- Sticks
- 【进阶android】Volley源码分析——Volley的缓存
- 多线程编程 基础篇 (四)
- Snail—OC学习之本地数据持久化(归档)
- 使用EasyUI实现文本框内容长度验证,对文本框输入内容提示“剩余可输入字数”
- ffmpeg解码流程学习总结
- poj_1065_Wooden Sticks
- LATEX 固定表格列宽并居中
- Android 颜色透明度
- json在线解析
- Maven setting.xml配置说明
- VMWare虚拟机提供的桥接、NAT和Host-only的区别
- PID算法完全讲解
- spring mvc + jpa 事务无法提交
- ListView的复用及如何优化