[算法入门经典] 区间最少覆盖问题 UVA 10020 - Minimal coverage
2014-08-11 21:15
411 查看
按算法入门经典的思路去模拟就好。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
using namespace std;
struct ak
{
int L, R;//分别代表区间左,右
}s[100001], ss[100001], tmp;
int cmp(const void *a, const void *b)
{
struct ak c = *(struct ak *)a;
struct ak d = *(struct ak *)b;
return c.L - d.L;
}
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
int m, k, kk, T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &m);
k = 0;
while(~scanf("%d %d", &s[k].L, &s[k].R), s[k].L || s[k].R)
{
k++;
}
qsort(s, k, sizeof(s[0]), cmp);
int temp = 0, maxx = 0, cnt = 0;//初始化区间起始位置,初始化计数
kk = 0;
if(s[0].L <= temp)//按L从小到大排序后,如果第一个区间的L > temp
{ //那一定无法覆盖[0,m]区间了
cnt = 1;
for(int i = 0 ; i < k && maxx < m; i++)
{
if(s[i].L > temp && s[i].R > maxx)//如果L > temp 并且 R > maxx
{ //那么需要更新temp开始位置,并且计数+1
temp = maxx;
++cnt;
ss[kk++] = tmp;
}
if(s[i].L <= temp)
{
if(s[i].R > maxx)
{
maxx = s[i].R;
tmp.L = s[i].L;
tmp.R = s[i].R;
}
}
else if(s[i].R > maxx)//如果L > temp 并且 R > maxx
{ //意味着不能完整覆盖,直接break
cnt = 0;
break;
}
}
}
if(kk == 0 || tmp.L != ss[kk-1].L || tmp.R != ss[kk-1].R)
{
ss[kk++] = tmp;
}
if(cnt)
{
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
{
printf("%d %d\n", ss[i].L, ss[i].R);
}
}
else
{
printf("0\n");
}
printf(T?"\n":"");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
using namespace std;
struct ak
{
int L, R;//分别代表区间左,右
}s[100001], ss[100001], tmp;
int cmp(const void *a, const void *b)
{
struct ak c = *(struct ak *)a;
struct ak d = *(struct ak *)b;
return c.L - d.L;
}
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
int m, k, kk, T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &m);
k = 0;
while(~scanf("%d %d", &s[k].L, &s[k].R), s[k].L || s[k].R)
{
k++;
}
qsort(s, k, sizeof(s[0]), cmp);
int temp = 0, maxx = 0, cnt = 0;//初始化区间起始位置,初始化计数
kk = 0;
if(s[0].L <= temp)//按L从小到大排序后,如果第一个区间的L > temp
{ //那一定无法覆盖[0,m]区间了
cnt = 1;
for(int i = 0 ; i < k && maxx < m; i++)
{
if(s[i].L > temp && s[i].R > maxx)//如果L > temp 并且 R > maxx
{ //那么需要更新temp开始位置,并且计数+1
temp = maxx;
++cnt;
ss[kk++] = tmp;
}
if(s[i].L <= temp)
{
if(s[i].R > maxx)
{
maxx = s[i].R;
tmp.L = s[i].L;
tmp.R = s[i].R;
}
}
else if(s[i].R > maxx)//如果L > temp 并且 R > maxx
{ //意味着不能完整覆盖,直接break
cnt = 0;
break;
}
}
}
if(kk == 0 || tmp.L != ss[kk-1].L || tmp.R != ss[kk-1].R)
{
ss[kk++] = tmp;
}
if(cnt)
{
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
{
printf("%d %d\n", ss[i].L, ss[i].R);
}
}
else
{
printf("0\n");
}
printf(T?"\n":"");
}
return 0;
}
相关文章推荐
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- 【区间覆盖问题】uva 10020 - Minimal coverage
- 【算法竞赛入门经典】多阶段决策问题 例题9-5 UVa12563
- 算法竞赛入门经典 8.3.1 棋盘覆盖问题
- UVA 10020 - Minimal coverage (贪心) 区间覆盖问题
- Uva 101 the block problem 木块问题(算法竞赛经典入门)STL vector
- UVA 10020 Minimal coverage 区间覆盖问题 贪心
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
- 【算法竞赛入门经典】树的最大独立集、树的唯一性问题 例题9-13 UVa1220
- 【算法竞赛入门经典】7.5 路径寻找问题 例题7-9 UVa1601(1)
- UVA - 10020 Minimal coverage(区间覆盖问题)
- 【算法竞赛入门经典】多阶段决策问题 例题9-4 UVa116
- 算法竞赛入门经典-前四章-UVa10300 - Ecological Premium-生态奖金-难度0
- 算法竞赛入门经典 例题9-3 硬币问题
- UVA 10382 Watering Grass (贪心 + 区间覆盖问题)
- 贪心算法之区间覆盖问题
- 算法入门经典 7.4.4 带宽问题 回溯 剪枝
- uvalive 2326 - Moving Tables(区间覆盖问题)
- DP DAG 9-3硬币问题(算法竞赛入门经典p162)
- UVA oj 练习水题 算法入门经典(大数系列)