• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

uva 10020 Minimal coverage (greedy)

uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=961

  Classical greedy problem!

The Code: 

View Code
 1 typedef pair<int, int> PII;
 2 typedef vector<PII> VPII;
 3 #define REP(i, n) for (int i = 0; i < (n); i++)
 4 
 5 VPII rec, out;
 6 
 7 void input() {
 8     rec.clear();
 9     int x, y;
10     while (~scanf("%d%d", &x, &y) && (x || y)) rec.PB(MPR(x, y));
11 }
12 
13 bool work(int n) {
14     if (!SZ(rec)) return false;
15     sort(ALL(rec));
16     int mk = -1, L = 0;
17     out.clear();
18     REP(i, SZ(rec)) {
19         if (rec[i].FI > L) {
20             if (mk == -1 || (rec[mk].SE < rec[i].FI) && (rec[mk].SE < n)) return false;
21             out.PB(rec[mk]);
22             if (rec[mk].SE >= n) return true;
23             L = rec[mk].SE;
24             mk = i;
25         }
26         if (~mk) {
27             if (rec[i].SE > rec[mk].SE) mk = i;
28         } else mk = i;
29     }
30     if (rec[mk].SE >= n) {
31         out.PB(rec[mk]);
32         return true;
33     }
34     return false;
35 }
36 
37 int main() {
38     int n, T;
39     scanf("%d", &T);
40     while (T-- && ~scanf("%d", &n)) {
41         input();
42         if (work(n)) {
43             printf("%d\n", SZ(out));
44             REP(i, SZ(out)) printf("%d %d\n", out[i].FI, out[i].SE);
45         } else {
46             puts("0");
47         }
48         if (T) puts("");
49     }
50     return 0;
51 }

 

——written by Lyon

posted @ 2013-02-24 22:51  LyonLys  阅读(156)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3