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条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段 ...
随机推荐
- mysql 主从,双主同步
1.创建用户并设置远程访问授权 1). A上添加: //ip地址为B的ip地址,用于B访问 ' with grant option; 2). B上添加://ip地址为A的ip地址,用于A访问 ' wi ...
- centos 以太坊多节点私链搭建
环境 centos 7 搭建 3 个节点的 私链. 第一步 安装 一些依赖的 工具 yum update -y && yum install git wget bzip2 vim ...
- JSTL(使用if-else实现分页)
一.认识标签 <c:choose>标签没有任何属性. <c:when>标签具有以下列出的一个属性. <c:otherwise>标签没有任何属性. <c:whe ...
- pycharm使用教程链接+部分练习题01
pycharm安装: https://www.cnblogs.com/jin-xin/articles/9811379.html pycharm使用教程: https://edu.51cto.com/ ...
- Neo4j-Cypher语言语法
Neo4j-Cypher语言语法 梦飞扬 2018-03-15 264 阅读 Neo4j 本文是记录Neo4j图数据库中实用的Cypher语言语法. Cypher是什么 "Cypher&qu ...
- Vuejs之Component slot 插槽详解
Vuejs的component的数据进行了沙箱隔离,除js全局变量如Math, Date之类外无法访问用户自定义的变量,所以使用component写组件或嵌套组件时明白变量的访问非常重要 编译作用域 ...
- winDbg + VMware + window 双机联调环境搭建
这里简单的介绍一下内核开发双机联调的搭建环境,尽管网上有很多类似的文章,但看了很多总是不太舒服,觉得不太明白,所以自己实践一下总结一篇.下面就拿我的环境简单介绍,希望别人可以看懂. 准备工具:装虚拟机 ...
- Drupal创建Omega 4.x 子主题layout笔记
Adding a new region to your Omega 4.0 subtheme (Drupal) Drupal: Creating a custom layout with Omega ...
- JS如何获取地址栏url后面的参数?
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:JS如何获取地址栏url后面的参数?: 这里提供了两种获取地址栏url后面参数的方法: 方式1 传参: window.location. ...
- 用JSON将一个字典写入到文件,通过loads()将JSON字符串在转换为本来的类型
通过dumps将字典转换为JSON的字符串,存到磁盘里面