CodeForces - 140C-New Year Snowmen
2016-12-16 15:49
281 查看
题意:
给出n个数字,要求找出不同的三个数字构成雪人,问最多可以构成多少组这种符合条件的要求
思路:
优先队列,排序为数字个数
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int a[100005][5];
struct node
{
int num,sum;
friend bool operator <(node n1,node n2)
{
return n1.sum<n2.sum;
}
}s[100005];
int b[100005];
int main()
{
int n,t;
cin>>n;
priority_queue<node > q;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
sort(b+1,b+1+n);
node c;
int x,y;
c.num=b[1],c.sum=1;
for(int i=2;i<=1+n;i++)
{
if(b[i]!=c.num )
{
q.push(c);
c.num=b[i];
c.sum=1;
}
else
{
c.sum++;
}
}
int ans=0;
int cnt=0;
while(!q.empty())
{
node first=q.top();
q.pop();
node second,third;
second.num=-1,third.num=-1;
if(!q.empty() )
{
second=q.top();
q.pop();
}
if(!q.empty() )
{
third=q.top();
q.pop();
}
if(second.num!=-1&&third.num!=-1)
{
a[++cnt][3]=third.num;
a[cnt][2]=second.num;
a[cnt][1]=first.num;
}
else
break;
first.sum--;
second.sum--;
third.sum--;
if(first.sum!=0)
{
q.push(first);
}
if(second.sum!=0)
{
q.push(second);
}
if(third.sum!=0)
{
q.push(third);
}
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++ )
{
int max1=max(max(a[i][1],a[i][2]),a[i][3]);
int max2=min(min(a[i][1],a[i][2]),a[i][3]);
int max3;
for(int j=1;j<=3;j++)
{
max3=a[i][j];
if(max3!=max1&&max3!=max2)
break;
}
cout<<max1<<" "<<max3<<" "<<max2<<endl;
}
return 0;
}
/*
8
3 4 5 3 2 3 5
*/
给出n个数字,要求找出不同的三个数字构成雪人,问最多可以构成多少组这种符合条件的要求
思路:
优先队列,排序为数字个数
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
int a[100005][5];
struct node
{
int num,sum;
friend bool operator <(node n1,node n2)
{
return n1.sum<n2.sum;
}
}s[100005];
int b[100005];
int main()
{
int n,t;
cin>>n;
priority_queue<node > q;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
sort(b+1,b+1+n);
node c;
int x,y;
c.num=b[1],c.sum=1;
for(int i=2;i<=1+n;i++)
{
if(b[i]!=c.num )
{
q.push(c);
c.num=b[i];
c.sum=1;
}
else
{
c.sum++;
}
}
int ans=0;
int cnt=0;
while(!q.empty())
{
node first=q.top();
q.pop();
node second,third;
second.num=-1,third.num=-1;
if(!q.empty() )
{
second=q.top();
q.pop();
}
if(!q.empty() )
{
third=q.top();
q.pop();
}
if(second.num!=-1&&third.num!=-1)
{
a[++cnt][3]=third.num;
a[cnt][2]=second.num;
a[cnt][1]=first.num;
}
else
break;
first.sum--;
second.sum--;
third.sum--;
if(first.sum!=0)
{
q.push(first);
}
if(second.sum!=0)
{
q.push(second);
}
if(third.sum!=0)
{
q.push(third);
}
}
cout<<cnt<<endl;
for(int i=1;i<=cnt;i++ )
{
int max1=max(max(a[i][1],a[i][2]),a[i][3]);
int max2=min(min(a[i][1],a[i][2]),a[i][3]);
int max3;
for(int j=1;j<=3;j++)
{
max3=a[i][j];
if(max3!=max1&&max3!=max2)
break;
}
cout<<max1<<" "<<max3<<" "<<max2<<endl;
}
return 0;
}
/*
8
3 4 5 3 2 3 5
*/
相关文章推荐
- [CodeForces - 140C New Year Snowmen] 贪心 + STL 优先队列
- CodeForces 140C - New Year Snowmen(数据结构)
- Codeforces 140C New Year Snowmen(贪心)
- codeforces 140C New Year Snowmen
- CodeForces - 140C New Year Snowmen(贪心)
- New Year Snowmen CodeForces - 140C
- CodeForces 140C - New Year Snowmen STL
- CodeForces 140C - New Year Snowmen(优先队列)
- CodeForces 140C - New Year Snowmen
- Codeforces Round #100//140C - New Year Snowmen C. New Year Snowmen
- CodeForces 140 C. New Year Snowmen 详解 (贪心+优先队列)
- CoderForce 140C-New Year Snowmen(贪心)
- New Year Snowmen CodeForces - 140C
- CodeForces 140A New Year Table
- 【CodeForces】500B - New Year Permutation(Floyd)(贪心)
- 【codeforces 500D】New Year Santa Network
- CodeForces Good Bye 2016 :C New Year and Rating、D New Year and Fireworks、E New Year and Old Subsequ
- Codeforces Good Bye 2015 A. New Year and Days
- codeforces-611C-New Year and Domino
- CodeForces-379A-New Year Candles