首页 > 其他分享 >POJ31900(贪心)

POJ31900(贪心)

时间:2023-08-23 10:08:12浏览次数:37  
标签:begin end temp POJ31900 int num order 贪心


想对了一半,还是不扎实

  1. 原本想将初始化和之后处理一起放到for里面的(i.e. 将push, ans = 1等放到for里面),发现比较麻烦,然后死磕这个,要建函数什么的,看了人家的代码之后发现没有必要,当然是美观了一点。其实能不能将初始化和处理一起写最重要的是看你的思路 is clear or not, sometimes it can unified. But in this case, it need something in the queue, otherwise we need to check every time.
  2. Know how to compare with struct, it has different flavors, I think the compares below is clear for use.
  3. Why there is no different when pop the earliest element then push a current element between find the nearest element to modify?
    Because the cows are sorted by starting time, which means the cow you currently maintains is the earliest one. And our greedy algorithm is earlier appear earlier to use.
//#define LOCAL

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 50010

using namespace std;

struct interval{
    int begin;
    int end;
    int num;

    friend bool operator <(interval a, interval b){
        if(a.end == b.end)
            return a.begin < b.begin;

        return a.end > b.end;
    }
}a[N];

bool cmp(interval a, interval b){
    if(a.begin == b.begin)
        return a.end < b.end;

    return a.begin < b.begin;   
}

int main(void){
#ifdef LOCAL
    freopen("data.in", "r", stdin);
#endif

    int n;
    int order[N]; 
    priority_queue<interval> q;

    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d%d", &a[i].begin, &a[i].end);
        a[i].num = i;
    }
    sort(a, a + n, cmp);

#ifdef LOCAL
    for(int i = 0; i < n; i++)
        printf("begin:%d end:%d num:%d\n", 
            a[i].begin, a[i].end, a[i].num);
#endif

    int ans = 1;
    q.push(a[0]);
    order[a[0].num] = ans;

    for(int i = 1; i < n; i++){
        interval temp = q.top();

#ifdef LOCAL
        printf("begin:%d end:%d num:%d\n", 
            temp.begin, temp.end, temp.num);
#endif

        if(temp.end < a[i].begin){
            order[a[i].num] = order[temp.num];
            q.pop();
        }else{
            ans++;
            order[a[i].num] = ans;
        }
        q.push(a[i]);
    }

    printf("%d\n", ans);
    for(int i = 0; i < n; i++)
        printf("%d\n", order[i]);

    return 0;
}


标签:begin,end,temp,POJ31900,int,num,order,贪心
From: https://blog.51cto.com/u_8999467/7199287

相关文章

  • POJ3253(贪心)
    1.要考虑到规模为20,000累加起来肯定会超的,要用longlong2.思想就是先从正着推,一定是先切掉最长的那块,这样之后都不会受影响;再反着来想,就是先合并最小的//#defineLOCAL#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#defineN20000using......
  • POJ2393(贪心)
    水题,comparecurrentstoringvalueandthepriceofthatday,ifcheaperthenassign,ifnotthenupdatethestorevalue.//#defineLOCAL#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;intmain(void){#ifd......
  • POJ3617(贪心)
    没啥好说的,书上原题,比较坑就是输出需要每80个换行。//#defineLOCAL#include<cstdio>#include<cstring>#include<algorithm>#defineSIZE2000usingnamespacestd;intmain(void){#ifdefLOCALfreopen("data.in","r",stdin);#endif......
  • POJ2376(贪心)
    参考:还是之前的技巧没有弄熟,在遇到这种直到满足某一条件时再进行改变时,就应该要想到用while,方便而且思路清晰。从这里可以看出逻辑还是不够清楚,边界条件还是拿捏的不好。//#defineLOCAL#include<cstdio>#include<cstring>#include<algorithm>#defineN25010usingnam......
  • POJ3069(贪心)
    贪心,自己太想当然了,直接left+2*R,这当然是不对的。。你咋知道left+R一定有点呢。。//#defineLOCAL#include<cstdio>#include<cstring>#include<algorithm>#defineN1000usingnamespacestd;intmain(void){#ifdefLOCALfreopen("data.in","r......
  • POJ2718(穷举,贪心)
    参考地址一开始连题意都没搞懂就开始直接做,tooyoung。应该静下来用5分钟分析,bytheway,maybethetypicalusageofbrute-forceis“void”functioninsteadoffunctionwithreturnvalue.//#defineLOCAL#include<cstdio>#include<cstring>#include<string>#incl......
  • hdu 1003 最大最长上升子序列 贪心
    要想找到符合条件的序列,我们应该有以下条件 一个数重头开始遍历相加,如果这个数大于0的话,继续加后面的数,如果小于0的话,重后面的数开始重新遍历;这个过程中保证了大数一定会出现,所以应该找出大数;sum大于0的话,与后面的数相加有可能是最大数;如果小于0,则,重新开始会比以前的数更大;一下是......
  • Prim算法是一种用于解决最小生成树问题的贪心算法。它通过逐步选择边来构建最小生成树
    importjava.util.*;classPrimAlgorithm{privatestaticfinalintINF=Integer.MAX_VALUE;publicvoidprimMST(int[][]graph){intvertices=graph.length;int[]parent=newint[vertices];//用于存储最小生成树的父节点int......
  • Kruskal算法是一种用于寻找图的最小生成树的贪心算法。它通过按照边的权重递增的顺序
    Kruskal算法可以通过生活中的例子来解释。我们可以将城市之间的道路网络看作是一个图,每个城市是一个顶点,道路是连接城市的边,而道路的长度可以看作是边的权重。假设我们想要修建一条连接所有城市的最小成本道路网络。首先,我们需要找到连接城市的所有道路,并按照道路的长度进行排......
  • 贪心,构造学习笔记
    贪心构造不会黄题绿题懵逼横批:依托答辩\(\text{CF1764C}\)题目描述有一些点,每一个点有一个点权\(a_i\)。你可以在任意点之间连边,最终的图需要满足不存在\(a,b,c\)满足\(a_a\leqslanta_b\leqslanta_c\)并且\(ab,bc\)之间有连边。思路点拨我们连出来的图一定可以......