• Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)


    比赛链接:http://codeforces.com/contest/433

    A. Kitahara Haruki's Gift
    time limit per test:1 second
    memory limit per test:256 megabytes

    Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.

    Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.

    But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?

    Input

    The first line contains an integer n (1 ≤ n ≤ 100) — the number of apples. The second line containsn integersw1, w2, ..., wn (wi = 100 or wi = 200), wherewi is the weight of thei-th apple.

    Output

    In a single line print "YES" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print "NO" (without the quotes).

    Sample test(s)
    Input
    3
    100 200 100
    
    Output
    YES
    
    Input
    4
    100 100 100 200
    
    Output
    NO
    
    Note

    In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.


    题目大意:有100克的苹果和200克的苹果若干。每一个苹果都不能切开,问是否能均分给两个人

    题目分析:n最大100。

    。随便搞。计算100克和200克的个数然后枚举全部可能


    #include <cstdio>
    
    int main()
    {
        int n;
        scanf("%d", &n);
        int cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < n; i++)
        {
            int num;
            scanf("%d", &num);
            if(num == 100)
                cnt1 ++;
            else
                cnt2 ++;
        }
        bool f = false;
        for(int i = 0; i <= cnt1; i++)
        {
            for(int j = 0; j <= cnt2; j++)
            {
                if(i * 100 + j * 200 == (cnt1 - i) * 100 + (cnt2 - j) * 200)
                {
                    f = true;
                    break;
                }
            }
            if(f)
                break;
        }
        if(f)
            printf("YES
    ");
        else
            printf("NO
    ");
    }


    B. Kuriyama Mirai's Stones
    time limit per test:2 seconds
    memory limit per test:256 megabytes

    Kuriyama Mirai has killed many monsters and got many (namelyn) stones. She numbers the stones from1 to n. The cost of thei-th stone is vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions:

    1. She will tell you two numbers, l and r (1 ≤ l ≤ r ≤ n), and you should tell her.
    2. Let ui be the cost of thei-th cheapest stone (the cost that will be on thei-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers,l andr (1 ≤ l ≤ r ≤ n), and you should tell her.

    For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy.

    Input

    The first line contains an integer n (1 ≤ n ≤ 105). The second line containsn integers:v1, v2, ..., vn (1 ≤ vi ≤ 109) — costs of the stones.

    The third line contains an integer m (1 ≤ m ≤ 105) — the number of Kuriyama Mirai's questions. Then followm lines, each line contains three integerstype,l andr (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. Iftype equal to1, then you should output the answer for the first question, else you should output the answer for the second one.

    Output

    Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.

    Sample test(s)
    Input
    6
    6 4 2 7 2 7
    3
    2 3 6
    1 3 4
    1 1 6
    
    Output
    24
    9
    28
    
    Input
    4
    5 5 2 3
    10
    1 2 4
    2 1 4
    1 1 1
    2 1 4
    2 1 2
    1 1 1
    1 3 3
    1 1 3
    1 4 4
    1 2 2
    
    Output
    10
    15
    5
    15
    5
    5
    2
    12
    3
    5
    
    Note

    Please note that the answers to the questions may overflow 32-bit integer type.


    题目大意:n个数字,m个查询。1表示查询下标l到r的和。2表示查询从小到大排完序下标l到r的和

    题目分析:这还用分析个啥。。。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define ll long long
    using namespace std;
    int const MAX = 1e5 + 5;
    ll a[MAX], b[MAX];
    ll sum1[MAX], sum2[MAX];
    
    int main()
    {
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%I64d", &a[i]);
            b[i] = a[i];
        }
        sort(b + 1, b + n + 1);
        memset(sum1, 0, sizeof(sum1));
        memset(sum2, 0, sizeof(sum2));
        for(int i = 1; i <= n; i++)
        {
            sum1[i] = sum1[i - 1] + a[i];
            sum2[i] = sum2[i - 1] + b[i];
        }
        int q;
        scanf("%d", &q);
        while(q--)
        {
            int t, l, r;
            scanf("%d %d %d", &t, &l, &r);
            if(t == 1)
                printf("%I64d
    ", sum1[r] - sum1[l - 1]);
            else
                printf("%I64d
    ", sum2[r] - sum2[l - 1]);
        }
    }



    C. Ryouko's Memory Note
    time limit per test:1 second
    memory limit per test:256 megabytes

    Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, calledRyouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.

    Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.

    Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from pagex to pagey,|x - y| pages should be turned. During analyzing, Ryouko needsm pieces of information, thei-th piece of information is on page ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is.

    Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges pagex to pagey, she would copy all the information on pagex toy (1 ≤ x, y ≤ n), and consequently, all elements in sequencea that wasx would becomey. Note thatx can be equal toy, in which case no changes take place.

    Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.

    Input

    The first line of input contains two integers n and m (1 ≤ n, m ≤ 105).

    The next line contains m integers separated by spaces:a1, a2, ..., am(1 ≤ ai ≤ n).

    Output

    Print a single integer — the minimum number of pages Ryouko needs to turn.

    Sample test(s)
    Input
    4 6
    1 2 3 4 3 2
    
    Output
    3
    
    Input
    10 5
    9 4 3 8 8
    
    Output
    6
    
    Note

    In the first sample, the optimal solution is to merge page 4 to 3, after merging sequencea becomes{1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is|1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.

    In the second sample, optimal solution is achieved by merging page 9 to 4.


    题目大意:给一些数字,仅仅能替换一个数字(这里是所有替换。比方用2替换3就是把所有的3都换成2),要求目标值最小。求这个最小的目标值。目标值为

    题目分析:中位数定理。用vector记录每一个相邻的关系(注意仅仅存相邻数字不同的,不然排序取中位数会出错),然后枚举每组的中位数计算改变了的目标值,最后取目标值的最小值


    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #define ll long long
    using namespace std;
    
    int const MAX = 1e5 + 5;
    ll a[MAX];
    vector <ll> vt[MAX];
    
    int main()
    {
        int n, m;
        ll ma = 0, dif = 0;
        scanf("%d %d", &n, &m);
        for(int i = 0; i < m; i++)
        {
            scanf("%I64d", &a[i]);
            ma = max(ma, a[i]);
        }
        for(int i = 0; i < m - 1; i++)
        {
            if(a[i] == a[i + 1])
                continue;
            dif += abs(a[i + 1] - a[i]);
            vt[a[i]].push_back(a[i + 1]);
            vt[a[i + 1]].push_back(a[i]);
        }
        ll ans = dif;
        for(int i = 1; i <= ma; i++)
        {
            ll tmp = dif;
            int sz = vt[i].size();
            if(sz == 0)
                continue;
            sort(vt[i].begin(), vt[i].end());
            int mid = vt[i][sz / 2];
            for(int j = 0; j < sz; j++)
                tmp = tmp + abs(vt[i][j] - mid) - abs(i - vt[i][j]);
            ans = min(ans, tmp);
        }
        printf("%I64d
    ", ans);
    }
    




    D. Nanami's Digital Board
    time limit per test:1 second
    memory limit per test:256 megabytes

    Nanami is an expert at playing games. This day, Nanami's good friend Hajime invited her to watch a game of baseball. Unwilling as she was, she followed him to the stadium. But Nanami had no interest in the game, so she looked around to see if there was something that might interest her. That's when she saw the digital board at one end of the stadium.

    The digital board is n pixels in height andm pixels in width, every pixel is either light or dark. The pixels are described by its coordinate. Thej-th pixel of the i-th line is pixel(i, j). The board displays messages by switching a combination of pixels to light, and the rest to dark. Nanami notices that the state of the pixels on the board changes from time to time. At certain times, certain pixels on the board may switch from light to dark, or from dark to light.

    Nanami wonders, what is the area of the biggest light block such that a specific pixel is on its side. A light block is a sub-rectangle of the board, in which all pixels are light. Pixel(i, j) belongs to a side of sub-rectangle with(x1, y1) and(x2, y2) as its upper-left and lower-right vertex if and only if it satisfies the logical condition:

    ((i = x1 ori = x2) and (y1 ≤ j ≤ y2)) or ((j = y1 or j = y2) and (x1 ≤ i ≤ x2)).

    Nanami has all the history of changing pixels, also she has some questions of the described type, can you answer them?

    Input

    The first line contains three space-separated integersn, m andq (1 ≤ n, m, q ≤ 1000) — the height and width of the digital board, and the number of operations.

    Then follow n lines, each line containingm space-separated integers. The j-th integer of the i-th line isai, j — the initial state of pixel(i, j).

    • If ai, j = 0, pixel(i, j) is initially dark.
    • If ai, j = 1, pixel(i, j) is initially light.

    Then follow q lines, each line containing three space-separated integersop, x, andy (1 ≤ op ≤ 2; 1 ≤ x ≤ n; 1 ≤ y ≤ m), describing an operation.

    • If op = 1, the pixel at(x, y) changes its state (from light to dark or from dark to light).
    • If op = 2, Nanami queries the biggest light block with pixel(x, y) on its side.
    Output

    For each query, print a single line containing one integer — the answer to Nanami's query.

    Sample test(s)
    Input
    3 4 5
    0 1 1 0
    1 0 0 1
    0 1 1 0
    2 2 2
    2 1 2
    1 2 2
    1 2 3
    2 2 2
    
    Output
    0
    2
    6
    
    Input
    3 3 4
    1 1 1
    1 1 1
    1 1 1
    2 2 2
    1 2 2
    2 1 1
    2 2 1
    
    Output
    6
    3
    3
    
    Note

    Consider the first sample.

    The first query specifies pixel (2, 2), which is dark itself, so there are no valid light blocks, thus the answer is 0.

    The second query specifies pixel (1, 2). The biggest light block is the block with (1, 2) as its upper-left vertex and(1, 3) as its lower-right vertex.

    The last query specifies pixel (2, 2), which became light in the third operation. The biggest light block is the block with(1, 2) as its upper-left vertex and (3, 3) as its lower-right vertex.


    题目大意:给一个n*m的0/1矩阵,q组操作,1表示改变(x,y)点的值(0变1,1变0),2表示查询点(x,y)所在矩形的最大面积

    题目分析:直接模拟,四个数组。up,down,left,right分别记录当前行/列向四个方向延伸的1的个数


    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std; 
    int const MAX = 1005;
    int n, m, q;
    int g[MAX][MAX];
    int up[MAX][MAX], down[MAX][MAX], left[MAX][MAX], right[MAX][MAX];
    int cnt[MAX];
    
    void pre()
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                if(g[i][j] == 0)
                    up[i][j] = left[i][j] = 0;
                else
                {
                    up[i][j] = up[i - 1][j] + 1;
                    left[i][j] = left[i][j - 1] + 1;
                }
            }
        }
        for(int i = n; i >= 1; i--)
        {
            for(int j = m; j >= 1; j--)
            {
                if(g[i][j] == 0)
                    down[i][j] = right[i][j] = 0;
                else
                {
                    down[i][j] = down[i + 1][j] + 1;
                    right[i][j] = right[i][j + 1] + 1;
                }
            }
        }
    }
    
    void change(int x, int y)
    {
        g[x][y] ^= 1;
        for(int i = 1; i <= n; i++)
        {
            if(g[i][y] == 0)
                up[i][y] = 0;
            else 
                up[i][y] = up[i - 1][y] + 1;
        }
        for(int i = 1; i <= m; i++)
        {
            if(g[x][i] == 0)
                left[x][i] = 0;
            else 
                left[x][i] = left[x][i - 1] + 1;
        }
        for(int i = n; i >= 1; i--)
        {
            if(g[i][y] == 0)
                down[i][y] = 0;
            else 
                down[i][y] = down[i + 1][y] + 1;
        }
        for(int i = m; i >= 1; i--)
        {
            if(g[x][i] == 0)
                right[x][i] = 0;
            else 
                right[x][i] = right[x][i + 1] + 1;
        }
    }
    
    int get_ma(int ma, int pos)
    {
        int ans = 0;
        for(int i = pos + 1; i <= ma; i++)
            cnt[i] = min(cnt[i - 1], cnt[i]);
        for(int i = pos - 1; i >= 1; i--)
            cnt[i] = min(cnt[i + 1], cnt[i]);
        int l = pos, r = pos;
        for(int i = cnt[pos]; i >= 1; i--)
        {
            while(cnt[l - 1] >= i && l > 1)
                l --;
            while(cnt[r + 1] >= i && r < ma)
                r ++;
            ans = max(ans, (r - l + 1) * i);
        }
        return ans;
    }
    
    int cal(int x, int y)
    {
        int ans = 0;
        for(int i = 1; i <= n; i++)
            cnt[i] = left[i][y];
        ans = max(ans, get_ma(n, x));
        for(int i = 1; i <= n; i++)
            cnt[i] = right[i][y];
        ans = max(ans, get_ma(n, x));
        for(int i = 1; i <= m; i++)
            cnt[i] = up[x][i];
        ans = max(ans, get_ma(m, y));
        for(int i = 1; i <= m; i++)
            cnt[i] = down[x][i];
        ans = max(ans, get_ma(m, y));
        return ans;
    }
    
    int main()
    {
        memset(g, 0, sizeof(g));
        scanf("%d %d %d", &n, &m, &q);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                scanf("%d", &g[i][j]);
        pre();
        while(q--)
        {
            int op, x, y;
            scanf("%d %d %d", &op, &x, &y);
            if(op == 1)
                change(x, y);
            else
                printf("%d
    ", cal(x, y));
        }
    }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    支付宝H5支付---证书模式
    Redis分布式锁
    Docker+Nginx+Ssl
    Java调用函数传递参数到底是值传递还是引用传递
    Mysql索引查询失效的情况
    mysql索引之最左前缀法则
    数据的三大范式以及什么是反三大范式
    SpringMvc的工作流程
    Android 环境搭建
    Python 爬虫: 抓取花瓣网图片
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4884584.html
Copyright © 2020-2023  润新知