Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int k; cin >> k;
cout << 100 / gcd(100, k) << endl;
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int &x : a) scanf("%d", &x);
int ans = 2;
if (is_sorted(a.begin(), a.end()))
ans = 0;
else if (a[0] == 1 || a[n - 1] == n)
ans = 1;
else if (a[0] == n && a[n - 1] == 1)
ans = 3;
printf("%d\n", ans);
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
struct bot{
int x, d;
};
int main() {
int t;
cin >> t;
forn(_, t){
int n, m;
scanf("%d%d", &n, &m);
vector<bot> a(n);
forn(i, n) scanf("%d", &a[i].x);
forn(i, n){
char c;
scanf(" %c", &c);
a[i].d = c == 'L' ? -1 : 1;
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&a](int x, int y){
return a[x].x < a[y].x;
});
vector<int> ans(n, -1);
vector<vector<int>> par(2);
for (int i : ord){
int p = a[i].x % 2;
if (a[i].d == -1){
if (par[p].empty())
par[p].push_back(i);
else{
int j = par[p].back();
par[p].pop_back();
ans[i] = ans[j] = (a[i].x - (a[j].d == 1 ? a[j].x : -a[j].x)) / 2;
}
}
else{
par[p].push_back(i);
}
}
forn(p, 2){
while (int(par[p].size()) > 1){
int i = par[p].back();
par[p].pop_back();
int j = par[p].back();
par[p].pop_back();
ans[i] = ans[j] = (2 * m - a[i].x - (a[j].d == 1 ? a[j].x : -a[j].x)) / 2;
}
}
forn(i, n){
printf("%d ", ans[i]);
}
puts("");
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
int main()
{
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++)
cin >> a[i];
vector<int> pos;
for(int i = 0; i < n; i++)
if(a[i] == 1)
pos.push_back(i);
int k = pos.size();
vector<vector<int>> dp(n + 1, vector<int>(k + 1, INF));
dp[0][0] = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j <= k; j++)
{
if(dp[i][j] == INF) continue;
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
if(j < k && a[i] == 0)
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(pos[j] - i));
}
cout << dp[n][k] << endl;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef pair<int, int> pt;
const int MOD = 998244353;
int norm(int a) {
while (a >= MOD)
a -= MOD;
while (a < 0)
a += MOD;
return a;
}
int mul(int a, int b) {
return int(a * 1ll * b % MOD);
}
int binPow(int a, int k) {
int ans = 1;
while (k > 0) {
if (k & 1)
ans = mul(ans, a);
a = mul(a, a);
k >>= 1;
}
return ans;
}
int inv(int a) {
return binPow(a, MOD - 2);
}
vector< vector<int> > d;
int n, m;
inline bool read() {
if(!(cin >> n >> m))
return false;
d.resize(n, vector<int>(m));
fore (i, 0, n) fore (j, 0, m)
cin >> d[i][j];
return true;
}
inline void solve() {
int invFact = 1;
fore (i, 1, n + 1)
invFact = mul(invFact, i);
invFact = inv(invFact);
int E = 0;
fore (j, 0, m) {
vector<int> cnt(n + 1, 0);
fore (i, 0, n)
cnt[n + 1 - d[i][j]]++;
vector<int> d(n + 1, 0);
d[0] = 1;
int rem = 0;
fore (i, 0, n) {
rem += cnt[i];
d[i + 1] = norm(d[i + 1] + mul(d[i], rem));
rem = max(0, rem - 1);
}
// cerr << d[n] << " - " << norm(1 - mul(d[n], invFact)) << endl;
E = norm(E + 1 - mul(d[n], invFact));
}
cout << E << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include <bits/stdc++.h>
using namespace std;
const int N = 543;
const long long INF = (long long)(1e18);
struct Matching
{
int n1, n2;
vector<set<int>> g;
vector<int> mt, used;
void init()
{
mt = vector<int>(n2, -1);
}
int kuhn(int x)
{
if(used[x] == 1) return 0;
used[x] = 1;
for(auto y : g[x])
if(mt[y] == -1 || kuhn(mt[y]) == 1)
{
mt[y] = x;
return 1;
}
return 0;
}
int calc()
{
init();
int sum = 0;
for(int i = 0; i < n1; i++)
{
used = vector<int>(n1, 0);
sum += kuhn(i);
}
return sum;
}
void remove_vertex(int v, bool right)
{
if(right)
{
for(int i = 0; i < n1; i++)
g[i].erase(v);
}
else
g[v].clear();
}
void add_edge(int x, int y)
{
g[x].insert(y);
}
Matching() {};
Matching(int n1, int n2) : n1(n1), n2(n2)
{
g.resize(n1);
};
};
int n, m, k;
long long dp[N][N];
int p[N][N];
vector<int> g[N];
long long x[N], y[N];
int main()
{
cin >> n >> m >> k;
for(int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
}
for(int i = 0; i < k; i++)
cin >> x[i] >> y[i];
Matching mt(n, n);
for(int i = 0; i < n; i++)
for(auto j : g[i])
mt.add_edge(i, j);
int cnt = mt.calc();
int cur = cnt;
vector<int> seq;
while(cur > 0)
{
int idx = 0;
for(int i = 0; i < n; i++)
{
Matching mt2 = mt;
mt2.remove_vertex(i, false);
if(mt2.calc() < cur)
idx = i + 1;
mt2 = mt;
mt2.remove_vertex(i, true);
if(mt2.calc() < cur)
idx = -(i + 1);
}
assert(idx != 0);
seq.push_back(idx);
mt.remove_vertex(abs(idx) - 1, idx < 0);
cur--;
}
reverse(seq.begin(), seq.end());
for(int i = 0; i <= k; i++)
for(int j = 0; j <= cnt; j++)
dp[i][j] = -INF;
dp[0][cnt] = 0;
for(int i = 0; i < k; i++)
for(int j = 0; j <= cnt; j++)
{
if(dp[i][j] == -INF) continue;
for(int z = 0; z <= j; z++)
{
if(i + 1 + z >= n) continue;
int t = j - z;
long long add = max(0ll, x[i] - t * y[i]);
if(dp[i + 1][z] < dp[i][j] + add)
{
dp[i + 1][z] = dp[i][j] + add;
p[i + 1][z] = j;
}
}
}
cur = max_element(dp[k], dp[k] + cnt + 1) - dp[k];
vector<int> res;
for(int i = k; i > 0; i--)
{
res.push_back(0);
for(int j = p[i][cur] - 1; j >= cur; j--)
res.push_back(seq[j]);
cur = p[i][cur];
}
reverse(res.begin(), res.end());
cout << res.size() << endl;
for(auto x : res) cout << x << " ";
cout << endl;
}