【CodeForces - 140C】New Year Snowmen (贪心)

题干:

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 r1, r2, ..., rn. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum 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 r1, r2, ..., 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

解题报告:

   贪心了半天发现贪错了,,题目要求的是堆最多的雪人,不是堆最大的雪人(并且你这样贪心出的方案也不是最大的)。

  所以我们应该按照同半径的雪球数量排序然后再贪心、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
typedef pair<int,int> pii;
map<int,int> mp;
map<int,int> :: iterator it;
int ans1[MAX],ans2[MAX],ans3[MAX];
int tot;
int main()
{priority_queue<pii> pq;int n;cin>>n;int tmp;for(int i = 1; i<=n; i++) {scanf("%d",&tmp);mp[tmp]++;}for(it = mp.begin(); it!=mp.end(); ++it) {pq.push(pm(it->se,it->fi));}while(pq.size() >= 3) {pii q1,q2,q3;q1=pq.top();pq.pop();q2=pq.top();pq.pop();q3=pq.top();pq.pop();ans1[++tot] = q1.se;ans2[tot] = q2.se;ans3[tot] = q3.se;if(q1.fi>1) pq.push(pm(q1.fi-1,q1.se));if(q2.fi>1) pq.push(pm(q2.fi-1,q2.se));if(q3.fi>1) pq.push(pm(q3.fi-1,q3.se));}for(int i = 1; i<=tot; i++) {}printf("%d\n",tot);int x[3];for(int i = 1,x1,x2,x3; i<=tot; i++) {x[0]=ans1[i],x[1]=ans2[i],x[2]=ans3[i];sort(x,x+3);printf("%d %d %d\n",x[2],x[1],x[0]);}return 0 ;}

错误代码:(没眼看了)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
set<ll> ss;
set<ll> :: iterator it;
int main()
{int n;cin>>n;ll tmp;for(int i = 1; i<=n; i++) {scanf("%lld",&tmp);ss.insert(tmp);}int ans = ss.size()/3;printf("%d\n",ans);int cur = 0;it = ss.end();--it;while(1) {if(cur >= ans) break;printf("%lld ",*it);--it;printf("%lld ",*it);it--;printf("%lld\n",*it);--it;cur++;}return 0 ;}

