zoukankan      html  css  js  c++  java
  • HDOJ 1050 Moving Tables 【贪心】

    HDOJ 1050 Moving Tables 【贪心】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1050


    这个题是根据贪心训练所以写的贪心
    但是有一种更简单的求法就是求最大重复区间:
    【将走廊分成200块,每次输入后就对涉及到的走廊+1
    最后输出这些数组里最大的数*10就可以了】
    上面这种写法比较简单,不贴代码了


    贪心的话首先是把输入的区间预处理:
    1、左区间小于右区间,不符合的话交换一下
    2、左边的数据是偶数的话就-1换成奇数
    3、右边的数据是奇数的话就+1换成偶数
    以上操作是将涉及到的区间扩展到最大边界
    然后贪心一直跑
    每跑一趟就将可以同时进行的操作标记为vis,同时num++
    然后再从头开始将没有访问过的区间标记vis,num++
    最后所有的区间都访问过后,输出最后的num*10即可


    这个题一开始区间排序是按右区间从小到大,再左区间从小到大排
    然而总是wa……
    换成先按左区间从小到大排再按右区间从小到大就过了……
    反例见下:
    1
    19
    75 154
    125 158
    176 48
    196 65
    21 171
    15 170
    17 100
    61 116
    3 189
    98 104
    112 19
    163 66
    42 14
    81 168
    53 165
    36 143
    84 140
    105 199
    195 151
    AC代码结果为150,WA代码结果为160
    **按照右房间排序后,右房间的号码一旦大于之后所有的左房间号码,
    那么之后的所有区间就是每个区间占用一个新的num
    **按照左房间排序,右房间号码过大后,下一个区间右房间依然可能变小,所以每次机会都是均等的,可以进行贪心
    所以……要那么排……就素酱紫……


    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    typedef struct point{
        int x, y;
        int vis;
        bool operator < (const point& p) const{
            if(x == p.x) return y < p.y;
            else return x < p.x;
        }
        bool operator > (const point& p) const{
            return p < *this;
        }
    }p;
    int T, N;
    p room[205];
    int End, num, cnt;
    
    int main(){
        scanf("%d", &T);
        while(T--){
            scanf("%d", &N);
            for(int i = 0; i < N; i++) scanf("%d%d", &room[i].x, &room[i].y);
            for(int i = 0; i < N; i++){
                room[i].vis = false;
                if(room[i].x > room[i].y) swap(room[i].x, room[i].y);
                if(!(room[i].x & 1)) room[i].x -= 1;
                if(room[i].y & 1) room[i].y += 1;
            }
            sort(room, room+N);
            //for(int i = 0; i < N; i++) printf("%d	%d
    ", room[i].x, room[i].y);
            cnt = 0;
            num = 0;
            while(cnt != N){
                for(int i = 0; i < N; i++){
                    if(room[i].vis == 0){
                        End = room[i].y;
                        room[i].vis = true;
                        cnt++;
                        num++;
                        break;
                    }
                }
                for(int i = 1; i < N; i++){
                    if(room[i].x >= End && room[i].vis == false){
                        End = room[i].y;
                        cnt++;
                        room[i].vis = true;
                    }
                }
            }
            printf("%d
    ", num*10);
        }
    
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    克罗内克符号
    子进程和线程
    力扣:数学问题
    iconv装换文件编码格式
    mac 上iterm终端显示中文为乱码解决方案
    Android启动时间测试方法
    Centos4.3安装MySQL-python-1.2.3,出现error: command 'gcc' failed with exit status 1
    java mail发送邮件
    Java通过socket实现smtp协议发送邮件
    vim解决中文显示乱码问题
  • 原文地址:https://www.cnblogs.com/miaowTracy/p/4836755.html
Copyright © 2011-2022 走看看