【BZOJ2529】[Poi2011]Sticks 贪心

【BZOJ2529】[Poi2011]Sticks

Description

给出若干木棍,每根木棍有特定的颜色和长度。问能否找到三条颜色不同的木棍构成一个三角形。
(注意这里所说的三角形面积要严格大于0)

第一行给出一个整数k(3<=k<=50),表示颜色的种数。这k种颜色被标号为1至k。
接下来k行,第i+1描述颜色为i的木棍的信息。
首先一个整数Ni(1<=Ni<=10^6)表示颜色为i的木棍的数量。
接下来Ni个整数,表示这Ni根木棍各自的长度。
所有木棍的长度<=10^9。总木棍数量<=10^6。

你的程序应该仅输出一行
如果有解,输出6个整数,分别表示第一条边的颜色,第一条边的长度,第二条边的颜色,第二条边的长度,第三条边的颜色,第三条边的长度,这六个整数以空格分割。
如果有多组解,随便输出一组即可。
如果无解,输出 NIE

Sample Input

4
1 42
2 6 9
3 8 4 8
1 12

Sample Output

3 8 4 12 2 9

题解:先将所有木棍排序,然后枚举最长的木棍,我们需要在所有比它小的木棍中找到最大的且颜色不同的两根与它比较。一开始以为用堆,但其实只需要维护最长的3根不同颜色的就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1000010;
int m,n;
struct stick
{
	int col,len;
}p[maxn],s[5];
bool cmp(stick a,stick b)
{
	return a.len<b.len;
}
inline int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<‘0‘||gc>‘9‘)	{if(gc==‘-‘)f=-f;	gc=getchar();}
	while(gc>=‘0‘&&gc<=‘9‘)	ret=ret*10+gc-‘0‘,gc=getchar();
	return ret*f;
}
int main()
{
	m=rd();
	int i,j,a;
	for(i=1;i<=m;i++)
	{
		a=rd();
		for(j=1;j<=a;j++)	p[++n].col=i,p[n].len=rd();
	}
	sort(p+1,p+n+1,cmp);
	for(i=1;i<=n;i++)
	{
		for(j=0;j<3;j++)	if(p[i].col==s[j].col)
		{
			s[j]=p[i],sort(s,s+3,cmp);
			break;
		}
		if(j==3)	s[0]=p[i],sort(s,s+3,cmp);
		if(s[0].len+s[1].len>s[2].len)
		{
			printf("%d %d %d %d %d %d",s[0].col,s[0].len,s[1].col,s[1].len,s[2].col,s[2].len);
			return 0;
		}
	}
	printf("NIE");
	return 0;
}
时间: 2024-10-14 06:50:01

【BZOJ2529】[Poi2011]Sticks 贪心的相关文章

[bzoj2529][Poi2011]Sticks_贪心

Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n \le 10^6$,$1\le k\le 50$. 想法:我们这么想:假设当前木棍是满足题意的三根木棍中的最大者,那么剩下两根木棍一定是越大越好.所以,将所有木棍按长度排序,每次记录一下连续的三个长度不同的三根木棍,然后比较.知道有答案位置. 最后,附上丑陋的代码... ... #include <

HDU 1051 Wooden Sticks (贪心)

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11249    Accepted Submission(s): 4629 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

ZOJ 1025. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2 Seconds      Memory Limit: 65536 KB There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some

HDOJ 1051. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15564    Accepted Submission(s): 6405 Problem Description There is a pile of n wooden sticks. The length and weight of each stick a

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

POJ 1065. Wooden Sticks 贪心 结构体排序

Wooden Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19992   Accepted: 8431 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworkin

poj 1065 Wooden Sticks 贪心

题目链接:http://poj.org/problem?id=1065 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1051 贪心 先按l升序再按w稳定升序 然后从头扫到尾取可以取的 然后再从头 直到全部取完 一年前第一次写这道题的时候写了两百行Orz 其中有70行归并排序自己手敲 刚刚翻看老代码真是感慨... #include <cstdio> #include <cstdlib> #include <ctime> #

HDU 1051 Wooden Sticks 贪心||DP

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22000    Accepted Submission(s): 8851 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

HDOJ1051-Wooden Sticks(贪心)

Problem Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to