Educational Codeforces Round 109 (Rated for Div. 2) [未完待续]

A. Potion-making

题目描述
image

参考题解

约分

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 105;

typedef long long LL;

int gcd(int a, int b)
{
    return b? gcd(b, a%b) : a;
}

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        //特例
        int a, b;
        scanf("%d", &a);
        b = 100-a;
        if(a > b)   swap(a, b);
        if(b == 100)
        {
            printf("1\n");
            continue;
        }
        int tp = gcd(a, b);
        a /= tp, b /= tp;
        printf("%d\n", a+b);
    }
    return 0;
}

B. Permutation Sort

题目描述
image
image

参考题解

智力题

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 105;

typedef long long LL;

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int n;
        scanf("%d", &n);
        int loca1 = -1, locan = -1;
        bool is_order = true;
        int lastNum = -1;
        for(int i = 1; i <= n; ++ i)
        {
            int tp;
            scanf("%d", &tp);
            if(lastNum != -1 && tp != lastNum+1)
            {
                is_order = false;
            }
            lastNum = tp;
            if(tp == 1)
            {
                loca1 = i;
            }
            if(tp == n)
            {
                locan = i;
            }
        }
        if(is_order)
        {
            printf("0\n");
            continue;
        }
        if(loca1 == n && locan == 1)
        {
            printf("3\n");
        }
        else if(loca1 == 1 || locan == n)
        {
            printf("1\n");
        }
        else
        {
            printf("2\n");
        }
    }
    return 0;
}

C. Robot Collisions

题目描述
image
image
image

参考题解

链接跳转

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>

using namespace std;

const int N = 3e5+5;

typedef long long LL;

int n, m;

int ans[N];

struct Node{
    int pos;
    bool is_right;
    int idx;
    bool operator<(const Node& t) const{
        return pos < t.pos;
    }
}node[N];

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        memset(node, 0, sizeof(node));
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; ++ i)
        {
            scanf("%d", &node[i].pos);
            node[i].idx = i;
        }
        for(int i = 0; i < n; ++ i)
        {
            char tp[2];
            scanf("%s", tp);
            if(tp[0] == 'R')
            {
                node[i].is_right = true;
            }
            else
            {
                node[i].is_right = false;
            }
        }
        sort(node, node+n);

        //消化奇数点
        for(int odd = 0; odd <= 1; ++ odd)
        {
            deque<int> stack;
            for(int i = 0; i < n; ++ i)
            {
                if(node[i].pos%2 == odd)    continue;
                if(stack.empty() || !(node[stack.back()].is_right && !node[i].is_right))
                {
                    stack.push_back(i);
                }
                else
                {
                    int topidx = stack.back();
                    stack.pop_back();
                    int res = (node[i].pos - node[topidx].pos)/2;
                    ans[node[topidx].idx] = ans[node[i].idx] = res;
                }
            }
            //front 部分回头
            while(stack.size())
            {
                int tfront = stack.front();
                stack.pop_front();
                if(node[tfront].is_right || stack.empty() || node[stack.front()].is_right)
                {
                    stack.push_front(tfront);
                    break;
                }
                int topidx = stack.front();
                stack.pop_front();
                int a = -node[tfront].pos;
                int res = (node[topidx].pos - a)/2;
                ans[node[tfront].idx] = ans[node[topidx].idx] = res;
            }
            //back 部分回头
            while(stack.size())
            {
                int tback = stack.back();
                stack.pop_back();
                if(!node[tback].is_right || stack.empty() || !node[stack.back()].is_right)
                {
                    stack.push_back(tback);
                    break;
                }
                int topidx = stack.back();
                stack.pop_back();
                int a = 2*m-node[tback].pos;
                int res = (a - node[topidx].pos)/2;
                ans[node[tback].idx] = ans[node[topidx].idx] = res;
            }
            //处理剩余部分
            while(stack.size())
            {
                int t2 = stack.back();
                stack.pop_back();
                if(stack.empty())
                {
                    ans[node[t2].idx] = -1;
                }
                else
                {
                    int t1 = stack.front();
                    stack.pop_front();
                    int a = - node[t1].pos;
                    int b = 2*m - node[t2].pos;
                    ans[node[t1].idx] = ans[node[t2].idx] = (b-a)/2;
                }
            }
        }
        for(int i = 0; i < n; ++ i)
        {
            if(i)
            {
                cout << " " << ans[i];
            }
            else
            {
                cout << ans[i];
            }
        }
        cout << endl;
    }
    return 0;
}

D. Armchairs

题目描述
image
image
image

参考题解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <deque>

using namespace std;

const int N = 5e3+5, INF = 0x3f3f3f3f;

typedef long long LL;

int a[N], b[N]; //a[i]: 第i个人的位置, b[i]: 第i个空位置的位置

int ac = 0, bc = 0;

int n, m; //n表示椅子数, m表示人数

int dp[N][N]; //dp[i][j]: 是前 i 个需要挪动的位置挪到前 j 个空位置的最小代价。
// dp[i][j] = min{ dp[i][j-1], dp[i-1][j-1] + abs(a[i]-j) }
// dp[0][k] = 0

int main()
{
    cin >> n;
    for(int i = 1; i <= n; ++ i)
    {
        int tp;
        scanf("%d", &tp);
        if(tp == 1)
        {
            a[++ac] = i;
        }
        else
        {
            b[++bc] = i;
        }
    }
    memset(dp, 0x3f, sizeof(dp));
    for(int j = 0; j <= bc; ++ j)
    {
        dp[0][j] = 0;
    }
    for(int i = 1; i <= ac; ++ i)
    {
        for(int j = i; j <= bc; ++ j)
        {
            dp[i][j] = min(dp[i][j-1], dp[i-1][j-1]+abs(a[i]-b[j]));
        }
    }
    cout << dp[ac][bc] << endl;
    return 0;
}
posted @ 2021-05-20 20:55  chaosliang  阅读(37)  评论(0)    收藏  举报