zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 46 (Rated for Div. 2)

    链接http://codeforces.com/contest/1000

    B. Light It Up
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.

    The lamp allows only good programs. Good program can be represented as a non-empty array aa, where 0<a1<a2<<a|a|<M0<a1<a2<⋯<a|a|<M. All aiai must be integers. Of course, preinstalled program is a good program.

    The lamp follows program aa in next manner: at moment 00 turns power and light on. Then at moment aiai the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 11 and then do nothing, the total time when the lamp is lit will be 11. Finally, at moment MM the lamp is turning its power off regardless of its state.

    Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program aa, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of aa, or even at the begining or at the end of aa.

    Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from xx till moment yy, then its lit for yxy−x units of time. Segments of time when the lamp is lit are summed up.

    Input

    First line contains two space separated integers nn and MM (1n1051≤n≤105, 2M1092≤M≤109) — the length of program aa and the moment when power turns off.

    Second line contains nn space separated integers a1,a2,,ana1,a2,…,an (0<a1<a2<<an<M0<a1<a2<⋯<an<M) — initially installed program aa.

    Output

    Print the only integer — maximum possible total time when the lamp is lit.

    Examples
    input
    Copy
    3 10
    4 6 7
    output
    Copy
    8
    input
    Copy
    2 12
    1 10
    output
    Copy
    9
    input
    Copy
    2 7
    3 4
    output
    Copy
    6
    Note

    In the first example, one of possible optimal solutions is to insert value x=3x=3 before a1a1, so program will be [3,4,6,7][3,4,6,7] and time of lamp being lit equals (30)+(64)+(107)=8(3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5x=5 in appropriate place.

    In the second example, there is only one optimal solution: to insert x=2x=2 between a1a1 and a2a2. Program will become [1,2,10][1,2,10], and answer will be (10)+(102)=9(1−0)+(10−2)=9.

    In the third example, optimal answer is to leave program untouched, so answer will be (30)+(74)=6(3−0)+(7−4)=6.

    B. Light It Up
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.

    The lamp allows only good programs. Good program can be represented as a non-empty array aa, where 0<a1<a2<<a|a|<M0<a1<a2<⋯<a|a|<M. All aiai must be integers. Of course, preinstalled program is a good program.

    The lamp follows program aa in next manner: at moment 00 turns power and light on. Then at moment aiai the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 11 and then do nothing, the total time when the lamp is lit will be 11. Finally, at moment MM the lamp is turning its power off regardless of its state.

    Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program aa, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of aa, or even at the begining or at the end of aa.

    Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from xx till moment yy, then its lit for yxy−x units of time. Segments of time when the lamp is lit are summed up.

    Input

    First line contains two space separated integers nn and MM (1n1051≤n≤105, 2M1092≤M≤109) — the length of program aa and the moment when power turns off.

    Second line contains nn space separated integers a1,a2,,ana1,a2,…,an (0<a1<a2<<an<M0<a1<a2<⋯<an<M) — initially installed program aa.

    Output

    Print the only integer — maximum possible total time when the lamp is lit.

    Examples
    input
    Copy
    3 10
    4 6 7
    output
    Copy
    8
    input
    Copy
    2 12
    1 10
    output
    Copy
    9
    input
    Copy
    2 7
    3 4
    output
    Copy
    6
    Note

    In the first example, one of possible optimal solutions is to insert value x=3x=3 before a1a1, so program will be [3,4,6,7][3,4,6,7] and time of lamp being lit equals (30)+(64)+(107)=8(3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5x=5 in appropriate place.

    In the second example, there is only one optimal solution: to insert x=2x=2 between a1a1 and a2a2. Program will become [1,2,10][1,2,10], and answer will be (10)+(102)=9(1−0)+(10−2)=9.

    In the third example, optimal answer is to leave program untouched, so answer will be (30)+(74)=6(3−0)+(7−4)=6.

    这是一道用贪心算法的题目,即在区间中插入一个数,整个序列最多能增加多少。假设区间为[a,b],则插入的值为a+1或b-1,取决于插入的位置是开还是关。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #define DEBUG(x) cout<<#x<<" = "<<x<<endl
    using namespace std;
    typedef long long ll;
    const int MAXN=1e5+10;
    int n,M;
    int prog[MAXN];
    ll litTime[MAXN];
    ll OnLitTimeToEnd[MAXN];
    ll OffLitTimeToEnd[MAXN];
    int main()
    {
    //    freopen("in.txt","r",stdin);
        scanf("%d%d",&n,&M);
        prog[0]=0,prog[n+1]=M;
        litTime[0]=0;
        for(int i=1;i<=n+1 ;i++ ){
            if(i<n+1)
                scanf("%d",&prog[i]);
            if(i%2)litTime[i]=litTime[i-1]+prog[i]-prog[i-1];
            else litTime[i]=litTime[i-1];
        }
        OffLitTimeToEnd[n+1]=0;
        OnLitTimeToEnd[n+1]=0;
        for(int i=n;i>=0 ;i-- ){
            OnLitTimeToEnd[i]=OffLitTimeToEnd[i+1]+prog[i+1]-prog[i];
    ///踩了一个坑     OnLitTimeToEnd[i]=OffLitTimeToEnd[i-1]+prog[i+1]-prog[i];
            OffLitTimeToEnd[i]=OnLitTimeToEnd[i+1];
        }
        int ans=litTime[n+1];
        for(int i=1;i<=n+1 ;i++ ){
            int t;
            if(i%2){///
                t=prog[i]-1-prog[i-1]+litTime[i-1]+OnLitTimeToEnd[i];
            }
            else {///
                t=prog[i]-(prog[i-1]+1)+litTime[i-1]+OffLitTimeToEnd[i];
            }
            ans=max(ans,t);
        }
        printf("%d
    ",ans);
    }
    View Code
    C. Covered Points Count
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given nn segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

    Your task is the following: for every k[1..n]k∈[1..n], calculate the number of points with integer coordinates such that the number of segments that cover these points equals kk. A segment with endpoints lili and riri covers point xx if and only if lixrili≤x≤ri.

    Input

    The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of segments.

    The next nn lines contain segments. The ii-th line contains a pair of integers li,rili,ri (0liri10180≤li≤ri≤1018) — the endpoints of the ii-th segment.

    Output

    Print nn space separated integers cnt1,cnt2,,cntncnt1,cnt2,…,cntn, where cnticnti is equal to the number of points such that the number of segments that cover these points equals to ii.

    Examples
    input
    Copy
    3
    0 3
    1 3
    3 8
    output
    Copy
    6 2 1 
    input
    Copy
    3
    1 3
    2 4
    5 7
    output
    Copy
    5 2 0 
    Note

    The picture describing the first example:

    Points with coordinates [0,4,5,6,7,8][0,4,5,6,7,8] are covered by one segment, points [1,2][1,2] are covered by two segments and point [3][3] is covered by three segments.

    The picture describing the second example:

    Points [1,4,5,6,7][1,4,5,6,7] are covered by one segment, points [2,3][2,3] are covered by two segments and there are no points covered by three segments.

    按照读入一个区间,统计区间里的点覆盖数目的做法肯定超时。但我想了很久也没有想出一个可行的办法。在网上搜了一下,除了惊叹于大神的能力,对自己平时只顾ac,没有深入理解算法技巧的刷题套路感到羞愧。其实这题用到的思路在运用线段树求面积的题目中出现过,核心思想就是保存当前的覆盖点的数目。具体的做法将所有的点按照坐标的大小从小到大排序,然后处理每一个点。看来不光要做题,更重要的是要体会总结算法这么做的原因和意义,才能够举一反三

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #define DEBUG(x) cout<<#x<<" = "<<x<<endl
    typedef long long ll;
    using namespace std;
    const int MAXN=4e5+10;
    struct Point{
        ll pos;
        ll delta;
        Point(){}
        Point(ll p,ll d):pos(p),delta(d){}
        bool operator<(const Point &p)const
        {
            if(pos!=p.pos)return pos<p.pos;
            return delta>p.delta;
        }
    };
    int N;
    Point points[MAXN];
    ll ans[MAXN];
    int main()
    {
    //    freopen("in.txt","r",stdin);
        scanf("%d",&N);
        for(int i=0;i<2*N ;i++ ){
            scanf("%lld",&points[i].pos);
            if(i%2==0)points[i].delta=1;
            else {
                    points[i].delta=-1;
                    ///需要包括右端点
                    points[i].pos++;
            }
        }
        sort(points,points+2*N);
        int curCov=0;
        for(int i=0;i<2*N-1 ;i++ ){
            curCov+=points[i].delta;
            ans[curCov]+=points[i+1].pos-points[i].pos;
        }
        for(int i=1;i<=N ;i++ ){
            printf("%lld",ans[i]);
            if(i==N)printf("
    ");
            else printf(" ");
        }
    }
    View Code
  • 相关阅读:
    【前缀和】【分类讨论】hdu5163 Taking Bus
    【DFS】bzoj2079 [Poi2010]Guilds
    【贪心】bzoj3850 ZCC Loves Codefires
    【分类讨论】bzoj3856 Monster
    【莫队算法】【权值分块】bzoj2223 [Coci 2009]PATULJCI
    【枚举】bzoj1709 [Usaco2007 Oct]Super Paintball超级弹珠
    【矩阵哈希】【二分答案】【哈希表】bzoj1567 [JSOI2008]Blue Mary的战役地图
    【矩阵哈希】【哈希表】bzoj2351 [BeiJing2011]Matrix
    【哈希表】CODEVS1230 元素查找
    【二分答案】【哈希表】【字符串哈希】bzoj2946 [Poi2000]公共串
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/9290360.html
Copyright © 2011-2022 走看看