zoukankan      html  css  js  c++  java
  • Codeforces Round #134 (Div. 2) A_B_C_D题

    A题:

    /*
    *题目大意:
    *        基础题,略。就是题目长,看了好久,要加强阅题速度啊。
    *解题思路:
    *        略。
    */
    View Code
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    const int MAXN = 305;
    int main(void)
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
        int n, k;
        while(scanf("%d %d", &n, &k) == 2)
        {
            int a[MAXN];
            int e = 2 * n + 1;
    
            for(int i = 0; i < e; i++)
                scanf("%d", &a[i]);
            int cnt = 0;
            for(int i = 1; i < e; i += 2)
            {
                int tmp = a[i] - 1;
                if(tmp > a[i - 1] && tmp > a[i + 1] && cnt < k)
                {
                    //cout << "hah" << endl;
                    a[i] = tmp;
                    cnt++;
                }
            }
            printf("%d", a[0]);
            for(int i = 1; i < e; i++)
                printf(" %d", a[i]);
            printf("\n");
        }
        return 0;
    }

    B题:

    /*
    *题目大意:
    *        略,此题比较容易看懂。
    *解题思路:
    *        两次贪心。求最小值一遍for,求最大值可以用优先队列。
    */
    View Code
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <functional>
    #include <vector>
    using namespace std;
    
    const int MAXN = 1024;
    int main(void)
    {
    #ifndef ONLINE_JUDGE
        //freopen("in.txt", "r", stdin);
    #endif
        int n, m, a[MAXN];
    
        while(scanf("%d %d", &n, &m) == 2)
        {
            for(int i = 0; i < m; i++)
                scanf("%d", &a[i]);
            sort(a, a + m);
            int tmp = n, Min = 0, Max = 0;
    
            for(int i = 0; i < m; i++)
            {
                if(a[i] < tmp)
                {
                    Min += (a[i] + 1) * a[i] / 2;
                    tmp -= a[i];
                }
                else
                {
                    int tol = (a[i] + 1) * a[i] / 2;
                    int t = a[i] - tmp;
                    int re = (t + 1) * t / 2;
                    Min += tol - re;
                    break;
                }
            }
    
            priority_queue<int> Q;
            for(int i = 0; i < m; i++)
                Q.push(a[i]);
            for(int i = 0; i < n; i++)
            {
                int pre = Q.top();
                Q.pop();
                Max += pre;
                pre--;
                if(pre != 0)
                    Q.push(pre);
            }
            printf("%d %d\n", Max, Min);
        }
        return 0;
    }

    C题:

    /*
    *题目大意:
    *        给n个带坐标的点,一个人只能往上,下,左,右移动,求至少增加
    *        几个点可以使他达到所有点。坐标点都为整型数据。
    *解题思路:
    *        其实很简单,先把一个点能到达的搜一遍,剩余的再增加一个点,就可以
    *        到达另一个点,之后另一个点再搜一遍,如此下去。
    *解题感想;
    *        下午刚睡醒,脑袋糊涂了。居然没有加dfs上去。晕菜~~
    */
    View Code
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    const int MAXN = 305;
    
    typedef struct _node
    {
        int x, y;
    }N;
    
    N node[MAXN];
    int vst[MAXN];
    
    void judge(N ind, int n)
    {
        for(int i = 0; i < n; i++)
        {
            if(!vst[i])
            {
                if(node[i].x == ind.x)
                {
                    vst[i] = 1;
                    judge(node[i], n);
                }
                if(node[i].y == ind.y)
                {
                    vst[i] = 1;
                    judge(node[i], n);
                }
            }
        }
    }
    
    bool isEmpty(int n)
    {
        for(int i = 0; i < n; i++)
        {
            if(!vst[i])
                return false;
        }
        return true;
    }
    
    int main(void)
    {
    #ifndef ONLINE_JUDGE
        //freopen("in.txt", "r", stdin);
    #endif
    
        int n;
        while(scanf("%d", &n) == 1)
        {
            memset(vst, 0, sizeof(vst));
    
            for(int i = 0; i < n; i++)
                scanf("%d %d", &node[i].x, &node[i].y);
    
            int ans = 0;
    
            judge(node[0], n);
    
            while(!isEmpty(n))
            {
                for(int i = 0; i < n; i++)
                {
                    if(!vst[i])
                    {
                        ans++;
                        judge(node[i], n);
                    }
                }
            }
            printf("%d\n", ans);
        }
        return 0;
    }

    D题:

    /*
    *题目大意:
    *        略。
    *解题思路:
    *        略。
    */
    View Code
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int> vec, ans;
    int MinMis;
    
    void get_que(int a, int b, int n)
    {
        vec.clear();
        bool flag;
        if(!a)
            flag = true;
        else
            flag = false;
        while(a > 0 && b > 0)
        {
            if(a > b)
            {
                a -= b;
                vec.push_back(0);//0是B,1是T
            }
            else
            {
                b -= a;
                vec.push_back(1);
            }
            if(b == 0)
            {
                if(a != 1)
                    flag = true;//不满足
                break;
            }
        }
    
        if(!flag && vec.size() == n)
        {
            int mis = 0;
            for(unsigned i = 0; i < vec.size() - 1; i++)
            {
                if(vec[i] == vec[i + 1])
                    mis++;
            }
            if(mis < MinMis)
            {
                MinMis = mis;
                ans = vec;
            }
        }
    }
    
    void init()
    {
        MinMis = INT_MAX;
        ans.clear();
    }
    
    int main(void)
    {
    #ifndef ONLINE_JUDGE
        //freopen("in.txt", "r", stdin);
    #endif
        int n, r;
        while(scanf("%d %d", &n, &r) == 2)
        {
            init();
            for(int i = 0; i <= r; i++)
            {
                get_que(i, r, n);
                get_que(r, i, n);
            }
    
            if(MinMis == INT_MAX)
                printf("IMPOSSIBLE\n");
            else
            {
                printf("%d\n", MinMis);
                reverse(ans.begin(), ans.end());
                for(unsigned i = 0; i < ans.size(); i++)
                {
                    if(ans[i] == 1)
                        printf("T");
                    else
                        printf("B");
                }
                printf("\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    update inner join
    Centos 7安装docker
    使用Let's Encrypt搭建永久免费的HTTPS服务
    upstream timed out (10060: A connection attempt failed because the connected party did not properly respond
    MySQL死锁分析一例
    solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)
    Java中的关键字 transient
    Spring 4 使用Freemarker模板发送邮件&添加附件
    Spring 4 创建REST API
    Spring 4 异常处理
  • 原文地址:https://www.cnblogs.com/cchun/p/2646022.html
Copyright © 2011-2022 走看看