错误代码2:(这个是还能过几个样例的)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
set<int> ss;
multiset<int> mss;
set<int> :: iterator sit;
multiset<int> ::iterator msit;
int ans1[MAX],ans2[MAX],ans3[MAX];
int main()
{int n;cin>>n;int tmp;for(int i = 1; i<=n; i++) {scanf("%d",&tmp);ss.insert(tmp);mss.insert(tmp);}int cur = 0;sit = ss.end();--sit;while(1) {if(ss.size() < 3) break;sit = ss.end();--sit;ans1[++cur] = *sit;//printf("%lld ",*sit);mss.erase((mss.find(*sit)));if(mss.find(*sit) == mss.end()) {ss.erase(*sit);sit=ss.end();--sit;}else --sit;ans2[cur] = *sit;//printf("%lld ",*sit);mss.erase((mss.find(*sit)));if(mss.find(*sit) == mss.end()) {ss.erase(*sit);sit=ss.end();--sit;}else --sit;ans3[cur] = *sit;//printf("%lld\n",*sit);mss.erase((mss.find(*sit)));if(mss.find(*sit) == mss.end()) ss.erase(*sit);//		printf("%d %d %d\n",ans1[cur],ans2[cur],ans3[cur]);}//但是其实这么贪心的话就不需要加虾米那这几行代码了, 虽然贪心也不对,并且贪错东西了
//	for(int i = 1; i<=cur; i++) {
//            if(ans1[i] < ans2[i]) swap(ans1[i], ans2[i]);  //别忘记这一步。。。
//            if(ans1[i] < ans3[i]) swap(ans1[i], ans3[i]);
//            if(ans2[i] < ans3[i]) swap(ans2[i], ans3[i]);
//       }printf("%d\n",cur);for(int i = 1; i<=cur; i++) {printf("%d %d %d\n",ans1[i],ans2[i],ans3[i]);}return 0 ;}
/*
6
1 1 2 2 3 3*/

总结:   

   刚开始因为没看见要排序wa了一发。。。(其实是感觉已经排好序了但是其实并没有,,因为你是贪心的数量最多啊)。

再贴一个巨佬的思路:(用的是二分)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[MAX];
int n,ans[MAX];
bool ok(int x) {int cur=0;for(int i = 1; i<=n; i++) {if(cur < x || a[i] > ans[cur-x]) ans[cur++] = a[i];}return cur >= 3*x;
}
int main()
{cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);sort(a+1,a+n+1);int l = 0,r = n;int mid = (l+r)/2;int aans;while(l<=r) {mid = (l+r)>>1;if(ok(mid)) l=mid+1,aans=mid;else r=mid-1;}printf("%d\n",aans);ok(aans);for(int i = 0; i<aans; i++) {printf("%d %d %d\n",ans[i+2*aans],ans[i+aans],ans[i]);}return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441041.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php正文重复度,百度如何判断网页文章的重复度?两个页面相似度确认方法介绍...

在这个科技高度发达的时代&#xff0c;百度已经成为人们能获取消息的主要途径。但如今的百度&#xff0c;到处充斥着一些重复的内容&#xff0c;对用户的访问造成很大的困扰。因此&#xff0c;百度需要对网页重复进行判断&#xff0c;对重复的网页&#xff0c;只选取一些高质量…

【CodeForces - 892C 】Pride (数学,思维构造,gcd)

题干&#xff1a; You have an array a with length n, you can perform operations. Each operation is like this: choose two adjacent elements from a, say x and y, and replace one of them with gcd(x, y), where gcd denotes the greatest common divisor. What is…

php webshell编写,php webshell学习

一、环境kali 192.168.43.177开户apache /etc/init.d/apache2 start/var/www/html/目录下编辑php代码hackbarhttps://github.com/Mr-xn/hackbar2.1.3二、php基础输出函数:echo - 可以输出一个或多个字符串print - 只允许输出一个字符串&#xff0c;返回值总为 1提示&#xff1a…

【CodeForces - 27E】Number With The Given Amount Of Divisors (数论,数学,反素数)

题干&#xff1a; Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018. Input The first line of the input contains integer n (1 ≤ n ≤ 1000). Outp…

js php c语言for循环,小蚂蚁学习C语言(8)——C语言for循环

最近听到流程控制和循环了&#xff0c;感觉语言之间的语法很相似&#xff0c;不听吧&#xff0c;怕耽误某一个不同点或知识点&#xff0c;听吧&#xff0c;消耗很多时间&#xff0c;着实很纠结&#xff0c;莫非这需要传说中的空杯心态&#xff1f;循环 定义和分类定义&#…

【HDU - 1559】最大子矩阵 (二维前缀和裸题)

题干&#xff1a; 给你一个mn的整数矩阵&#xff0c;在上面找一个xy的子矩阵&#xff0c;使子矩阵中所有元素的和最大。 Input 输入数据的第一行为一个正整数T&#xff0c;表示有T组测试数据。每一组测试数据的第一行为四个正整数m,n,x,y&#xff08;0<m,n<1000 AND 0…

php _invoke 闭包,PHP新特性之闭包、匿名函数

闭包闭包是什么&#xff1f;1).闭包和匿名函数在PHP5.3中被引入。2).闭包是指在创建时封装函数周围状态的函数&#xff0c;即使闭包所在的环境不存在了&#xff0c;闭包封装的状态依然存在&#xff0c;这一点和Javascript的闭包特性很相似。3).匿名函数就是没有名称的函数&…

*【UVA - 10382】Watering Grass(贪心,区间覆盖问题,思维)

题干&#xff1a; 题目大意&#xff1a; 有一块草坪&#xff0c;长为l&#xff0c;宽为w&#xff0c;在它的水平中心线上有n个位置可以安装喷水装置&#xff0c;各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数&#xff0c;如果无论如何…

oracle如何把字符集改回默认,更改oracle字符集

在安装oracle时&#xff0c;选了默认字符集是utf8&#xff0c;后来发现与plsql developer工具联合使用时&#xff0c;会出现各种乱码问题。再加上我的项目也是gbk的&#xff0c;因此&#xff0c;将字符集改成gbk试试。步骤如下&#xff1a;1.查看当前的字符集和语言select * fr…

【HDU - 2570】迷瘴 (贪心,水题,排序,卡精度有坑)

题干&#xff1a; 通过悬崖的yifenfei&#xff0c;又面临着幽谷的考验—— 幽谷周围瘴气弥漫&#xff0c;静的可怕&#xff0c;隐约可见地上堆满了骷髅。由于此处长年不见天日&#xff0c;导致空气中布满了毒素&#xff0c;一旦吸入体内&#xff0c;便会全身溃烂而死。 幸好…

oracle 1天后,Oracle Code One - 第1天 精彩亮点回顾

原标题&#xff1a;Oracle Code One - 第1天 精彩亮点回顾原文作者&#xff1a;Padmini Murthy 产品营销总监2018年Oracle CodOracle CodeOne大会实况 – 第1天2018年Oracle CodeOne大会开幕第一天&#xff0c;精彩纷呈&#xff0c;乐趣繁多。从Developers Exchange活动的欢畅交…

【牛客 - 303D第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Campaign(二进制枚举,位运算,暴力,思维)

题干&#xff1a; 星际争霸(StarCraft)单人战役模式中有很多供人游玩的任务关卡。 tokitsukaze新开始了一关单人战役模式下的任务。在这场战役中&#xff0c;你要作为指挥官指挥克鲁普星区的艾伦人类(Terran)来防御人类的敌人——邪恶异虫(Zerg)的袭击。 这一次&#xff0c;…

oracle mssql 实例,oracle,mysql,SqlServer三种数据库的分页查询的实例

MySql&#xff1a;MySQL数据库实现分页比较简单&#xff0c;提供了 LIMIT函数。一般只需要直接写到sql语句后面就行了。LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量&#xff0c;它有一个或两个参数&#xff0c;如果给出两个参数&#xff0c; 第一个参数指定返回的第…

【牛客 - 303H第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Protoss and Zerg(快速幂取模,组合数学)

题干&#xff1a; 1v1&#xff0c;是星际争霸(StarCraft)中最常见的竞技模式。 tokitsukaze进行了n场1v1。在每一场的1v1中&#xff0c;她都有星灵(Protoss)和异虫(Zerg)两个种族可以选择&#xff0c;分别有a个单位和b个单位。因为tokitsukaze不太擅长玩人类(Terran)&#x…

oracle 控制文件冗余,Oracle 添加冗余控制文件 for RAC On Linux

萌哒萌哒的分割线注&#xff1a;添加冗余控制文件期间需要有关库操作&#xff0c;需注意&#xff01;&#xff01;&#xff01;备份控制文件SQL> alter session set tracefile_identifier‘backupctrl‘;Session altered.SQL> alter database backup controlfile to trac…

【牛客 - 303B第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Fibonacci and Counting(Fib数性质,gcd辗转相除法性质)

题干&#xff1a; 我们这样定义斐波那契数列&#xff0c;F[1]1,F[2]1&#xff0c;当n>2时F[n]F[n-1]F[n-2]。 斐波那契数列的前10项为&#xff1a;1,1,2,3,5,8,13,21,34,55。 欧几里得算法求解两个数的最大公约数。我们记gcd(a,b)为整数a与b的最大公约数。 当b0时&…

【牛客 - 303K第十五届浙江大学宁波理工学院程序设计大赛(同步赛)】Technology Tree(树形dp,tricks)

题干&#xff1a; 在星际争霸(StarCraft)中&#xff0c;有3个种族。对于任意一个种族&#xff0c;他们的建筑建造都是有一个顺序的。这个顺序正好是一个树形结构&#xff0c;我们称之为"科技树"(Technology tree)。 在科技树中&#xff0c;只有一个建筑是不需要前…

oracle基表恢复,查找V$PARAMETER 基表 – 专业Oracle数据库恢复,或许是您恢复数据的最后机会@phone:13429648788 - 专业Oracle数据库恢复技术支持...

1、使用trace查找show parameter执行语句alter session set events 10046 trace name context forever,level 12;show parameter process;alter session set events 10046 trace name context off;2、查找trace文件SELECT d.VALUE|| /|| LOWER (RTRIM (i.INSTANCE, CHR (0)))||…

【HDU - 1599】find the mincost route (Floyd最小环,最短路问题)

题干&#xff1a; 杭州有N个景区&#xff0c;景区之间有一些双向的路来连接&#xff0c;现在8600想找一条旅游路线&#xff0c;这个路线从A点出发并且最后回到A点&#xff0c;假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同…

linux中创建目录树,如何在C++/Linux中创建目录树?

Jonathan Lef..58这是一个可以用C编译器编译的C函数./*(#)File: $RCSfile: mkpath.c,v $(#)Version: $Revision: 1.13 $(#)Last changed: $Date: 2012/07/15 00:40:37 $(#)Purpose: Create all directories in path(#)Author: J Leffler(#)Copyright: (C) JLSS 1990-91,1997-98…