A. Suborrays
原题链接:https://codeforces.com/contest/1391/problem/A
A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( ( ( 2 appears twice in the array ) ) ) and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 (n=3 (n=3 but there is 4 4 4 in the array ) ) ).
For a positive integer n n n, we call a permutation p of length n good if the following condition holds for every pair i i i and j j j ( 1 ≤ i ≤ j ≤ n ) (1≤i≤j≤n) (1≤i≤j≤n)—
- ( p i (p_i (pi O R OR OR p i + 1 p_{i+1} pi+1 O R OR OR … … … O R OR OR p j − 1 p_{j−1} pj−1 O R OR OR p j ) ≥ j − i + 1 p_j)≥j−i+1 pj)≥j−i+1 , where O R OR OR denotes the bitwise O R OR OR operation.
In other words, a permutation p p p is good if for every subarray of p p p, the O R OR OR of all elements in it is not less than the number of elements in that subarray.
Given a positive integer n n n, output any good permutation of length n n n. We can show that for the given constraints such a permutation always exists.
Input
Each test contains multiple test cases. The first line contains the number of test cases
t
(
1
≤
t
≤
100
)
t (1≤t≤100)
t(1≤t≤100). Description of the test cases follows.
The first and only line of every test case contains a single integer n ( 1 ≤ n ≤ 100 ) n (1≤n≤100) n(1≤n≤100).
Output
For every test, output any good permutation of length n on a separate line.
Example
input
3
1
3
7
output
1
3 1 2
4 3 5 2 7 1 6
Note
For n = 3,
[
3
,
1
,
2
]
[3,1,2]
[3,1,2] is a good permutation. Some of the subarrays are listed below.
- 3 3 3 O R OR OR 1 1 1 = 3 ≥ 2 ( i = 1 , j = 2 ) =3≥2 (i=1,j=2) =3≥2(i=1,j=2)
- 3 3 3 O R OR OR 1 1 1 O R OR OR 2 = 3 ≥ 3 ( i = 1 , j = 3 ) 2=3≥3 (i=1,j=3) 2=3≥3(i=1,j=3)
- 1 1 1 O R OR OR 2 = 3 ≥ 2 ( i = 2 , j = 3 ) 2=3≥2 (i=2,j=3) 2=3≥2(i=2,j=3)
-
1
≥
1
(
i
=
2
,
j
=
2
)
1≥1 (i=2,j=2)
1≥1(i=2,j=2)
Similarly, you can verify that [ 4 , 3 , 5 , 2 , 7 , 1 , 6 ] [4,3,5,2,7,1,6] [4,3,5,2,7,1,6] is also good.
题意:输入一个长度n,用
1
1
1 ~
n
n
n的数字进行随机排列。定义对于一个正整数n,如果对于每一对
i
i
i 和
j
(
1
≤
i
≤
j
≤
n
)
j(1≤i≤j≤n)
j(1≤i≤j≤n)都满足,对于p的每个子数组,其中所有元素的OR不小于该子数列中元素的个数。我们称长度为
n
n
n 的排列
p
p
p 是好的。
题解:两个数进行位运算了,且范围是1~n,那么进行位运算后的结果不会小于元素的个数。简而言之就是:无论怎么输出这个置换序列都是正确的。(我是逆序输出的。)
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = n; i >= 1; i--)
printf("%d ",i);
cout << endl;
}
return 0;
}
其实这次的div2做的有点像div3的难度。