zoukankan      html  css  js  c++  java
  • BFS:CF356C-Compartments

    C. Compartments
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.

    The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.

    However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.

    After all the swaps each compartment should either have no student left, or have a company of three or four students.

    Input

    The first line contains integer n (1 ≤ n ≤ 106) — the number of compartments in the carriage. The second line contains n integersa1, a2, ..., an showing how many students ride in each compartment (0 ≤ ai ≤ 4). It is guaranteed that at least one student is riding in the train.

    Output

    If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.

    Sample test(s)
    input
    5
    1 2 2 4 3
    
    output
    2
    
    input
    3
    4 1 1
    
    output
    2
    
    input
    4
    0 3 0 4
    
    output
    0
    


    解题心得:

    1、这是一个很烦躁的题,需要好好理一下思路,首先应该处理的是2,因为2上可到3,下可到1,所以先将1将2处理掉,移动一个就可以消去两个不合法要求,然后将2全部处理成3和1,2自身和自身组合,移动两个人就可以消去三个不合法要求,如果剩下了一个2则用4或1,如果剩下了一个1就用3。

    2、上面说了这么多,不论说没说清楚,看没看懂,反正就是一个贪心的思想。




    #include<stdio.h>
    using namespace std;
    const int maxn = 1e6+10;
    int num[maxn];
    int num1,num2,num3,num4;
    long long step;
    int n;
    
    void c1()
    {
        void c2();
        int now = num1 / 3;
        num3 += now;
        num1 = num1 % 3;
        step += now * 2;
        if(num4 == 0)
        {
            if(num3 > num1)
            {
                step += num1;
                num4 += num1;
                num3 -= num1;
                num1 = 0;
            }
            else if(num3 <= num1)
            {
                step += num3;
                num1 -= num3;
                num4 += num3;
                num3 = 0;
            }
        }
        if(num1 == 2)
        {
            step += 1;
            num2 += 1;
            num1 = 0;
        }
        c2();
    }
    
    void c2()
    {
        if(num1 >= num2)
        {
            num1 -= num2;
            num3 += num2;
            step += num2;
            num2 = 0;
        }
        if(num1 < num2)
        {
            step += num1;
            num3 += num1;
            num2 -=  num1;
            num1 = 0;
        }
        if(num2 != 0)
        {
            int now = num2*2 / 3;
            step += now;
            num3 += now;
            num2 = num2 * 2 % 3;
            if(num2 % 2)
            {
                num1 += 1;
                num2 = 0;
            }
            else if(num2 == 2)
                num2 = 1;
        }
        else if(num1 < num2 && (num1+num4)>= num2)
        {
            step += num2;
            num4 = num4 - num2 + num1;
            num1 = 0;
            num2 = 0;
            num3 += num2;
        }
        if((num1 + num4) < num2)
        {
            step += num1 + num4;
            num3 += num4 + num1;
            num2 -= num1 + num4;
            num1 = 0;
            num4 = 0;
        }
    }
    
    void c3()
    {
        if(num1 == 0)
        {
            if(num2 == 0)
            {
                printf("%lld
    ",step);
                return;
            }
        }
    mark:
        if(num1 == 1)
        {
            if(num2 == 1)
            {
                step += 1;
                printf("%lld
    ",step);
                return;
            }
            if(num2 == 0)
            {
                if(num3 != 0)
                {
                    step += 1;
                    printf("%lld
    ",step);
                    return;
                }
                else if(num4 > 1)
                {
                    step += 2;
                    printf("%lld
    ",step);
                    return;
                }
                else
                {
                    printf("-1
    ");
                    return ;
                }
            }
        }
        if(num1 == 2)
        {
            if(num2 == 1)
            {
                step += 1;
                num2 -- ;
                num1 --;
            }
            if(num3 >= 2)
            {
                step+=2;
                printf("%lld
    ",step);
                return;
            }
            if(num3 == 1)
            {
                step += 1;
                num3 ++;
                num1 --;
                goto mark;
            }
            else if(num4 > 1)
                step += 2;
            else
            {
                printf("-1
    ");
                return ;
            }
        }
    
        if(num2 == 0)
        {
            printf("%lld
    ",step);
            return;
        }
        else if(num2 == 1)
        {
            if(num4 != 0)
            {
                step++;
                printf("%lld
    ",step);
                return;
            }
            else if(num3 > 1)
            {
                step += 2;
                printf("%lld
    ",step);
                return;
            }
            else
            {
                printf("-1
    ");
                return;
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            num1 = num2 = num3 = step = 0;
            for(int i=0; i<n; i++)
            {
                scanf("%d",&num[i]);
                if(num[i] == 1)
                    num1 ++;
                if(num[i] == 2)
                    num2 ++;
                if(num[i] == 3)
                    num3 ++;
                if(num[i] == 4)
                    num4 ++;
            }
            c2();//处理2
            c1();//处理1
            c3();//处理3
        }
    }
    

  • 相关阅读:
    ES基础知识
    ES工具使用
    ES环境部署
    C# 新特性
    自动化测试工程师--面试
    python 报错 pip安装三方库不成功:WARNING: You are using pip version 20.2.3; however, version 20.2.4 is available
    jmeter 测试调用https接口
    JDK升级操作
    Jmeter之post上传文件
    弱网测试—Network-Emulator-Toolkit(一)
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107348.html
Copyright © 2011-2022 走看看