zoukankan      html  css  js  c++  java
  • poj 1065 Wooden Sticks 【贪心 新思维】

    题目地址:http://poj.org/problem?id=1065

    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

    题目抽象:给你一个序列,序列的每个元素包含两个值(x,y).现在希望找到最少数目的条件序列。
    条件序列是这样的:cur.x<=(cur+1).x && cur.y<=(cur+1).y
    满足条件的序列的最少的数目是多少?

    代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <ctype.h>
     5 #include <math.h>
     6 #include <cmath>
     7 #include <iostream>
     8 #include <string>
     9 #include <queue>
    10 #include <stack>
    11 #include <vector>
    12 #include <map>
    13 #include <algorithm>
    14 #define N 100000+100
    15 
    16 using namespace std;
    17 
    18 struct node
    19 {
    20     int len, weight;
    21     bool operator<(const node &dd)const{
    22         if(len==dd.len)
    23             return weight<dd.weight;
    24         else
    25             return len<dd.len;
    26     }
    27 }q[5010];
    28 
    29 int main()
    30 {
    31     int tg; scanf("%d", &tg);
    32     int i, j, k;
    33     int n;
    34 
    35     while(tg--){
    36         scanf("%d", &n);
    37         for(i=0; i<n; i++){
    38             scanf("%d %d", &q[i].len, &q[i].weight );
    39         }
    40 
    41         sort(q, q+n);
    42         int ans=0;
    43         node cur=q[0];
    44 
    45         bool vis[5010];
    46         memset(vis, false, sizeof(vis));
    47         vis[0]=true;
    48         //从当前出发 遍历整个数组只要有结构体满足条件就贪心吃掉
    49         //不断的进行这样的贪心,直到整个结构体数组集合元素全被吃掉为止
    50         //不同于以往的贪心的是:当前的不可用,不代表以后的不可用 并非遇到遇到一个不可用的
    51         //情况就停止了本组的计算!
    52         while(true){
    53             for(j=1; j<n; j++){
    54                 if( vis[j]==false && q[j].len>=cur.len && q[j].weight>=cur.weight ){
    55                     cur=q[j]; vis[j]=true;
    56                 }
    57             }//
    58             ans++; //完成了一次
    59             for(j=1; j<n; j++){
    60                 if(vis[j]==false){
    61                     cur=q[j]; vis[j]=true; break;
    62                 }
    63             }
    64             if(j==n) break;
    65         }
    66         printf("%d
    ",ans);
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    51nod 1179 最大的最大公约数 (数论)
    POJ 3685 二分套二分
    POJ 3045 贪心
    LIC
    HDU 1029 Ignatius and the Princess IV
    HDU 1024 Max Sum Plus Plus
    HDU 2389 Rain on your Parade
    HDU 2819 Swap
    HDU 1281 棋盘游戏
    HDU 1083 Courses
  • 原文地址:https://www.cnblogs.com/yspworld/p/4700372.html
Copyright © 2011-2022 走看看