code force-27C---Unordered Subsequence (模拟+暴力)
来源:互联网 发布:虚拟机无法桥接网络 编辑:程序博客网 时间:2024/06/07 00:00
C. Unordered Subsequence
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.
A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
The first line of the input contains one integer n (1 ≤ n ≤ 105). The second line containsn space-separated integers — the given sequence. All numbers in this sequence do not exceed106 by absolute value.
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then outputk integers from the range [1..n] — indexes of the elements of this subsequence. If there are several solutions, output any of them.
567 499 600 42 23
31 3 5
31 2 3
0
32 3 1
31 2 3
题意: 题目规定非递增和非递减的序列为有序的序列,然后题目要求找到一个最短的无序的序列,可以不连续;
思路:如果这个序列存在,那么肯定最短是3个,有两种形式,小大小,大小大;想清楚这个后就很好办了,拿小大小这种情况举例子,首先从第一个数开始扫找到第一个a[i]<a[i+1]的转折点,然后此时的a[i]就为第一个数,再继续往后扫,如果能找到a[i]>a[i+1]这个转折点,此时的a[i]为第二个数,a[i+1]为第三个数;第二种情况一样;
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<map>#define inf 0xfffffff#define maxn 100100#define ll segtree[root].l#define rr segtree[root].rusing namespace std;int n;int a[100010];int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); int p; int x,y,z; int flag=0,k=0; //第一种情况 for(int i=1;i<n;i++) { if(a[i]<a[i+1]) { x=i;p=i+1; k=1; break; } } if(k) { for(int i=p;i<n;i++) { if(a[i]>a[i+1]) { y=i; z=i+1; flag=1; break; } } } if(flag==0) { k=0; for(int i=1;i<n;i++)//第二种情况 { if(a[i]>a[i+1]) { x=i; y=i+1; p=i+1; k=1; break; } } if(k) { for(int i=p;i<n;i++) { if(a[i]<a[i+1]) { z=i+1; flag=1; break; } } } } if(flag) { printf("3\n"); printf("%d %d %d\n",x,y,z); } else printf("0\n"); } return 0;}
- code force-27C---Unordered Subsequence (模拟+暴力)
- CF 27C Unordered Subsequence
- C题:CodeForces 27C Unordered Subsequence
- 【code force】模拟 746C
- CODE FORCE 500C (New Year Book Reading) (贪心or 模拟)
- C. Dasha and Password(模拟,暴力)
- unordered
- 暴力破解(Brute Force)
- code force 449 div2 C. Nephren gives a riddle
- code force 449 div2 C. Nephren gives a riddle
- HLJUOJ1117(暴力模拟)
- HDU2555(模拟暴力)
- HDU6168(暴力模拟)
- 防范ssh暴力破解攻击(ssh brute force attack)
- 防范ssh暴力破解攻击(ssh brute force attack)
- Brute Force匹配(暴力匹配)和FLANN匹配区别
- CodeForces 144C Anagram Search(暴力模拟)
- CSU 1553 Good subsequence(暴力求解)
- Android studio 命令行运行gradle命令
- (HDU
- Listview多条目的代码
- js原生数组工具方法总结
- Python学习笔记——20170823
- code force-27C---Unordered Subsequence (模拟+暴力)
- ES6新特性
- WHQL认证中搭建了服务器和客户端,计算机池中Selection中无任何选项的解决方案
- 心跳包
- [Leetcode] 368. Largest Divisible Subset 解题报告
- Linux下安装jboss并设置自启动服务
- GalaxyOJ-792 (思维)
- CSS实现loading加载效果
- Special Special Judge III ZOJ