日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

第十二届蓝桥杯A组省赛试题 I: 双向排序(Java)

發布時間:2023/12/2 java 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第十二届蓝桥杯A组省赛试题 I: 双向排序(Java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

試題 I: 雙向排序
本題總分:25 分

【問題描述】
給定序列 (a1, a2, · · · , an) = (1, 2, · · · , n),即 ai = i。
小藍將對這個序列進行 m 次操作,每次可能是將 a1, a2, · · · , aqi 降序排列,
或者將 aqi , aqi+1, · · · , an 升序排列。
請求出操作完成后的序列。

【輸入格式】
輸入的第一行包含兩個整數 n, m,分別表示序列的長度和操作次數。
接下來 m 行描述對序列的操作,其中第 i 行包含兩個整數 pi, qi 表示操作
類型和參數。當 pi = 0 時,表示將 a1, a2, · · · , aqi 降序排列;當 pi = 1 時,表示
將 aqi , aqi+1, · · · , an 升序排列。

【輸出格式】
輸出一行,包含 n 個整數,相鄰的整數之間使用一個空格分隔,表示操作
完成后的序列。

【樣例輸入】
3 3
0 3
1 2
0 2

【樣例輸出】
3 1 2

【樣例說明】
原數列為 (1, 2, 3)。
第 1 步后為 (3, 2, 1)。
第 2 步后為 (3, 1, 2)。
第 3 步后為 (3, 1, 2)。
與第 2 步操作后相同,因為前兩個數已經是降序了。

【評測用例規模與約定】
對于 30% 的評測用例,n, m ≤ 1000;
對于 60% 的評測用例,n, m ≤ 5000;
對于所有評測用例,1 ≤ n, m ≤ 100000,0 ≤ pi ≤ 1,1 ≤ qi ≤ n。


60分,通過6/10測評點,其余測評點超時,Java代碼

import java.util.Arrays; import java.util.Comparator; import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);Comparator<Integer> comparator = new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return o2 - o1; //降序排序}};int n = scanner.nextInt();int m = scanner.nextInt();Integer[] num = new Integer[n];for (int i = 0; i < num.length; i++) {num[i] = i + 1;}for (int i = 0; i < m; i++) {int p = scanner.nextInt();int q = scanner.nextInt();if (p == 0) {Arrays.sort(num, 0, q, comparator);}else {Arrays.sort(num, q-1, num.length);}}for (Integer integer : num) {System.out.print(integer + " ");}} }

滿分Java代碼
參考博客

import java.util.Scanner;public class Main {static class pair { int x, y;public pair(int x, int y) {super();this.x = x;this.y = y;}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int m = scanner.nextInt();pair[] pairs= new pair[n+1];int top=0;for(int i=0;i<m;i++) { //記錄有效步驟int p = scanner.nextInt();int q = scanner.nextInt();if(p == 1 && top > 0) {while(top > 0 && pairs[top].x == 1) {//連續出現相同的操作if(pairs[top].y<=q) q=pairs[top].y;top--;}//相鄰的相同操作的范圍小于當前范圍while(top >= 2 && pairs[top-1].y >= q) {top -= 2;}pairs[++top] = new pair(1,q);}if(p==0){while(top > 0 && pairs[top].x == 0) {if(pairs[top].y >= q) q=pairs[top].y;top--;}while(top >= 2 && pairs[top-1].y <= q) top -= 2;pairs[++top] = new pair(0,q); }}int num[] = new int[n+1];int k = n;int l = 1,r = n;for(int i = 1; i <= top; i++) {if(pairs[i].x == 0) {while(l <=r && pairs[i].y < r)num[r--] = k--;}if(pairs[i].x == 1) {while(l <=r && pairs[i].y > l)num[l++] = k--;}}if(top % 2 == 0) {//為操作1while(l <= r)num[r--] = k--;}else {//為操作0while(l <= r)num[l++] = k--;}for(int i = 1; i <= n; i++) {System.out.print(num[i] + " ");}} }

總結

以上是生活随笔為你收集整理的第十二届蓝桥杯A组省赛试题 I: 双向排序(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。