BZOJ2529: [Poi2011]Sticks
2529: [Poi2011]Sticks
Time Limit: 10 Sec Memory Limit: 128 MBSec Special Judge
Submit: 257 Solved: 133
[Submit][Status][Discuss]
Description
Johnny was given a birthday present by his grandparents. This present
is a box of sticks of various lengths and colours. Johnny wonders if
there are three sticks in the set he has been given that would form a
triangle with different-coloured sides. Let us note that Johnny is
interested in non-degenerate triangles only, i.e., those with positive
area.
给出若干木棍,每根木棍有特定的颜色和长度。问能否找到三条颜色不同的木棍构成一个三角形。
(注意这里所说的三角形面积要严格大于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
Input
the first line of the standard input an integer k(3<=k<=50)is
given, which is the number of different colours of sticks. The colours
themselves are numbered from 1 to k.
following klines contain descriptions of the sticks of particular
colours. The line no. i+1holds integers that describe the sticks of
colour , separated by single spaces. The first of these numbers,
Ni(1<=Ni<=10^6) denotes the number of sticks of colour . It is
followed, in the same line, by Niintegers denoting the lengths of the
sticks of colour . All lengths are positive and do not exceed10^9.
Furthermore, the total number of all sticks does not exceed 10^6.0020
Output
integers, separated by single spaces, that describe the construction of
a triangle with different-coloured sides as follows: the colour and the
length of the first stick, the colour and the length of the second
stick, and the colour and the length of the third stick,
there are multiple triples of different-coloured sticks that give rise
to a triangle, your program may pick one such triple arbitrarily.
Sample Input
1 42
2 6 9
3 8 4 8
1 12
Sample Output
HINT
Source
从前到后扫描。维护最长的三个颜色不同的木棍即可
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b)) inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const int MAXN = + ; int k, n; struct Node
{
int num,color;
}node[MAXN]; bool cmp(Node a, Node b)
{
return a.num < b.num;
} int main()
{
read(k);
if(k < )
{
printf("NIE");
return ;
}
for(register int i = ;i <= k;++ i)
{
int tmp;read(tmp);
for(register int j = ;j <= tmp;++ j)
{
node[++n].color = i;
read(node[n].num);
}
}
std::sort(node + , node + + n, cmp);
int p1 = n - ;
while(node[p1].color == node[n].color)-- p1;
int p2 = p1 - ;
while(node[p2].color == node[p1].color || node[p2].color == node[n].color)-- p2;
if(node[p1].num + node[p2].num > node[n].num)
{
printf("%d %d %d %d %d %d", node[n].color,node[n].num,node[p1].color,node[p1].num,node[p2].color,node[p2].num);
return ;
}
for(register int i = n - ;i >= ;-- i)
{
while(node[p1].color == node[i].color)-- p1;
p2 = min(p1 - , p2);
while(node[p2].color == node[p1].color || node[p2].color == node[i].color)-- p2;
if(p2 < || p1 < )break;
if(node[p1].num + node[p2].num > node[i].num)
{
printf("%d %d %d %d %d %d", node[i].color,node[i].num,node[p1].color,node[p1].num,node[p2].color,node[p2].num);
return ;
}
}
printf("NIE");
return ;
}
BZOJ2529
BZOJ2529: [Poi2011]Sticks的更多相关文章
- BZOJ2529 [Poi2011]Sticks 【贪心】
题目链接 BZOJ2529 题解 要组成三角形,当且仅当最长边长度小于另两条边之和 我们就枚举最长边,另两条边当然是越大越好 我们将所有边排序,从小枚举并记录各个颜色的最长边 当枚举到当前边时,找到除 ...
- [bzoj2529][Poi2011]Sticks_贪心
Sticks bzoj-2529 Poi-2011 题目大意:给你n根木棒,每种木棒有长度和颜色,颜色共有k种,求满足条件的3根木棒使得这3根木棒颜色互不相同且可以围成三角形. 注释:$1\le n ...
- 【bzoj2529】[Poi2011]Sticks 贪心
题目描述 给出若干木棍,每根木棍有特定的颜色和长度.问能否找到三条颜色不同的木棍构成一个三角形.(注意这里所说的三角形面积要严格大于0) 输入 第一行给出一个整数k(3<=k<=50),表 ...
- POI2011题解
POI2011题解 2214先咕一会... [BZOJ2212][POI2011]Tree Rotations 线段树合并模板题. #include<cstdio> #include< ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- LOJ#2170. 「POI2011」木棍 Sticks
题目链接 题意就是给你一堆线段,然后线段有长度和颜色,让你选三条组成一个三角形,这三条线段颜色不能一样 题解: 做法:贪心 首先按照长度给这些线段排序一遍 然后贪心的去选,对于已经选出来同种颜色的,就 ...
- BZOJ_2529_[Poi2011]Sticks_贪心
BZOJ_2529_[Poi2011]Sticks_贪心 Description Little Johnny was given a birthday present by his grandpare ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 2653 Pick-up sticks (线段相交)
题意:给你n条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
随机推荐
- React中的this.props.children
React this.props.children this.props对象的属性与组件的属性一一对应,但是有一个例外,就是this.props.children属性.它表示组件的所有子节点. var ...
- 深入浅出 Java Concurrency (30): 线程池 part 3 Executor 生命周期[转]
我们知道线程是有多种执行状态的,同样管理线程的线程池也有多种状态.JVM会在所有线程(非后台daemon线程)全部终止后才退出,为了节省资源和有效释放资源关闭一个线程池就显得很重要.有时候无法正确的关 ...
- PHP面向对象魔术方法之__get 和 __set函数
l 基本的介绍 (1) 当我们去使用不可以访问的属性时,系统就会调用__get方法. (2) 不可以访问的属性指的是(1 . 该属性不存在 2. 直接访问了protected或者private属性) ...
- Servlet与Struts的区别
启动: ● Servlet:无 ● Struts:配置filter,设置struts入口 创建: ● Servlet:继承HttpServlet,重写doGet与doPost方法: 添加注解或配置we ...
- <scrapy爬虫>爬取校花信息及图片
1.创建scrapy项目 dos窗口输入: scrapy startproject xiaohuar cd xiaohuar 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # ...
- adb的安装及配置
1.下载adb的安装包进行下载 2.将安装报进行解压 3.配置环境变量,将adb的根目录添加到path环境变量中 4.在终端命令行中输入adb servion命令,检查是否安装成功,如显示版本号则安 ...
- springcloud(四):Eureka的配置详解
在Eureka的服务治理体系中,主要分为服务端和客户端两个不同的角色,服务端为服务注册中心,客户端为提供各个接口的微服务应用,这里介绍一下Eureka的配置. Eureka服务端配置 在实际应用中,我 ...
- let和const的一些知识点
let和const 不可以重复声明 不会发生变量提升,因此必须在声明之后使用,否则报错! 只在声明所在的块级作用域内有效 let 同一个作用域内不能重复声明同一个变量: function func() ...
- Luogu P1401 城市(二分+网络流)
P1401 城市 题意 题目描述 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最 ...
- C/C++操作SQLite
最近几天在学习sqlite3,颇有点收获,下面介绍一下简单用法:1.先下载sqlite3.h和sqlite3.c(如果不知道怎么下载的话就去www.sqlite.org)如果要编译成lib.则需要用到 ...