麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

1101. Quick Sort (25)

2019-11-11 06:12:52
字體:
來源:轉載
供稿:網(wǎng)友

題目鏈接:https://www.patest.cn/contests/pat-a-PRactise/1101 There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be the selected pivot for this partition?

For example, given N = 5 and the numbers 1, 3, 2, 4, and 5. We have:

1 could be the pivot since there is no element to its left and all the elements to its right are larger than it; 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well; 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well; and for the similar reason, 4 and 5 could also be the pivot. Hence in total there are 3 pivot candidates.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then the next line contains N distinct positive integers no larger than 109. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input: 5 1 3 2 4 5 Sample Output: 3 1 4 5

#include<cstdio>#include<algorithm>using namespace std;const int maxn=100010;const int INF=0x7fffffff;int a[maxn];int leftMax[maxn],rightMin[maxn];//分別表示位置i左邊的最大值(不包含i),位置i右邊的最小值(不包含i)int ans[maxn],cnt=0;int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } leftMax[0]=-1; for(int i=1;i<n;i++){ leftMax[i]=max(leftMax[i-1],a[i-1]); } rightMin[n-1]=INF; for(int i=n-2;i>=0;i--){ rightMin[i]=min(rightMin[i+1],a[i+1]); } for(int i=0;i<n;i++){ if(a[i]>leftMax[i]&&a[i]<rightMin[i]){ ans[cnt++]=a[i]; } } sort(ans,ans+cnt); printf("%d/n",cnt); for(int i=0;i<cnt;i++){ printf("%d",ans[i]); if(i<cnt-1) printf(" "); } printf("/n");//若沒有這個,會有一個測試點格式錯誤,因為當cnt=0時,第二行雖然沒有主元,但必須輸出換行 return 0;}

法二:直接暴力,會超時

#include<cstdio>#include<algorithm>using namespace std;const int maxn=100010;int a[maxn],temp[maxn];int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } int cnt=0; for(int i=0;i<n;i++){ int j=i-1; bool flag=true; while(j<i&&j>=0){ if(a[j]>a[i]){ flag=false; break; } j--; } if(flag==true){ int k=i+1; while(k<n){ if(a[k]<a[i]){ flag=false; break; } k++; } } if(flag==true){ temp[cnt++]=a[i]; } } sort(temp,temp+cnt); printf("%d/n",cnt); for(int i=0;i<cnt;i++){ printf("%d",temp[i]); if(i<cnt-1) printf(" "); } printf("/n"); return 0; }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品99久久久久久大便 | 久久日韩在线 | 经典三级在线视频 | 日本一级黄色大片 | 国产精品99久久久久久董美香 | 久久久久久久一区二区三区 | 午夜国产小视频 | 国内性爱视频 | 中文区永久区 | 99视频观看 | 久久精品成人免费国产片桃视频 | 国产精品久久久久久影院8一贰佰 | 久久久www成人免费精品 | 欧美性色生活片免费播放 | 在线视频观看一区二区 | 亚洲成人在线免费观看 | 99riav视频一区二区 | 久久亚洲精选 | 九九热视频这里只有精品 | 日本在线观看视频网站 | 亚洲欧美成aⅴ人在线观看 av免费在线播放 | 92看片淫黄大片欧美看国产片 | 国产一及毛片 | 欧美精品一级 | 激情小说激情图片激情电影 | 日韩黄色免费观看 | 久久久久久亚洲综合影院红桃 | 日韩欧美精品中文字幕 | 久久久国产精品免费观看 | 成人在线视频精品 | 性爱视频在线免费 | 得得啪在线视频 | 最新一区二区三区 | 成人在线观看免费观看 | 深夜网站在线观看 | 欧美黄色试片 | www.成人在线| 一区二区三区欧洲 | 久久久久久久一区二区三区 | 91丝袜| 亚洲午夜视频 |