zoukankan      html  css  js  c++  java
  • hdu 6119 小小粉丝度度熊 (区间处理+尺取)

    http://acm.hdu.edu.cn/showproblem.php?pid=6119

    解题思路:给出的出发时间和结束时间对有重合的部分进行处理,然后用尺取法找出最后的结果。比赛的时候的确想到了用尺取的想法完成题目,但是代码能力不行没有想出来。

    AC代码:

     1 #include <iostream>
     2 #include <bits/stdc++.h>
     3 using namespace std;
     4 const int maxn=100005;
     5 struct node
     6 {
     7     int l,r;
     8 }p[maxn];
     9 int sum[maxn];
    10 bool cmp(node a,node b)
    11 {
    12     return a.l<b.l;
    13 }
    14 int main()
    15 {
    16     int n,m;
    17     while(~scanf("%d%d",&n,&m))
    18     {
    19         for(int i=0;i<n;i++)
    20         scanf("%d%d",&p[i].l,&p[i].r);
    21         sort(p,p+n,cmp);
    22         int len=0;
    23         int nowl=p[0].l,nowr=p[0].r;
    24         for(int i=1;i<n;i++)
    25         {
    26             if(p[i].l<=nowr)
    27             nowr=max(nowr,p[i].r);
    28             else
    29             {
    30                 p[len].l=nowl;
    31                 p[len++].r=nowr;
    32                 nowl=p[i].l;
    33                 nowr=p[i].r;
    34             }
    35         }
    36         p[len].l=nowl;
    37         p[len++].r=nowr;
    38         for(int i=0;i<len-1;i++)
    39         sum[i]=max(0,p[i+1].l-p[i].r-1);
    40         int l=0,r=0;
    41         int now=0,ans=0;
    42         while(r<len)
    43         {
    44             ans=max(ans,p[r].r-p[l].l+1+m-now);
    45             now+=sum[r++];
    46             while(now>m)
    47             now-=sum[l++];
    48         }
    49         printf("%d
    ",ans);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    hdu 4460spfa用map来实现
    hdu 2579
    hdu 2845
    hdu 4462
    hdu 4557
    hdu 4639
    URAL 2078 Bowling game
    UVA
    HDU 5773 The All-purpose Zero 脑洞LIS
    Codeforces Round #368 (Div. 2) C. Pythagorean Triples 数学
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/7356520.html
Copyright © 2011-2022 走看看