zoukankan      html  css  js  c++  java
  • ACM International Collegiate Programming Contest, Arab Collegiate Programming Contest 2013


    #include <bits/stdc++.h> using namespace std; int a[1005][1005] = {0}, n, m; int main() { int t; cin >> t; while(t--) { memset(a, 0, sizeof(a)); cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); } } int ans = -0x3f3f3f3f; for(int i = n; i >= 1; i--) { for(int j = m; j >= 1; j--) { a[i][j] += a[i][j + 1] + a[i + 1][j] - a[i + 1][j + 1]; ans = max(ans, a[i][j]); } } cout << ans << endl; } return 0; }
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            long long n, m, x;
            cin >> n >> m >> x;
            if(n < x) printf("NO
    ");
            else
            {
                int f = n % m;
                if(!f) f = m;
                if(f == x) printf("YES
    ");
                else printf("NO
    ");
             } 
        }
        return 0;
    }

    A.The Alphabet Sticker

    When we were kids, we used to play with some stickers where these stickers contain some (but not necessarily all) lower case English alphabet letters.

    Each sticker contains some letters arranged in a single row, where all occurrences of the same letter are adjacent to each other. A sticker can be represented as a string of characters, for example the following are valid stickers’ representations: “aabcc”, “ccccab” and “mmaw”. And the following are not valid (because not all occurrences of the same letter are adjacent to each other): “abacc”, “cccabc” and “mawm”.

    Now we found some stickers with some missing letters, but we are sure that all missing letters belong to the visible letters set (that is, for every missing letter, there is at least one visible letter that matches the missing one). In this problem a question mark letter represents a missing letter. Given some stickers’ representations with zero or more missing letters, your task is to count the number of possible original configurations for each sticker.

    For example, this sticker “aa??bb” with missing letters could have been one of the following original stickers“aaaabb”, “aaabbb” or “aabbbb”. But it could not have been any of the following original stickers “aababb”(it is invalid sticker) and “aaccbb” (because the letter ‘c’ did not appear in the given configuration).

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in one line which contains a non-empty string which consists of up to 10,000 letters, each letter is either a lowercase English letter (from ‘a’ to ‘z’) or a question mark (‘?’). This string represents a sticker configuration which contains zero or more question marks, it will also contain at least one letter which is not a question mark and there will be at least one valid original configuration for it.

    Output

    For each test case, print a single line which contains a single integer representing the number of possible original configurations for the sticker, since the result may be very large, print it modulo 1,000,000,007(10^9 + 7).

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入复制

    4

    aa??bb

    aaccbb

    ?a?

    a??a

    样例输出复制

    3

    1

    1

    1

    如果每段连续的?左右两边字母不同(比如a???bb)那么问号处很显然可以填aaa aab abb bbb这四种,由此可得这样的连续问号的填法一共有len + 1;如果两边字母相同则仅有一种。问题转化为判断有几个这种连续问号,把答案乘起来即可。注意和左右边界相接的问号段也仅有一种填法。

    #include <bits/stdc++.h>
    #define mod 1000000007
    using namespace std;
    struct line
    {
        int len;
        int diff;//0表示两边不同 1表示两边相同或者只有一边 
    };
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            string s;
            cin >> s;
            int start = -1, len = 0;
            char start_letter;
            vector<line> v;
            for(int i = 0; i < s.size(); i++)
            {
                if(i == 0)
                {
                    if(s[0] == '?')
                    {
                        start = 0;
                        len++;
                        start_letter = ' ';
                    }
                    continue;
                }
                if(s[i] == '?')
                {
                    if(s[i - 1] != '?')
                    {
                        len = 1;
                        start = i;
                        start_letter = s[i - 1];
                    }
                    else
                    {
                        len++;
                    }
                }
                else
                {
                    if(s[i - 1] == '?')
                    {
                        if(s[i] != start_letter && start != 0) v.push_back(line{i - start, 0});
                        else v.push_back(line{i - start, 1});
                        
                        len = 0;
                        start_letter = ' ';
                        start = -1;
                    }
                    else continue;
                }
            }
            if(len)
            {
                v.push_back(line{s.size() - 1 - start, 1});//最后还剩下 
            }
            long long ans = 1;
            if(v.size())
            {
                for(int i = 0; i < v.size(); i++) 
                {
                    if(!v[i].diff) ans = ans % mod * 1ll * (v[i].len % mod + 1) % mod;
                }
                cout << ans << endl;
            }
            else cout << 1 << endl;
        }
        return 0;
    }

    E. Balloons Colors

    Assigning a balloon color to each problem is one of the tasks we need to do every year, and sometimes itis tricky.

    We noticed that some contestants assume some colors for some problems according to the difficulty. Forexample, the easiest problem is the red one and the hardest problem is the black one. 

    We do not want these assumptions to be true, so we decided to add constraints for the easiest and thehardest problems.

    There are N problems, numbered from 1 to N , the easiest problem is problem number 1, and the hardestproblem is problem number N . Also there are N unique colors, for simplicity we will give each color aunique number from 1 to N .

    We want to assign each color to exactly 1 problem, such that the easiest problem does not get the colorX and the hardest problem does not get the color Y . 

    Given N , X , Y and an assignment of the colors, your task is to find if this assignment satisfies the aboveconditions or not.

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integer T,the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, the first line of each test case contains3 integers separated by a single space N X Y (3 ≤ N ≤ 100) and (1 ≤ X , Y ≤ N ) representing thenumber of problems, the color which the easiest problem should not get and the color which the hardestproblem should not get, respectively. Followed by a line which contains N integers separated by a singlespace (each integer from 1 to N should appear exactly once), the first integer is the color for the firstproblem (the easiest), the second integer is the color for the second problem and so on (the last integer isthe color for the hardest problem). 

    Output

    For each test case, print a single line which contains a single word, this word should be (without thequotes): 

    - “BOTH”: If both the easiest and hardest problems got colors which they should not get.

    - “EASY”: If only the easiest problem got a color which it should not get.

    - “HARD”: If only the hardest problem got a color which it should not get.

    - “OKAY”: If both the easiest and hardest problems got colors which they can get.

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    4
    3 1 2
    1 3 2
    5 3 4
    3 1 2 4 5
    6 1 6
    2 1 3 4 5 6
    7 7 7
    1 7 2 3 4 5 6

    样例输出

    BOTH
    EASY
    HARD
    OKAY

    水题。

    #include <bits/stdc++.h>
    using namespace std;
    int a[105]; 
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            int n, x, y;
            cin >> n >> x >> y;
            for(int i = 1; i <= n; i++)
            {
                cin >> a[i];
            }
            if(a[1] == x && a[n] == y)
            {
                cout << "BOTH" << endl; 
            } 
            else if(a[1] == x)
            {
                cout << "EASY" << endl;
            }
            else if(a[n] == y)
            {
                cout << "HARD" << endl;
            }
            else cout << "OKAY" << endl;
        }
    }

    F. NASSA's Robot

    NASSA’s robot landed on Mars. The place where it landed can be modeled as an infinite 2-dimensionalplane with perpendicular X-axis and Y-axis coordinates.

    The robot continuously reports its location back to Earth, but due to a serious design flaw, it only reportsthe moves it makes instead of the coordinates of its exact location. Some signals went missing and neverreached our reception. 

    In one of the space exploration missions, the robot sent a sequence of signals, which can be representedby a string composed of the following characters: ‘U’, ‘R’, ‘D’, ‘L’ or ‘?’. ‘U’ represents up (Y-coordinateincreases by 1), ‘R’ represents right (X-coordinate increases by 1), ‘D’ represents down (Y-coordinatedecreases by 1), ‘L’ represents left (X-coordinate decreases by 1) and ‘?’ represents a missed signal. Everycharacter in the sequence is a single step in the corresponding direction. A missed signal is a single stepin one of the four directions. The robot is initially at X-coordinate 0 and Y-coordinate 0 before startingto send the given signals.

    After sending some signals while the robot is moving, its software crashed and the robot could not do anyfurther moves. The researchers on the base want to limit the space where they can look for the robot. Inother words, they want to find the minimum possible X-coordinate, the minimum possible Y-coordinate,the maximum possible X-coordinate and the maximum possible Y-coordinate of the current location ofthe robot. 

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in oneline which contains a non-empty string which consists of up to 100,000 letters, each letter is ‘U’, ‘R’, ‘D’,‘L’ or ‘?’. This string represents the sequence of signals as described above. 

    Output

    For each test case, print a single line which contains 4 integers separated by a single space, which are theminimum possible X-coordinate, the minimum possible Y-coordinate, the maximum possible X-coordinateand the maximum possible Y-coordinate for the location of the robot after it stopped moving.

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    3
    RUL?R?D
    ????????
    RRRUU

    样例输出

    -1 -2 3 2
    -8 -8 8 8
    3 2 3 2

    贪心,构造四个串,如果要求最大的y就把问号都换成U,其他同理,最后直接计算即可。

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            int R = 0, L = 0, U = 0, D = 0, Q = 0;
            string s;
            cin >> s;
            for(int i = 0; i < s.size(); i++)
            {
                if(s[i] == 'L') L++;
                else if(s[i] == 'R') R++;
                else if(s[i] == 'U') U++;
                else if(s[i] == 'D') D++;
                else Q++;
            }
            cout << - L + R - Q << ' ' << - D + U - Q <<' ' << - L + R + Q << ' ' << - D + U + Q <<  endl;
        }
        return 0;
    }

    G. The Stones Game

    The stones game is a simple game, it is also a very old game which is unknown to almost everyone.

    The game starts with N stones and M players, the players are numbered from 1 to M. The players playin turns, player number 1 plays first, then player number 2 and so on until player number M plays, afterthis player number 1 plays again and they keep playing until the end of the game.

    For each turn, the players do the following 2 steps:

    1. The player gets a chance to remove a stone, and he/she should remove a stone in this step if he/shedecided to do so. 

    2. Regardless of the decision of the current player (whether or not he/she removed a stone in the firststep), if this is not the first turn and in the previous turn the player decided not to remove a stonein his/her first step, then the current player must remove a stone in this step (if in the previous turnthe player decided to remove a stone in his/her first step, then the current player must not removea stone in this step).

    This means in some turns a player might remove 0, 1 or 2 stones according to the above rules. In thisgame, the player who removes the last stone wins the game.

    Now you are given the total number of stones, the total number of players and a player number and youare asked to answer the following question:

    Is there a strategy for this player to win the game regardless of the actions taken by the other players intheir turns?

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described in oneline which contains 3 integers separated by a single space N M X (1 ≤ N , M ≤ 10^9) and (1 ≤ X ≤M) representing the number of stones, the number of players and the player number, respectively. 

    Output

    For each test case, print a single line which contains a single word, this word is either “YES” or “NO”(without the quotes) representing the answer for the above question for the given player number.

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    2
    2 2 2
    2 2 1

    样例输出

    YES
    NO

    博弈。观察推一下可得前n个人有n块石头时必胜。(看错题意导致把问题转换成了判断两个非常复杂的不等式的解的存在性问题了TAT)

    I. Omar Loves Candies

    Omar loves to eat a lot of candies, but unfortunately most of the candies are not healthy. So his parentsfound a way to give each candy a score, a higher score means a healthier candy (the score is an integerthat can be positive, zero or negative). 

    One day he went with his parents to buy some candies, and they found a strange store where all the candiesare stored in a 2-dimensional grid of N rows with M candies in each row. The rows are numbered from1 to N from top to bottom, and the columns are numbered from 1 to M from left to right and every cellcontains one candy.

    They noticed something else, any candy (except for those in the first row) is healthier than the candywhich is exactly above it, and any candy (except for those in the first column) is healthier than the candywhich is exactly to its left (healthier means having higher score as defined above).

    There is one more strange thing about this store, to buy some candies you have to select a sub-rectangleof the candies’ grid and buy all the candies within this sub-rectangle. 

    Omar’s parents want to select a non-empty sub-rectangle that has the maximum sum of candies’ scoresamong all possible sub-rectangles.

    For example, consider the grid in the example input. Some of the possible sub-rectangles of candies theycan select are [-2, -1, 2, 3], [-4, -2, -1] or [2, 3, 4, 5]. The last sub-rectangle has the maximum sum ofscores, which is 14. They can not select the following lists of candies [1, 2, 3, 4, 5] or [-2, -1, 2] (becausethese lists do not form a sub-rectangle of the given grid). 

    Can you help them by writing a program which finds the non-empty sub-rectangle with the maximumpossible sum of scores in the given grid?

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case starts with aline containing two integers separated by a single space N M (1 ≤ N , M ≤ 1,000) representing thedimensions of the candies’ grid, followed by N lines, each one contains M integers separated by a singlespace, representing the candies’ scores in this row. The given grid representation will satisfy the conditionsmentioned above, and each integer in the grid will not be less than -2,000 and will not be greater than2,000.

    Output

    For each test case, print a single line which contains a single integer representing the maximum sum ofscores they can get from a non-empty sub-rectangle.

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    1
    3 3
    -4 -2 -1
    -3 2 3
    1 4 5

    样例输出

    14

    右下角开始求二维前缀和,更新当前最大值即可。

    L. Omar's Bug

    Omar is the youngest programer in the world, he is just 11 months old. He has read about the binarysearch algorithm, and he decided to write it by himself.

    He wrote the following function (its syntax is valid and it does the same job in both C++ and Java):

     
    xxxxxxxxxx
     
    1
    int findFirstGreaterThanOrEqual(int array[], int N, int X) {
    2
        int start = 0, end = N;
    3
        while (start < end) {
    4
            int middle = (start + end) / 2;
    5
            if (array[middle] > X) {
    6
                end = middle;
    7
            } else {
    8
                start = middle + 1;
    9
            }
    10
        }
    11
        return start;
    12
    } 

    The function will always be called with parameters which satisfy the following conditions: 

    1. The first parameter is an array which contains at least 1 integer and at most 99 integers.

    2. The array contains distinct integers and it is sorted in increasing order. 

    3. The integers in the array are positive and each one is at most 100.

    4. N is the number of elements in this array.

    5. X is a positive integer which is at most 100.

    Here is how Omar wants the function to work:

    If there is no integer in the given array which is greater than or equal to X , Omar wants the function toreturn N . Otherwise, Omar wants the function to return the index of the smallest integer in the arraythat is greater than or equal to X (the first element in the array has index 0).

    Unfortunately there is a bug in the function and it returns wrong result for some cases, and Omar cannot find the bug (he is too young). Can you help him to generate some arrays to test his function?

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integerT, the number of test cases (1 ≤ T ≤ 100). Followed by the test cases, each test case is described inone line which contains 3 integers separated by a single space N X Y (1 ≤ N ≤ 99), (1 ≤ X ≤ 100)and (1 ≤ Y ≤ 2). N is the value of the second parameter and X is the value of the third parameter. IfY is 1, you should generate an array which when passed to the function with the given parameters, thefunction will return a correct result. If Y is 2, you should generate an array which when passed to thefunction with the given parameters, the function will return a wrong result. In both cases, the generatedarray must satisfy the above conditions.

    Note that correct or wrong result is relative to the way which Omar wants the function to work.

    Output

    For each test case, print on a single line, N integers separated by a single space, the first integer is thefirst integer in the generated array, the second integer is the second integer in the generated array and soon (the generated array should satisfy the above conditions). If there is more than one correct solution,print the lexicographically smallest one.

    When comparing two different arrays of the same length, the lexicographically smaller array is the onewith a smaller value on the smallest index where they differ. 

    输出时每行末尾的多余空格,不影响答案正确性

    样例输入

    2
    3 3 1
    4 7 2

    样例输出

    1 2 4
    1 2 3 7

    按题解说的,代码直接跳过了相等的情况(或者像我一样瞎邒找规律)就能知道只要出现和x相等的数就会出错。分四种情况(y=0,1以及n和x的大小关系)分别按字典序最小构造即可。

    #include <bits/stdc++.h>
    using namespace std;
    int n, x, y;
    int main()
    {
        int t;
        cin >> t;
        //1~99个数
        //元素互异 升序
        // 全是正数且最大为100
         
        while(t--)
        {
            scanf("%d%d%d", &n, &x, &y);
            if(y == 1)//构造输出正确的 
            {
                if(n > x)//构造不会出现自己的 
                {
                    for(int i = 1; i <= n + 1; i++)
                    {
                        if(i == x) continue;
                        else cout << i << ' ';
                    }
                    cout << endl;
                }
                else if(n < x)
                {
                    for(int i = 1; i <= n; i++) cout << i << ' ';
                    cout << endl;
                }
                else //构造不会出现自己的 如果出现自己会偏移一下 
                {
                    for(int i = 1; i <= n - 1; i++) cout << i << ' ';
                    cout << x + 1 << endl;
                }
            }
            else//构造输出错误的 
            {
                if(n > x)
                {
                    for(int i = 1; i <= n; i++) cout << i << ' ';//前n个字典序最小而且一定会出现自己 
                    cout << endl;
                }
                else if(n <= x)
                {
                    for(int i = 1; i <= n - 1; i++) cout << i << ' ';//为了保证字典序最小 把自己放在最后 
                    cout << x << endl;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    (LeetCode 141/142)Linked List Cycle
    (算法)随机播放歌曲
    (数组)数组排序,使所有奇数在左边,所有偶数在右边
    遗失的乔布斯访谈(文字版)
    幻想·梦想·理想
    立刻辞职,时不我待
    彩票漏洞让你快速致富
    剪刀石头布常胜秘笈
    9个心理学研究,让你学习更高效
    石头剪刀布手套:不止是寂寞宅的消遣
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13363452.html
Copyright © 2011-2022 走看看