upc组队赛6 Odd Gnome【枚举】
Odd Gnome
题目描述
According to the legend of Wizardry and Witchcraft, gnomes live in burrows underground, known as gnome holes. There they dig
up and eat the roots of plants, creating little heaps of earth around gardens, causing considerable damage to them.
Mrs. W, very annoyed by the damage, has to regularly de-gnome her garden by throwing the gnomes over the fence. It is a lot of work to throw them one by one because there are so many. Fortunately, the species is so devoted to their kings that each group always follows its king no matter what. In other words, if she were to throw just the king over the fence, all the other gnomes in that group would leave.
So how does Mrs. W identify the king in a group of gnomes? She knows that gnomes travel in a certain order, and the king, being special, is always the only gnome who does not follow that order.
Here are some helpful tips about gnome groups:
• There is exactly one king in a group.
• Except for the king, gnomes arrange themselves in strictly increasing ID order.
• The king is always the only gnome out of that order.
• The king is never the first nor the last in the group, because kings like to hide themselves.
Help Mrs. W by finding all the kings!
输入
The input starts with an integer n, where 1 ≤ n ≤ 100, representing the number of gnome groups. Each of the n following lines contains one group of gnomes, starting with an integer g, where 3 ≤ g ≤ 1 000,representing the number of gnomes in that group. Following on the same line are g space-separated integers, representing the gnome ordering. Within each group all the integers (including the king) are unique and in the range [0, 10 000]. Excluding the king, each integer is exactly one more than the integer preceding it.
输出
For each group, output the king’s position in the group (where the first gnome in line is number one).
样例输入
3
7 1 2 3 4 8 5 6
5 3 4 5 2 6
4 10 20 11 12
样例输出
5
4
2
题解
给出一个序列,序列中有一个King,除了King其他都是递增的,找出King的位置
King不会在第一个和最后一个的位置
那很明显就是枚举2--n-1的位置,找出满足以下两个条件的位置
1.a[i-1] < a[i+1]
2.a[i] > a[i + 1] && a[i] > a[i - 1] 或者 a[i - 1] < a[i + 1]&& a[i] < a[i + 1]
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 1000005;
int a[maxn];
int n;
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 2; i < n; i++)
if ((a[i] > a[i + 1] && a[i] > a[i - 1] && a[i - 1] < a[i + 1] )
||( a[i - 1] < a[i + 1]&& a[i] < a[i + 1] && a[i] < a[i - 1])) {
printf("%d\n", i);
break;
}
}
return 0;
}
upc组队赛6 Odd Gnome【枚举】的更多相关文章
- upc组队赛6 GlitchBot【枚举】
GlitchBot 题目描述 One of our delivery robots is malfunctioning! The job of the robot is simple; it shou ...
- Odd Gnome【枚举】
问题 I: Odd Gnome 时间限制: 1 Sec 内存限制: 128 MB 提交: 234 解决: 144 [提交] [状态] [命题人:admin] 题目描述 According to t ...
- upc组队赛17 Bits Reverse【暴力枚举】
Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...
- upc组队赛12 Cardboard Container【枚举】
Cardboard Container Problem Description fidget spinners are so 2017; this years' rage are fidget cub ...
- upc组队赛2 Super-palindrome【暴力枚举】
Super-palindrome 题目描述 You are given a string that is consisted of lowercase English alphabet. You ar ...
- upc组队赛5 Ground Defense【枚举】
Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...
- upc组队赛16 Melody【签到水】
Melody 题目描述 YellowStar is versatile. One day he writes a melody A = [A1, ..., AN ], and he has a sta ...
- upc组队赛15 Lattice's basics in digital electronics【模拟】
Lattice's basics in digital electronics 题目链接 题目描述 LATTICE is learning Digital Electronic Technology. ...
- upc组队赛6 Bumped!【最短路】
Bumped! 题目描述 Peter returned from the recently held ACM ICPC World finals only to find that his retur ...
随机推荐
- 利用print函数模拟打印进度条
import time , , ): time.sleep(0.1) num = i // 2 # 地板除,即取不大于/后的最小整数(3//2 = 1, 9//4 = 2, -7//2 = -4) s ...
- 用css3写出的倒三角形
<!DOCTYPE html><html><head><meta charset="gb2312" /><title>无 ...
- centos netstat 查看是否开放了端口
netstat命令各个参数说明如下: -a :所有 -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protoc ...
- MYSQL中常用的工具
1.mysql(客户端链接工具): -u :指定用户名 -p:指定密码 -h:指定服务器ip或者域名 -P(大写):指定端口 例子:mysql -u root -h 202.194. ...
- Java中synchronized 修饰在static方法和非static方法的区别
[问题描述]关于Java中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...
- [转]图片压缩功能在JBoss AS7出错有关问题
图片压缩功能在JBoss AS7出错问题近日在为平台(http://www.zyiqibook.com)做些小的整改,途中基本很顺利,但就在后面出现了些纠结的问题,而这问题我之前遇到过,因为一时没有想 ...
- js转换成数字
/** * 转换成数字,有两种方法: * 1.Number(var) * 2.parseInt(var)与parseFloat(var) */ // 字符串转换成数字 // 纯字母 var foo = ...
- K8S操作
一.K8Spods操作 kubectl delete all --all //删除 所有pods
- VBA-数据库操作
基本概念 1 怎么样才能操作数据库?使用ADO建立和数据库的连接,然后用ADO对象和sql语言对数据库进行操作. 2 SQL是什么?SQL(Structured Query Language)是一种查 ...
- 解决VMwave下卡死的办法
在VMwave路径下找到vmwave.log文件: 如上图所示:在资源监视器中找到name = vmwave-vmx.exe ,pid = 5940的进程,然后杀死.