题目链接:点击打开链接
给出n个数的序列, 对任意奇下标要小于后一个, 任意偶下标大于后一个, 可以交换一次, 问有多少种复合题意的方法.
暴力枚举, 记录不符合题意的位置, 暴力交换两个位置的数进行判断, 判断时只需判断交换位置的两个数以及不符合题意数的位置.
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
#define X first
#define Y second
#define exp 1e-8
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 15e4 + 5;
int n, a[MAXN], ans;
bool flag[MAXN];
std::vector<int> v;
bool Judge(int x)
{
int val = a[x];
if(x & 1) {
if(x < n && val >= a[x + 1]) return false;
if(x > 1 && val >= a[x - 1]) return false;
}
else {
if(x < n && val <= a[x + 1]) return false;
if(x > 1 && val <= a[x - 1]) return false;
}
return true;
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for(int i = 1; i <= n; ++i)
if(!Judge(i)) {
v.push_back(i);
flag[i] = true;
}
if(v.size() > 10) {
printf("0\n");
return 0;
}
for(auto x : v)
for(int y = 1; y <= n; ++y)
if(!flag[y] || y > x) {
swap(a[x], a[y]);
bool valid = Judge(x) && Judge(y);
for(auto z : v)
if(!Judge(z)) valid = false;
if(valid) ans++;
swap(a[x], a[y]);
}
printf("%d\n", ans);
return 0;
}