solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
set<int> obs[2][200003];
map<int, bool> is[100002];
int n, m, fac = 1;
const int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};
bool add;
void add_obs(int x, int y) {
obs[0][x - y + m + 1].insert(x);
obs[1][x + y].insert(x);
is[x][y] = true;
}
long long ans;
pair<pair<int, int>, int> nxt(int x, int y, int d) {
set<int>::iterator it =
obs[d & 1][d & 1 ? x + y : x - y + m + 1].lower_bound(x);
if (dx[d] == -1) --it;
if (add) ans += abs(*it - x);
int newx = (*it) - dx[d];
int dist = abs(x - newx);
int newy = y + dist * dy[d];
bool a = is[newx][newy + dy[d]], b = is[newx + dx[d]][newy];
if (a && !b)
return make_pair(make_pair(newx + dx[d], newy), d ^ 1);
else if (!a && b)
return make_pair(make_pair(newx, newy + dy[d]), d ^ 3);
else {
fac = 2;
return make_pair(make_pair(newx, newy), d ^ 2);
}
}
int main() {
int k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
add_obs(t1, t2);
}
for (int i = 0; i <= n + 1; ++i) {
add_obs(i, 0);
add_obs(i, m + 1);
}
for (int i = 1; i <= m; ++i) {
add_obs(0, i);
add_obs(n + 1, i);
}
pair<pair<int, int>, int> st;
char tmp[10];
scanf("%d%d%s", &st.first.first, &st.first.second, tmp);
st.second = (tmp[0] == 'S' ? 2 : 0);
st.second ^= ((st.second >> 1) ^ (tmp[1] != 'W'));
st = nxt(st.first.first, st.first.second, st.second);
pair<pair<int, int>, int> init = st;
add = true;
for (st = nxt(st.first.first, st.first.second, st.second); st != init;)
st = nxt(st.first.first, st.first.second, st.second);
printf("%lld", ans / fac);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, n = 3, s1, s2, s3;
vector<vector<long long int> > v(n, vector<long long int>(n));
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) cin >> v[i][j];
}
s1 = v[0][1] + v[0][2];
s2 = v[1][0] + v[1][2];
s3 = v[2][0] + v[2][1];
for (i = 0; i <= 3 * pow(10, 5); i++) {
if (s1 + s2 + s3 == 2 * i && i - s1 > 0 && i - s2 > 0 && i - s3 > 0) break;
}
v[0][0] = i - s1;
v[1][1] = i - s2;
v[2][2] = i - s3;
for (i = 0; i < n; i++) {
for (long long int it = 0; it < v[i].size(); it++) cout << v[i][it] << " ";
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m, t;
scanf("%d%d%d%d", &n, &k, &m, &t);
int l = n;
for (auto i = (1); i <= (t); ++i) {
int type, x;
scanf("%d%d", &type, &x);
if (type == 0) {
if (x < k) {
k -= x;
l -= x;
} else
l -= l - x;
} else {
if (x <= k) {
++k;
++l;
} else
++l;
}
printf("%d %d\n", l, k);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, k, l;
map<int, int> mp;
for (i = 2, j = 5; i <= 9; i++, j += 2) {
mp[i] = j;
}
cin >> n;
n = mp[n];
for (i = 0, k = 0; i < n; i++) {
for (j = 0; j < n; j++) {
bool flag = 1;
if (i <= n / 2) {
if (j == n / 2) {
l = k;
cout << k;
if (l <= 0) flag = 0;
} else if (j < (n / 2)) {
l = (n / 2) - j;
l = k - l;
if (l < 0)
cout << ' ';
else
cout << l;
} else {
l--;
if (l >= 0) cout << l;
if (l <= 0) flag = 0;
}
} else {
if (j == n / 2) {
l = k;
cout << k;
if (l <= 0) flag = 0;
} else if (j < (n / 2)) {
l = (n / 2) - j;
l = k - l;
if (l < 0)
cout << ' ';
else
cout << l;
} else {
l--;
if (l >= 0) cout << l;
if (l <= 0) flag = 0;
}
}
if (flag && j != n - 1) cout << " ";
}
if (i < n / 2)
k++;
else
k--;
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, p;
char str[10001];
int mem[1001][1001];
char GetMin(int m1, int m2) {
m1 -= 'a';
m2 -= 'a';
for (int i = 0; i < p; i++) {
if (i != m1 && i != m2) return i + 'a';
}
return 'a';
}
string nextString(string str, bool &isGood) {
isGood = true;
string res = str;
bool bOk = false;
for (int i = str.length() - 1; i >= 0; i--) {
char c = str[i] + 1;
while ((c - 'a') < p && c <= 'z') {
isGood = true;
if (i - 1 >= 0) {
if (c == res[i - 1]) {
isGood = false;
}
}
if (i - 2 >= 0) {
if (c == res[i - 2]) isGood = false;
}
if (isGood) {
bOk = true;
break;
}
c++;
}
if (bOk) {
res[i] = c;
int cnt = 0;
for (int j = i + 1; j < str.length(); j++) {
res[j] = GetMin((j - 1 >= 0) ? res[j - 1] : 'A',
(j - 2 >= 0) ? res[j - 2] : 'B');
cnt = (cnt + 1) % 3;
}
break;
} else {
res[i] = 'a';
}
}
if (!bOk) res = "";
return res;
}
int main() {
bool bGotIt = false;
string str;
string res = "NO";
cin >> n >> p;
cin >> str;
string nxtString = str;
bool isGood;
if ((nxtString = nextString(str, isGood)) != "") {
res = nxtString;
}
cout << res << endl;
return 0;
}
| 4 |
//ダニヤ
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int id = uniform_int_distribution<int>(l, r)(rng);
// result -= result / i;
// ll res = h[r] - (h[l - 1] * pw[r - l + 1]) % mod;
#include <bits/stdc++.h>
using namespace std;
#define khan ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define ll long long
#define pii pair < int, int >
#define pb push_back
const int N = 3e5 + 5, mod = 998244353, INF = 1e9 + 7;
const ll BIG = 1e18;
const int block = 316;
int n, m, x, p[N], sz[N];
ll su = 0, a[N];
vector < pii > g[N], edges[N];
vector < int > ans;
set < pair < ll, int > > s;
int get(int v){
if(v == p[v])
return v;
return p[v] = get(p[v]);
}
bool unite(int v, int u){
if(get(v) == get(u)){
return 0;
}
v = get(v);
u = get(u);
s.erase({a[v], v});
s.erase({a[u], u});
if(sz[v] > sz[u])
swap(u, v);
if(edges[v].size() > edges[u].size())
swap(edges[u], edges[v]);
for(auto e : edges[v]){
edges[u].pb(e);
}
sz[u] += sz[v];
a[u] = a[v] + a[u] - x;
if(a[u] < 0)
assert(0);
p[v] = u;
return 1;
}
int main(){
khan
cin >> n >> m >> x;
for(int i = 1; i <= n; i++){
cin >> a[i];
su += a[i];
}
for(int i = 1; i <= m; i++){
int u, v;
cin >> u >> v;
g[u].pb({v, i});
edges[u].pb({v, i});
g[v].pb({u, i});
edges[v].pb({u, i});
}
for(int i = 1; i <= n; i++){
p[i] = i;
sz[i] = 1;
}
if(su < (n - 1) * 1ll * x){
cout << "NO\n";
return 0;
}
for(int i = 1; i <= n; i++){
s.insert({a[i], i});
}
while(s.size() > 1){
pii cur = *s.rbegin();
int v = cur.second;//, val = cur.first;
v = get(v);
while(!edges[v].empty()){
pii qa = edges[v].back();
int num = qa.second;
bool cond = unite(v, qa.first);
if(cond){
ans.pb(num);
v = get(v);
s.insert({a[v], v});
break;
}
edges[v].pop_back();
}
}
cout << "YES\n";
for(auto e : ans){
cout << e << endl;
}
cout << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void rwFile() {
freopen("INP.inp", "r", stdin);
freopen("OUT.out", "w", stdout);
}
void solveC_VanyaAndScales() {
long long w, m;
cin >> w >> m;
if (w == 2) {
cout << "YES";
return;
}
int able = 1;
while (m) {
long long tmp = m % w;
m /= w;
if (tmp == w - 1) {
m++;
continue;
}
if (tmp == 1 || tmp == 0) continue;
able = 0;
break;
}
if (able == 1)
cout << "YES";
else
cout << "NO";
}
void solveC_BearAndPrimeNumbers() {
const int maxN = 1e6 + 5;
int prime[maxN];
memset(prime, 0, sizeof(prime));
for (int i = 2; i * i < maxN; i++) {
if (prime[i] == 0) {
for (int j = i * i; j < maxN; j += i) prime[j] = i;
}
}
vector<int> primes;
for (int i = 2; i < maxN; i++) {
if (prime[i] == 0) {
prime[i] = i;
primes.push_back(i);
}
}
int n;
cin >> n;
map<int, int> M;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
while (x != 1) {
int p = prime[x];
while (x % p == 0) x /= p;
M[p]++;
}
}
vector<int> res;
int len = 0;
for (int i : primes) {
if (i == 2)
res.push_back(M[i]);
else
res.push_back(res[len - 1] + M[i]);
len++;
}
int m;
cin >> m;
while (m--) {
int l, r;
cin >> l >> r;
int left = lower_bound(primes.begin(), primes.end(), l) - primes.begin();
int right = upper_bound(primes.begin(), primes.end(), r) - primes.begin();
if (left == 0)
cout << res[right - 1] << '\n';
else
cout << res[right - 1] - res[left - 1] << '\n';
}
}
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
void solveC_NekoDoesMaths() {
int a, b;
cin >> a >> b;
if (a % b == 0 || b % a == 0) {
cout << 0;
return;
}
int diff = abs(a - b);
long long minLCM = a / gcd(a, b) * 1LL * b;
int res = 0;
for (int i = 1; i * i <= diff; i++) {
if (diff % i != 0) continue;
int tmp = i;
int newRes = (tmp - a % tmp) % tmp;
int newA = a + newRes;
int newB = b + newRes;
long long newLCM = 1LL * newA / gcd(newA, newB) * newB;
if (newLCM < minLCM) {
minLCM = newLCM;
res = newRes;
}
tmp = diff / i;
newRes = (tmp - a % tmp) % tmp;
newA = a + newRes;
newB = b + newRes;
newLCM = 1LL * newA / gcd(newA, newB) * newB;
if (newLCM < minLCM) {
minLCM = newLCM;
res = newRes;
}
}
cout << res;
}
const int maxN = 2e6 + 5;
const long long MOD = 1e9 + 7;
long long Fact[maxN];
long long Pow(int b, int e = MOD - 2) {
if (e == 0) return 1LL;
long long x = Pow(b, e / 2);
if (e % 2 == 0) return x * x % MOD;
return (x * x % MOD) * b % MOD;
}
long long Choose(int n, int k) {
return (Fact[n] * Pow(Fact[k]) % MOD) * Pow(Fact[n - k]) % MOD;
}
void solveH_Bots() {
Fact[0] = 1;
for (int i = 1; i < maxN; i++) Fact[i] = Fact[i - 1] * i % MOD;
int n;
cin >> n;
long long res = 0;
for (int i = 1; i <= n + 1; i++) {
res += Choose(n + i, i);
res %= MOD;
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
solveH_Bots();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1234;
struct node {
int l, r;
} mp[MAXN];
int ans[MAXN];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> mp[i].l >> mp[i].r;
int cur = 1;
int i = 0;
while (i < n) {
while (i < n && mp[i].r < cur) ans[i++] = 0;
if (cur >= mp[i].l)
ans[i++] = cur++;
else
cur++;
}
for (int j = 0; j < n - 1; j++) cout << ans[j] << " ";
cout << ans[n - 1] << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAX = 1e5 + 5;
const long long MAX2 = 11;
const int MOD = 1000000000 + 7;
const long long INF = 20000;
const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const double pi = acos(-1);
int n, a, b, c, sp[17][MAX], dep[MAX], bt, q, sz[MAX], c1, c2, tgt;
vector<int> v[MAX];
void dfs(int nw) {
sz[nw] = 1;
for (auto i : v[nw]) {
if (dep[i]) continue;
dep[i] = dep[nw] + 1;
sp[0][i] = nw;
dfs(i);
sz[nw] += sz[i];
}
return;
}
inline void init() {
for (long long j = 1; j <= bt; j++)
for (long long i = 1; i <= n; i++)
if (sp[j - 1][i]) sp[j][i] = sp[j - 1][sp[j - 1][i]];
}
inline int lca(int x, int y) {
for (int i = (int)bt; i >= (int)0; i--)
if (dep[sp[i][x]] >= dep[y]) x = sp[i][x];
if (x == y) return x;
for (int i = (int)bt; i >= (int)0; i--) {
if (sp[i][x] == sp[i][y]) continue;
x = sp[i][x];
y = sp[i][y];
}
c1 = x, c2 = y;
return sp[0][x];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 2; i <= n; i++) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
dep[1] = 1;
dfs(1);
bt = log2(n);
init();
cin >> q;
while (q--) {
cin >> a >> b;
if (dep[a] < dep[b]) a ^= b ^= a ^= b;
if (a == b)
cout << n << "\n";
else if (dep[a] == dep[b]) {
c = lca(a, b);
cout << n - sz[c1] - sz[c2] << "\n";
} else if ((dep[a] - dep[b]) & 1)
cout << 0 << "\n";
else {
c = lca(a, b);
tgt = dep[a] - (dep[a] + dep[b] - dep[c] - dep[c]) / 2;
for (int i = (int)bt; i >= (int)0; i--)
if (dep[sp[i][a]] > tgt) a = sp[i][a];
c = sp[0][a];
cout << sz[c] - sz[a] << "\n";
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
string s;
cin >> s;
int a[n], count[3] = {0};
for (int i = 0; i < n; ++i) {
a[i] = s[i] - '0';
count[a[i]]++;
}
for (int i = 0; i < 3; ++i) {
count[i] -= n / 3;
}
if (count[0] > 0) {
for (int i = n - 1; i >= 0; --i) {
if (a[i] == 0) {
if (count[2] < 0) {
a[i] = 2;
++count[2];
--count[0];
} else if (count[1] < 0) {
a[i] = 1;
++count[1];
--count[0];
}
}
if (count[0] == 0) break;
}
}
if (count[2] > 0) {
for (int i = 0; i < n; ++i) {
if (a[i] == 2) {
if (count[0] < 0) {
a[i] = 0;
++count[0];
--count[2];
} else if (count[1] < 0) {
a[i] = 1;
++count[1];
--count[2];
}
}
if (count[2] == 0) break;
}
}
if (count[1] > 0) {
if (count[0] < 0) {
for (int i = 0; i < n; ++i) {
if (a[i] == 1) {
a[i] = 0;
++count[0];
--count[1];
}
if (count[0] == 0) break;
}
}
if (count[2] < 0) {
for (int i = n - 1; i >= 0; --i) {
if (a[i] == 1) {
a[i] = 2;
++count[2];
--count[1];
}
if (count[2] == 0) break;
}
}
}
for (int i = 0; i < n; ++i) {
cout << a[i];
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a[n], b[n], i, min = 1000, s = 0;
for (i = 0; i < n; i++) {
scanf("%d%d", &a[i], &b[i]);
if (b[i] < min) {
min = b[i];
}
s += (a[i] * min);
}
printf("%d", s);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int aa[100], cc[100];
int b[100];
int main() {
int a, c;
cin >> a >> c;
int tot1 = 0, tot2 = 0;
while (a) {
aa[tot1++] = a % 3;
a /= 3;
}
while (c) {
cc[tot2++] = c % 3;
c /= 3;
}
int minl = tot1 < tot2 ? tot1 : tot2;
int maxl = tot1 < tot2 ? tot2 : tot1;
for (int i = 0; i < maxl; i++) {
b[i] = (cc[i] + 3 - aa[i]) % 3;
}
int num = 0;
for (int i = maxl; i >= 0; i--) {
num = num * 3 + b[i];
}
cout << num << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
template <class T>
inline T abs1(T a) {
return a < 0 ? -a : a;
}
template <class T>
inline T max1(T a, T b) {
return a > b ? a : b;
}
template <class T>
inline T max1(T a, T b, T c) {
return max1(max1(a, b), c);
}
template <class T>
inline T max1(T a, T b, T c, T d) {
return max1(max1(a, b, c), d);
}
template <class T>
inline T max1(T a, T b, T c, T d, T e) {
return max1(max1(a, b, c, d), e);
}
template <class T>
inline T min1(T a, T b) {
return a < b ? a : b;
}
template <class T>
inline T min1(T a, T b, T c) {
return min1(min1(a, b), c);
}
template <class T>
inline T min1(T a, T b, T c, T d) {
return min1(min1(a, b, c), d);
}
template <class T>
inline T min1(T a, T b, T c, T d, T e) {
return min1(min1(a, b, c, d), e);
}
template <class T>
inline T lowb(T num) {
return num & (-num);
}
inline int jud(double a, double b) {
if (abs(a) < eps && abs(b) < eps)
return 0;
else if (abs1(a - b) / abs1(a) < eps)
return 0;
if (a < b) return -1;
return 1;
}
template <typename t>
inline int jud(t a, t b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) {
int be = 0, en = na - 1;
if (*a <= *(a + na - 1)) {
if (f_lb == 0)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != 1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != -1)
en = mid;
else
be = mid + 1;
}
if (f_small && jud(*(a + be), val) == 1) be--;
if (!f_small && jud(*(a + be), val) == -1) be++;
} else {
if (f_lb)
while (be < en) {
int mid = (be + en + 1) / 2;
if (jud(*(a + mid), val) != -1)
be = mid;
else
en = mid - 1;
}
else
while (be < en) {
int mid = (be + en) / 2;
if (jud(*(a + mid), val) != 1)
en = mid;
else
be = mid + 1;
}
if (!f_small && jud(*(a + be), val) == -1) be--;
if (f_small && jud(*(a + be), val) == 1) be++;
}
return be;
}
inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(int nValue) { return __builtin_popcount(nValue); }
inline int bitnum(unsigned long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitnum(long long nValue) {
return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32);
}
inline int bitmaxl(unsigned int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(int a) {
if (a == 0) return 0;
return 32 - __builtin_clz(a);
}
inline int bitmaxl(unsigned long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
inline int bitmaxl(long long a) {
int temp = a >> 32;
if (temp) return 32 - __builtin_clz(temp) + 32;
return bitmaxl(int(a));
}
long long pow(long long n, long long m, long long mod = 0) {
if (m < 0) return 0;
long long ans = 1;
long long k = n;
while (m) {
if (m & 1) {
ans *= k;
if (mod) ans %= mod;
}
k *= k;
if (mod) k %= mod;
m >>= 1;
}
return ans;
}
const int maxn = 201000;
int n, m;
int can[maxn][2];
map<int, vector<int> > mp;
vector<pair<int, int> > seg[maxn];
int y[maxn];
int lseg;
int ldp[2];
pair<int, int> dp[2][maxn];
int main() {
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
mp[a].push_back(b);
}
map<int, vector<int> >::iterator it = mp.begin();
for (; it != mp.end(); it++, lseg++) {
sort(it->second.begin(), it->second.end());
int last = 0;
it->second.push_back(n);
for (int i = 0; i < (int)it->second.size(); i++) {
if (it->second[i] > last)
seg[lseg].push_back(make_pair(last, it->second[i] - 1));
last = it->second[i] + 1;
}
y[lseg] = it->first;
}
seg[lseg].push_back(make_pair(n - 1, n - 1));
y[lseg++] = n;
int now = 0, nxt = 1;
ldp[0] = 1;
if (y[0] != 0)
dp[0][0] = make_pair(0, n - 1);
else
dp[0][0] = seg[0][0];
for (int i = 0; i < lseg; i++) {
if (i && y[i] > y[i - 1] + 1 && ldp[now]) {
ldp[now] = 1;
dp[now][0].second = n - 1;
}
ldp[nxt] = 0;
for (int link1 = 0, link2 = 0;
link1 < ldp[now] && link2 < (int)seg[i].size();) {
pair<int, int> a;
a.first = max(seg[i][link2].first, dp[now][link1].first);
a.second = seg[i][link2].second;
if (dp[now][link1].second >= seg[i][link2].first &&
dp[now][link1].first <= seg[i][link2].second)
if (a.first <= a.second) dp[nxt][ldp[nxt]++] = a;
if (dp[now][link1].second < seg[i][link2].second)
link1++;
else
link2++;
}
swap(now, nxt);
}
if (ldp[now])
printf("%d\n", 2 * (n - 1));
else
printf("-1\n");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int a[10][10];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
int x1 = a[0][1] + a[0][2];
int x2 = a[1][0] + a[1][2];
int x3 = a[2][0] + a[2][1];
int y = (x1 + x2 + x3) / 2;
a[0][0] = y - x1;
a[1][1] = y - x2;
a[2][2] = y - x3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
#define all(vec) vec.begin(),vec.end()
using namespace std;
const int maxn = 105;
int n, q;
const int mod = 1e9 + 7;
int dp[maxn][maxn*maxn];
int s[maxn][maxn*maxn];
int b[maxn], c[maxn];
int sol(int x){
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
fors(i,0,maxn*maxn) s[0][i] = 1;
int low = 0, up = 0;
for(int i = 1; i <= n; ++i){
up += c[i];
low += b[i-1]+x;
for(int j = max(0, low); j <= up; ++j){
int l = max(0, j-c[i]), r = j;
if(l == 0) dp[i][j] = s[i-1][r];
else dp[i][j] = (s[i-1][r]-s[i-1][l-1])%mod;
}
s[i][0] = dp[i][0];
fors(j,1,maxn*maxn) s[i][j] = (dp[i][j]+s[i][j-1])%mod;
}
return (s[n][maxn*maxn-1]+mod)%mod;
}
const int sed = 10007;
map<int,int> mp;
int main()
{
cin>>n; fors(i,1,n+1) cin>>c[i];
fors(i,1,n) cin>>b[i]; cin>>q;
fors(i,1,n) b[i]+=b[i-1];
//int x; cin>>x;
int l = 0x3f3f3f3f;
int r = 0x3f3f3f3f;
int low = 0, up = 0;
fors(i,1,n+1){
up += c[i];
low += b[i-1];//low + i*x <= up
r = min(r, (up-low)/i);
l = min(l, -low/i);
}
//if( n > 10) cout<<"l:"<<l<<" r:"<<r<<endl;
l--; r++;
fors(i,l,r+1) mp[i] = sol(i);
//if(n > 10) cout<<"sol:"<<sol(l)<<endl;
int ca = 0;
while(q--){
int x; scanf("%d", &x);
if(x < l) x = l;
if(x > r) printf("0\n");
else printf("%d\n", mp[x]);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M, X, count;
int *A, *D;
set<int> visited;
cin >> N >> M;
A = new int[N];
D = new int[N];
for (int i = 0; i < N; i++) cin >> A[i];
for (int i = N - 1; i >= 0; i--) {
visited.insert(A[i]);
D[i] = visited.size();
}
for (int i = 0; i < M; i++) {
cin >> X;
cout << D[--X] << '\n';
}
cin.ignore();
getchar();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
long long t[505050], ans;
set<long long> pos;
set<pair<long long, pair<long long, long long>>> cand;
map<pair<long long, long long>, long long> cst;
int main() {
scanf("%d%d", &k, &n);
for (int i = 0; i < n; i++) scanf("%lld", &t[i]);
sort(t, t + n);
n = unique(t, t + n) - t;
for (int i = 0; i < n; i++) pos.insert(t[i]);
for (int i = 1; i < n; i++) {
cand.insert({t[i] - t[i - 1], {t[i - 1], t[i]}});
cst[{t[i - 1], t[i]}] = t[i] - t[i - 1];
}
while (k--) {
while (cand.size()) {
auto it = *cand.begin();
cand.erase(it);
auto it1 = pos.find(it.second.first);
auto it2 = pos.find(it.second.second);
if (it1 == pos.end() or it2 == pos.end()) continue;
ans += it.first;
long long lft = -1, rgt = -1;
auto it4 = it1;
if (it4 != pos.begin()) lft = *--it4;
auto it3 = it2;
it3++;
if (it3 != pos.end()) rgt = *it3;
if (lft != -1 and rgt != -1) {
long long lcst = cst[{lft, *it1}];
long long rcst = cst[{*it2, rgt}];
long long ncst = lcst + rcst - it.first;
cst[{lft, rgt}] = ncst;
cand.insert({ncst, {lft, rgt}});
cand.erase({lcst, {lft, *it1}});
cand.erase({rcst, {*it2, rgt}});
}
pos.erase(it1);
pos.erase(it2);
break;
}
}
cout << ans << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long int MAX = 100000;
const int N = 2e5 + 5;
const long long int mod = 1e9 + 7;
const long long int mx = 1e16;
bool comp(const int a, const int b) { return a > b; }
const double PI = 2 * acos(0.0);
int main() {
long long int t, s, x;
scanf("%lld %lld %lld", &t, &s, &x);
if (x < t) {
cout << "NO";
return 0;
}
if (x == t + 1) {
cout << "NO";
return 0;
}
x -= t;
x %= s;
if (x == 0) {
cout << "YES";
return 0;
}
if (x == 1) {
cout << "YES";
return 0;
}
cout << "NO";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[100005], sum = 0;
bool b[100005];
int main(int argc, char const *argv[]) {
memset(b, false, sizeof b);
int n, i, j, k, p;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
for (i = n - 1; i >= 0; i--) {
if (sum > 0) {
sum -= a[i];
b[i] = true;
} else {
sum += a[i];
}
}
if (sum > 0) {
for (i = 0; i < n; i++)
if (b[i])
cout << "-";
else
cout << "+";
} else {
for (i = 0; i < n; i++)
if (b[i])
cout << "+";
else
cout << "-";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool myComp(pair<string, int> p1, pair<string, int> p2) {
if (p1.second > p2.second) {
return true;
} else if (p1.second == p2.second && p1.first < p2.first) {
return true;
}
return false;
}
int main() {
map<string, int> m;
string user;
cin >> user;
int q;
cin >> q;
string line;
string first;
string second;
string newSecond;
string action1;
string action2;
string distination;
cin.ignore();
while (q--) {
getline(cin, line);
stringstream ss(line);
ss >> first;
ss >> action1;
if (action1 != "likes") {
ss >> action2;
}
ss >> second;
newSecond = second.substr(0, second.find('\''));
ss >> distination;
if (first == user) {
if (action1 == "posted") {
m[newSecond] += 15;
} else if (action1 == "commented") {
m[newSecond] += 10;
} else {
m[newSecond] += 5;
}
} else if (newSecond == user) {
if (action1 == "posted") {
m[first] += 15;
} else if (action1 == "commented") {
m[first] += 10;
} else {
m[first] += 5;
}
} else {
if (m[first] == 0) {
m[first] = 0;
}
if (m[newSecond] == 0) {
m[newSecond] = 0;
}
}
}
vector<pair<string, int>> vec;
map<string, int>::iterator it;
for (it = m.begin(); it != m.end(); it++) {
vec.push_back(make_pair(it->first, it->second));
}
sort(vec.begin(), vec.end(), myComp);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i].first << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
int n;
string s1, s2;
cin >> n >> s1 >> s2;
int ans = 0;
if (s2 == "month") {
for (int i = 0; i <= 12; i++) {
if (month[i] >= n) ans++;
}
cout << ans;
} else {
if (n == 5 || n == 6)
cout << 53;
else
cout << 52;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
int arr[300005];
bool check(int mid, int n, int k) {
int i, tot = 0;
for (i = 1; i <= n; i++) {
if (arr[i] <= mid) {
tot++;
if (i < n) {
i++;
tot++;
}
}
}
if (tot >= k) return true;
tot = 1;
for (i = 2; i <= n; i++) {
if (arr[i] <= mid) {
tot++;
if (i < n) {
i++;
tot++;
}
}
}
if (tot >= k) return true;
return false;
}
int bin(int n, int k) {
int lo = 0;
int hi = 1e9;
while (hi - lo > 2) {
int mid = (lo + hi) / 2;
if (check(mid, n, k)) {
hi = mid;
} else {
lo = mid + 1;
}
}
while (check(hi - 1, n, k)) hi--;
return hi;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int a, b, c, i, j, k, n, m, t, cs = 1;
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> arr[i];
cout << bin(n, k) << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const double PI = 3.14159265358979323846;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void solve() {
int s, b;
cin >> s >> b;
vector<int> query(s);
for (int i = 0; i < int(query.size()); ++i) cin >> query[i];
vector<pair<int, int> > w(b);
for (int i = 0; i < int(b); ++i) cin >> w[i].first >> w[i].second;
sort(w.begin(), w.end());
vector<int> f(b + 1);
for (int i = 1; i < int(b + 1); ++i) f[i] = f[i - 1] + w[i - 1].second;
vector<int> d(b);
for (int i = 0; i < int(b); ++i) d[i] = w[i].first;
for (int i = 0; i < int(s); ++i)
cout << f[upper_bound(d.begin(), d.end(), query[i]) - d.begin()] << " ";
cout << endl;
}
int main() {
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, k;
char a[N], b[N];
int main() {
scanf("%d%d%s%s", &n, &k, a, b);
long long cur = 1, ans = 0;
if (k == 1) {
cout << n << endl;
return 0;
}
for (int i = 0; i < n; i++) {
cur *= 2;
if (a[i] != 'a') --cur;
if (b[i] != 'b') --cur;
cur = min(cur, k * 1ll);
ans += cur;
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, rev, cnt, a[111];
void rec(int l) {
if (l == m) {
cnt++;
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
if (a[i] > a[j]) rev++;
return;
}
for (int i = 0; i < n; i++)
for (int j = i; j < n; j++) {
for (int k = 0; i + k < j - k; k++) swap(a[i + k], a[j - k]);
rec(l + 1);
for (int k = 0; i + k < j - k; k++) swap(a[i + k], a[j - k]);
}
}
int main() {
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
rec(0);
printf("%.15lf\n", double(rev) / double(cnt));
return 0;
}
| 5 |
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ll long long int
#define ld long double
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
void solve(){
long long int n;
cin >> n;
long long int a[n];
long long sum = 0;
for(int i = 0; i < n; ++i){
cin >> a[i];
sum += a[i];
}
cout << (sum % n)*(n - (sum%n)) << '\n';
}
int main(){
fast;
int t = 1;
cin >> t;
while(t--)
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, x, ans[500005], tw[25];
char opt[5];
void ckmax(int &x, int y) { x = (x > y) ? x : y; }
struct edge {
int nxt, v, vo;
edge() {}
edge(int A, int B, int C) : nxt(A), v(B), vo(C) {}
} e[500005];
int tot, fr[500005], sz[500005], son[500005], t[500005], vc[500005], xo[500005];
int cnt, dep[500005], cdp[500005], dfn[500005], rin[500005], in[500005],
out[500005], lst[500005];
int c[(1 << 22) + 5], c2[(1 << 22) + 5];
void add(int x, int y, int z) {
++tot;
e[tot] = edge(fr[x], y, z), fr[x] = tot;
}
void dfs(int u) {
sz[u] = 1;
int mx = -1;
for (int i = fr[u]; i; i = e[i].nxt) {
int v = e[i].v;
cdp[v] = cdp[u] ^ (1 << e[i].vo);
dep[v] = dep[u] + 1;
dfs(v);
sz[u] += sz[v];
if (sz[v] > mx) son[u] = v, mx = sz[v], vc[u] = 1 << e[i].vo;
}
}
void dfs2(int u, int tp) {
dfn[++cnt] = u;
in[u] = cnt;
t[u] = tp;
if (!son[u]) {
rin[u] = cnt + 1;
out[u] = cnt;
return;
}
dfs2(son[u], tp);
rin[u] = cnt + 1;
xo[u] = xo[son[u]] ^ vc[u];
for (int i = fr[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == son[u]) continue;
dfs2(v, v);
}
out[u] = cnt;
}
void calc(int u, int x, int d1, int d2) {
for (int i = 0; i <= 22; ++i)
if (c[x ^ tw[i]]) ckmax(ans[u], c[x ^ tw[i]] + d1 - (d2 << 1));
}
void calc2(int u, int x, int d1, int d2) {
for (int i = 0; i <= 22; ++i)
if (c2[x ^ tw[i]]) ckmax(ans[u], c2[x ^ tw[i]] + d1 - (d2 << 1));
}
void solve(int u) {
if (!son[u]) {
if (t[u] == u) return;
++c[0];
}
for (int i = fr[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == son[u]) continue;
solve(v);
ckmax(ans[u], ans[v]);
}
solve(son[u]), ckmax(ans[u], ans[son[u]]);
c2[0] = dep[u];
for (int i = fr[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == son[u]) continue;
for (int j = in[v]; j <= out[v]; ++j)
calc2(u, cdp[dfn[j]] ^ cdp[u], dep[dfn[j]], dep[u]);
for (int j = in[v]; j <= out[v]; ++j)
ckmax(c2[cdp[dfn[j]] ^ cdp[u]], dep[dfn[j]]);
}
c2[0] = 0;
for (int i = fr[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (v == son[u]) continue;
for (int j = in[v]; j <= out[v]; ++j) c2[cdp[dfn[j]] ^ cdp[u]] = 0;
}
for (int i = rin[u]; i <= out[u]; ++i)
calc(u, cdp[dfn[i]] ^ cdp[u] ^ xo[u], dep[dfn[i]], dep[u]);
calc(u, xo[u], dep[u], dep[u]);
for (int i = rin[u]; i <= out[u]; ++i)
lst[dfn[i]] = xo[u] ^ cdp[dfn[i]] ^ cdp[u],
ckmax(c[lst[dfn[i]]], dep[dfn[i]]);
lst[u] = xo[u], ckmax(c[lst[u]], dep[u]);
if (t[u] == u) {
for (int i = in[u]; i <= out[u]; ++i) c[lst[dfn[i]]] = 0;
}
}
int main() {
tw[0] = 0;
for (int i = 1; i <= 22; ++i) tw[i] = 1 << i - 1;
scanf("%d", &n);
for (int i = 2; i <= n; ++i) {
scanf("%d", &x);
scanf("%s", opt);
add(x, i, opt[0] - 'a');
}
dep[1] = 1, dfs(1);
dfs2(1, 1);
solve(1);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
putchar('\n');
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
while (scanf("%d %d", &n, &m) == 2) {
char arr[160][160];
for (int i = 0, _n = n; i < _n; i++) scanf("%s", arr[i]);
int kanan[160];
int kiri[160];
for (int i = 0, _n = 160; i < _n; i++) kanan[i] = -1;
for (int i = 0, _n = 160; i < _n; i++) kiri[i] = 2000000000;
for (int i = 0, _n = n; i < _n; i++) {
for (int j = 0, _n = m; j < _n; j++) {
if (arr[i][j] == 'W') {
kiri[i] = min(kiri[i], j);
kanan[i] = max(kanan[i], j);
}
}
}
int sum = 0;
int pos = 0;
for (int i = 0; i < n; ++i) {
if (i % 2 == 0) {
if (kanan[i] != -1) {
sum += abs(pos - kanan[i]);
pos = kanan[i];
}
if (kanan[i + 1] != -1) {
if (kanan[i + 1] > pos) {
sum += abs(pos - kanan[i + 1]);
pos = kanan[i + 1];
}
}
bool ada = false;
for (int j = i + 1, _n = n; j < _n; j++) {
if (kanan[j] != -1 && kiri[j] != 2000000000) {
ada = true;
break;
}
}
if (!ada) break;
sum++;
} else {
if (kiri[i] != 2000000000) {
sum += abs(pos - kiri[i]);
pos = kiri[i];
}
if (kiri[i + 1] != 2000000000) {
if (kiri[i + 1] < pos) {
sum += abs(pos - kiri[i + 1]);
pos = kiri[i + 1];
}
}
bool ada = false;
for (int j = i + 1, _n = n; j < _n; j++) {
if (kanan[j] != -1 && kiri[j] != 2000000000) {
ada = true;
break;
}
}
if (!ada) break;
sum++;
}
}
printf("%d\n", sum);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int a[262144], b[262144];
int cnt;
void add(int x, int k) {
const int u = 9, d = 0;
0;
a[x] += k, a[x + 1] += k;
if (a[x + 1] > u) add(x + 1, u - a[x + 1]);
if (a[x + 1] < d) add(x + 1, d - a[x + 1]);
int t = k > 0 ? 1 : -1;
while (k) {
printf("%d %d\n", x + 1, t), k -= t;
if (++cnt >= ((int)1e5)) exit(0);
}
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%1d", a + i);
for (int i = 0; i < n; ++i) scanf("%1d", b + i);
long long int k = 0, ans = 0;
for (int i = 0; i < n; ++i) k = (b[i] - a[i]) - k, ans += std::abs(k);
if (k) return puts("-1"), 0;
printf("%lld\n", ans);
for (int i = 0; i < n; ++i)
if (b[i] != a[i]) add(i, b[i] - a[i]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
void rd(int &x) {
x = 0;
static char c;
while (c = getchar(), c < 48)
;
do x = x * 10 + (c ^ 48);
while (c = getchar(), c > 47);
}
const int N = 205, M = (int)1e3 + 5;
int n;
namespace Network_Flow {
int src, sink, tot_edge, lst[N], arc[N], dist[N];
bool vis[N];
struct Edge {
int to, nxt, cap;
Edge() {}
Edge(int to, int nxt, int cap) : to(to), nxt(nxt), cap(cap) {}
} edge[M << 1];
inline void init() { memset(lst, -1, n << 2); }
void add_edge(int u, int v, int cap) {
edge[tot_edge] = Edge(v, lst[u], cap);
lst[u] = tot_edge++;
}
void add_edges(int u, int v, int cap) {
add_edge(u, v, cap);
add_edge(v, u, cap);
}
void bfs() {
static int que[N];
int L = 0, R = 0;
memset(dist, -1, n << 2);
for (dist[que[R++] = src] = 0; L < R;) {
int u = que[L++];
for (int i = lst[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (edge[i].cap && !~dist[v]) dist[que[R++] = v] = dist[u] + 1;
}
}
}
int dfs(int u, int f) {
if (u == sink) return f;
int flow = 0;
vis[u] = true;
for (int &i = arc[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (edge[i].cap && !vis[v] && dist[v] == dist[u] + 1) {
int d = dfs(v, min(f, edge[i].cap));
if (d) {
edge[i].cap -= d;
edge[i ^ 1].cap += d;
flow += d;
if (!(f -= d)) break;
}
}
}
return flow;
}
int Dinic() {
for (int i = 0; i < tot_edge; i += 2)
edge[i].cap = edge[i ^ 1].cap = edge[i].cap + edge[i ^ 1].cap >> 1;
for (int flow = 0;;) {
bfs();
if (!~dist[sink]) return flow;
memcpy(arc, lst, n << 2);
memset(vis, false, n);
flow += dfs(src, inf);
}
}
void redfs(int u) {
vis[u] = true;
for (int i = lst[u]; ~i; i = edge[i].nxt) {
int v = edge[i].to;
if (!vis[v] && edge[i].cap) redfs(v);
}
}
void cope() {
memset(vis, false, n);
redfs(src);
}
} // namespace Network_Flow
int cnt, par[N << 1];
vector<int> adj[N << 1];
struct Edge {
int u, v, cost;
Edge() {}
Edge(int u, int v, int cost) : u(u), v(v), cost(cost) {}
inline bool operator<(const Edge &rhs) const { return cost > rhs.cost; }
} edge[N];
int get_root(int u) { return par[u] == u ? u : par[u] = get_root(par[u]); }
void dfs(int u) {
if (u < n) printf("%d%c", u + 1, " \n"[++cnt == n]);
for (int v : adj[u]) dfs(v);
}
int main() {
int m;
rd(n), rd(m);
Network_Flow::init();
for (int u, v, c; m--;) {
rd(u), rd(v), rd(c);
Network_Flow::add_edges(u - 1, v - 1, c);
}
m = 0;
for (int u = (1), _t = (n); u < _t; ++u) {
Network_Flow::src = u;
Network_Flow::sink = par[u];
edge[m++] = Edge(u, par[u], Network_Flow::Dinic());
Network_Flow::cope();
for (int v = (u + 1), _t = (n); v < _t; ++v)
if (par[v] == par[u] && Network_Flow::vis[v]) par[v] = u;
}
int ans = 0, tot = n;
sort(edge, edge + m);
for (int i = (0), _t = (n << 1); i < _t; ++i) par[i] = i;
for (int i = (0), _t = (m); i < _t; ++i) {
ans += edge[i].cost;
int u = get_root(edge[i].u), v = get_root(edge[i].v);
adj[tot].push_back(u);
adj[tot].push_back(v);
par[u] = par[v] = tot++;
}
cout << ans << endl;
dfs(tot - 1);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
template <int MAXSIZE>
struct Tree {
typedef function<void(int, int)> EdgeCallback;
typedef function<void(int)> NodeCallback;
inline Tree() {
cnt = 0;
firstlink_id.fill(-1);
}
inline void add_edge(int u, int v) {
add_edge_inner(u, v);
add_edge_inner(v, u);
}
void dfs(int u, int parent, NodeCallback const& pre_callback,
EdgeCallback const& callback, NodeCallback const& post_callback) {
pre_callback(u);
for (int i = firstlink_id[u]; i >= 0; i = links[i].nxtsibling_id) {
int v = links[i].dst;
if (v == parent) continue;
dfs(v, u, pre_callback, callback, post_callback);
callback(u, v);
}
post_callback(u);
}
private:
struct Data {
int nxtsibling_id;
int dst;
};
array<int, MAXSIZE> firstlink_id;
array<Data, 2 * MAXSIZE> links;
int cnt;
inline void add_edge_inner(int u, int v) {
links[cnt].dst = v;
links[cnt].nxtsibling_id = firstlink_id[u];
firstlink_id[u] = cnt++;
}
};
struct ValIncDec {
long long v, i = 0, d = 0;
};
const int MAX = 100001;
array<ValIncDec, MAX + 1> r;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
using Tree = Tree<MAX>;
int(n);
cin >> n;
Tree tree;
for (int i = 0; i < n - 1; i++) {
int(a);
cin >> a;
int(b);
cin >> b;
tree.add_edge(a, b);
}
for (int i = 1; i <= n; i++) {
cin >> r[i].v;
}
tree.dfs(
1, -1, [](int) {},
[](int u, int v) {
r[u].i = max(r[u].i, r[v].i);
r[u].d = min(r[u].d, r[v].d);
},
[](int u) {
r[u].v += r[u].i + r[u].d;
if (r[u].v < 0)
r[u].i -= r[u].v;
else
r[u].d -= r[u].v;
});
cout << (r[1].i - r[1].d) << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e2 + 8;
string a, b;
int main() {
cin >> a >> b;
long long n = a.size();
vector<long long> cnt(n + 5, 0);
for (long long i = 0; i < n; ++i) {
if (a[i] == '0') ++cnt[i];
if (b[i] == '0') ++cnt[i];
}
long long ptr = 0;
long long ans = 0;
while (ptr < n) {
if (cnt[ptr] == 2 && cnt[ptr + 1] == 2 && cnt[ptr + 2] == 2) {
ans += 2;
ptr = ptr + 3;
} else if (cnt[ptr] == 2 && cnt[ptr + 1] == 2) {
++ans;
ptr = ptr + 2;
} else if (cnt[ptr] == 2 && cnt[ptr + 1] == 1) {
++ans;
ptr = ptr + 2;
} else if (cnt[ptr] == 1 && cnt[ptr + 1] == 2) {
++ans;
ptr = ptr + 2;
} else {
ptr = ptr + 1;
}
}
cout << ans << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300, E = 200 * 200;
int head[N], nxt[E], to[E], cap[E];
int edgeCount;
int vis[N];
int TC = 0;
void init() {
edgeCount = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int f, int t, int c) {
nxt[edgeCount] = head[f];
head[f] = edgeCount;
cap[edgeCount] = c;
to[edgeCount++] = t;
}
void addBi(int f, int t, int c) {
addEdge(f, t, c);
addEdge(t, f, 0);
}
bool hat_path(int cur, int snk, int mx) {
if (cur == snk) return 1;
if (vis[cur] == TC) return 0;
vis[cur] = TC;
for (int i = head[cur]; i != -1; i = nxt[i]) {
if (cap[i] < mx) continue;
if (hat_path(to[i], snk, mx)) {
cap[i] -= mx;
cap[i ^ 1] += mx;
return 1;
}
}
return 0;
}
int maxFlow(int src, int snk) {
int res = 0, Mx = (1 << 20);
for (; Mx; Mx >>= 1) {
TC++;
while (hat_path(src, snk, Mx)) {
TC++;
res += Mx;
}
}
return res;
}
int solb[N], sola[N];
int res[N][N];
int main() {
int sum = 0, s2 = 0;
int n, m;
scanf("%d%d", &n, &m);
init();
for (int i = 0; i < n; i++) scanf("%d", &solb[i]), s2 += solb[i];
for (int i = 0; i < n; i++) scanf("%d", &sola[i]), sum += sola[i];
int f, t;
for (int i = 0; i < m; i++) {
scanf("%d%d", &f, &t);
addBi(f - 1, n + t - 1, solb[f - 1]);
addBi(t - 1, n + f - 1, solb[t - 1]);
}
for (int i = 0; i < n; i++) {
addBi(i, i + n, solb[i]);
}
for (int i = 0; i < n; i++) {
addBi(n + n + 1, i, solb[i]);
}
for (int i = 0; i < n; i++) {
addBi(n + i, n + n + 2, sola[i]);
}
int mx = maxFlow(n + n + 1, n + n + 2);
if (mx != sum || sum != s2) {
cout << "NO";
return 0;
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
for (int j = head[i]; j != -1; j = nxt[j]) {
if (cap[j ^ 1]) {
res[i][to[j] - n] = cap[j ^ 1];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << res[i][j] << " ";
}
cout << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int N, a[100010], b[100010], posA[100010];
multiset<int> S;
int get(int x) {
multiset<int>::iterator it = S.lower_bound(x), jt = it;
return min(abs(*it - x), abs(*--jt - x));
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%d", &a[i]), posA[a[i]] = i;
for (int i = 1; i <= N; i++) scanf("%d", &b[i]), S.insert(i - posA[b[i]]);
S.insert(-1e9);
for (int x = 1; x <= N; x++) {
printf("%d\n", get(x - 1));
S.erase(S.find(x - posA[b[x]]));
S.insert(x - posA[b[x]] + N);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
const long long inf = 1e18;
const long long maxn = 2e5 + 100;
long long pow(long long a, long long b) {
if (b < 0) return 0LL;
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
long long binpow(long long a, long long b, long long m) {
if (b < 0) return 0LL;
if (a <= 0) return 0LL;
a %= m;
long long ans = 1LL;
while (b) {
if (b & 1) ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
long long modinv(long long n) { return binpow(n, mod - 2, mod); }
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
void modInverse(int a, int m) {
int x, y;
int g = gcdExtended(a, m, &x, &y);
if (g != 1)
cout << "Inverse doesn't exist";
else {
int res = (x % m + m) % m;
cout << "Modular multiplicative inverse is " << res;
}
}
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
const long long N = 1e6 + 10;
long long sp[N];
void sieve(long long N) {
for (long long i = 1; i < N; i++) sp[i] = i;
for (long long i = 2; i < N; i++) {
if (sp[i] == i) {
for (long long j = i * i; j < N; j += i) {
if (sp[j] == j) sp[j] = i;
}
}
}
}
void solve() {
int n, d;
cin >> n >> d;
vector<long long> x(n);
for (long long i = 0; i < n; i++) {
cin >> x[i];
}
long long ans = 0;
for (long long i = 0; i < n - 2; i++) {
int tar = x[i] + d;
int l = i + 1, r = n - 1;
int mid;
while (l <= r) {
mid = (l + r) / 2;
if (x[mid] == tar) {
break;
}
if (x[mid] < tar) {
l = mid + 1;
}
if (x[mid] > tar) {
r = mid - 1;
}
}
if (l > r) {
if (r - i >= 2) {
ans += ((r - i) * (r - i - 1)) / 2;
}
} else {
if (mid - i >= 2) {
ans += ((mid - i) * (mid - i - 1)) / 2;
}
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;
const long double pi = 3.14159265359;
const int INF = (1LL << 63);
int n, m, x, y;
vector<pair<int, int> > e, le;
bool ck(int k) {
if (k >= n) return 0;
le.clear();
for (auto i : e) {
x = (i.first + k) % n;
y = (i.second + k) % n;
if (x > y) swap(x, y);
le.push_back(make_pair(x, y));
}
sort(le.begin(), le.end());
for (int i = 0; i < (int)e.size(); i++) {
if (le[i] != e[i]) return 0;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--, y--;
if (x > y) swap(x, y);
e.push_back(make_pair(x, y));
}
sort(e.begin(), e.end());
for (int i = 1; i * i <= n; i++) {
if (n % i == 0 && (ck(i) || ck(n / i))) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 9999999999999999;
int queries, order;
vector<long long> graphSize;
vector<long long> aToFirst, aToLast, bToFirst, bToLast;
void countLength(vector<long long> &toFirst, vector<long long> &toLast,
long long v, int n) {
if (n == 0) {
toFirst[0] = 0;
toLast[0] = 0;
return;
}
if (n == 1) {
toFirst[1] = v == 2;
toLast[1] = v == 1;
return;
}
if (v <= graphSize[n - 1]) {
countLength(toFirst, toLast, v, n - 1);
toFirst[n] = min(toFirst[n - 1], toLast[n - 1] + 2);
toLast[n] = min(toFirst[n - 1], toLast[n - 1]) + (n - 1) / 2 + 1;
} else {
countLength(toFirst, toLast, v - graphSize[n - 1], n - 2);
toFirst[n] = toFirst[n - 2] + 1;
toLast[n] = toLast[n - 2];
}
}
long long findMinLength(long long a, long long b, int n) {
if (a == b) return 0;
if (n <= 2) return 1;
if (a <= graphSize[n - 1]) {
if (b >= graphSize[n - 1] + 1) {
return min(aToFirst[n - 1], aToLast[n - 1]) + bToFirst[n - 2] + 1;
} else {
long long d1 = min(aToFirst[n - 1] + bToLast[n - 1] + 2,
aToLast[n - 1] + bToFirst[n - 1] + 2);
return min(d1, findMinLength(a, b, n - 1));
}
} else {
return findMinLength(a - graphSize[n - 1], b - graphSize[n - 1], n - 2);
}
}
int main() {
cin >> queries >> order;
graphSize.push_back(1);
graphSize.push_back(2);
int iMax = 2;
for (; graphSize.back() < MAX; ++iMax) {
graphSize.push_back(graphSize[iMax - 1] + graphSize[iMax - 2]);
}
for (int i = 1; i <= queries; ++i) {
long long a, b;
cin >> a >> b;
if (a > b) swap(a, b);
aToFirst.resize(min(order, iMax) + 1);
aToLast.resize(min(order, iMax) + 1);
bToFirst.resize(min(order, iMax) + 1);
bToLast.resize(min(order, iMax) + 1);
countLength(aToFirst, aToLast, a, min(order, iMax));
countLength(bToFirst, bToLast, b, min(order, iMax));
cout << findMinLength(a, b, min(order, iMax)) << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E5;
int a[MAXN];
long long dp[MAXN + 1];
int where[MAXN + 1];
void solve(int l, int r)
{
if (l > r)
return;
int ones = 0;
for (int i = l; i <= r; i++)
{
if (a[i] == 1)
{
ones++;
}
}
if (ones == r - l + 1)
{
cout << "1";
for (int i = l + 1; i <= r; i++)
{
cout << "+1";
}
return;
}
if (a[l] == 1)
{
cout << "1";
l++;
while (a[l] == 1)
{
cout << "+1";
l++;
}
cout << "+";
}
int oldR = r;
while (a[r] == 1)
r--;
long long prod = 1;
for (int i = l; i <= r; i++)
{
prod *= a[i];
if (prod >= (long long) 1E16)
{
break;
}
}
if (prod >= (long long) 1E16)
{
cout << a[l];
for (int i = l + 1; i <= r; i++)
cout << "*" << a[i];
}
else
{
vector<int> positions;
dp[1] = a[l];
where[1] = 1;
positions.push_back(1);
for (int i = 2; i <= r - l + 1; i++)
{
int ind = i + l - 1;
if (a[ind] == 1)
{
dp[i] = dp[i - 1] + 1;
where[i] = i;
}
else
{
dp[i] = a[ind] + dp[i - 1];
where[i] = i;
long long curProd = a[ind];
for (int j = (int) positions.size() - 1; j >= 0; j--)
{
curProd *= a[positions[j] + l - 1];
if (curProd + dp[positions[j] - 1] > dp[i])
{
dp[i] = curProd + dp[positions[j] - 1];
where[i] = positions[j];
}
}
positions.push_back(i);
}
//cout << "dp " << i << " " << dp[i] << endl;
}
vector<char> sym(r - l + 1);
int cur = r - l + 1;
while (cur >= 1)
{
for (int i = cur - 1; i >= where[cur]; i--)
{
sym[i] = '*';
}
if (where[cur] - 1 >= 1)
sym[where[cur] - 1] = '+';
cur = where[cur] - 1;
}
for (int i = 1; i < r - l + 1; i++)
{
cout << a[i + l - 1] << sym[i];
}
cout << a[r];
}
if (oldR > r)
{
cout << "+1";
oldR--;
while (oldR > r)
{
cout << "+1";
oldR--;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
string s;
cin >> s;
bool add = false;
bool sub = false;
bool mult = false;
for (char ch : s)
{
if (ch == '+')
add = true;
if (ch == '-')
sub = true;
if (ch == '*')
mult = true;
}
if (!mult)
{
cout << a[0];
for (int i = 1; i < n; i++)
{
if (add)
cout << "+";
else
cout << "-";
cout << a[i];
}
cout << "\n";
}
else
{
if (!add && !sub)
{
cout << a[0];
for (int i = 1; i < n; i++)
{
cout << "*";
cout << a[i];
}
cout << "\n";
}
else if (!add)
{
cout << a[0];
for (int i = 1; i < n; i++)
{
if (a[i] == 0)
cout << "-";
else
cout << "*";
cout << a[i];
}
cout << "\n";
}
else //if (!sub)
{
int cur = 0;
for (int i = 0; i < n;)
{
if (a[i] == 0)
{
solve(cur, i - 1);
if (i)
cout << "+";
while (i < n && a[i] == 0)
{
cout << "0";
if (i < n - 1)
cout << "+";
i++;
}
cur = i;
}
else
i++;
}
if (cur < n)
{
solve(cur, n - 1);
}
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int consider(char *s) {
int len = strlen(s);
if (len % 2 != 0) return 0;
int i = 0, j = len / 2;
while (j < len) {
if (s[i] != s[j]) return 0;
i++;
j++;
}
return 1;
}
void solution() {
int times;
scanf("%d", ×);
for (int t = 0; t < times; t++) {
char s[1000];
scanf("%s", s);
if (consider(s))
printf("YES\n");
else
printf("NO\n");
}
}
int main() {
solution();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 1e3 + 7;
const int INF = 1e9 + 7;
int n, l[N], c[N << 1][N << 1];
char d[N];
vector<int> vx, vy;
void gao(int lx, int ly, int rx, int ry) {
if (lx > rx) swap(lx, rx);
if (ly > ry) swap(ly, ry);
lx = lower_bound(vx.begin(), vx.end(), lx) - vx.begin();
rx = lower_bound(vx.begin(), vx.end(), rx) - vx.begin();
ly = lower_bound(vy.begin(), vy.end(), ly) - vy.begin();
ry = lower_bound(vy.begin(), vy.end(), ry) - vy.begin();
for (int i = (lx), I = (rx + 1); i < I; ++i)
for (int j = (ly), I = (ry + 1); j < I; ++j) c[i][j] = 1;
}
void inv() {
queue<pair<int, int> > que;
c[0][0] = 2, que.push(make_pair(0, 0));
while (!que.empty()) {
int sx = que.front().first, sy = que.front().second;
que.pop();
for (int i = (0), I = (4); i < I; ++i) {
int x = sx + dx[i], y = sy + dy[i];
if (x < 0 || x >= ((int)(vx).size()) || y < 0 ||
y >= ((int)(vy).size()) || c[x][y])
continue;
c[x][y] = 2, que.push(make_pair(x, y));
}
}
}
int main() {
scanf("%d", &n);
vx.push_back(0), vx.push_back(1), vx.push_back(-INF), vx.push_back(INF);
vy.push_back(0), vy.push_back(1), vy.push_back(-INF), vy.push_back(INF);
int x = 0, y = 0;
for (int i = (0), I = (n); i < I; ++i) {
scanf(" %c %d", &d[i], &l[i]);
if (d[i] == 'L') {
x -= l[i];
} else if (d[i] == 'R') {
x += l[i];
} else if (d[i] == 'D') {
y -= l[i];
} else {
y += l[i];
}
vx.push_back(x), vx.push_back(x + 1);
vy.push_back(y), vy.push_back(y + 1);
}
sort(vx.begin(), vx.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
sort(vy.begin(), vy.end());
vy.erase(unique(vy.begin(), vy.end()), vy.end());
x = 0, y = 0;
for (int i = (0), I = (n); i < I; ++i) {
if (d[i] == 'L') {
gao(x, y, x - l[i], y);
x -= l[i];
} else if (d[i] == 'R') {
gao(x, y, x + l[i], y);
x += l[i];
} else if (d[i] == 'D') {
gao(x, y, x, y - l[i]);
y -= l[i];
} else {
gao(x, y, x, y + l[i]);
y += l[i];
}
}
inv();
long long ans = 0;
for (int i = (0), I = (((int)(vx).size()) - 1); i < I; ++i)
for (int j = (0), I = (((int)(vy).size()) - 1); j < I; ++j)
if (c[i][j] != 2) ans += 1ll * (vx[i + 1] - vx[i]) * (vy[j + 1] - vy[j]);
printf("%lld", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 1e5;
const double EPS = 1e-9;
const long long mod = 1e9 + 7;
int n, arr[1010];
long long memo[1010];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
memo[0] = 0;
memo[1] = 2;
for (int i = 2; i <= n; i++) {
memo[i] = 2;
for (int j = arr[i]; j < i; j++) {
memo[i] = (memo[i] + memo[j]) % mod;
}
}
long long sum = 0;
for (int i = 1; i <= n; i++) sum = (sum + memo[i]) % mod;
printf("%lld\n", sum);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool issubset(vector<int> &a, vector<int> &b) {
if (a.size() > b.size()) return false;
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] == b[j]) {
i++;
}
j++;
}
if (i == a.size()) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m, n;
cin >> m >> n;
vector<vector<int> > x(m), y(m);
for (int i = 0; i < m; i++) {
int nn;
cin >> nn;
vector<int> vis(n + 1, 0);
while (nn--) {
int p;
cin >> p;
x[i].push_back(p);
vis[p] = 1;
}
sort(x[i].begin(), x[i].end());
for (int j = 1; j < n + 1; j++)
if (!vis[j]) y[i].push_back(j);
}
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
if (issubset(x[i], y[j])) {
cout << "impossible\n";
return 0;
}
}
}
cout << "possible\n";
return 0;
}
| 6 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <deque>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;
const long long mod = 1000000007;
const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define srtrev(c) sort(all(c)); reverse(all(c))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define read(x) scanf("%d", &x)
#define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i])
#define pb push_back
#define mp make_pair
void mul(vi & a, int k) {
vi b(k,0);
for(auto x : a) b.pb(x);
forn(i,0,a.size()) b[i] = (b[i] + mod-a[i])%mod;
a.resize(b.size() - 1);
for(int i = b.size() - 1; i > 0; i--) {
a[i-1] = b[i];
b[i-1] = (b[i] + b[i-1])%mod;
}
}
vi c, b;
int n;
int get(int x) {
vi p(1,1);
int sb = 0;
int sbt = 0;
int add = 0;
forn(i,0,n) {
mul(p, c[i]+1);
int cur = x*(i+1) + sbt - add;
if(cur > (int)p.size()) {
return 0;
}
if(cur>0) {
forn(j,0,p.size() - cur) p[j] = p[j + cur];
p.resize(p.size() - cur);
add += cur;
}
sb+=b[i];
sbt+=sb;
}
ll ans = 0;
forn(i,0,p.size()) ans = ans+p[i];
return ans % mod;
}
set<int> cool;
int death = 1000;
void getcool() {
int sb = 0;
int sbt = 0;
int dp = 0;
forn(i,0,n) {
dp += c[i];
forn(x,-sbt/(i+1),(dp-sbt)/(i+1)+2) {
int cur = x*(i+1) + sbt;
if(cur >0 && cur <= dp) {
cool.insert(x);
}
else if(cur > dp) {
death = min(death, x);
}
}
sb+=b[i];
sbt+=sb;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
scanf("%d", &n);
// readv(c,n);
// readv(b,n-1);
c.resize(n);
b.resize(n-1);
forn(i,0,n) scanf("%d", &c[i]);
forn(i,0,n-1) scanf("%d", &b[i]);
int q;
read(q);
getcool();
map<int, int> ans;
for(auto x : cool) {
if(x < death) ans[x] = get(x);
else ans[x] = 0;
}
ll full = 1;
forn(i,0,n) full = (full * (c[i]+1))%mod;
while(q--) {
int x;
read(x);
if(cool.find(x) != cool.end()) printf("%d\n", ans[x]);
else if(x >= death) printf("0\n");
else printf("%d\n", full);
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, iv6 = 166666668;
long long m;
template <const int T>
class modint {
public:
int dat;
modint() { dat = 0; }
modint(int x) { dat = x; }
modint(long long x) { dat = x % T; }
modint<T> operator+(const modint<T> &t) const {
modint ret(dat + t.dat);
if (ret.dat >= T) ret.dat -= T;
return ret;
}
modint<T> operator-(const modint<T> &t) const {
modint ret(dat - t.dat);
if (ret.dat < 0) ret.dat += T;
return ret;
}
modint<T> operator*(const modint<T> &t) const {
long long tmp = 1ll * dat * t.dat;
return modint<T>(tmp);
}
modint<T> operator^(int t) const {
modint<T> ret(1), a = *this;
while (t) {
if (t & 1) ret = ret * a;
a = a * a;
t >>= 1;
}
return ret;
}
};
modint<mod> s2[1000010], s4[1000010], s6[1000010];
int main() {
scanf("%lld", &m);
for (long long i = 1; i * i <= m; i++) {
s2[i] = s2[i - 1] + modint<mod>(i * i);
s4[i] = s4[i - 1] + (modint<mod>(i) ^ 4);
s6[i] = s6[i - 1] + (modint<mod>(i) ^ 6);
}
modint<mod> ans, tm(m);
modint<mod> S = tm * (tm + 1) * (tm + 2);
for (long long i = 0; i * i <= m; i++) {
modint<mod> x(i);
long long ymax = sqrt(m - i * i) + 1e-7;
modint<mod> cur =
(S * ymax + (x ^ 6) * ymax * 2 + s2[ymax] * (x ^ 4) * 6 +
s4[ymax] * (x ^ 2) * 6 + s6[ymax] * 2 -
(tm + 2) * ((x ^ 4) * ymax + x * x * s2[ymax] * 2 + s4[ymax]) * 3 +
(tm * 3 + 4) * (x * x * ymax + s2[ymax])) *
2 +
S + (x ^ 6) * 2 + (tm * 3 + 4) * x * x - (tm + 2) * (x ^ 4) * 3;
if (i == 0)
ans = ans + cur;
else
ans = ans + cur * 2;
}
printf("%d\n", (ans * iv6).dat);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int id1[1005], id2[1005];
vector<pair<int, int> > nei[3005];
int co[3005];
bool flag;
void dfs(int v, int col) {
if (~co[v] && co[v] != col) {
flag = 1;
return;
} else if (~co[v])
return;
co[v] = col;
for (int i = 0; i < nei[v].size(); i++) {
int u = nei[v][i].first, c = nei[v][i].second;
dfs(u, col ^ c);
}
}
const long long MOD = 998244353;
int main() {
string s;
cin >> s;
int cnt = 1;
for (int i = 0; i < s.size(); i++) {
id1[i] = cnt++;
}
long long res = 0;
for (int i = 0; i < (int)s.size() - 1; i++) {
memset(co, -1, sizeof co);
flag = 0;
int tcnt = cnt;
for (int j = 0; j < 2 * (int)s.size(); j++) nei[j].clear();
for (int j = 0; j <= i; j++) {
id2[j] = tcnt++;
if (s[j] != '?') {
nei[id1[j]].push_back(make_pair(id2[j], s[j] - '0'));
nei[id2[j]].push_back(make_pair(id1[j], s[j] - '0'));
}
}
nei[0].push_back(make_pair(id2[i], 1));
for (int j = i + 1; j < s.size(); j++) {
if (s[j] != '?') nei[0].push_back(make_pair(id1[j], s[j] - '0'));
}
for (int j = 0; j < (i + 1) / 2; j++) {
nei[id2[j]].push_back(make_pair(id2[i - j], 0));
nei[id2[i - j]].push_back(make_pair(id2[j], 0));
}
for (int j = 0; j < (int)s.size() / 2; j++) {
nei[id1[j]].push_back(make_pair(id1[(int)s.size() - j - 1], 0));
nei[id1[(int)s.size() - j - 1]].push_back(make_pair(id1[j], 0));
}
dfs(0, 0);
long long dif = 1;
for (int j = 1; j < tcnt; j++) {
if (co[j] == -1) {
dfs(j, 0);
dif *= 2;
if (dif >= MOD) dif -= MOD;
}
}
if (!flag) res += dif;
if (res >= MOD) res -= MOD;
}
cout << res;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long f[2][1000005];
long long h[1000005];
int n;
int main() {
scanf("%d", &n);
for (int i = (0); i < (n); i++) scanf("%lld", h + i);
long long res = 0;
for (int i = (1); i < (n + 1); i++) {
long long mx = h[i - 1] - 1;
f[1][i] = h[i - 1] - 1;
if (i != 1) {
if (h[i - 1] >= h[i - 2]) {
long long mn = h[i - 1] - h[i - 2] + 1;
f[1][i] += (mx - mn + 1) * f[0][i - 1];
} else {
long long mn = 1;
f[1][i] += (mx - mn + 1) * f[0][i - 1];
}
}
f[1][i] %= 1000000007ll;
res += f[1][i];
if (i != n) {
long long mn = h[i - 1] >= h[i] ? h[i - 1] - h[i] + 1 : 1;
f[0][i] += mx - mn + 1;
if (i != 1) {
if (h[i - 1] >= h[i - 2]) {
long long MN = max(mn, h[i - 1] - h[i - 2] + 1);
f[0][i] += (mx - MN + 1) * f[0][i - 1];
} else {
f[0][i] += (mx - mn + 1) * f[0][i - 1];
}
}
}
f[0][i] %= 1000000007ll;
}
printf("%lld\n", res % 1000000007ll);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long a, b;
cin >> a >> b;
long long arr[a][2];
long long brr[b][2];
for (auto i = 0; i < a; i++) cin >> arr[i][0] >> arr[i][1];
for (auto i = 0; i < b; i++) cin >> brr[i][0] >> brr[i][1];
vector<long long> c(11);
for (auto i = 0; i < 11; i++) c[i] = 0;
for (auto i = 0; i < a; i++) {
long long n = arr[i][0], m = arr[i][1];
for (auto j = 0; j < b; j++) {
long long k = brr[j][0], l = brr[j][1];
if (n == k && m == l) {
continue;
}
if (n == l && m == k) {
continue;
}
if (n == k || n == l) {
c[n] += 1;
}
if (m == k || m == l) {
c[m] += 1;
}
}
}
long long jj = 0, ll;
for (auto i = 0; i < 11; i++) {
if (c[i]) {
jj++;
ll = i;
}
}
if (jj == 1) {
cout << ll;
return;
}
for (auto i = 0; i < a; i++) {
vector<long long> cc(11);
for (auto i = 0; i < 11; i++) cc[i] = 0;
long long n = arr[i][0], m = arr[i][1];
for (auto j = 0; j < b; j++) {
long long k = brr[j][0], l = brr[j][1];
if (n == k && m == l) {
continue;
}
if (n == l && m == k) {
continue;
}
if (n == k || n == l) {
cc[n] += 1;
}
if (m == k || m == l) {
cc[m] += 1;
}
}
long long count = 0;
for (auto i = 0; i < 10; i++) {
if (cc[i]) {
count++;
}
}
if (count > 1) {
cout << "-1";
return;
}
}
for (auto i = 0; i < b; i++) {
vector<long long> cc(11);
for (auto i = 0; i < 11; i++) cc[i] = 0;
long long n = brr[i][0], m = brr[i][1];
for (auto j = 0; j < a; j++) {
long long k = arr[j][0], l = arr[j][1];
if (n == k && m == l) {
continue;
}
if (n == l && m == k) {
continue;
}
if (n == k || n == l) {
cc[n] += 1;
}
if (m == k || m == l) {
cc[m] += 1;
}
}
long long count = 0;
for (auto i = 0; i < 10; i++) {
if (cc[i]) {
count++;
}
}
if (count > 1) {
cout << "-1";
return;
}
}
cout << "0";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long tests = 1;
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> LA(100100);
int res;
int miembros[100010];
int dfs(int hijo, int padre) {
miembros[hijo] = 1;
for (int i = 0; i < LA[hijo].size(); i++) {
if (LA[hijo][i] != padre) {
int aux = dfs(LA[hijo][i], hijo);
if (aux == 0) {
miembros[hijo] += miembros[LA[hijo][i]];
}
}
}
if (miembros[hijo] % 2 == 0) {
res++;
return 1;
} else {
return 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int nodo1, nodo2;
cin >> n;
for (int i = 0; i < n - 1; i++) {
cin >> nodo1 >> nodo2;
LA[nodo1].push_back(nodo2);
LA[nodo2].push_back(nodo1);
}
if (n % 2 == 1) {
cout << -1 << endl;
return 0;
}
if (n == 2) {
cout << 0 << endl;
return 0;
}
res = 0;
dfs(1, 0);
cout << res - 1 << endl;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
const int maxn = 1e5 + 5, maxm = 135;
int Okabe[maxm], Rintaro[maxm];
unsigned Chris[maxm], Tina[maxm];
int n, p, q;
unsigned answer = 0;
unsigned fastpow(unsigned x, unsigned y) {
if (y == 0) return 1;
unsigned tmp = fastpow(x, y >> 1);
return y & 1 ? tmp * tmp * x : tmp * tmp;
}
int main() {
scanf("%d%d%d", &n, &p, &q);
if (p >= n) p = n - 1;
for (int i = 0; i <= p; i++) {
int now = i;
while (now && now % 2 == 0) {
Okabe[i]++;
now >>= 1;
}
now = n - i;
while (now && now % 2 == 0) {
Rintaro[i]++;
now >>= 1;
}
}
for (int i = 0; i <= p; i++) {
Chris[i] = 1;
for (int k = 1; k <= i; k++) Chris[i] *= k / (1 << Okabe[k]);
Chris[i] = fastpow(Chris[i], (1u << 31) - 1);
int cnt = 0;
Tina[i] = 1;
for (int k = 1; k <= i; k++)
cnt += Rintaro[k - 1] - Okabe[k],
Tina[i] *= (n - k + 1) / (1 << Rintaro[k - 1]);
Tina[i] *= 1u << cnt;
}
for (int i = 1; i <= q; i++) {
unsigned now = 0, base = 1;
for (int j = 0; j <= p; j++, base *= i) now += Chris[j] * Tina[j] * base;
answer ^= ((unsigned)i * now);
}
printf("%u\n", answer);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
void solve() {
long long n, b;
cin >> n >> b;
vector<pair<long long, int>> v;
long long ans = 1e18;
for (long long x = 2; x * x <= b; x++)
if (b % x == 0) {
int cnt = 0;
while (b % x == 0) b /= x, cnt++;
v.push_back({x, cnt});
}
if (b > 1) v.push_back({b, 1});
for (auto it : v) {
long long p = 1, sum = 0;
while (p <= n / it.first) {
p *= it.first;
sum += n / p;
}
ans = min(ans, sum / it.second);
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
long long ans;
int sz[1000002];
int main() {
scanf("%d%d", &N, &M);
for (int i = 0, x, y; i < M; i++) {
scanf("%d%d", &x, &y);
sz[x]++, sz[y]++;
}
for (int i = 1; i <= N; i++) {
long long x = sz[i], y = N - sz[i] - 1;
ans += x * (x - 1) + y * (y - 1) - x * y;
}
printf("%I64d\n", ans / 6);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long gsd(long long a, long long b) {
while (b) a %= b, swap(a, b);
return a;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
long long m, d, w;
cin >> m >> d >> w;
long long w1 = w / gsd(d - 1, w), k = min(d, m) / w1;
cout << min(d, m) * k - (1 + k) * k / 2 * w1 << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
double A, B, L;
namespace Ninit {
void init() {
cin >> A >> B >> L;
if (A > B) swap(A, B);
}
} // namespace Ninit
namespace Nsolve {
double Top(double x) { return cos(x) * (B - L * sin(x) + A * tan(x)); }
void solve() {
if (L <= A) return printf("%.9lf\n", L), void();
if (L <= B) return printf("%.9lf\n", A), void();
double l, r, t1, t2;
for (l = 0, r = acos(0); l + 1e-8 < r; Top(t1) < Top(t2) ? r = t2 : l = t1)
t1 = (l * 2 + r) / 3, t2 = (l + r * 2) / 3;
Top(l) < 1e-8 ? printf("My poor head =(\n") : printf("%.9lf\n", Top(l));
}
} // namespace Nsolve
int main() {
Ninit::init();
Nsolve::solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
char buf[1 << 20], *p1, *p2;
inline int read() {
int s = 0, f = 1;
char x = getchar();
while (!isdigit(x)) f = (x == '-' ? -1 : 1), x = getchar();
while (isdigit(x)) s = s * 10 + x - '0', x = getchar();
return s * f;
}
pair<int, int> a[200005];
int s[200005];
int c[400005];
const int p = 1e9 + 7;
int n;
void mdf(int x, int k) {
for (int i = x; i <= 2 * n; i += (i & -i)) c[i] = (c[i] + k) % p;
}
int query(int x) {
int ans = 0;
for (int i = x; i; i -= (i & -i)) ans = (ans + c[i]) % p;
return ans;
}
bool ins[400005];
int main() {
n = read();
for (int i = 1; i <= n; i++) {
int x = read(), y = read();
a[i] = make_pair(x, y);
}
int m = read();
for (int i = 1; i <= m; i++) s[i] = read(), ins[a[s[i]].first] = 1;
sort(s + 1, s + m + 1,
[](int x, int y) { return a[x].second < a[y].second; });
int las = a[s[1]].first, mx = a[s[n]].second;
sort(a + 1, a + n + 1,
[](pair<int, int> x, pair<int, int> y) { return x.second < y.second; });
for (int i = n; i >= 1; i--) {
if (!ins[a[i].first]) {
if (query(2 * n - a[i].first + 1)) ins[a[i].first] = 1;
} else
mdf(2 * n - a[i].first + 1, 1);
}
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++) {
int f = (query(2 * n) - query(a[i].first) + p) % p;
mdf(a[i].first, (f + 1) % p);
}
int ans = 0;
for (int i = 1; i <= 2 * n; i++)
if (ins[i]) ans = (ans + query(i) - query(i - 1)) % p, ans = (ans + p) % p;
printf("%d\n", ans);
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int panjang, kk = 0, n;
string s;
cin >> s;
panjang = s.length();
for (int i = 1; i < panjang - 1; i++) {
if (s[i] != 'a' && s[i] != 'i' && s[i] != 'u' && s[i] != 'e' &&
s[i] != 'o' && s[i] != ' ') {
if (s[i - 1] != 'a' && s[i - 1] != 'i' && s[i - 1] != 'u' &&
s[i - 1] != 'e' && s[i - 1] != 'o' && s[i - 1] != ' ') {
if (s[i + 1] != 'a' && s[i + 1] != 'i' && s[i + 1] != 'u' &&
s[i + 1] != 'e' && s[i + 1] != 'o' && s[i + 1] != ' ') {
if (s[i] != s[i - 1] || s[i] != s[i + 1] || s[i - 1] != s[i + 1]) {
s.insert(i + 1, " ");
panjang++;
}
}
}
}
}
cout << s << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void Fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cerr.tie(NULL);
}
inline int D() {
int t;
scanf("%d", &t);
return t;
}
inline long long llD() {
long long t;
scanf("%lld", &t);
return t;
}
long long MOD = 1e9 + 7;
const int N = 2e5 + 5;
char arr[4] = {'a', 'b', 'c'}, s1[3], s2[3];
long long n, m, k, cnt, SU, MX, idx, sz = 1e9;
long long a, b, c, l, r, mid;
vector<pair<int, int>> vec;
vector<int> ans;
void printStr(string str) {
for (int i = 0; i < n; ++i) printf("%s", str.c_str());
}
char ans1[3], ans2[3], ans3[3], ans4[3];
void valid() {
puts("YES");
if (s2[0] == s2[1]) swap(s1[0], s2[0]), swap(s1[1], s2[1]);
if (s1[0] == s1[1] && s2[0] == s2[1]) {
printStr(arr);
return;
}
if (s1[0] == s1[1] && s1[0] != s2[0] && s1[0] != s2[1] && s2[0] != s2[1]) {
arr[0] = s2[0];
arr[1] = s1[0];
arr[2] = s2[1];
printStr(arr);
return;
}
do {
ans1[0] = arr[0], ans1[1] = arr[1];
ans2[0] = arr[2], ans2[1] = arr[2];
ans3[0] = arr[1], ans3[1] = arr[2];
ans4[0] = arr[1], ans4[1] = arr[0];
if (strcmp(ans1, s1) && strcmp(ans1, s2) && strcmp(ans3, s1) &&
strcmp(ans3, s2) && s1[0] != s1[1] && s2[0] != s2[1]) {
ans1[1] = ans2[1] = ans3[1] = '\0';
printStr(ans1);
printStr(ans3);
printStr(ans2);
return;
}
if (strcmp(ans1, s1) && strcmp(ans1, s2) && strcmp(ans2, s1) &&
strcmp(ans2, s2) && strcmp(ans3, s1) && strcmp(ans3, s2) &&
strcmp(ans4, s1) && strcmp(ans4, s2)) {
printStr(ans1);
printStr(arr + 2);
return;
}
} while (next_permutation(arr, arr + 3));
}
int main() {
scanf("%d", &n);
scanf("%s", s1);
scanf("%s", s2);
valid();
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:128777216")
const int null = 0;
using namespace std;
template <class T>
int size(const T &a) {
return int(a.size());
}
template <class T>
T abs(const T &a) {
return (a < 0 ? -a : a);
}
template <class T>
T sqr(const T &a) {
return a * a;
}
const int max_n = 52, mod = 1000000007;
int dist[max_n][max_n][2];
int dp[max_n][max_n][2];
bool was[max_n][max_n][2];
struct Pos {
int i, j, s;
int d;
void init(int i, int j, int s, int d) {
this->i = i;
this->j = j;
this->s = s;
this->d = d;
was[i][j][s] = true;
dist[i][j][s] = d;
}
} q[max_n * max_n * 2];
int cmn[max_n][max_n];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i <= n; i++) {
cmn[0][i] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cmn[i][j] = cmn[i - 1][j - 1] + cmn[i][j - 1];
}
}
k /= 50;
int c0 = 0, c1 = 0;
for (int i = 0; i < n; i++) {
int t;
scanf("%d", &t);
if (t == 50) {
c0++;
} else {
c1++;
}
}
q[0].init(0, 0, 1, 0);
dp[0][0][1] = 1;
for (int d = 0, u = 1; d < u; d++) {
Pos p = q[d];
int mc0, mc1;
if (p.s) {
mc0 = c0 - p.i;
mc1 = c1 - p.j;
} else {
mc0 = p.i;
mc1 = p.j;
}
long long cnt = dp[p.i][p.j][p.s];
for (int i = 0; i <= mc0; i++) {
for (int j = 0; j <= mc1; j++) {
if (i + j > 0 && i + j * 2 <= k) {
int c0, c1, s0 = p.s ^ 1;
if (p.s) {
c0 = p.i + i;
c1 = p.j + j;
} else {
c0 = p.i - i;
c1 = p.j - j;
}
long long cur = cnt * cmn[i][mc0] % mod * cmn[j][mc1] % mod;
if (!was[c0][c1][s0]) {
q[u++].init(c0, c1, s0, p.d + 1);
dp[c0][c1][s0] = cur;
} else if (p.d + 1 == dist[c0][c1][s0]) {
dp[c0][c1][s0] = (cur + dp[c0][c1][s0]) % mod;
}
}
}
}
}
if (!was[c0][c1][0]) {
printf("-1\n0\n");
return 0;
}
printf("%d\n%d\n", dist[c0][c1][0], dp[c0][c1][0]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, ara[1000], j, mx = 0, temp, temp2;
cin >> n;
for (i = 0; i < n; i++) {
cin >> ara[i];
}
for (i = 0; i < n; i++) {
temp = ara[i];
mx = max(temp, mx);
for (j = i + 1; j < n; j++) {
temp = temp ^ ara[j];
mx = max(temp, mx);
}
}
cout << mx << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = 0;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++)
if (s[i] == '8') cnt++;
cout << min(cnt, (n / 11));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, s;
int t[200010];
vector<int> sol;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &t[i]), s += t[i];
for (int i = 1; i <= n; i++) {
int ss = s - t[i];
if (ss % (n - 1) == 0 && ss / (n - 1) == t[i]) sol.push_back(i);
}
printf("%d\n", sol.size());
for (int i = 0; i < (int)sol.size(); i++) printf("%d ", sol[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char a[31] = "qwertyuiopasdfghjkl;zxcvbnm,./";
int main(void) {
string str;
char ch;
cin >> ch >> str;
int l = str.length(), i, j;
if (ch == 'R') {
for (i = 0; i < l; i++) {
for (j = 0; j < 30; j++)
if (a[j] == str[i]) break;
cout << a[j - 1];
}
} else {
for (i = 0; i < l; i++) {
for (j = 0; j < 30; j++)
if (a[j] == str[i]) break;
cout << a[j + 1];
}
}
cout << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long inf = 1e9;
long long a[maxn];
long long n, A, R, M;
long long check(long long x) {
long long up = 0, down = 0;
for (int i = 1; i <= n; i++) {
if (a[i] < x) up += x - a[i];
if (a[i] > x) down += a[i] - x;
}
long long cnt = min(up, down);
if (M < A + R)
return (up - cnt) * A + (down - cnt) * R + cnt * M;
else
return up * A + down * R;
}
int main() {
cin >> n >> A >> R >> M;
long long sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long l = 0, r = inf, midl, midr;
long long cntl, cntr;
while (r - l > 100) {
midl = (l + r) / 2;
midr = (midl + r) / 2;
cntl = check(midl);
cntr = check(midr);
if (cntl < cntr) {
r = midr;
} else {
l = midl;
}
}
long long ans = inf * inf;
for (long long i = l; i <= r; i++) {
ans = min(ans, check(i));
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000050;
int abs(int x) { return (x > 0) ? x : -x; }
int main() {
int a1, a2, a3, a4;
cin >> a1 >> a2 >> a3 >> a4;
bool ok = true;
ok &= (a3 <= a1 && a3 <= a2);
ok &= (a4 <= a1 && a4 <= a2);
ok &= (abs(a3 - a4) <= 1);
ok &= ((a3 != a4) or ((a4 + 1 <= a1) or (a4 + 1 <= a2)));
if (ok) {
string S;
if (a3 > a4) {
for (int i = 0; i < a3; i++) {
S.push_back('4');
S.push_back('7');
a1--, a2--;
}
} else if (a4 > a3) {
for (int i = 0; i < a4; i++) {
S.push_back('7');
S.push_back('4');
a1--, a2--;
}
} else {
if (a4 + 1 <= a1) {
for (int i = 0; i < a4; i++) {
S.push_back('4');
S.push_back('7');
a1--, a2--;
}
S.push_back('4');
a1--;
} else if (a4 + 1 <= a2) {
for (int i = 0; i < a4; i++) {
S.push_back('7');
S.push_back('4');
a1--, a2--;
}
S.push_back('7');
a2--;
}
}
string s4, s7;
for (int i = 0; i < a1; i++) s4.push_back('4');
for (int i = 0; i < a2; i++) s7.push_back('7');
S.insert(1, s4);
int length = S.size();
S.insert(length - 1, s7);
cout << S << endl;
} else {
cout << -1 << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int sum = 0, a[101];
for (int i = 0; i <= n - 1; i++) {
int x;
scanf("%d", &x);
a[i] = x;
sum += x;
}
int x = sum / (n / 2);
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j <= n - 1; j++) {
if (i != j && a[i] != 0 && a[j] != 0 && a[i] + a[j] == x) {
printf("%d %d\n", i + 1, j + 1);
a[i] = 0;
a[j] = 0;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int aux[15], a[15], a2[15], aux2[15];
int getNueve() {
int nueves = 0;
for (int i = 0; i < 10; ++i) nueves += min(aux[i], aux2[9 - i]);
return nueves;
}
int main() {
char s[100005];
int l;
scanf("%s", &s);
l = strlen(s);
memset(a, 0, sizeof(a));
memset(a2, 0, sizeof(a2));
string n1 = "", n2 = "";
for (int i = 0; s[i]; ++i) {
a[s[i] - '0']++;
a2[s[i] - '0']++;
}
int v1 = 0, v2 = 0, nueves = 0;
int maxi = -1, mini = 1 << 30;
for (int i = 0; i < 10; ++i) {
int t = min(a[i], a2[10 - i]);
int nv = min(a[i], a2[9 - i]);
if (t != 0 && nv == 0) {
v1 = i;
v2 = 10 - i;
break;
} else if (t != 0 && nv != 0) {
for (int j = 0; j < 15; ++j) {
aux[j] = a[j];
aux2[j] = a2[j];
}
aux[i]--;
aux2[10 - i]--;
int n = getNueve();
if (n > maxi) {
maxi = n;
v1 = i;
v2 = 10 - i;
}
}
}
if (v1 != 0) {
n1 += (char)(v1 + '0');
n2 += (char)(v2 + '0');
a[v1]--;
a2[v2]--;
}
for (int i = 0; i < 10; ++i) {
int nv = min(a[i], a2[9 - i]);
for (int j = 0; j < nv; ++j) {
n1 += (char)(i + '0');
n2 += (char)(9 - i + '0');
}
a[i] -= nv;
a2[9 - i] -= nv;
}
int z = min(a[0], a2[0]);
for (int i = 0; i < z; ++i) {
n1 = "0" + n1;
n2 = "0" + n2;
}
a[0] -= z;
a2[0] -= z;
for (int i = 1; i < 10; ++i) {
int t = min(a[i], a2[10 - i]);
for (int j = 0; j < t; ++j) {
n1 += (char)(i + '0');
n2 += (char)(10 - i + '0');
}
a[i] -= t;
a2[10 - i] -= t;
}
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < a[i]; ++j) n1 += (char)(i + '0');
for (int j = 0; j < a2[i]; ++j) n2 += (char)(i + '0');
}
reverse(n1.begin(), n1.end());
reverse(n2.begin(), n2.end());
printf("%s\n%s\n", n1.c_str(), n2.c_str());
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a, b, mini = 0;
while (n--) {
cin >> a >> b;
mini = max(mini, a + b);
}
cout << mini << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int s[200123];
int n, b, a, aux1, aux2, resp;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> b >> a;
for (int i = 0; i < n; i++) cin >> s[i];
aux1 = a;
aux2 = b;
for (int i = 0; i < n; i++) {
if (s[i] == 0) {
if (a == aux1) {
resp++;
aux1--;
} else {
if (aux1 > 0) {
resp++;
aux1--;
} else {
if (aux2 > 0) {
resp++;
aux2--;
} else {
break;
}
}
}
} else {
if (a == aux1) {
resp++;
aux1--;
} else {
if (aux2 > 0) {
aux2--;
resp++;
aux1++;
} else {
if (aux1 > 0) {
resp++;
aux1--;
} else {
break;
}
}
}
}
}
cout << resp << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, 1, -1, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, p;
cin >> n >> p;
int a, b;
vector<int> vec(n, 0);
map<pair<int, int>, int> mp;
vector<pair<int, int> > input;
while (n--) {
cin >> a >> b;
a--, b--;
vec[a]++, vec[b]++;
if (a > b) swap(a, b);
mp[make_pair(a, b)]++;
input.push_back(make_pair(a, b));
}
long long ans = 0;
sort(input.begin(), input.end());
input.erase(unique(input.begin(), input.end()), input.end());
for (int i = 0; i < ((int)input.size()); i++) {
a = input[i].first, b = input[i].second;
if ((vec[a] + vec[b] >= p) && (vec[a] + vec[b] - mp[input[i]] < p)) {
ans -= 2;
}
}
sort(vec.begin(), vec.end());
for (int i = 0; i < ((int)vec.size()); i++) {
long long tmp = vec.end() - lower_bound(vec.begin(), vec.end(), p - vec[i]);
ans += tmp - (vec[i] >= (p - vec[i]));
}
cout << ans / 2 << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int nhang, ncot;
int v1[1111][1111];
int v2[1111][1111];
int v3[1111][1111];
int v4[1111][1111];
int s[1111][1111];
int kq;
void input() {
int n;
int i, j;
for (i = 1; i <= nhang; i++) {
for (j = 1; j <= ncot; j++) {
scanf("%d", &s[i][j]);
}
}
}
void solve() {
int i, j;
memset(v1, 0, sizeof(v1));
memset(v2, 0, sizeof(v2));
memset(v3, 0, sizeof(v3));
memset(v4, 0, sizeof(v4));
for (i = 1; i <= nhang; i++)
for (j = 1; j <= ncot; j++)
v1[i][j] = max(v1[i - 1][j], v1[i][j - 1]) + s[i][j];
for (i = nhang; i > 0; i--)
for (j = 1; j <= ncot; j++)
v2[i][j] = max(v2[i + 1][j], v2[i][j - 1]) + s[i][j];
for (i = 1; i <= nhang; i++)
for (j = ncot; j > 0; j--)
v3[i][j] = max(v3[i - 1][j], v3[i][j + 1]) + s[i][j];
for (i = nhang; i > 0; i--)
for (j = ncot; j > 0; j--)
v4[i][j] = max(v4[i + 1][j], v4[i][j + 1]) + s[i][j];
kq = -1;
for (i = 2; i < nhang; i++) {
for (j = 2; j < ncot; j++) {
kq = max(kq, v1[i][j - 1] + v2[i + 1][j] + v3[i - 1][j] + v4[i][j + 1]);
kq = max(kq, v1[i - 1][j] + v2[i][j - 1] + v3[i][j + 1] + v4[i + 1][j]);
}
}
printf("%d\n", kq);
}
int main() {
while (scanf("%d %d", &nhang, &ncot) > 0) {
input();
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int tree[1000009], n = 1000004, ans[1000009];
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += (idx & -idx);
}
}
int sum(int idx) {
int val = 0;
while (idx > 0) {
val += tree[idx];
idx -= (idx & -idx);
}
return val;
}
int query(int val) {
int low = 0;
int high = n + 1;
int mid = (low + high) / 2;
while (low < high) {
int mid = (low + high) / 2;
if (sum(mid) >= val)
high = mid;
else
low = mid + 1;
}
return low;
}
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
vector<int> rem;
for (int i = int(0); i <= int(m - 1); i++) {
int p;
scanf("%d", &p);
rem.push_back(p);
}
int glo = 1;
for (int i = int(0); i <= int(n - 1); i++) {
int p;
scanf("%d", &p);
if (p == -1) {
int idx = upper_bound(rem.begin(), rem.end(), sum(1000000)) - rem.begin();
vector<int> mark;
for (int i = int(0); i <= int(idx - 1); i++)
mark.push_back(query(rem[i]));
for (vector<int>::iterator it = (mark).begin(); it != (mark).end(); it++)
update(*it, -1);
} else {
ans[glo] = p;
update(glo, 1);
glo++;
}
}
vector<int> fin;
for (int i = int(1); i <= int(glo - 1); i++) {
if (sum(i) - sum(i - 1) > 0) fin.push_back(ans[i]);
}
if (fin.empty())
printf("Poor stack!\n");
else {
for (vector<int>::iterator it = (fin).begin(); it != (fin).end(); it++) {
printf("%d", *it);
}
printf("\n");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
mt19937_64 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<vector<int>> g;
vector<int> sz;
vector<int64_t> dp;
int64_t ans = 0;
void countSize(int v = 0, int p = -1) {
sz[v] = 1;
for (int to : g[v]) {
if (to == p) continue;
countSize(to, v);
sz[v] += sz[to];
}
}
void countDp(int v = 0, int p = -1) {
dp[v] = sz[v];
for (int to : g[v]) {
if (to == p) continue;
countDp(to, v);
dp[v] += dp[to];
}
}
void dfs(int v = 0, int p = -1) {
ans = max(ans, dp[v]);
for (int to : g[v]) {
if (to == p) continue;
dp[v] -= sz[to];
dp[v] -= dp[to];
sz[v] -= sz[to];
sz[to] += sz[v];
dp[to] += dp[v];
dp[to] += sz[v];
dfs(to, v);
dp[to] -= sz[v];
dp[to] -= dp[v];
sz[to] -= sz[v];
sz[v] += sz[to];
dp[v] += dp[to];
dp[v] += sz[to];
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
g = vector<vector<int>>(n);
sz = vector<int>(n);
dp = vector<int64_t>(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
countSize();
countDp();
dfs();
cout << ans;
cout << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long a[100005];
long long sum;
bool check(long long mid) {
long long ret = 0;
for (long long i = 1; i <= n; i++) {
ret += min(mid, a[i]);
}
return ret <= k;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < k) {
cout << "-1\n";
return 0;
}
if (sum == k) {
return 0;
}
long long hi = 1e14;
long long lo = 0;
while (hi > lo) {
long long mid = lo + (hi - lo + 1) / 2;
if (check(mid))
lo = mid;
else
hi = mid - 1;
}
long long ret = 0;
for (long long i = 1; i <= n; i++) {
ret += min(hi, a[i]);
}
for (long long i = 1; i <= n; i++) a[i] = max(0LL, a[i] - hi);
long long pt = 1;
k -= ret;
for (long long i = 1; i <= n && k; i++) {
if (a[i]) {
a[i]--;
k--;
pt = i + 1;
if (pt == n + 1) pt = 1;
}
}
long long st = pt;
while (1) {
if (a[pt] != 0) cout << pt << " ";
pt++;
if (st == 0 && pt == n + 1) break;
if (pt == n + 1) pt = 1;
if (pt == st) break;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 1e6 + 3;
vector<pair<int, int> > G[maxn];
int pre[maxn], fa[maxn][19], dep[maxn], mx[maxn][19], ans[maxn];
struct no {
int id, u, v, w;
} a[maxn];
bool cmp(no a, no b) { return a.w < b.w; }
int ffind(int x) {
if (pre[x] == x) return x;
pre[x] = ffind(pre[x]);
return pre[x];
}
void dfs(int u, int p) {
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].first;
if (p == v) continue;
dep[v] = dep[u] + 1;
fa[v][0] = u;
mx[v][0] = G[u][i].second;
dfs(v, u);
}
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
for (int i = 0; i < 18; i++)
if ((dep[v] - dep[u]) & (1 << i)) v = fa[v][i];
if (u == v) return u;
for (int i = 17; i >= 0; i--)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
int ask(int u, int st) {
int ret = 0;
for (int i = 0; i < 18; i++)
if (st & (1 << i)) ret = max(ret, mx[u][i]), u = fa[u][i];
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
a[i].id = i;
scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
}
for (int i = 1; i <= n; i++) pre[i] = i;
sort(a, a + m, cmp);
for (int i = 0; i < m; i++) {
int u = ffind(a[i].u), v = ffind(a[i].v);
if (u != v) {
pre[u] = v;
ans[a[i].id] = -1;
G[a[i].u].push_back({a[i].v, a[i].w});
G[a[i].v].push_back({a[i].u, a[i].w});
}
}
dep[1] = 1;
dfs(1, 0);
for (int i = 1; i < 18; i++)
for (int j = 1; j <= n; j++) {
fa[j][i] = fa[fa[j][i - 1]][i - 1];
mx[j][i] = max(mx[j][i - 1], mx[fa[j][i - 1]][i - 1]);
}
for (int i = 0; i < m; i++)
if (ans[a[i].id] != -1) {
int u = a[i].u, v = a[i].v, w = lca(u, v);
ans[a[i].id] = max(ask(u, dep[u] - dep[w]), ask(v, dep[v] - dep[w]));
}
for (int i = 0; i < m; i++) {
if (ans[i] != -1) printf("%d ", ans[i]);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[55], t[55], i = 0, j = 0;
scanf("%s", s);
scanf("%s", t);
while (t[i] != '\0') {
if (t[i] == s[j]) j++;
i++;
if (j >= strlen(s)) break;
}
cout << j + 1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int n, m;
int pro[MAXN], in[MAXN];
vector<int> adj[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> pro[i];
queue<int> q;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[v].push_back(u);
in[u]++;
}
int ans = 0;
deque<int> ps1;
deque<int> ps2;
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
if (pro[i] == 0)
ps2.push_back(i);
else
ps1.push_back(i);
}
}
while (true) {
if (ps1.size() == 0 && ps2.size() == 0) break;
while (ps2.size()) {
int x = ps2.front();
ps2.pop_front();
for (auto y : adj[x]) {
in[y]--;
if (in[y] == 0) {
if (in[y] == 0) {
if (pro[y] == 1)
ps1.push_back(y);
else
ps2.push_back(y);
}
}
}
}
if (ps1.size()) ans++;
while (ps1.size()) {
int x = ps1.front();
ps1.pop_front();
for (auto y : adj[x]) {
in[y]--;
if (in[y] == 0) {
if (pro[y] == 1)
ps1.push_back(y);
else
ps2.push_back(y);
}
}
}
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int minimum(int x, int y) {
if (x > y)
return y;
else
return x;
}
int f(int n, int a[]) {
int i, j, min, m = 0;
min = a[0];
for (i = 0; i < n; i++) {
if (a[i] == 0) {
if (i != n - 1) {
int a1[n - i - 1];
for (j = 0; j < n - i - 1; j++) {
a1[j] = a[i + j + 1];
}
m = m + f(j, a1);
}
break;
} else {
if (min > a[i]) min = a[i];
}
}
if (min != 0) {
if (min > i)
return m + i;
else {
int a2[i];
for (j = 0; j < i; j++) {
a2[j] = a[j] - min;
}
m = m + minimum(i, min + f(i, a2));
}
}
return m;
}
int main() {
int n;
cin >> n;
int h[n];
for (int i = 0; i < n; i++) {
cin >> h[i];
}
int cnt = 0;
cnt = f(n, h);
cout << cnt << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l;
cin >> n >> l;
int a[n], b[n];
set<int> s, s2;
for (int i = 0; i < n; i++) {
cin >> a[i];
s.insert(a[i]);
}
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < l; i++) {
s2.clear();
for (int j = 0; j < n; j++) {
b[j]++;
b[j] %= l;
s2.insert(b[j]);
}
bool boo = true;
for (set<int>::iterator it = s.begin(), it2 = s2.begin(); it != s.end();
++it, ++it2) {
if (*it != *it2) {
boo = false;
break;
}
}
if (boo) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 2 |
#include <bits/stdc++.h>
int a, b, c;
int m[105][105];
int main() {
scanf("%d%d%d", &a, &b, &c);
for (int i = 0; i < a; i++)
for (int j = 0; j < b; j++) scanf("%d", &m[i][j]);
bool locked[105];
int used[105];
int out[105];
memset(locked, false, sizeof locked);
memset(out, 0, sizeof out);
for (int i = 0; i < b; i++) {
memset(used, 0, sizeof used);
for (int j = 0; j < a; j++)
if (!out[j] && m[j][i] != 0) used[m[j][i]]++;
for (int j = 0; j < a; j++)
if (!out[j] && m[j][i] != 0) {
if (used[m[j][i]] >= 2 || locked[m[j][i]]) {
locked[m[j][i]] = true;
out[j] = i + 1;
}
}
}
for (int i = 0; i < a; i++) printf("%d\n", out[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string weights;
int m;
stack<int> soln;
bool dfs(int placed, int last, int balance) {
if (placed == m) {
cout << "YES" << endl;
return true;
}
for (int i = 0; i < weights.size(); i++) {
if (weights[i] == '1') {
if (i + 1 != last) {
if ((i + 1) > abs(balance)) {
int next_balance = (i + 1) - abs(balance);
next_balance *= (balance >= 0 ? -1 : 1);
if (dfs(placed + 1, i + 1, next_balance)) {
soln.push(i + 1);
return true;
}
}
}
}
}
return false;
}
int main() {
cin >> weights;
cin >> m;
if (!dfs(0, -1, 0)) {
cout << "NO" << endl;
} else {
while (!soln.empty()) {
cout << soln.top() << " ";
soln.pop();
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 500;
int X = 1, Y = 1;
int N;
int ask(int a, int b, int A, int B) {
cout << "? " << a << ' ' << b << ' ' << A << ' ' << B << '\n';
cout.flush();
string ok;
cin >> ok;
return ok == "YES";
}
vector<pair<int, int>> poses;
int main() {
cin >> N;
string ans;
poses.push_back({1, 1});
for (int i = 1; i < N; ++i) {
int canRight = (Y < N);
int right = 0;
if (canRight) {
right = ask(X, Y + 1, N, N);
}
int canDown = (X < N);
int down = 0;
if (canDown) {
down = ask(X + 1, Y, N, N);
}
if (right) {
ans.push_back('R');
Y++;
} else {
ans.push_back('D');
X++;
}
poses.push_back({X, Y});
}
X = N, Y = N;
string ans2;
poses.pop_back();
for (int i = 1; i < N; ++i) {
int canRight = (Y > 0);
int right = 0;
if (canRight) {
right = ask(poses.back().first, poses.back().second, X, Y - 1);
}
int canDown = (X > 0);
int down = 0;
if (canDown) {
down = ask(poses.back().first, poses.back().second, X - 1, Y);
}
if (down) {
ans2.push_back('D');
X--;
} else {
ans2.push_back('R');
Y--;
}
poses.pop_back();
}
reverse(ans2.begin(), ans2.end());
cout << "! " << ans << ans2 << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int arr[10];
int arr1[10];
int arr2[10];
while (cin >> n) {
for (int i = 0; i <= 10; i++) {
arr[i] = 0;
arr1[i] = 0;
arr2[i] = 0;
}
int x;
for (int i = 1; i <= n; i++) {
if (i == 1) {
for (int j = 1; j <= 6; j++) {
cin >> x;
arr[x] = 1;
}
}
if (i == 2) {
for (int j = 1; j <= 6; j++) {
cin >> x;
arr1[x] = 1;
}
}
if (i == 3) {
for (int j = 1; j <= 6; j++) {
cin >> x;
arr2[x] = 1;
}
}
}
int ck = 0;
for (int i = 1; i <= 9; i++) {
if (arr[i] + arr1[i] + arr2[i] == 0) {
cout << i - 1 << endl;
ck = 1;
break;
}
}
int ck1 = 0;
if (ck == 0) {
for (int i = 10; i <= 99; i++) {
ck1 = 0;
int a = i;
int x = a % 10;
a /= 10;
int y = a % 10;
if ((arr[x] == 1 && (arr1[y] == 1 || arr2[y] == 1)) ||
(arr[y] == 1 && (arr1[x] == 1 || arr2[x] == 1)))
ck1 = 1;
if ((arr1[x] == 1 && (arr[y] == 1 || arr2[y] == 1)) ||
(arr1[y] == 1 && (arr[x] == 1 || arr2[x] == 1)))
ck1 = 1;
if ((arr2[x] == 1 && (arr[y] == 1 || arr1[y] == 1)) ||
(arr2[y] == 1 && (arr1[x] == 1 || arr[x] == 1)))
ck1 = 1;
if (ck1 == 0) {
cout << i - 1 << endl;
break;
}
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void next(string &s) {
int i = s.size() - 1;
while (s[i] == '9' && i >= 0) {
s[i] = '0';
i--;
}
if (i == -1) {
s = '1' + s;
} else {
s[i]++;
}
}
int dist(string t, string s) {
int res = 0;
for (int i = 0; i < 6; ++i) {
res += (t[i] != s[i]);
}
return res;
}
int main() {
string t;
cin >> t;
int ans = 1e9;
string s = "000000";
while (s != "1000000") {
if (s[0] + s[1] + s[2] == s[3] + s[4] + s[5]) {
ans = min(ans, dist(t, s));
}
next(s);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
long long x, y;
Point(long long = 0, long long = 0);
long long cross(const Point &) const;
Point operator-(const Point &) const;
bool operator<(const Point &) const;
friend istream &operator>>(istream &, Point &);
};
Point::Point(long long x, long long y) : x(x), y(y) {}
bool Point::operator<(const Point &p) const {
return y < p.y || (y == p.y && x < p.x);
}
long long Point::cross(const Point &p) const { return x * p.y - y * p.x; }
Point Point::operator-(const Point &p) const { return Point(x - p.x, y - p.y); }
istream &operator>>(istream &is, Point &p) {
is >> p.x >> p.y;
return is;
}
struct Polygon {
int cs;
vector<Point> points;
Polygon(long long = 0);
void ShiftNorm();
bool inside(const Point &) const;
friend istream &operator>>(istream &, Polygon &);
};
Polygon::Polygon(long long cs) : cs(cs) { points.resize(cs); }
void Polygon::ShiftNorm() {
auto min_it = min_element(points.begin(), points.end());
vector<Point> tmp(cs);
copy(min_it, points.end(), tmp.begin());
copy_backward(points.begin(), min_it, tmp.end());
copy(tmp.begin(), tmp.end(), points.begin());
}
bool Polygon::inside(const Point &p) const {
Point p0 = *points.begin();
int l = 0, r = cs;
while (l != r - 1) {
int m = (l + r) / 2;
long long pr = (p - p0).cross(points[m] - p0);
if (pr == 0 && l == 0) break;
if (pr > 0)
r = m;
else
l = m;
}
Point v1 = points[l + 1] - points[l], v2 = p - points[l];
return (v1.cross(v2) > 0);
}
istream &operator>>(istream &is, Polygon &p) {
for (int i = 0; i < p.cs; ++i) is >> p.points[i];
reverse(p.points.begin(), p.points.end());
return is;
}
int main() {
int n, m;
cin >> n;
Polygon A(n);
cin >> A;
A.ShiftNorm();
cin >> m;
Polygon B(m);
cin >> B;
for (auto pt : B.points) {
if (!A.inside(pt)) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int s1[10][10];
int temp[20];
map<int, int> ma;
int main() {
int n;
while (scanf("%d", &n) != EOF) {
ma.clear();
for (int a = 1; a <= n; a++)
for (int b = 1; b <= 6; b++) scanf("%d", &s1[a][b]);
int sum = 0;
if (n >= 1) {
for (int i = 1; i <= n; i++)
for (int a = 1; a <= 6; a++)
if (!ma[s1[i][a]]) {
ma[s1[i][a]] = 1;
sum++;
}
}
if (n >= 2) {
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
for (int a = 1; a <= 6; a++) {
for (int b = 1; b <= 6; b++) {
int temp1 = s1[i][a] * 10 + s1[j][b];
int temp2 = s1[i][a] + s1[j][b] * 10;
if (!ma[temp1] && s1[i][a] != 0) {
ma[temp1] = 1;
sum++;
}
if (!ma[temp2] && s1[j][b] != 0) {
ma[temp2] = 1;
sum++;
}
}
}
}
if (n >= 3) {
for (int a = 1; a <= 6; a++) {
for (int b = 1; b <= 6; b++) {
for (int c = 1; c <= 6; c++) {
int all = 1;
if (s1[1][a] != 0) {
temp[all++] = s1[1][a] * 100 + s1[2][b] * 10 + s1[3][c];
temp[all++] = s1[1][a] * 100 + s1[2][b] + s1[3][c] * 10;
}
if (s1[2][b] != 0) {
temp[all++] = s1[1][a] * 10 + s1[2][b] * 100 + s1[3][c];
temp[all++] = s1[1][a] + s1[2][b] * 100 + s1[3][c] * 10;
}
if (s1[3][c] != 0) {
temp[all++] = s1[1][a] + s1[2][b] * 10 + s1[3][c] * 100;
temp[all++] = s1[1][a] * 10 + s1[2][b] + s1[3][c] + 100;
}
for (int a = 1; a < all; a++) {
if (!ma[temp[a]]) {
ma[temp[a]] = 1;
sum++;
}
}
}
}
}
}
int tt = 0;
for (int a = 1; a <= 1000; a++) {
if (ma[a])
continue;
else {
tt = a;
break;
}
}
printf("%d\n", tt - 1);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1000, oo = 1e9;
const long long M = 1e9 + 7, x = 137;
int mx = 0, mn = 1e9;
long long count(int v, const vector<int> &cur, const vector<long long> &pref) {
int l = -1, r = ((int)(cur).size());
while (r - l > 1) {
int m = (l + r) >> 1;
if (cur[m] > v)
r = m;
else
l = m;
}
int left = r;
l = -1, r = ((int)(cur).size());
while (r - l > 1) {
int m = (l + r) >> 1;
if (cur[m] >= mx)
r = m;
else
l = m;
}
long long ans = (long long)(((int)(cur).size()) - r) * (mx - v);
int right = l;
if (left <= right)
ans += -(long long)(right - left + 1) * v +
(long long)(left == 0 ? pref[right] : pref[right] - pref[left - 1]);
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> h(n);
vector<long long> pref(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
mx = max(mx, h[i]);
mn = min(mn, h[i]);
}
sort(h.begin(), h.end());
for (int i = 0; i < n; ++i) {
pref[i] = h[i];
if (i) pref[i] += pref[i - 1];
}
int ans = 0;
while (mx != mn) {
int l = mn - 1, r = mx;
while (r - l > 1) {
int m = (l + r) >> 1;
if (count(m, h, pref) <= k)
r = m;
else
l = m;
}
assert(r != mx);
ans++;
mx = r;
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const long double PI = acos(-1);
const long long M = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long mod(long long n, long long m = M) {
long long ret = n % m;
if (ret < 0) ret += m;
return ret;
}
long long exp(long long n, long long k, long long m = M) {
if (k == 0) return 1;
if (k == 1) return n;
long long ax = exp(n, k / 2, m);
ax = mod(ax * ax, m);
if (k % 2) ax = mod(ax * n, m);
return ax;
}
long long gcd(long long a, long long b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
void solution(long long testcase) {
long long n;
cin >> n;
long long v[n];
for (long long i = 0; i < n; i += 1) cin >> v[i];
vector<long long> dp(n, 1e18);
vector<long long> g[n];
for (long long u = 0; u < n; u++) {
long long prv = u - v[u];
long long nxt = u + v[u];
if (prv >= 0) g[prv].push_back(u);
if (nxt < n) g[nxt].push_back(u);
}
for (long long p = 0; p < 2; p++) {
queue<long long> q;
vector<long long> d(n, 1e18);
for (long long i = 0; i < n; i++)
if (p == v[i] % 2) d[i] = 0, q.push(i);
while (!q.empty()) {
long long u = q.front();
q.pop();
for (long long v : g[u]) {
if (d[v] > d[u] + 1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
for (long long i = 0; i < n; i++)
if (v[i] % 2 != p) dp[i] = d[i];
}
for (long long i = 0; i < n; i++) cout << (dp[i] == 1e18 ? -1 : dp[i]) << " ";
cout << '\n';
}
int32_t main() {
long long t = 1;
for (long long _testcase = 1; _testcase <= t; _testcase++)
solution(_testcase);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
double PI = 3.14159265358979323846;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long get(long long x, long long n) {
long long l, r;
if (x > n) return 0;
long long acc = 0;
if (x % 2 == 0) {
l = x;
r = x + 1;
} else {
l = 2 * x;
r = 2 * x + 1;
acc += 1;
}
while (r <= n) {
acc += r - l + 1;
l *= 2;
r = 2 * r + 1;
}
if (l <= n) acc += (n - l + 1);
return acc;
}
int dfs(int x, vector<bool> &chk, vector<int> &val, int step,
vector<map<int, int> > &e) {
chk[x] = true;
int maxval = -1e9 - 1;
for (auto &k : e[x]) {
if (chk[k.first]) continue;
maxval = max(maxval, dfs(k.first, chk, val, step + 1, e));
}
if (step == 0)
maxval = max(maxval, val[x]);
else if (step == 1)
maxval = max(maxval, val[x] + 1);
else
maxval = max(maxval, val[x] + 2);
return maxval;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n + 1);
int mmm = -1e9 - 1;
for (int i = 0; i < n; i++) {
cin >> a[i + 1];
mmm = max(mmm, a[i + 1]);
}
set<int> s;
for (int i = 1; i <= n; i++) {
if (a[i] == mmm) s.insert(i);
}
vector<map<int, int> > v(n + 1);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
v[x][y] = 1;
v[y][x] = 1;
}
vector<bool> chk(n + 1);
if (s.size() == 1) {
int ans = dfs(*(s.begin()), chk, a, 0, v);
cout << ans << endl;
} else {
bool isok = false;
for (int i = 1; i <= n; i++) {
if (v[i].size() + 1 < s.size()) continue;
bool isok2 = true;
for (auto k : s) {
if (v[i][k] != 1 && k != i) {
isok2 = false;
break;
}
}
if (isok2) {
isok = true;
break;
}
}
if (isok) {
cout << mmm + 1 << endl;
} else {
cout << mmm + 2 << endl;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 10;
long double p[mn], dp[mn];
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int n, i;
cin >> n;
long double c = 0, l = 0, sq = 0, s = 0;
for (i = 1; i <= n; i++) cin >> p[i];
n++;
c = 1;
for (i = 1; i <= n; i++) {
dp[i] = (1 - p[i]) * (sq + s);
s *= p[i];
c *= p[i];
l *= p[i];
sq *= p[i];
sq += 2 * l + c;
l += c;
c += 1 - p[i];
s += dp[i];
}
printf("%.20Lf", dp[n]);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> a;
long long int c, d;
vector<pair<long long int, long long int> > tree;
void update(long long int idx, long long int pos, long long int s,
long long int e) {
if (pos < s || pos > e) return;
if (s == e) {
tree[idx] = {min(a[s], c), min(a[s], d)};
return;
}
long long int mid = s + ((e - s) >> 1);
update(2 * idx, pos, s, mid);
update(2 * idx + 1, pos, mid + 1, e);
tree[idx] = {tree[2 * idx].first + tree[2 * idx + 1].first,
tree[2 * idx].second + tree[2 * idx + 1].second};
return;
}
long long query(long long int idx, long long int l, long long int r,
long long int type, long long int s, long long int e) {
if (r < s || l > e) return 0;
if (s >= l && e <= r) {
if (type == 1)
return tree[idx].first;
else
return tree[idx].second;
}
long long int mid = s + ((e - s) >> 1);
return (query(2 * idx, l, r, type, s, mid) +
query(2 * idx + 1, l, r, type, mid + 1, e));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, k, i, l, r, q, t, ans;
cin >> n >> k >> c >> d >> q;
a.resize(n);
tree.resize(4 * n + 2);
while (q--) {
cin >> t;
if (t == 1) {
cin >> i >> l;
i--;
a[i] += l;
update(1, i, 0, n - 1);
} else {
cin >> i;
i--;
ans = query(1, 0, i - 1, 2, 0, n - 1);
i += k;
ans += query(1, i, n - 1, 1, 0, n - 1);
cout << ans << "\n";
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int M = 1e2 + 2;
const int P = 1e9 + 7;
LL dp[M][2][M][M];
int c[M][M];
int n, m, k;
void make_cmod() {
int i, j;
for (i = 0; i < M; i++) c[i][0] = 1;
for (i = 1; i < M; i++)
for (j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
if (c[i][j] > 100) c[i][j] = 200;
}
}
void clear(int id) {
int i, p, q;
for (i = 0; i <= n; i++)
for (p = 0; p <= n; p++)
for (q = 1; q <= k; q++) dp[i][id][p][q] = 0;
}
int main() {
make_cmod();
int i, j, p, q, t, mn, ks, flag = 0;
bool first = true;
LL res = 0, tmp = 0;
cin >> n >> m >> k;
n++;
mn = min(m, n);
for (i = 1; i <= n; i++) dp[i][1][i - 1][1] = 1;
for (j = 2; j <= mn; j++, flag ^= 1) {
clear(flag);
for (i = 0; i <= n; i++)
for (p = 1; p < n; p++)
for (q = 1; q <= k; q++) {
if (dp[i][flag ^ 1][p][q]) {
for (t = p; t <= n - i; t++) {
ks = q * c[t - 1][p - 1];
if (ks > k) continue;
dp[i + t][flag][t - p][ks] =
(dp[i + t][flag][t - p][ks] + dp[i][flag ^ 1][p][q]) % P;
}
}
}
tmp = 0;
for (i = 2; i <= n; i++)
for (q = 1; q <= k; q++) tmp = (tmp + dp[i][flag][0][q]) % P;
res = (res + tmp * (m - j + 1) % P) % P;
}
cout << res;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long int m, ct[27];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m;
long long int tot = 0;
long long int cto = 0;
for (long long int i = 1; i <= m; ++i) {
cin >> ct[i];
tot += ct[i];
if (ct[i] % 2) cto++;
}
if (m == 1) {
cout << ct[1] << '\n';
while (ct[1]--) cout << 'a';
cout << '\n';
return 0;
}
long long int n = tot / 2;
if (cto > 1) {
cout << 0 << '\n';
for (long long int i = 1; i <= m; ++i)
while (ct[i]--) cout << (char)('a' + i - 1);
cout << '\n';
return 0;
}
if (cto == 1) {
long long int st = 1;
while (st <= tot) {
if (tot % st) {
st++;
continue;
}
long long int sz = tot / st;
bool notpos = 0;
for (long long int i = 1; i <= m; ++i) {
if (ct[i] % 2) {
if (ct[i] < sz) notpos = 1;
long long int rem = ct[i] - sz;
if (rem % sz) notpos = 1;
} else {
if (ct[i] % sz) notpos = 1;
}
}
if (notpos) {
st++;
continue;
}
cout << tot / st << '\n';
string ans;
char chc;
for (long long int i = 1; i <= m; ++i) {
if (ct[i] % 2) {
long long int rem = ct[i] - sz;
rem /= sz;
rem /= 2;
while (rem--) ans += ('a' + i - 1);
chc = (char)('a' + i - 1);
continue;
}
long long int times = ct[i] / sz;
times /= 2;
while (times--) ans += ('a' + i - 1);
}
string rev = ans;
reverse(rev.begin(), rev.end());
ans += chc;
ans += rev;
long long int times = sz;
string res;
while (times--) res += ans;
cout << res << '\n';
return 0;
}
return 0;
}
long long int st = m;
long long int mi = 1e9;
for (long long int i = 1; i <= m; ++i) mi = min(mi, ct[i]);
while (st <= n) {
if (n % st) {
st++;
continue;
}
if ((2 * n) / st > mi) {
st++;
continue;
}
long long int sz = (2 * n) / st;
bool pos = 1;
for (long long int i = 1; i <= m; ++i)
if (ct[i] % sz) pos = false;
if (!pos) {
st++;
continue;
}
cout << (2 * n) / st << '\n';
string ans;
for (long long int i = 1; i <= m; ++i) {
long long int times = ct[i] / sz;
while (times--) ans += ('a' + i - 1);
}
string rev = ans;
reverse(rev.begin(), rev.end());
ans += rev;
long long int times = (2 * n) / (2 * st);
string res;
while (times--) res += ans;
cout << res << '\n';
return 0;
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
set<string> dee;
map<string, int> mmp;
void dfs(string s) {
int i = 0;
while (s[i] != '?' && i < s.size()) i++;
if (i == s.size()) {
dee.insert(s);
return;
}
for (char ch = 'a'; ch <= 'e'; ch++) {
s[i] = ch;
dfs(s);
}
dfs(s.substr(0, i) + s.substr(i + 1));
}
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
mmp.clear();
int n, m, h;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s;
mmp[s]++;
}
for (int i = 0; i < m; i++) {
h = 0;
dee.clear();
cin >> s;
dfs(s);
for (string ss : dee) {
if (mmp.count(ss) > 0) h += mmp[ss];
}
printf("%d\n", h);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX - 1e10;
const long long maxn = 1e6 + 5;
const long long mo_num = 1e9 + 7;
long long n, m, a[maxn], b[maxn], ans;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (long long i = 1; i <= 4; i++) cin >> a[i];
long long key = 0;
if (a[1] == a[4]) {
if (!a[3])
key = 1;
else if (a[1] > 0)
key = 1;
}
cout << key ? 1 : 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int count = 0;
while ((count * (count + 1)) / 2 <= n) {
++count;
}
--count;
int val = (count * (count + 1)) / 2;
int diff = n - val;
cout << count << "\n";
for (int i = 1; i <= count; ++i) {
if (i == count) {
cout << count + diff << " ";
break;
}
cout << i << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1.01e9;
const long long infll = (long long)1.01e18;
const long double eps = 1e-9;
const long double pi = acos((long double)-1);
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x) { return mrand() % x; }
void precalc() {}
const int maxn = 8005;
char tmp[maxn];
int n;
bitset<maxn> g[maxn];
bool read() {
if (scanf("%d", &n) < 1) {
return false;
}
for (int i = 0; i < n; i++) {
scanf("%s", tmp);
for (int j = 0; j < n / 4; j++) {
int x = (isdigit(tmp[j]) ? tmp[j] - '0' : 10 + tmp[j] - 'A');
for (int k = 0; k < 4; k++) {
g[i][j * 4 + k] = ((x >> (3 - k)) & 1);
}
}
}
return true;
}
const int C = 614;
int deg[maxn];
vector<int> good, bad;
vector<int> gus, bus, us;
void mySort(vector<int>::iterator l, vector<int>::iterator r) {
int k = r - l;
if (k <= 1) {
return;
}
int p = rnd(k);
int v = *(l + p);
const auto &gv = g[v];
auto m0 = l, m1 = l;
for (auto it = l; it != r; it++) {
if (*it != v && !gv[*it]) {
swap(*it, *m0);
if (m1 == m0) {
m1++;
}
m0++;
}
if (*it == v) {
swap(*it, *m1);
m1++;
}
}
mySort(l, m0);
mySort(m1, r);
}
bool cmp(int v, int u) { return g[v][u]; }
void solve() {
{
int t = 0;
for (int v = 0; v < n; v++) {
deg[v] = g[v].count();
if (deg[v] < deg[t]) {
t = v;
}
}
good.clear();
bad.clear();
for (int v = 0; v < n; v++) {
if (!g[t][v]) {
good.push_back(v);
} else {
bad.push_back(v);
}
}
mySort(good.begin(), good.end());
}
long long res = 0;
for (int v = 0; v < n; v++) {
gus.clear();
for (auto u : good) {
if (u == v) {
continue;
}
if (g[v][u]) {
res++;
continue;
}
gus.push_back(u);
}
bus.clear();
for (auto u : bad) {
if (u == v) {
continue;
}
if (g[v][u]) {
res++;
continue;
}
bus.push_back(u);
}
mySort(bus.begin(), bus.end());
us.resize(((int)(gus).size()) + ((int)(bus).size()));
merge(gus.begin(), gus.end(), bus.begin(), bus.end(), us.begin(), cmp);
bool b = false;
for (int i = 0; i < ((int)(us).size()); i++) {
int u = us[i];
int need = n - i - 1;
assert(deg[u] <= need);
if (deg[u] == need) {
if (b) {
res += 3;
} else {
res += C * n;
}
} else {
res += 2;
b = true;
}
}
}
printf("%lld\n", res);
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
vector<int> v;
int zero = 0, sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v.push_back(x);
if (x == 0) {
zero++;
}
sum += x;
}
sum += zero;
int ans = 0;
ans += zero;
if (sum == 0) {
ans++;
}
cout << ans << endl;
v.clear();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
class Coor {
public:
int x, y;
Coor() {}
Coor(int xi, int yi) : x(xi), y(yi) {}
void input() { scanf("%d %d", &x, &y); }
};
int n, m;
Coor pt[260];
int see[1000050];
inline long long cross(Coor a, Coor b) {
return (long long)a.x * b.y - (long long)a.y * b.x;
}
inline long long cross(Coor o, Coor a, Coor b) {
return cross(Coor(a.x - o.x, a.y - o.y), Coor(b.x - o.x, b.y - o.y));
}
inline int solve() {
for (int i = 1; i <= n; i++) see[i] = 1;
for (int i = 0; i < m; i++) {
for (int j = i + 1; j < m; j++) {
Coor p = pt[i], q = pt[j];
if (p.y == q.y) continue;
if (p.y > q.y) swap(p, q);
int cnt = 0;
for (int k = 0; k < m; k++)
if (cross(p, q, pt[k]) == 0) cnt++;
long long c = (p.x - q.x) * (long long)p.y;
long long d = (q.y - p.y);
if (c % d) continue;
long long x = p.x + c / d;
if (x < 1 || x > n) continue;
see[x] = max(see[x], cnt);
}
}
int sol = 0;
for (int i = 1; i <= n; i++) sol += see[i];
return sol;
}
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) pt[i].input();
printf("%d\n", solve());
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> data;
for (int _n = n, i = 0; i < _n; ++i) {
int t;
cin >> t;
data.push_back(t);
}
vector<int> points;
points.push_back(-1);
points.push_back(0);
for (int _n = data.size(), i = 0; i < _n; ++i) {
points.push_back(data[i] - 1);
points.push_back(data[i]);
points.push_back(data[i] + 1);
}
for (int _n = points.size(), i = 0; i < _n; ++i) {
int x = points[i];
int cnt = 0;
for (int _n = data.size(), j = 0; j < _n; ++j) {
if (x >= 0 && x <= data[j]) cnt++;
}
if (cnt == k) {
cout << x << " " << x;
return 0;
}
}
cout << -1;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int MAXN = 550;
const int eps = 1e-10;
double a[MAXN];
int main() {
int n, m, i, x, y;
double z, j, dmax;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) scanf("%lf", &a[i]);
dmax = 0.0;
for (i = 1; i <= m; i++) {
scanf("%d%d%lf", &x, &y, &z);
if (z > eps) j = (a[x] + a[y]) / z;
if (j > dmax) dmax = j;
}
printf("%.12lf\n", dmax);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<vector<int>> A(N, vector<int>(M));
for (int i = 0; i < (N); i++)
for (int j = 0; j < (M); j++) cin >> A[i][j];
vector<vector<int>> B(N, vector<int>(M));
vector<pair<int, int>> ans;
auto hoge = [&](int i, int j) {
B[i][j] = B[i + 1][j] = B[i][j + 1] = B[i + 1][j + 1] = 1;
ans.emplace_back(i, j);
};
for (int i = 0; i < (N - 1); i++)
for (int j = 0; j < (M - 1); j++)
if (A[i][j] && A[i + 1][j] && A[i][j + 1] && A[i + 1][j + 1]) {
hoge(i, j);
}
if (A == B) {
cout << ans.size() << endl;
for (auto x : ans) cout << x.first + 1 << ' ' << x.second + 1 << '\n';
} else {
cout << -1 << endl;
}
}
| 2 |
Subsets and Splits