zoukankan      html  css  js  c++  java
  • Codeforces Round 428 B Game of the rows 贪心 思维

      题目链接: http://codeforces.com/contest/839/problem/B

      题目描述: 有K种人, 有N排的11, 1111, 11的座位, 要求不同种人不能坐在相邻的位置, 给你K种人每种人的人数, 问能不能安排的下?

      解题思路: 首先我们先放不小于三个的,挨个遍历, 只能放4 或者 2 * 2, 先放4, 再放 2 * 2 如果这时候不够放了, 直接输出NO

        然后就剩下一个的还有两个的, 我们先放两个的, 两个当然放在2是最优的, 然后再放4, 这时注意放4之后1的数量加加, 这样的话如果四个或者两个都没有的话可以拆成两个1

        最后就只有一堆一个了, 我们先看1还有没有, 有就放1 ,没有放2, 再则放4, 同时1加加, 这样最后判一遍放没放完就行了

      代码: 

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iterator>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <deque>
    #include <map>
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    #define mem0(a) memset(a,0,sizeof(a))
    #define meminf(a) memset(a,0x3f,sizeof(a))
    using namespace std;
    int a[107];
    
    int main() {
        int n, k;
        cin >> n >> k;
        for( int i = 1; i <= k; i++ ) {
            cin >> a[i];
        }
        int s4 = n;
        int s2 = n << 1;
        int s1 =0;
        for( int i = 1; i <= k; i++ ) {
            while( s4 > 0 && a[i] >= 3 ) {
                s4--;
                a[i] -= 4;
            }
            if( s4 == 0 ) break;
        }
        for( int i = 1; i <= k; i++ ) {
            while( s2 > 0 && a[i] >= 3 ) {
                s2 -= 2;
                a[i] -= 4;
            }
        }
        if( s2 < 0 ) {
            cout << "NO" << endl;
            return 0;
        }
    //    for( int i = 1; i <= k; i++ ) {
    //        cout << a[i] << " ";
    //    }
    //    cout << endl;
        for( int i = 1; i <= k; i++ ) {
            if( a[i] == 2 ) {
                if( s2 > 0 ) {
                    s2--;
                    a[i] -= 2;
                }
                else if( s4 > 0 ) {
                    s4--;
                    a[i] -= 2;
                    s1++;
                }
                else if( s1 >= 1 ) {
                    s1 -= 2;
                    a[i] -= 2;
                }
            }
        }
        if( s1 < 0 ) {
            cout << "NO" << endl;
            return 0;
        }
        for( int i = 1; i <= k; i++ ) {
            if( a[i] >= 2 ) {
                cout << "NO" << endl;
                return 0;
            }
        }
        for( int i = 1; i <= k; i++ ) {
            if( a[i] == 1 ) {
                if( s2 > 0 ) {
                    s2--;
                    a[i] = 0;
                }
                else if( s4 > 0 ) {
                    s4--;
                    s1++;
                    a[i] = 0;
                }
                else if( s1 > 0 ) {
                    s1--;
                    a[i] = 0;
                }
            }
        }
        for( int i = 1; i <= k; i++ ) {
            if( a[i] > 0 ) {
                cout << "NO" << endl;
                return 0;
            }
        }
        cout << "YES" << endl;
        return 0;
    }
    View Code

      思考: 代码写的巨丑, 昨天做的CF, 卡在B题一直出不来, 自己刚开始贪心4是对的, 但是后来应该有好多情况没有, 我多考虑造成结果错了, 还是没想清楚

  • 相关阅读:
    MySQL监控全部执行过的sql语句
    Linux之网络编程:时间服务器
    人生哲理 |南怀瑾最经典的50句话
    TCP/IP协议(一)网络基础知识 网络七层协议
    Linux下进程通信之管道
    一个完整的项目管理流程
    Linux编程之select
    (笔记)电路设计(十四)之放大器的应用
    ds18b20采集温度并上报服务器
    java中基于swing库自定义分页组件
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7356657.html
Copyright © 2011-2022 走看看