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

    好久没打CF了,今天为了复健随手玩了一套题目

    A. Vasya and Book
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

    Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.

    Input

    The first line contains one integer tt (1t1031≤t≤103) — the number of testcases.

    Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1n,d1091≤n,d≤109, 1x,yn1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

    Output

    Print one line for each test.

    If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print 1−1.

    Example
    input
    Copy
    3
    10 4 5 2
    5 1 3 4
    20 4 19 3
    
    output
    Copy
    4
    -1
    5
    
    Note

    In the first test case the optimal sequence is: 421354→2→1→3→5.

    In the second test case it is possible to get to pages 11 and 55.

    In the third test case the optimal sequence is: 47101316194→7→10→13→16→19.

    可以直接翻到or翻到首页或者末页翻到

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            int n,x,y,d;
            cin>>n>>x>>y>>d;
            if(abs(x-y)%d==0)
            {
                cout<<abs(x-y)/d<<"
    ";
                continue;
            }
            else
            {
                if((y-1)%d&&(n-y)%d)
                {
                    cout<<"-1
    ";
                }
                else
                {
                    int ans=1e9+7;
                    if((y-1)%d==0)ans=min(ans,(x-2+d)/d+(y-1)/d);
                    if((n-y)%d==0)ans=min(ans,(n-x+d-1)/d+(n-y)/d);
                    cout<<ans<<"
    ";
                }
            }
        }
        return 0;
    }
    B. Vova and Trophies
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.

    The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.

    Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

    Input

    The first line contains one integer nn (2n1052≤n≤105) — the number of trophies.

    The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the ii-th trophy is a golden one, otherwise it's a silver trophy.

    Output

    Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

    Examples
    input
    Copy
    10
    GGGSGGGSGG
    
    output
    Copy
    7
    
    input
    Copy
    4
    GGGG
    
    output
    Copy
    4
    
    input
    Copy
    3
    SSS
    
    output
    Copy
    0
    
    Note

    In the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 77.

    In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44.

    In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 00.

    贪心,其实每次搞的都必须是相邻的两段,然后看看有没有多余的G就好

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n,f1=0,f2=0,f=0,ma=0;
        string s;
        cin>>n;
        cin>>s;
        for(int i=0; i<n; i++)
        {
            if(s[i]=='G')f++,f1++;
            else f2=f1,f1=0;
            ma=max(ma,f2+f1+1);
        }
        cout<<min(ma,f);
    }
    C. Multi-Subject Competition
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A multi-subject competition is coming! The competition has mm different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students.

    He has nn candidates. For the ii-th person he knows subject sisi the candidate specializes in and riri — a skill level in his specialization (this level can be negative!).

    The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.

    Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.

    (Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).

    Input

    The first line contains two integers nn and mm (1n1051≤n≤105, 1m1051≤m≤105) — the number of candidates and the number of subjects.

    The next nn lines contains two integers per line: sisi and riri (1sim1≤si≤m, 104ri104−104≤ri≤104) — the subject of specialization and the skill level of the ii-th candidate.

    Output

    Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or 00 if every valid non-empty delegation has negative sum.

    Examples
    input
    Copy
    6 3
    2 6
    3 6
    2 5
    3 5
    1 9
    3 1
    
    output
    Copy
    22
    
    input
    Copy
    5 3
    2 6
    3 6
    2 5
    3 5
    1 11
    
    output
    Copy
    23
    
    input
    Copy
    5 2
    1 -1
    1 -5
    2 -1
    2 -1
    1 -10
    
    output
    Copy
    0
    
    Note

    In the first example it's optimal to choose candidates 11, 22, 33, 44, so two of them specialize in the 22-nd subject and other two in the 33-rd. The total sum is 6+6+5+5=226+6+5+5=22.

    In the second example it's optimal to choose candidates 11, 22 and 55. One person in each subject and the total sum is 6+6+11=236+6+11=23.

    In the third example it's impossible to obtain a non-negative sum.

    这就是一个模拟题

    #include<stdio.h>
    #include<bits/stdc++.h>
    using namespace std;
    #define lson l,(l+r)/2,rt<<1
    #define rson (l+r)/2+1,r,rt<<1|1
    #define dbg(x) cout<<#x<<" = "<< (x)<< endl
    #define pb push_back
    #define fi first
    #define se second
    #define ll long long
    #define sz(x) (int)(x).size()
    #define pll pair<long long,long long>
    #define pii pair<int,int>
    #define pq priority_queue
    const int N=1e5+5,MD=1e9+7,INF=0x3f3f3f3f;
    const ll LL_INF=0x3f3f3f3f3f3f3f3f;
    const double eps=1e-9,e=exp(1),PI=acos(-1.);
    int n,m,ans;
    vector<int> a[N];
    int sum[N];
    int main()
    {
        cin.sync_with_stdio(0),cin.tie(0),cout.tie(0);
        cin>>n>>m;
        for(int i=1,s,r; i<=n; i++)cin>>s>>r,a[s].push_back(r);
        for(int i=1; i<=m; i++)
        {
            sort(a[i].begin(),a[i].end(),greater<int>());
            if(a[i].size())sum[1]+=max(0,a[i][0]);
            for(int j=1; j<a[i].size(); j++)a[i][j]+=a[i][j-1],sum[j+1]+=max(0,a[i][j]);
        }
        for(int i=1; i<=n; i++)ans = max(ans, sum[i]);
        cout<<ans<<"
    ";
        return 0;
    }
    D. Maximum Diameter Graph
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Graph constructive problems are back! This time the graph you are asked to build should match the following properties.

    The graph is connected if and only if there exists a path between every pair of vertices.

    The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.

    The degree of a vertex is the number of edges incident to it.

    Given a sequence of nn integers a1,a2,,ana1,a2,…,an construct a connected undirected graph of nn vertices such that:

    • the graph contains no self-loops and no multiple edges;
    • the degree didi of the ii-th vertex doesn't exceed aiai (i.e. diaidi≤ai);
    • the diameter of the graph is maximum possible.

    Output the resulting graph or report that no solution exists.

    Input

    The first line contains a single integer nn (3n5003≤n≤500) — the number of vertices in the graph.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ain11≤ai≤n−1) — the upper limits to vertex degrees.

    Output

    Print "NO" if no graph can be constructed under the given conditions.

    Otherwise print "YES" and the diameter of the resulting graph in the first line.

    The second line should contain a single integer mm — the number of edges in the resulting graph.

    The ii-th of the next mm lines should contain two integers vi,uivi,ui (1vi,uin1≤vi,ui≤n, viuivi≠ui) — the description of the ii-th edge. The graph should contain no multiple edges — for each pair (x,y)(x,y) you output, you should output no more pairs (x,y)(x,y) or (y,x)(y,x).

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

    Here are the graphs for the first two example cases. Both have diameter of 22.

    d1=1a1=2d1=1≤a1=2

    d2=2a2=2d2=2≤a2=2

    d3=1a3=2d3=1≤a3=2

    d1=1a1=1d1=1≤a1=1

    d2=4a2=4d2=4≤a2=4

    d3=1a3=1d3=1≤a3=1

    d4=1a4=1d4=1≤a4=1

  • 相关阅读:
    SQL数据去重复 Distinct 和 row_number() over()
    Excel闪退问题解决
    SQL Server 修改服务器登录名称以及密码
    从底层角度看ASP.NET-A low-level Look at the ASP.NET Architecture
    MD5加密
    MD5实例化异常 未能执行FIPS验证加密算法
    JDBC(上)
    自学MySQL第二天
    自学MySQL第一天
    自学JavaWeb第五天jQuery进阶
  • 原文地址:https://www.cnblogs.com/BobHuang/p/10067563.html
Copyright © 2011-2022 走看看