zoukankan      html  css  js  c++  java
  • HDU ACM 1051/ POJ 1065 Wooden Sticks

    Wooden Sticks

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8899    Accepted Submission(s): 3630

    【解题思路】初学贪心仅知道一些理论的知识,这题说是贪心自己也没啥感觉,做题的思路是在抛开贪心的概念然后按照自己的想法实现出来,实现之后想在思路中找到贪心的影子,只能看到每次都是找尽可能多的stick仅此而已,Pursuiting... 

    Wa的原因是当时没考虑到stick的选择可以是不连续的,直接将排序后的次元素的比较上将后面一个跟前面一个进行比较。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #define SIZE 5002
     6 
     7 using namespace std;
     8 
     9 typedef struct sticks{
    10     int L, w;
    11 }sticks;
    12 sticks stick[SIZE];
    13 bool visit[SIZE]; 
    14 int n;
    15 
    16 bool cmp(const sticks& a, const sticks& b)
    17 {
    18     if(a.w == b.w)
    19         return a.L < b.L;
    20     else return a.w < b.w;
    21 }
    22 
    23 int main()
    24 {
    25     #ifndef ONLINE_JUDGE
    26     freopen("input.txt", "r", stdin);
    27     #endif
    28     int T, sum;
    29     scanf("%d", &T);
    30     while(T--)
    31     {
    32         scanf("%d", &n);
    33         for(int i=0; i<n; ++i)
    34             scanf("%d%d", &stick[i].L, &stick[i].w);
    35         sort(stick, stick+n, cmp);
    36         sum = 0;
    37         memset(visit, false, sizeof(visit));
    38         int curmax = 0;
    39         for(int i=0; i<n; ++i)
    40         {
    41             if(visit[i]) continue;
    42             curmax = stick[i].L;
    43             sum++;
    44             for(int j=i+1; j<n; ++j)
    45             {
    46                 if(!visit[j] && stick[j].L >= curmax)
    47                 {
    48                     visit[j] = true;
    49                     curmax = stick[j].L;
    50                 }
    51             }    
    52         }
    53 /*        for(int i=0; i<n; ++i)
    54             if(!i || stick[i].L<stick[i-1].L) sum++;
    55 */
    56         printf("%d
    ", sum);
    57     }
    58     return 0;
    59 }

     

  • 相关阅读:
    vi 批量加注释与替换
    linux常用快捷键
    kubectl常用命令
    软件管理方法
    zabbix3.2升级3.4报错Database error
    zabbix显示中文乱码
    主从复制延迟及原因
    Python-- 文件与操作系统
    层次聚类
    盲源分离
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/3199850.html
Copyright © 2011-2022 走看看