首先将的每一对相邻两项求和,得到一个数组。那么数组的最大值减最小值即为数组的权值。
例如,若,那么,数组的极差是1。因此数组的权值为1。
现在小美希望你能构造一个长度为的排列,满足权值尽可能小。你能帮帮她吗?排列是指一个长度为的数组,其中 1 到每个元素恰好出现一次。
一个正整数,代表排列的长度。
一个合法的排列。如果有多解输出任意即可。
3
2 1 3
这个数组的权值为 1。输出[2,3,1]等排列也是合法的。
import sys for line in sys.stdin: a = line.split() n = int(a[0]) res = [0 for i in range(n)] max_n = n min_n = 1 for i in range(1,n,2): res[i] = max_n max_n = max_n - 1 for i in range(0,n,2): res[i] = min_n min_n +=1 out = " ".join(str(r) for r in res) print(out)
def sol(n): arr = [i+1 for i in range(n)] arr.sort(reverse=True) ans = [] left,right = 0,n-1 while left<=right: if left == right: ans.append(arr[left]) break ans.append(arr[left]) ans.append(arr[right]) left += 1 right -=1 str_ans = ' '.join([str(x) for x in ans[::-1]]).strip() return str_ans while 1: try: n = int(input()) ans = sol(n) print(ans) except: break
看我,看我,时间复杂度O(n),空间复杂度O(1)。
#include <iostream> using namespace std; int main() { int n; cin>>n; int left = 2; int right = n; cout << 1 << " "; bool flag = true; while(left<=right){ if(flag){ cout << right <<" "; --right; } else{ cout << left << " "; ++left; } flag=!flag; } cout <<endl; }
import sys n = int(input()) m = n // 2 ans = [] for i in range(m): ans.append(i+1) ans.append(n - i) if n % 2: ans.append(m + 1) for i in ans: print(i, end=' ') else: for i in ans: print(i, end=' ')
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), flag = 1; Deque<Integer> q = new LinkedList<>(); for (int i = 1, j = n; i <= j; i++, j--) { if (flag == 1) { q.offerFirst(i); if (i < j) q.offerFirst(j); } } for (int a : q) { System.out.printf(a + " "); } }
public static void main(String[] args) { int left = 1; int right = new Scanner(System.in).nextInt(); StringBuilder stringBuilder = new StringBuilder(); while(left < right) { stringBuilder.append(left).append(" ").append(right).append(" "); left ++; right --; } if (left == right) { stringBuilder.append(left); } System.out.println(stringBuilder); }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=1;i<=n/2;i++){ System.out.print(n-i+1); System.out.print(" "); System.out.print(i); if(i!=n/2)System.out.print(" "); } if(n%2 == 1) { System.out.print(" "); System.out.print(n - n / 2); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private static final Scanner sc = new Scanner(System.in); public static void main(String[] args) { question05(); } private static void question05() { int left = 1, right = sc.nextInt(); StringBuilder sb = new StringBuilder(); while (left < right) { sb.append(left++).append(" ").append(right--).append(" "); } if (left == right) { sb.append(left); } System.out.println(sb); } }
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; //初始化构造顺序排列 vector<int> a; int i = 1, j = n; // int mid = n/2 + 1; while(a.size()<n && i < j ){ // 交叉重新排列数组 a.push_back(i); a.push_back(j); i++; j--; } a.push_back(i); for(int c = 0; c<n; c++){ cout << a[c] << " "; } } // 64 位输出请用 printf("%lld")