A
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
const int N = 1e5 + 5;
int n, m, _, k;
int a[N];
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n >> k;
rep (i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
ll ans = 0;
if (a[1] <= k) rep (i, 2, n) if (k >= a[i]) ans += (k - a[i]) / a[1];
cout << ans << '
';
}
return 0;
}
B
除非是 a + a = T, 其他的成对分开就行, 对于这个 a, 对半分开
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
const int N = 1e5 + 5;
int n, m, _, k;
int b[N];
bool v[N];
ll a[N];
bool cmp(int x, int y) {
return a[x] < a[y];
}
int main() {
IOS;
for (cin >> _; _; --_) {
ll k; cin >> n >> k;
bool f = 0;
rep (i, 1, n) cin >> a[i], b[i] = i;
sort(b + 1, b + 1 + n, cmp);
unordered_set<ll> st;
rep (i, 1, n) {
if (a[b[i]] > k) a[b[i]] = 0;
else if ((a[b[i]] << 1) == k && f) v[b[i]] = 1, f = 0;
else if ((a[b[i]] << 1) == k) v[b[i]] = 0, f = 1;
else if (!st.count(k - a[b[i]])) v[b[i]] = 0, st.insert(a[b[i]]);
else v[b[i]] = 1;
}
rep (i, 1, n) cout << v[i] << ' ';
cout << '
';
}
return 0;
}
C
遍历 O(n)
对每个字符x, 记录他上次出现的位置e[x], 则对于这个数字最小的区间为 c[x] = max(c[x], i - e[x])
最后在使得 c[x] + e[x] - 1 <= n, 即覆盖到最后一个字符即可
当然要离散化, 不然每次初始化 3e5 的 c[x] 和 e[x] 会超时
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
const int N = 3e5 + 5;
int n, m, _, k;
int a[N], b[N];
int c[N], d[N] = {1000000000}, id[N], e[N];
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; int tot = 0; b[n + 1] = -1;
rep (i, 1, n) cin >> a[i], b[i] = a[i], d[i] = 1e9;
sort(b + 1, b + 1 + n);
rep (i, 1, n)
if (b[i] != b[i + 1]) {
id[b[tot] = b[i]] = ++tot;
e[tot] = c[tot] = 0;
}
rep(i, 1, n) {
int x = id[a[i]];
umax(c[x], i - e[x]);
e[x] = i;
}
rep(x, 1, tot) umax(c[x], n + 1 - e[x]), umin(d[c[x]], x);
rep(i, 1, n) {
umin(d[i], d[i - 1]);
if (d[i] == 1e9) cout << -1 << ' ';
else cout << b[d[i]] << ' ';
}
cout << '
';
}
return 0;
}
D
3n, 对于每个数交换三次, 且第三次一定使得自己为平均数
那么从 2~n, 先让 1 给 i 数, 使得 a[i] % i == 0, 然后把 i 全给 1
最终所有数都到了 1, 然后让 1 给 i 平均数即可
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef double db;
template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; }
template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; }
template<class T> void clear(T& a) { T().swap(a); }
const int N = 3e5 + 5;
int n, m, _, k;
int a[N];
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; ll s = 0;
rep (i, 1, n) cin >> a[i], s += a[i];
if (s % n) { cout << "-1
"; continue; }
s /= n; cout << 3 * (n - 1) << '
';
rep (i, 2, n) {
cout << "1 " << i << ' ' << (a[i] % i ? i - a[i] % i : 0) << '
';
cout << i << " 1 " << (a[i] + i - 1) / i << '
';
}
rep (i, 2, n) cout << "1 " << i << ' ' << s << '
';
}
return 0;
}