poj 1065 贪心算法

Wooden Sticks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21899   Accepted: 9350

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 ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) .

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 2n 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

Source

Taejon 2001

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

struct Node{
    int x;
    int y;
};

int cmp(Node a,Node b)
{
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;

}
int main()
{
    int t;
    int n;
    Node node[5005];
    bool flag[5005]; //标志是否已经分组
    while(cin>>t)
    {

        while(t--)
        {
            memset(flag,0,sizeof(flag));
            cin>>n;
            int ans=0;
            for(int i=0;i<n;i++)
                cin>>node[i].x>>node[i].y;
            sort(node,node+n,cmp);
            for(int i=0;i<n;i++)
            {
                if(flag[i]==0)
                {
                    int p=node[i].y;
                    for(int j=i;j<n;j++)
                    {
                        if(flag[j]==0&&node[j].y>=p) {flag[j]=1;p=node[j].y;}
                    }
                    ans++;

                }
            }
            cout<<ans<<endl;

        }
    }

}

   整体思路就是,先按照长度对结构体排序,再进行遍历分组,如果满足p<y就满足分组条件,就修改标志位,这样遍历一遍,有ans个0,就有ans组,答案就是ans;

时间: 2024-10-12 21:45:10

poj 1065 贪心算法的相关文章

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 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 woodworkin

poj 3617 Best Cow Line (字符串反转贪心算法)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

poj 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心

参考链接:http://blog.csdn.net/xiaohuan1991/article/details/6956629 (HDU 1257 解题思路一样就不继续讲解) POJ 1065题意:给你n个木块,分别给出其长度和重量,然后要对这些木块进行加工,如果木块1的长度和重量都不大于木块2, 那么这两个木块可以放在一起加工,从而只用花费1个时间单位.问要如何进行加工从而能够花费最少时间单位. 知识点: 偏序集:若一个集合A为偏序集,那么满足:1.任意一个元素X属于集合,那么这个元素X≤X 2

贪心算法 -- painter

poj 2709 painter 题目要求 给定涂料,每套涂料含有3-12种不同的颜色(开始时候给定选用的颜料套的颜色数目),且一套涂料中每种颜色均有50ml.且一套涂料中的任意三种不同的颜色各X ml混合都可以获得 灰色颜料 X ml.  现在给定需要的各个颜色的数量(这些颜色都属于同一套颜料),以及需要的灰色颜色的数量.求最少需要多少套颜料才能获得这些颜色. 题目分析 直觉告诉我们,使用贪心算法可以解决这个问题.通过使用最多量的颜色可以获得至少需要的颜料的套数 minset, 用minset

贪心算法 -- gone fishing

poj 1042 gone fishing 题目要求: 由有n个湖, 按照顺序排列,一个人从第一个湖向最后一个湖行进(方向只能从湖0到湖n-1),途中可以在湖中钓鱼.在每个湖中钓鱼时,开始的5分钟内可以钓到 f[i] 条,之后每5分钟钓到的鱼数目递减 d[i] ,且每两个相邻的湖的距离 t[i] 给出(t[i] 表示 由第 i 个湖向第 i + 1个湖行进在路上花费5分钟的倍数的时间, 即如果t[3] = 4,则表示从第三个湖向第四个湖行进需要花费20分钟). 现给定总时间h 小时,求出在每个湖

POJ 1065 Wooden Sticks Greed,DP

排序后贪心或根据第二关键字找最长下降子序列 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath>

基本算法之贪心算法

看了刘汝佳大牛的黑书果然很有体会,虽然很难,但是真的题题经典,一定要坚持坐下去,下面我们来说说贪心法 贪心算法即是每次选择局部最优策略进行实施,而不去考虑对今后的影响.贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择,选择的贪心策略必须具备无后效性,即某个状态以前的过程不会影响以后的状态,只与当前状态有关. 下面来看一个题目: POJ1042 钓鱼(黑书) 链接:http://poj.org/problem?id=1042 贪心:为了不讨论在路上花费的时间,可以枚举到过的湖:比如:

回溯算法 和 贪心算法(全排列)

一:简介 (1)回溯法 又称试探法 回溯法的基本做法是深度优先搜索,是一种组织得井井有条的.能避免不必要重复搜索的穷举式搜索算法:基本思想是:从一条路往前走,能进则进,不能进则退回来,换一条路再试. 适用场景:当遇到某一类问题时,它的问题可以分解,但是又不能得出明确的动态规划或是递归解法,此时可以考虑用回溯法解决此类问题.回溯法的优点在于其程序结构明确,可读性强,易于理解,而且通过对问题的分析可以大大提高运行效率.但是,对于可以得出明显的递推公式迭代求解的问题,还是不要用回溯法,因为它花费的时间

POJ 2231 贪心吗?怎么感觉像是数学。。。。

过了两次用了两种不同方法,其实也差不多,如果真的暴力去求得话铁定超时,(好像优化减去一些不可能的情况也能过,但是我没尝试)所以找一下规律咯,没学过算法的渣只能靠思维做题了... #include<stdio.h> int a[10009]; int main() { int n,m,t,i,j; long long sum; while(scanf("%d",&n)!=EOF) { sum=0; for(i=1;i<=n;i++) scanf("%d