• USACO Pearl Pairing


    洛谷 P2902 [USACO08MAR]珍珠配对Pearl Pairing

    https://www.luogu.org/problem/P2902

    JDOJ 2577: USACO 2008 Mar Gold 3.Pearl Pairing

    https://neooj.com:8082/oldoj/problem.php?id=2577

    Description

    At Bessie's recent birthday party, she received N (2 <= N <= 100,000;
    N%2 == 0) pearls, each painted one of C different colors (1 <= C
    < = N).

    Upon observing that the number of pearls N is always even, her
    creative juices flowed and she decided to pair the pearls so that
    each pair of pearls has two different colors.

    Knowing that such a set of pairings is always possible for the
    supplied testcases, help Bessie perform such a pairing. If there
    are multiple ways of creating a pairing, any solution suffices.

    Input

    * Line 1: Two space-separated integers: N and C

    * Lines 2..C + 1: Line i+1 tells the count of pearls with color i: C_i

    Output

    * Lines 1..N/2: Line i contains two integers a_i and b_i indicating
            that Bessie can pair two pearls with respective colors a_i and
            b_i.

    Sample Input

    8 3 2 2 4

    Sample Output

    1 3 1 3 2 3 3 2

    HINT

    INPUT DETAILS:

    There are 8 pearls and 3 different colors. Two pearls have color I; two
    have color II; four have color III.

    OUTPUT DETAILS:

    Bessie pairs each pearl of color III with one of color I and II.

     
    题目大意:

    在Bessie最近的生日聚会上,她收到N(2<=N<=100,000; N%2==0)珍珠,每个都涂上C种不同颜色之一(1<=C<=N)。

    观察到珍珠N的数量总是均匀的,她的创意来了,决定配对珍珠,使每双珍珠有两种不同的颜色。数据保证存在答案。请帮助Bessie执行这样的配对,如果有多种创建配对的方法,任意输出即可。

    SPJ开了。

    我是为了刷背包才看到这道题的,想破脑袋也没想出来咋背包,后来用模拟做了一遍,思路是这个样子。

    既然数据保证存在答案,我们便开始思考什么样子的数据才会保证存在答案。

    然后我们发现每种颜色的数量不可能超过N/2要不然没法玩。

    所以我们就想出了这样一种方案:

    记录下每个珍珠的颜色,然后分成两组(因为N是偶数)

    由于不可能有颜色数量超过N/2,所以我们枚举N/2次,每次取两组的第i位,就可以百分百保证不会有重复。

    脑筋急转弯的味道很浓,你信我的就没问题。

    其实也不需要再打一遍sort,因为你存数组的时候就是从小到大存的。

    代码如下:

    #include<cstdio>
    using namespace std;
    int s[100001];
    int n,c;
    int k;
    int main()
    {
        scanf("%d%d",&n,&c);
        for(int i=1;i<=c;i++)
        { 
            int x;
            scanf("%d",&x);
            for(int j=1;j<=x;j++)
                s[++k]=i;
        }
        for(int i=1;i<=n/2;i++)
            printf("%d %d
    ",s[i],s[i+(n/2)]);
        return 0;
    }

    sort与否随你便。

  • 相关阅读:
    C#中使用OpenSSL的公钥加密/私钥解密
    .NET跨平台:在Linux Ubuntu上编译coreclr/corefx/dnx(20150617)
    ASP.NET Web API与Owin OAuth:调用与用户相关的Web API
    ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
    在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
    C# 中 async/await 调用传统 Begin/End 异步方法
    将GitLab的数据库导入阿里云PostgreSQL RDS
    在Mac/Linux/Windows上编译corefx遇到的问题及解决方法
    在Mac上用自己编译出的DNX运行.NET程序
    [DNX]解决dnu restore时找不到Newtonsoft.Json的问题
  • 原文地址:https://www.cnblogs.com/fusiwei/p/11258171.html
Copyright © 2020-2023  润新知