zoukankan      html  css  js  c++  java
  • cf

    C. New Year Snowmen
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made n snowballs with radii equal to r1r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 12 and 3 can be used to make a snowman but 223 or 222 cannot. Help Sergey and his twins to determine whatmaximum number of snowmen they can make from those snowballs.

    Input

    The first line contains integer n (1 ≤ n ≤ 105) — the number of snowballs. The next line contains n integers — the balls' radii r1r2, ...,rn (1 ≤ ri ≤ 109). The balls' radii can coincide.

    Output

    Print on the first line a single number k — the maximum number of the snowmen. Next k lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them.

    Examples
    input
    7
    1 2 3 4 5 6 7
    
    output
    2
    3 2 1
    6 5 4
    
    input
    3
    2 2 3
    
    output
    0

    题意:给你n个已知半径的小球,半径互不相同的三个雪球可以堆成一个雪人,每个雪球只能用一次,求最多可以堆几个雪人并从大到小输出雪球的半径,如果数目一样,随便输出分配方案。
    思路:很自然的会想到 O(n^3) 的暴力求解,肯定不行了。

    仔细想想这组数据:

    14
    1 1 2 2 3 3 4 4 4 4 5 5 5 5

    怎样分配才是最优呢,如果从1开始选,最终分配只有(1 2 3)(1 2 3)这两种,必然不会最优。如果从5考虑呢 (5 4 3) (5 4 3) ((5 4 2)|| (5 4 1))*2。会有四种情况出现,从4考虑呢,会发现也是这么多种。那么结果出来了,从仙童半径的雪球个数多的先考虑,依次向下考虑不同的半径。
    利用map统计相同半径的个数,利用结构体存半径以及个数,利用优先队列按半径的数目从多到少存结构体。每次pop三个,并将 num-1,如果 num != 0,继续push,否则舍弃。

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 struct Node{
     5     int arr,num;        //半径,数目
     6 }node[100005];
     7 map <int,int> M;
     8 priority_queue <Node> Q;        //默认大根队列,先出队列的为最大的
     9 vector <int> V[3];
    10 
    11 bool operator < (Node a,Node b){
    12     return a.num < b.num;
    13 }
    14 int main()
    15 {
    16     ios_base::sync_with_stdio(0);
    17     cin.tie(0);
    18 
    19     int n;
    20     while(cin>>n){
    21         while(!Q.empty())
    22             Q.pop();
    23         M.clear();
    24         for(int i = 0;i < 3;i ++)   V[i].clear();
    25 
    26         int temp;
    27         for(int i = 1;i <= n;i ++){
    28             cin>>temp;
    29             M[temp]++;
    30         }
    31 
    32         int len = 0;
    33         map <int,int>::iterator it;
    34         for(it = M.begin();it != M.end();it ++){        //结构体存半径和数目
    35             node[len].arr = it->first;
    36             node[len++].num = it->second;
    37         }
    38         for(int i = 0;i < len;i ++)     //入队列
    39             Q.push(node[i]);
    40 
    41         //cout<<"Check 2"<<endl;
    42         len = 0;
    43         while(!Q.empty()){
    44             Node res1 = Q.top();
    45             Q.pop();
    46             if(Q.empty())   break;
    47             Node res2 = Q.top();
    48             Q.pop();
    49             if(Q.empty())   break;
    50             Node res3 = Q.top();
    51             Q.pop();
    52 
    53             //cout<<res1.num<<"  "<<res2.num<<"  "<<res3.num<<endl;
    54 
    55             int temp[3] = {res1.arr,res2.arr,res3.arr};
    56             sort(temp,temp+3);
    57             V[0].push_back(temp[2]);
    58             V[1].push_back(temp[1]);
    59             V[2].push_back(temp[0]);
    60 
    61             res1.num--,res2.num--,res3.num--;
    62             if(res1.num)   Q.push(res1);        //数目不为0时继续入队列
    63             if(res2.num)   Q.push(res2);
    64             if(res3.num)   Q.push(res3);
    65         }
    66         cout<<V[0].size()<<endl;
    67         for(int i = 0;i < V[0].size();i ++)
    68             cout<<V[0][i]<<" "<<V[1][i]<<" "<<V[2][i]<<endl;
    69     }
    70     return 0;
    71 }
    View Code




  • 相关阅读:
    .vue文件在phpstorm中红线解决办法
    javascript预编译详解
    javascript数据类型判断及数据隐式和显示转换
    用git版本库工具上传项目到github(新手简单易上手)
    javascript的数据运算和条件判断
    javascript基础类型和引用类型
    uniapp开发钉钉小程序遇到的坑!!!
    pdf解析器 用于小程序 h5
    百度的Ueditor(php)上传到aws s3存储方案
    css3 pointer-event属性
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351931.html
Copyright © 2011-2022 走看看