Problem - A - Codeforces
题目大意:给定长度为n的排列,每组排列有山峰数k,请你进行构造使得n的排列恰好有k个山峰.
模拟题:首先山峰的最大数量为(n-1)/2,如果超过这个数那必然无法构造出来,然后再根据k的数量,从大到小放置n, n-1, n-2,…就可以了.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y; x<=z;++x)
#define dec(x,y,z) for(int x = y; x>=z;--x)
#define INF 0x3f3f3f3f
#define ll long long
#define LOOP ll _; cin>> _; for(ll LP_ = 1; LP_<=_; ++LP_)
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
int main(){
LOOP{
int n, k;
cin>>n>>k;
int num = (n-1)/2;
if (k > num)cout<<-1<<endl;
else{
int right = n;
int left = 1;
while(k--){
cout << left++ << " ";
cout << right-- << " ";
}
while(left <= right){
cout << left++ << " ";
}
cout <<endl;
}
}
return 0;
}
Problem - B - Codeforces
题目大意:对于一个序列a1,a2,…an,其中对任意一个j而言,满足a1&a2&…&aj = aj+1&aj+2&…&an,我们称这样的序列是一个good序列,下面给你一个序列,请问该序列有多少种排列方式,使得整个序列是一个good序列.
思路:首先要明确的一点就是,为了使对于任意j,左半边和右半边的&运算结果都相等,那么一定有两个相同且所有位上匹配值最低的数放在两边,(这里的匹配值指的是所有位上,1尽可能地少,与其他数尽可能不重叠,这样的话才能保证无论和谁进行&运算,运算结果一定是它本身),那么这个匹配值最低的数一定是a1&a2&…&an,然后对于其他位上的数,可以进行全排列,理清楚了思路,代码实现就比较简单.
#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int x = y