Educational Codeforces Round 11
https://codeforces.com/contest/660
A. Co-prime Array
\(1\) 与任何数的 \(gcd\) 都为 \(1\),直接在不符合条件的两点间塞就可以了
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1005;
int a[N], n, ans;
bool vis[N];
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i <= n; i++) {
if (__gcd (a[i-1], a[i]) != 1) vis[i-1] = true, ans ++;
}
cout << ans << endl;
for (int i = 1; i <= n; i++) {
cout << a[i] << ' ';
if (vis[i]) cout << "1 ";
}
}
signed main () {
solve ();
}
B. Seating On Bus
仔细读题,注意细节。
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve () {
int n, m;
cin >> n >> m;
if (m <= 2 * n) {
for (int i = 1; i <= m; i++) cout << i << ' ';
return ;
}
for (int i = 1; i <= 2 * n; i++) {
if (i + 2 * n <= m) cout << i + 2 * n << ' ';
cout << i << ' ';
}
}
signed main () {
solve ();
}
C. Hard Process
此题有很多做法。
这里用双指针主要是考虑从暴力转移,区间 \([l,r]\) 范围内 \(0\) 的个数,\(\leq k\),不难发现这是一个区间和问题,故可以用双指针来维护,和这题 D - Enough Array 很类似
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int N = 3e5 + 5;
int n, k, a[N];
void solve () {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
int l = 1, r = 1, sum = 0, ans = 0, ansl, ansr;
while (r <= n) {
sum += !a[r]; //0的个数
if (sum > k) sum -= !a[l ++];
if (r - l + 1 > ans) {
ans = r - l + 1;
ansl = l, ansr = r;
}
r ++;
}
cout << ans << endl;
for (int i = ansl; i <= ansr; i++) a[i] = 1;
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
}
signed main () {
solve ();
}
D. Number of Parallelograms
利用平行四边形对角线互相平分的性质,记录两点之间的中点,即可最简化代码。
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> pii;
const int N = 2005;
pii p[N];
int n, ans;
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i].first >> p[i].second;
map<pii, int> mp;
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
mp[{p[i].first + p[j].first, p[i].second + p[j].second}] ++;
}
}
for (auto i : mp) {
ans += (i.second - 1) * i.second / 2;
}
cout << ans << endl;
}
signed main () {
solve ();
}