【模拟】【codeforces】451B Sort the Array

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=52107

给出一个数列,问截取哪一部分连续子列进行翻转可以使整个数列升序排列,

如果有这样的字串输出yes和截取位置,没有输出no

就是先记录原始位置进行排序,之后一旦发现一个元素排序后更改了位置那它就被翻转过。

完全逆序排列应该是排序后原先的位置依次减1,这样得到被翻转的前后位置。

之后继续扫描,如果还有位置不对的,说明翻转一段不能完成,输出no

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LEN 100005
 4 typedef struct point{
 5     int a, p;
 6     bool operator < (const point& p) const{
 7         return a < p.a;
 8     }
 9 }point;
10 point arr[LEN];
11
12 int main(){
13     int n;
14     while(~scanf("%d", &n)){
15         memset(arr, 0, sizeof(arr));
16         for(int i = 1; i <= n; i++){
17             scanf("%d", &arr[i].a);
18             arr[i].p = i;
19         }
20         sort(arr+1, arr+1+n);
21         int lpos = 1, rpos = 1;
22         bool f = true;
23         for(int i = 1; i <= n; i++)
24             if(arr[i].p != i){
25                 lpos = i;
26                 rpos = i;
27                 break;
28             }
29         for(int i = lpos+1; i <= n; i++){
30             if(arr[i].p == arr[i-1].p-1) rpos = i;
31             else break;
32         }
33         for(int i = rpos+1; i <= n; i++)
34             if(arr[i].p != i){
35                 f = false;
36                 break;
37             }
38         if(f == false) puts("no");
39         else{
40             puts("yes");
41             printf("%d %d\n", lpos, rpos);
42         }
43     }
44     return 0;
45 }
时间: 2024-08-08 18:17:14

【模拟】【codeforces】451B Sort the Array的相关文章

CodeForces 451B Sort the Array

Description Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a b

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #258 (Div. 2) B. Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

Codeforces 442C Artem and Array(stack+贪心)

题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分.问最大得分是多少. 解题思路:首先将连续的a,b,c,a > b && c > b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分. 样例:4 10 2 2 8 #include <cstdio> #include

CodeForces 451B

#include <iostream> #include <algorithm> using namespace std; #define max 100010 int a[max],b[max]; bool cmp(int a,int b){ return a<b; } int main(){ int n; while(cin>>n){ for(int i=0;i<n;i++){ cin>>a[i]; b[i]=a[i]; } sort(a,a

CodeForces 451B (翻转一次递减子序列得到递增序列) 简单题

题目大意:判断能否找到递减的子列,将其翻转后得到的整个数列递增,只能找一次,最后输出,如果能还要输出翻转的首尾位 置,注意数数组下标,不是首尾的数,当然如果本来数列就递增,就随便找个数翻一下. 我的方法是直接模拟,直接从a[0]开始寻找递减序列,然后判断递减序列翻转后能否构成递增序列. #include<stdio.h> int n,a[100005]; int main() { int i,j,h; bool bo=true; scanf("%d",&n); fo

Sort the Array

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array,

[2016-04-09][codeforces][660][A][ Co-prime Array]

时间:2016-04-09 22:50:56 星期六 题目编号:[2016-04-09][codeforces][660][A][ Co-prime Array] 题目大意:给定一个数列,问至少需要插入多少个1 1091 109中的任一数字,才能使得相邻两个数字是互质的,输出最少次数和最后的数列 分析:直接扫一遍,相邻元素不互质的,中间插个1, #include<cstdio> #include<vector> using namespace std; const int maxn

Educational Codeforces Round 21 D. Array Division

题目链接:Educational Codeforces Round 21 D. Array Division 题意: 给你n个数,现在你可以改变1<=个数的位置,然后问你是否存在有一个k,使得sum(a[i])(1<=i<=k)==sum(a[j])(k+1<=j<=n) 题解: 分析: 如果需要将一个数移动,无非就是将这个数从第一部分移到第二部分,或者从第二部分移到第一部分. 所以,我们只需要开两个map来记录一下两部分有哪些数. 当两部分的差值/2等于其中一部分的一个数时