zoukankan      html  css  js  c++  java
  • Codeforces Round #371 (Div. 2) A ,B , C 水,水,trie树

    A. Meeting of Old Friends
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!

    Sonya is an owl and she sleeps during the day and stay awake from minute l1 to minute r1 inclusive. Also, during the minute k she prinks and is unavailable for Filya.

    Filya works a lot and he plans to visit Sonya from minute l2 to minute r2 inclusive.

    Calculate the number of minutes they will be able to spend together.

    Input

    The only line of the input contains integers l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ r2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks.

    Output

    Print one integer — the number of minutes Sonya and Filya will be able to spend together.

    Examples
    input
    1 10 9 20 1
    output
    2
    input
    1 100 50 200 75
    output
    50
    Note

    In the first sample, they will be together during minutes 9 and 10.

    In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.

    题意:两个区间交集,去掉k这个点;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=1e6+1010,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int main()
    {
        ll l1,r1,l2,r2,k;
        scanf("%lld%lld%lld%lld%lld",&l1,&r1,&l2,&r2,&k);
        ll l=max(l1,l2);
        ll r=min(r1,r2);
        if(l>r)
        printf("0
    ");
        else if(k>=l&&k<=r)
        printf("%lld
    ",max(r-l,0LL));
        else
        printf("%lld
    ",max(0LL,r-l+1));
        return 0;
    }
    B. Filya and Homework
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help.

    Filya is given an array of non-negative integers a1, a2, ..., an. First, he pick an integer x and then he adds x to some elements of the array (no more than once), subtract x from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal.

    Now he wonders if it's possible to pick such integer x and change some elements of the array using this x in order to make all elements equal.

    Input

    The first line of the input contains an integer n (1 ≤ n ≤ 100 000) — the number of integers in the Filya's array. The second line containsn integers a1, a2, ..., an (0 ≤ ai ≤ 109) — elements of the array.

    Output

    If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes).

    Examples
    input
    5
    1 3 3 2 1
    output
    YES
    input
    5
    1 2 3 4 5
    output
    NO
    Note

    In the first sample Filya should select x = 1, then add it to the first and the last elements of the array and subtract from the second and the third elements.

    题意:找到一个x,将数组中每个数+x,-x,不变使得数组全为一个数;

    思路:去重,剩下的数个数大于3肯定no,小于3肯定yes,等于3,判断是否等差;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=1e6+1010,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int a[N];
    int main()
    {
        int x;
        scanf("%d",&x);
        for(int i=0;i<x;i++)
        scanf("%d",&a[i]);
        sort(a,a+x);
        int ans=unique(a,a+x)-a;
        if(ans>3)
        printf("NO
    ");
        else if(ans<3)
        printf("YES
    ");
        else
        {
            sort(a,a+3);
            if(a[1]-a[0]==a[2]-a[1])
            printf("YES
    ");
            else
            printf("NO
    ");
        }
        return 0;
    }
    C. Sonya and Queries
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type:

    1.  +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
    2.  -  ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset.
    3. s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.

    For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not.

    Input

    The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.

    Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.

    It's guaranteed that there will be at least one query of type '?'.

    It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.

    Output

    For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.

    Examples
    input
    12
    + 1
    + 241
    ? 1
    + 361
    - 241
    ? 0101
    + 101
    ? 101
    - 101
    ? 101
    + 4000
    ? 0
    output
    2
    1
    2
    1
    1
    input
    4
    + 200
    + 200
    - 200
    ? 0
    output
    1
    Note

    Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input.

    1. 1 and 241.
    2. 361.
    3. 101 and 361.
    4. 361.
    5. 4000.

     思路:trie数模板题,map应该也可以过;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define mod 1000000007
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=4e6+10,inf=1e9+10;
    int a[M][3],sum[M],len;
    void init()
    {
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        len=1;
    }
    void insertt(ll x)
    {
        int num[20];
        memset(num,0,sizeof(num));
        int flag=0;
        while(x)
        {
            num[flag++]=x%2;
            x/=10;
        }
        int u=0,n=19;
        for(int i=n; i>=0; i--)
        {
            if(!a[u][num[i]])
            {
                a[u][num[i]]=len++;
            }
            u=a[u][num[i]];
            sum[u]++;
        }
    }
    void del(ll x)
    {
        int num[20];
        memset(num,0,sizeof(num));
        int flag=0;
        while(x)
        {
            num[flag++]=x%2;
            x/=10;
        }
        int u=0,n=19;
        for(int i=n; i>=0; i--)
        {
            int v=a[u][num[i]];
            sum[v]--;
            if(!sum[v])
            a[u][num[i]]=0;
            u=v;
        }
    }
    char gg[20];
    int getans(ll x)
    {
        scanf("%s",&gg);
        int num[20];
        memset(num,0,sizeof(num));
        int flag=0;
        for(int i=strlen(gg)-1;i>=0;i--)
        num[flag++]=gg[i]-'0';
        int u=0,n=19,v,w;
        int ans=0;
        for(int i=n; i>=0; i--)
        {
            if(!a[u][num[i]])
                return 0;
            u=a[u][num[i]];
        }
        return sum[u];
    }
    char ch[10];
    int main()
    {
        int T,cas;
        ll x;
        init();
        scanf("%d",&T);
        for(cas=1; cas<=T; cas++)
        {
            scanf("%s%",ch);
            if(ch[0]=='+')
            {
                scanf("%lld",&x);
                insertt(x);
            }
            else if(ch[0]=='-')
            {
                scanf("%lld",&x);
                del(x);
            }
            else
            {
                printf("%d
    ",getans(x));
            }
    
        }
        return 0;
    }
  • 相关阅读:
    一笔画问题(搜索)
    Sum
    js获取时间日期
    [Hibernate 的left join]Path expected for join!错误
    关于firefox下js中动态组装select时指定option的selected属性的失效
    mooltools扩展之前已经定义好的方法和json数据
    HttpSession, ActionContext, ServletActionContext 区别
    japidcontroller自动绑定的数据类型
    ConcurrentModificationException
    Hibernate中使用COUNT DISTINCT
  • 原文地址:https://www.cnblogs.com/jhz033/p/5873050.html
Copyright © 2011-2022 走看看