POJ 1716 Integer Intervals#贪心

(~ ̄▽ ̄)~*

//求一个集合,这个集合与任意一个区间的交集,需至少有两个数字
//贪心过程:按n个区间的最右值从小到大对区间进行排列,
//集合首先取第一个区间的最右两个数字,
//到第二个区间,判断集合里的数有没有在区间里
//没有的话,就从第二个区间的最右开始往左取(cnt=0取最后两个数,cnt=1取最后一个数)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

struct interval
{
    int l,r;
}a[10005];

bool cmp(interval a,interval b)
{
    return a.r<b.r;
}

int main()
{
    int n,res=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d %d",&a[i].l,&a[i].r);
    sort(a,a+n,cmp);

    vector<int> Set;
    Set.push_back(a[0].r-1);
    Set.push_back(a[0].r);
    res=2;
    for(int i=1;i<n;i++)
    {
        int cnt=0;
        for(int j=0;j<Set.size();j++)
        {
            if(Set[j]>=a[i].l&&Set[j]<=a[i].r)
                cnt++;

        }
        if(cnt==0)
        {
            Set.push_back(a[i].r-1);
            Set.push_back(a[i].r);
            res+=2;
        }
        else if(cnt==1)
        {
            Set.push_back(a[i].r);
            res++;
        }
    }
    printf("%d\n",res);
    return 0;
}
时间: 2024-12-21 02:28:40

POJ 1716 Integer Intervals#贪心的相关文章

poj 1716 Integer Intervals

 Integer Intervals http://poj.org/problem?id=1716 Time Limit: 1000MS   Memory Limit: 10000K       Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. Write a program that: finds the m

POJ - 1716 Integer Intervals(差分约束系统)

题目大意:给出N个区间,要求你找出M个数,这M个数满足在每个区间都至少有两个不同的数 解题思路:还是不太懂差分约束系统,数学不太好 借鉴了别人的思路,感觉有点DP思想 设d[i]表示[0,i-1]这个区间有d[i]个数满足要求 则给定一个区间[a,b],就有d[b + 1] - d[a] >= 2(b + 1是因为b也算在区间内) 将其转换为d[a] - d[b + 1] <= -2,这是第一个式子 接着在区间[i,i+1]中满足 d[i + 1] - d[i] >= 0 转换为 d[i

poj1716 Integer Intervals 贪心

#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct node { int left,right; }c[10005]; bool cmp(node x,node y)//按照右端点排序 { if(x.right<y.right) return true; if(x.right==y.right&&x.left<y.lef

POJ 1716 Interger Intervals 差分约束(入门题)

题意:给出n个区间[a,b] n,a,b<=1e4,要求找到一个最小集合 使得每个区间至少有两个数在集合中.设d[i]为0~i中有多少个元素在集合中,mn,mx分别为左右端点 则对每个i=1..n都要满足 d[b[i]]-d[a[i]-1]>=2 保证等式有意义,d[i+1]<=d[i]+1 , d[i]<=d[i+1]全部化为小于号 d[a[i]-1]-d[b[i]]<=-2 若答案为ans 则d[mx]-d[mn-1]>=ans 把mx当作源点,求出到mn-1的最短

【POJ 1716】Integer Intervals(差分约束系统)

[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13425   Accepted: 5703 Description An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending w

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3085 Quick Change (贪心)

Quick Change Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5801   Accepted: 4175 Description J.P. Flathead's Grocery Store hires cheap labor to man the checkout stations. The people he hires (usually high school kids) often make mistak

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv