【Codeforces】 Codeforces Round 873 (Div. 2) (ABC)(+63!!!)

Divisible Array

解题思路

1+2+3+4+...+n=\frac{n(n+1)}{2}

2+4+6+...+n=n(n+1)

很明显第二个式子满足题目条件

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define fornk(i, l, r, k) for (int i = l; i <= r; i += k)
const int N = 2e5 + 5;
ll T;
ll n;
ll a[N];
void solve() {
	cin >> n;
	forn(i, 1, n) cout << 2 * i << ' ';
	cout << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Permutation Swap

解题思路

\left | a_i-i \right | 就是a_i离正确位置的距离,题目让我们求使用最大的移动步数移动全部到正确的位置,那么这个最大移动距离的既是所有\left | a_i-i \right |的最大公约数

参考代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define fornk(i, l, r, k) for (int i = l; i <= r; i += k)
const int N = 2e5 + 5;
ll T;
ll n;
ll a[N];
ll ans;
void solve() {
	ans = 0;
	cin >> n;
	forn(i, 1, n) cin >> a[i];
	forn(i, 1, n) ans = __gcd( abs(a[i] - i), ans);
	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

Counting Orders

解题思路

我们定义数组c为a数组中严格大于b_i的个数,那么从从第一个数开始我们有c_1个数可以选,接下来是选c_2-1,...c_3-2那么答案既是  \prod_{i=1}^{n} {c_i-i+1},注意对c_i小于1的情况特判

参考代码 

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cy cout << "YES" << endl
#define cn cout << "NO" << endl
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define fornk(i, l, r, k) for (int i = l; i <= r; i += k)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define fornk_(i, l, r, k) for (int i = l; i >= r; i -= k)
const int N = 2e5 + 5;
ll T;
ll n;
ll a[N], b[N], c[N];
ll ans;
ll mod = 1e9 + 7;
bool cmp(ll a, ll b) {
	return a > b;
}
void solve() {
	ans = 1;
	cin >> n;
	forn(i, 1, n) cin >> a[i];
	forn(i, 1, n) cin >> b[i];
	sort(a + 1, a + n + 1, cmp);
	sort(b + 1, b + 1 + n);
	ll cnt = n;
	forn(i, 1, n) {
		while (a[cnt] <= b[i]) cnt--;
		c[i] = cnt;
	}
	cnt = 0;
	forn_(i, n, 1) {
		if (c[i] - cnt < 1) ans = 0;
		ans *= (c[i] - cnt++);
		ans %= mod;
	}
	cout << ans << endl;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	T = 1;
	cin >> T;
	while (T--) solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值