uva10020-最小覆盖

题目链接 http://acm.hust.edu.cn/vjudge/problem/19688

解题思路

区间的最小覆盖问题

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX_LEN 100050
using namespace std;
typedef struct line {
    int l, r;
}Line;
Line lines[MAX_LEN];
Line cpy[MAX_LEN];
Line ans[MAX_LEN];
int m, n;
bool compare(Line x, Line y)
{
    return x.l<y.l;
}
void Solve()
{
    int start=0, ansp=0;
    int j=0;
    bool flag = true;
    while(j<n) {
        for(int i=j; i<n; i++) {
            cpy[i] = lines[i];
            if(cpy[i].l<start) cpy[i].l = start;
        }
        int maxr=0, now = -1;
        while(j<n && cpy[j].l==start) {
            if(cpy[j].r == start) { j++; continue; }
            else if(cpy[j].r > maxr) {
                maxr = cpy[j].r; now=j;
                j++;
            }
            else j++;
        }
    //    if(j<n && cpy[j].l!=start) j++;
        if(now!=-1) {
            start = lines[now].r;
            ans[ansp++] = lines[now];
        }
        else { flag = false; break; }
        if(maxr>=m) break;
    }
    if(flag) {
        printf("%d\n", ansp);
        for(int i=0; i<ansp; i++) printf("%d %d\n", ans[i].l, ans[i].r);
    }
    else printf("0\n");
}
int main()
{
    int cases;
    scanf("%d", &cases);
    while(cases--) {
        scanf("%d", &m);
        int l, r;
        char ch;
        n = 0;
        scanf("%d%d", &l, &r);
        while(!(l==0&&r==0)) {
            lines[n].l = l; lines[n].r = r; n++;
            scanf("%d%d", &l, &r);
        }
        sort(lines, lines+n, compare);
        Solve();
        if(cases) printf("\n");
    }
    return 0;
}
时间: 2024-10-08 12:19:15

uva10020-最小覆盖的相关文章

uva 11419 SAM I AM (最小覆盖 K&#246;nig定理)

uva 11419 SAM I AM 题目大意:给出一个R×C的网格,网格上棉纺了一些目标.可以在网格外发射子弹,子弹会沿着垂直或水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算出最少需要多少子弹,各从哪个位置发射,才能把所有目标全部打掉. 解题思路:K?nig定理:最小覆盖数等于最大匹配数.把目标所在的坐标,转化为XY结点,行看成X结点,列看成Y结点.那现在问题就变成了,如何选最少的结点,覆盖所有的边. 求最小覆盖的步骤大致如下:1)在右边找到一个未被匹配过的点,标记.2)走一条没被

KMP算法 - 求最小覆盖子串

KMP与最小覆盖子串 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串. 比如: 对于s="abcab",它的最小覆盖子串p="abc",因为p通过在它后面再接上一个p(即重叠0个字符),可以得到q="abcabc",此时s是q的子串. 对于s="ababab",它的最小覆盖子串为p="ab". 根据KMP算法的next数组的定

hdu-----(1151)Air Raid(最小覆盖路径)

Air Raid Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3378    Accepted Submission(s): 2223 Problem Description Consider a town where all the streets are one-way and each street leads from one

poj2185 Milking Grid (最小覆盖矩阵)

//给定一个由字符组成的矩阵,求出它的面积最小的覆盖矩阵 //可以求出每一行的最小覆盖子串的长度,只要对这些长度求最小公倍数,就可以获得最小覆盖矩阵的宽度. //同理,求出每一列的最小覆盖子串的长度,再求最小公倍数,就可以获得最小覆盖矩阵的高度了. # include <stdio.h> # include <string.h> # include <algorithm> using namespace std; char a[10010][100]; int next

Maple trees(最小覆盖圆)

Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 222 Accepted Submission(s): 79   Problem Description There are a lot of trees in HDU. Kiki want to surround all the trees with the minim

[hdu2119]二分图最小覆盖,最大匹配

题意:给一个01矩阵,每次可以选一行或一列,打掉上面所有的1,求打掉所有的1所需的最小次数. 思路:经典的模型了,二分图最小覆盖=最大匹配.所谓最小覆盖是指选最少的点关联所有的边.容易得到将行和列看成点,1看成边,那么就是选尽量少的行和列来关联所有的1,最小覆盖模型,用最大匹配做.可以选择匈牙利算法,或者直接最大流. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

hdu-----(1150)Machine Schedule(最小覆盖点)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5817    Accepted Submission(s): 2932 Problem Description As we all know, machine scheduling is a very classical problem in comput

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

POJ--2158--------------Milking Grid(最小覆盖字符矩阵)---(开二维kmp)

Milking Grid Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6169   Accepted: 2573 Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75

二分图最小覆盖

hdu1150 最小顶点覆盖 hdu1498 50 years, 50 colors 最小顶点覆盖 +枚举 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queu