zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 22

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!

    This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.

    Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx ≤ T ≤ rx.

    Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.

    Input

    The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers ai (1 ≤ ai ≤ 105) — the time Pasha needs to solve ith problem.

    The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 ≤ lj < rj ≤ 105) — the starting and the ending moment of jth period.

    It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1is met.

    Output

    If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.

    Otherwise print "-1" (without brackets).

    Examples
    input
    2
    3 4
    2
    1 4
    7 9
    output
    7
    input
    1
    5
    1
    1 4
    output
    -1
    input
    1
    5
    1
    1 5
    output
    5
    Note

    In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.

    In the second example Pasha invents the solution only after the website stops working for the last time.

    In the third example Pasha sends the solution exactly at the end of the first period.

    A题是个简单题,你有n个问题,每个问题都有一定的时间,给你m个开始的时间和结束的时间,如果他可以做完就输出他花时间最小的那个。做不完就输出-1

    所以你需要知道你需要有多长的时间ak,即n个问题的时间,进行判断,如果开始的时间结束的时间比总时间小,就说明可能,但是开始的时间比总时间小就说明最小时间是总时间,否则就输出开始的时间

    #include<bits/stdc++.h>
    using namespace std;
    int main() {
        int n,x,sum=0,l,r;
        scanf("%d",&n);
        while(n--) {
            scanf("%d",&x);
            sum+=x;
        }
        scanf("%d",&n);
        while(n--) {
            scanf("%d%d",&l,&r);
            if(r<sum) {
                continue;
            }
            if(l<=sum) {
                printf("%d
    ",sum);
                return 0;
            }
            printf("%d
    ",l);
            return 0;
        }
        printf("-1
    ");
        return 0;
    }
    C. The Tag Game
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Alice got tired of playing the tag game by the usual rules so she offered Bob a little modification to it. Now the game should be played on an undirected rooted tree of n vertices. Vertex 1 is the root of the tree.

    Alice starts at vertex 1 and Bob starts at vertex x (x ≠ 1). The moves are made in turns, Bob goes first. In one move one can either stay at the current vertex or travel to the neighbouring one.

    The game ends when Alice goes to the same vertex where Bob is standing. Alice wants to minimize the total number of moves and Bob wants to maximize it.

    You should write a program which will determine how many moves will the game last.

    Input

    The first line contains two integer numbers n and x (2 ≤ n ≤ 2·105, 2 ≤ x ≤ n).

    Each of the next n - 1 lines contains two integer numbers a and b (1 ≤ a, b ≤ n) — edges of the tree. It is guaranteed that the edges form a valid tree.

    Output

    Print the total number of moves Alice and Bob will make.

    Examples
    input
    4 3
    1 2
    2 3
    2 4
    output
    4
    input
    5 2
    1 2
    2 3
    3 4
    2 5
    output
    6
    Note

    In the first example the tree looks like this:

    The red vertex is Alice's starting position, the blue one is Bob's. Bob will make the game run the longest by standing at the vertex 3 during all the game. So here are the moves:

    B: stay at vertex 3

    A: go to vertex 2

    B: stay at vertex 3

    A: go to vertex 3

    In the second example the tree looks like this:

    The moves in the optimal strategy are:

    B: go to vertex 3

    A: go to vertex 2

    B: go to vertex 4

    A: go to vertex 3

    B: stay at vertex 4

    A: go to vertex 4

     

     这个题就是Bob和Alice在玩游戏,这个游戏最后的操作肯定是一个人一直往下追,另一个人往上爬一点以后开始向下跑,一遍dfs

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN=2e5+10;
    int n,x,sum=-1,u,v,ans[2][MAXN];
    bool mark[2][MAXN];
    vector<int>adj[MAXN];
    void dfs(int v,int num,bool id){
        mark[id][v]=true;
        ans[id][v]=num++;
        for(auto i:adj[v])
            if(!mark[id][i])
                dfs(i,num,id);
        return;
    }
    int main(){
        ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        cin>>n>>x;
        for(int i=1;i<n;i++){
            cin>>u>>v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        dfs(1,0,0);
        dfs(x,0,1);
        ans[0][1]=1;
        ans[1][x]=1;
        sum=2*ans[0][x];
        for(int i=1;i<=n;i++)
            if(ans[0][i]>ans[1][i])
                sum=max(sum,2*ans[0][i]);
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    不自导会专门样
    人撒娇地撒基督教扫ID祭扫我京东is啊单间
    随机生成字符
    阿朵洒洒的撒多撒多撒啊
    死循环
    ui其实比接口好做
    租到房子了
    阿里PTS基础版真的坑
    終于解決调用wordpress 4.3 xmlrpc api 发布包含分类的文章时返回“抱歉,文章类型不支持您的分类法”错误的問題
    诸恶莫作、众善奉行、自净其意、是诸佛教
  • 原文地址:https://www.cnblogs.com/BobHuang/p/6953364.html
Copyright © 2011-2022 走看看