solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<iostream>
using namespace std;
int main()
{
int n, m, d, v, a[101];
while( cin >> n >> m )
{
if( n + m == 0 )
break;
for( int i = 0; i < 101; i++ )
a[i] = 0;
for( int i = 0; i < n; i++ )
{
cin >> d >> v;
if( a[d] < v )
a[d] = v;
}
int sum = 0;
for( int i = 1; i <= m; i++ )
sum += a[i];
cout << sum << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5123;
string t;
int n, a, b;
int memo[MAXN];
int step[MAXN];
int z[MAXN + MAXN];
void zfunction(const string& s) {
memset(z, 0, sizeof z);
int n = s.size();
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && s[R - L] == s[R]) R++;
z[i] = R - L;
R--;
} else {
int k = i - L;
if (z[k] < R - i + 1)
z[i] = z[k];
else {
L = i;
while (R < n && s[R - L] == s[R]) R++;
z[i] = R - L;
R--;
}
}
}
}
int getBiggest(int x) {
string s;
for (int i = x; i < n; ++i) s += t[i];
s += '#';
int start = s.size();
for (int i = 0; i < x; ++i) s += t[i];
int ans = 0;
zfunction(s);
for (int i = start; i < s.size(); ++i) {
ans = max(ans, z[i]);
}
return x + ans;
}
int main() {
cin >> n >> a >> b;
cin >> t;
for (int i = 0; i < n; ++i) {
step[i] = getBiggest(i);
}
memo[n] = 0;
for (int i = n - 1; i >= 0; --i) {
memo[i] = memo[i + 1] + a;
memo[i] = min(memo[i], memo[step[i]] + b);
}
cout << memo[0] << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline void up(int& x, int y) {
x = x + y < 1000000007 ? x + y : x + y - 1000000007;
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
int a, b[111], c[111], d[111], ans = 0;
cin >> a;
c[0] = d[0] = 0;
for (int m = 1; m <= a; m++) {
scanf("%d", &b[m]);
c[m] = c[m - 1];
d[m] = d[m - 1];
if (b[m])
d[m]++;
else
c[m]++;
}
for (int m = 0; m <= a; m++) ans = max(c[m] + d[a] - d[m], ans);
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, T;
cin >> T;
while (T--) {
scanf("%d", &a);
printf("1 %d\n", a - 1);
}
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
string s;
cin>>t;
while(t--)
{
cin>>s;
for(int i=0;i<s.length();i++)
{
if(i%2==0)
{
if(s[i]=='a') cout<<'b';
else cout<<'a';
}
else
{
if(s[i]=='z') cout<<'y';
else cout<<'z';
}
}
cout<<endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 69;
const int MOD = 1e9 + 7;
int n, ans, par[MAXN], depth[MAXN];
void add(int v) { par[v] = v; }
void find(int v) {
if (par[v] != v) {
find(par[v]);
depth[v] = (depth[v] + depth[par[v]]) % MOD;
par[v] = par[par[v]];
}
}
void upd(int v, int u, int x) {
ans = (ans + (depth[v] + x) % MOD) % MOD;
depth[par[v]] = (depth[v] + x) % MOD;
par[par[v]] = u;
}
int main() {
int k, v, x;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> k, add(i);
for (int j = 1; j <= k; j++) cin >> v >> x, find(v), upd(v, i, x);
}
cout << (ans + MOD) % MOD << "\n";
}
| 5 |
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
using namespace std;
int n;
vector<ll>v;
int base;
int ans;
bool can(int i){
if(i>0 && i<n-1){
if(v[i]>v[i-1] && v[i]>v[i+1]) return true;
if(v[i]<v[i-1] && v[i]<v[i+1]) return true;
}
return false;
}
void f(int i,int val){
int awal=0;
if(can(i)) awal++;
if(can(i+1)) awal++;
if(can(i-1)) awal++;
int tp=v[i];
v[i]=val;
int akhir=0;
if(can(i)) akhir++;
if(can(i+1)) akhir++;
if(can(i-1)) akhir++;
v[i]=tp;
ans=min(ans,base-(awal-akhir));
}
void solve(){
cin>>n;
v.resize(n);
for(int i=0;i<n;i++) cin>>v[i];
if(n<=2){
cout<<0<<endl;
return;
}
ans=0;
for(int i=1;i<n-1;i++){
if(can(i)) ans++;
}
base=ans;
for(int i=0;i<n;i++){
if(i-1>=0) f(i,v[i-1]);
if(i+1<n) f(i,v[i+1]);
}
// cout<<base<<" "<<ans<<endl;
cout<<ans<<endl;
}
int main(){
fastio();
int t=1;
cin>>t;
for(int tt=0;tt<t;tt++){
// cout<<"Case "<<tt+1<<":\n";solve();
solve();
}
return 0;
} | 2 |
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 2e5 + 7;
const int maxm = 1e6 + 7;
const long long mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-9;
long long n, m;
long long p[maxn];
long long fac[maxn];
long long tree[maxn];
long long lowbit(long long k) { return k & -k; }
void add(long long x, long long k) {
while (x <= n) {
tree[x] += k;
x += lowbit(x);
}
}
long long sum(long long x) {
long long ans = 0;
while (x != 0) {
ans += tree[x];
x -= lowbit(x);
}
return ans;
}
bool vis[maxn];
vector<long long> v;
long long poww(long long a, long long b) {
long long ans = 1, base = a;
while (b != 0) {
if (b & 1 != 0) ans = (ans * base) % mod;
base = (base * base) % mod;
b >>= 1;
}
return ans;
}
long long ans;
int main(int argc, char const *argv[]) {
fac[0] = 1;
for (long long i = 1; i < maxn; i++) fac[i] = (fac[i - 1] * i) % mod;
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &p[i]);
if (p[i] != -1) vis[p[i]] = 1;
}
for (long long i = 1; i <= n; i++)
if (!vis[i]) v.push_back(i);
m = v.size();
sort(v.begin(), v.end());
long long cnt = 0;
for (int i = 1; i <= n; i++) {
if (p[i] != -1) {
long long rk = n - p[i] + 1;
cnt = (cnt + sum(rk)) % mod;
add(rk, 1);
}
}
ans = (ans + (cnt * fac[m]) % mod) % mod;
long long tmp =
(((m * (m - 1)) % mod * fac[m]) % mod * poww(4, mod - 2)) % mod;
ans = (ans + tmp) % mod;
long long k = 0;
for (int i = n; i >= 1; i--) {
if (p[i] != -1) {
long long s = lower_bound(v.begin(), v.end(), p[i]) - v.begin();
ans = (ans + ((k * s) % mod * fac[m - 1]) % mod) % mod;
} else
k++;
}
k = 0;
for (int i = 1; i <= n; i++) {
if (p[i] != -1) {
long long s = v.end() - upper_bound(v.begin(), v.end(), p[i]);
ans = (ans + ((k * s) % mod * fac[m - 1]) % mod) % mod;
} else
k++;
}
ans = (ans * poww(fac[m], mod - 2)) % mod;
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string A ,S = "Three";
cin >> N;
for(int i = 0; i < N; i++){
cin >> A;
if(A == "Y"){
S = "Four";
break;
}
}
cout << S;
}
| 0 |
#include<bitset>
#include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
using namespace std;
const int N=510,mod=998244353;
int n,m,y,rk,ans,a[N][N],pw[N];
bitset<N> b[N];
int main(){
scanf("%d%d",&n,&m); pw[0]=1;
rep(i,1,n){
bitset<N>x(0);
rep(j,1,m) scanf("%d",&y),x[j-1]=y;
for(int j=m-1; ~j; j--)
if (x[j]){
if (b[j].any()) x^=b[j];
else{ b[j]=x; break; }
}
}
rep(i,1,max(n,m)) pw[i]=2*pw[i-1]%mod;
rep(i,0,m-1) if (b[i].any()) rk++;
ans=1ll*(pw[n]-pw[n-rk])*pw[m-1]%mod;
printf("%d\n",(ans+mod)%mod);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
long int q;
cin >> q;
long int inp[200000];
long int count[200000] = {0};
long int _max = -1, _min = 2000001;
for (long int i = 0; i < q; i++) {
cin >> inp[i];
count[inp[i]]++;
if (inp[i] > _max) _max = inp[i];
if (inp[i] < _min) _min = inp[i];
}
vector<pair<long int, long int> > pos;
bool flag = false;
for (long int i = 0; i < q; i++)
if (count[inp[i]] >= 2) {
flag = true;
pos.push_back(make_pair(inp[i], i));
}
sort(pos.begin(), pos.end());
long int _size = 1000000;
for (long int i = 1; i < pos.size(); i++) {
if (pos[i].first == pos[i - 1].first)
if (abs(pos[i].second - pos[i - 1].second + 1) < _size)
_size = abs(pos[i].second - pos[i - 1].second + 1);
if (_size == 2) break;
}
if (flag == false) _size = -1;
cout << _size << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a[100008], b[100009];
map<long long, long long> mp;
map<long long, long long> mp1;
int main() {
cin.sync_with_stdio(false);
long long i, j, k, l, c, v, bbbb[1000], aa[1009], bb[1000], n, m, r, t;
long long x1, x2, y1, y2, x3, x4, x5, x6;
string s, s1, s2, s3, ss[10000];
while (cin >> n) {
c = 0;
k = 0;
l = v = 0;
cin >> k;
for (i = 0; i < n; i++) {
cin >> m;
l += 86400 - m;
if (l >= k && v == 0) {
j = i + 1;
v = 1;
}
}
cout << j;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef struct {
long long int b, c;
} No;
vector<int> grafo[100100];
No arv[4 * 100100];
int pos[100100];
pair<int, int> axpos[100100];
long long int custo[100100];
long long int subida[100100];
long long int biscoito[100100];
long long int n, t;
void atualiza(int no, int i, int j, int posicao, long long int bisc,
long long int cust) {
if (i == j) {
arv[no].c += cust;
arv[no].b += bisc;
} else {
int esq = no * 2;
int dir = no * 2 + 1;
int meio = (i + j) / 2;
arv[no].b += bisc;
arv[no].c += cust * abs(bisc);
if (posicao <= meio)
atualiza(esq, i, meio, posicao, bisc, cust);
else
atualiza(dir, meio + 1, j, posicao, bisc, cust);
}
}
long long int consulta(int no, int i, int j, long long int tempo) {
if (arv[no].b <= 0 || tempo <= 0) return 0;
if (i == j) {
return min(tempo / arv[no].c, arv[no].b);
} else {
if (tempo >= arv[no].c) return arv[no].b;
int esq = 2 * no;
int dir = 2 * no + 1;
int meio = (i + j) / 2;
long long int res = consulta(esq, i, meio, tempo);
long long int w = arv[esq].c;
if (meio == i) w *= arv[esq].b;
res += consulta(dir, meio + 1, j, tempo - w);
return res;
}
}
long long int dfs(int v, long long int tempo) {
if (tempo <= 0) return 0;
long long int fica, m1 = 0, m2 = 0, ax;
atualiza(1, 1, n, pos[v], biscoito[v], custo[v]);
fica = consulta(1, 1, n, tempo);
for (int i = 0; i < (int)grafo[v].size(); i++) {
int u = grafo[v][i];
ax = dfs(u, tempo - subida[u] * 2);
if (ax > m1) {
m2 = m1;
m1 = ax;
} else if (ax > m2) {
m2 = ax;
}
}
atualiza(1, 1, n, pos[v], -biscoito[v], -custo[v]);
if (v == 1) return max(fica, m1);
return max(m2, fica);
}
int main() {
int i;
cin >> n >> t;
for (i = 1; i <= n; i++) {
cin >> biscoito[i];
}
for (i = 1; i <= n; i++) {
cin >> custo[i];
axpos[i].second = i;
axpos[i].first = custo[i];
}
sort(axpos + 1, axpos + 1 + n);
for (i = 1; i <= n; i++) {
pos[axpos[i].second] = i;
}
int pai;
for (i = 2; i <= n; i++) {
cin >> pai >> subida[i];
grafo[pai].push_back(i);
}
cout << dfs(1, t) << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int a[N], dis[N], ans, num;
bool vis[N];
vector<int> pic[N], prime;
queue<int> que;
void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
x *= f;
}
int init(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i) continue;
int cnt = 0;
while (x % i == 0) {
x /= i;
cnt++;
}
if (cnt & 1) x *= i;
}
return x;
}
void bfs(int s) {
while (!que.empty()) que.pop();
for (int i = 0; i < num; i++) {
dis[prime[i]] = N;
vis[prime[i]] = 0;
}
vis[s] = 1;
que.push(s);
dis[s] = 0;
while (!que.empty()) {
int now = que.front();
que.pop();
vis[now] = 0;
for (int i = 0; i < pic[now].size(); i++) {
int t = pic[now][i];
if (dis[t] > dis[now] + 1) {
dis[t] = dis[now] + 1;
que.push(t);
vis[t] = 1;
} else if (vis[t])
ans = min(ans, dis[t] + dis[now] + 1);
}
}
}
int main() {
int n, maxn = -1;
read(n);
for (int i = 1; i <= n; i++) {
read(a[i]);
maxn = max(a[i], maxn);
a[i] = init(a[i]);
}
sort(a + 1, a + 1 + n);
if (a[1] == 1) {
printf("1\n");
return 0;
}
int len = unique(a + 1, a + 1 + n) - a - 1;
if (len < n) {
printf("2\n");
return 0;
}
for (int i = 1; i <= len; i++) {
vector<int> tmp(2, -1);
tmp[0] = a[i];
for (int j = 2; j * j <= a[i]; j++) {
if (a[i] % j == 0) {
a[i] /= j;
tmp[0] = j;
if (a[i] > 1) tmp[1] = a[i];
break;
}
}
if (tmp[1] == -1) tmp[1] = 1;
for (int j = 0; j < tmp.size(); j++) prime.push_back(tmp[j]);
pic[tmp[0]].push_back(tmp[1]);
pic[tmp[1]].push_back(tmp[0]);
}
sort(prime.begin(), prime.end());
num = unique(prime.begin(), prime.end()) - prime.begin();
ans = N;
for (int i = 0; i < num; i++) {
if (1LL * prime[i] * prime[i] >= maxn) break;
bfs(prime[i]);
}
printf("%d\n", ans == N ? -1 : ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
return y <= x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
return x <= y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int MAXN = 1 << 18;
const int MAXM = 2000005;
const int MX = 5000;
const int mods = 998244353;
const int SZ = 131;
const int inv2 = (mods + 1) >> 1;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
*oT = obuf + SIZE - 1, c;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char c) {
*oS++ = c;
if (oS == oT) flush();
}
inline void getc(char &c) {
for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++);
!isdigit(c) && c != EOF;
c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++))
;
}
inline void reads(char *st) {
char c;
int n = 0;
getc(st[++n]);
for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++);
isdigit(c);
c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++))
st[++n] = c;
st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-') f = -1;
for (x = 0; c >= '0' && c <= '9';
c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
(iS == iT ? EOF : *iS++))
: *iS++))
x = (x << 3) + (x << 1) + (c & 15);
x *= f;
}
template <class I>
inline void print(I x) {
if (x < 0) putc('-'), x = -x;
if (!x) putc('0');
while (x) que[++num] = x % 10 + 48, x /= 10;
while (num) putc(que[num--]);
}
inline void putstr(string st) {
for (int i = 0; i < (int)st.size(); ++i) putc(st[i]);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_Flusher_;
} // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::putstr;
using FastIO ::read;
using FastIO ::reads;
char st[20];
vector<int> V;
long long F[20][MAXN], h[20][MAXN], g[MAXN][20], f[MAXN];
int n, cnt[MAXN], flag[20][20];
map<vector<int>, long long> Map;
void FWT(long long *A, int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < 1 << n; ++j)
if ((j >> i) & 1) A[j] += A[j ^ (1 << i)];
}
void IFWT(long long *A, int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < 1 << n; ++j)
if ((j >> i) & 1) A[j] -= A[j ^ (1 << i)];
}
void dfs(int len, int x, int lst) {
if (x == n) {
long long sum = 0;
for (int i = 0; i < 1 << n; ++i) {
if ((n - cnt[i]) & 1)
sum -= F[len][i];
else
sum += F[len][i];
}
Map[V] = sum;
return;
}
for (int i = lst; i <= n - x; ++i) {
for (int j = 0; j < 1 << n; ++j) F[len + 1][j] = F[len][j] * h[i][j];
V.push_back(i);
dfs(len + 1, x + i, i);
V.pop_back();
}
}
signed main() {
read(n);
for (int i = 1; i <= n; ++i) {
reads(st);
for (int j = 1; j <= n; ++j) flag[i - 1][j - 1] = (st[j] - '0');
}
for (int i = 1; i < 1 << n; ++i) cnt[i] = cnt[i >> 1] + (i & 1);
for (int i = 0; i < n; ++i) g[1 << i][i] = 1;
for (int i = 1; i < 1 << n; ++i) {
for (int j = 0; j < n; ++j)
if ((i >> j) & 1) {
for (int k = 0; k < n; ++k)
if (!((i >> k) & 1) && flag[j][k]) g[i | (1 << k)][k] += g[i][j];
h[cnt[i]][i] += g[i][j];
}
}
for (int i = 1; i <= n; ++i) FWT(h[i], n);
for (int i = 0; i < 1 << n; ++i) F[0][i] = 1;
dfs(0, 0, 1);
for (int i = 0; i < 1 << (n - 1); ++i) {
V.clear();
int num = 1;
for (int j = 0; j < n - 1; ++j) {
if (!((i >> j) & 1)) V.push_back(num), num = 0;
++num;
}
V.push_back(num);
sort(V.begin(), V.end());
f[((1 << (n - 1)) - 1) ^ i] = Map[V];
}
IFWT(f, n - 1);
for (int i = (1 << (n - 1)) - 1; i >= 0; --i) print(f[i]), putc(' ');
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int res;
int DN[2005][2005];
char S1[2005], S2[2005];
int main() {
scanf("%s%s", S1 + 1, S2 + 1);
int uz1 = strlen(S1 + 1);
int uz2 = strlen(S2 + 1);
for (int i = 1; i <= uz1; i++)
for (int j = 1; j <= uz2; j++) {
DN[i][j] = DN[i - 1][j - 1] + (S1[i] == S2[j]);
res = max(res, DN[i][j]);
}
printf("%d\n", uz2 - res);
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define T top()
#define E end()
#define PS push
#define Q queue
#define PP pop()
#define L0 (LL)0
#define V vector
#define ST stack
#define FI first
#define MAX 4e18
#define MIN -MAX
#define DQ deque
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define SE second
#define DL double
#define G getline
#define IN insert
#define endl "\n"
#define EM empty()
#define cnn continue
#define MD 1000000007
#define PSB push_back
#define PI acos(-1.00)
#define PPB pop_back()
#define PSF push_front
#define ub upper_bound
#define lb lower_bound
#define PPF pop_front()
#define CF cout.flush()
#define line cout<<endl
#define LL long long int
#define one cout<<1<<endl
#define zer cout<<0<<endl
#define PQ priority_queue
#define nil cout<<-1<<endl
#define N cout<<"NO"<<endl
#define NN cout<<"No"<<endl
#define Y cout<<"YES"<<endl
#define YY cout<<"Yes"<<endl
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define MAU cout<<"Case "<<mau<<": "
#define SP(pre) fixed<<setprecision(pre)
#pragma GCC optimization ("unroll-loops")
#define MMAU cout<<"Case "<<mau<<":"<<endl
#define __lcm(A1,A2) (A1/(__gcd(A1,A2)))*A2
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define UOSMOY v.C;w.C;ww.C;uu.C;uo.C;vo.C;vu.C;ab.C;bc.C;cc.C;uuu.C;a.C;b.C;c.C;u.C
V<LL>v;
V<LL>w,ab,bc;
set<LL>uo;
map<LL,LL>ww,cc,u;
V<pair<LL,LL> >uu,su;
map<LL,V<LL> >uuu,suu;
map<pair<LL,LL>,LL> vo;
map<pair<LL,LL>,LL> vu;
map<LL,pair<LL,pair<LL,LL > > > vs;
priority_queue<LL,V<LL>,greater<LL> > moy;
LL dx[]= {-1,0,1,0,1,1,-1,-1};
LL dy[]= {0,1,0,-1,-1,1,1,-1};
LL dxx[]= {2,1,-1,-2,-2,-1,1,2};
LL dyy[]= {1,2,2,1,-1,-2,-2,-1};
LL dp[101][10001];
LL ar[10][10];
int main()
{
// BRR;
// file;
FAST;
char ch;
string a,b,c;
DL x,y,d,e,f,g,h;
LL n,i,j,k,p,q,o,l,s,tt=1,m,t,z,aa,r=0;
cin>>tt;
for(LL mau=1; mau<=tt; mau++)
{
UOSMOY;
cin>>n;
for(i=0; i<n; i++)
{
cin>>o;
v.PSB(o);
}
for(i=0; i<n; i++)
{
cin>>o;
uuu[v[i]].PSB(o);
}
suu.C;
for(auto it=uuu.B; it!=uuu.E; it++)
{
sort(it->SE.B,it->SE.E,greater<LL>());
k=0;
o=it->SE.SZ;
for(i=0; i<o; i++)
{
k+=it->SE[i];
suu[o*1000000+(it->FI)].PSB(k);
}
}
for(i=1; i<=n; i++)
{
l=0;
for(auto it=suu.rbegin(); it!=suu.rend(); it++)
{
o=it->SE.SZ;
if(o<i)
{
break;
}
o/=i;
o*=i;
o--;
l+=(it->SE)[o];
}
cout<<l<<" ";
}
line;
}
return 0;
//IN
//AL
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int tmp[111];
void permuteArr(int *arr1, int *per, int sz) {
for (int i = 0; i < sz; i++) tmp[per[i] - 1] = arr1[i];
for (int i = 0; i < sz; i++) arr1[i] = tmp[i];
}
bool isSame(int *arr1, int *arr2, int sz) {
for (int i = 0; i < sz; i++)
if (arr1[i] != arr2[i]) return 0;
return 1;
}
void printArr(int *arr, int sz) {
for (int i = 0; i < sz; i++) cout << arr[i] << " ";
cout << "\n";
}
int main() {
int n, k, s[111], q[111], p[111], t1[111], t2[111], distPS = 111,
distSP = 111;
bool can = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> q[i];
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++) p[i] = i + 1;
for (int i = 0; i < n; i++) {
t1[i] = p[i];
t2[i] = s[i];
}
for (int i = 0; i <= k; i++) {
if (isSame(t1, s, n) && distPS > i) distPS = i;
if (isSame(t2, p, n) && distSP > i) distSP = i;
permuteArr(t1, q, n);
permuteArr(t2, q, n);
}
if (distSP == k || distPS == k) {
can = 1;
}
if (distSP > 1 && distPS <= k && (k - distPS) % 2 == 0) {
can = 1;
}
if (distPS > 1 && distSP <= k && (k - distSP) % 2 == 0) {
can = 1;
}
if (distPS > 1 && distPS <= k && (k - distPS) % 2 == 0) {
can = 1;
}
if (distSP > 1 && distSP <= k && (k - distSP) % 2 == 0) {
can = 1;
}
if (can == 0)
cout << "NO\n";
else
cout << "YES\n";
return 0;
}
| 4 |
#include <iostream>
#define N 15
using namespace std;
int jump(int,int);
int X,Y,data[N][N],dp[N][N];
int main(){
bool q[N][N];
while(1){
cin >> X >> Y;
if(X==0&&Y==0) break;
for(int i=0;i<Y;i++)
for(int j=0;j<X;j++) cin >> data[i][j],q[i][j]=false,dp[i][j]=0;
for(int i=0;i<X;i++)
if(data[0][i]==0) q[0][i]=true,dp[0][i]=1;
for(int i=0;i<Y-1;i++){
for(int j=0;j<X;j++){
if(q[i][j]==true){
if(0<=j-1&&data[i+1][j-1]==0) dp[i+1][j-1]+=dp[i][j],q[i+1][j-1]=true;
if(j+1<X&&data[i+1][j+1]==0) dp[i+1][j+1]+=dp[i][j],q[i+1][j+1]=true;
if(data[i+1][j]==0) dp[i+1][j]+=dp[i][j],q[i+1][j]=true;
else if(data[i+1][j]==2){
int r=jump(i+1,j);
if(r==-1) dp[Y-1][j]+=dp[i][j];
else if(r!=-2) dp[r][j]+=dp[i][j],q[r][j]=true;
}
}
}
}
/* cout << endl;
for(int i=0;i<Y;i++){
for(int j=0;j<X;j++){
cout << q[i][j] << ' ';
}
cout << endl;
}
cout << endl;
for(int i=0;i<Y;i++){
for(int j=0;j<X;j++){
cout << dp[i][j] << ' ';
}
cout << endl;
}*/
int cnt=0;
for(int i=0;i<X;i++) cnt+=dp[Y-1][i];
cout << cnt << endl;
}
return 0;
}
int jump(int y,int x){
while(1){
y+=2;
if(y<Y&&data[y][x]==1) break;
if(y>=Y-1) return -1;
if(data[y][x]==0) return y;
}
return -2;
} | 0 |
#include <iostream>
using namespace std;
int main()
{
double x[4], y[4];
int n;
cin >> n;
while (n--) {
for (int i = 0; i < 4; ++i) cin >> x[i] >> y[i];
double A1 = y[1] - y[0];
double B1 = x[0] - x[1];
double C1 = A1 * x[0] + B1 * y[0];
double A2 = y[3] - y[2];
double B2 = x[2] - x[3];
double C2 = A1 * x[2] + B1 * y[2];
if (A1 * B2 - A2 * B1 == 0)
cout << "YES";
else
cout << "NO";
cout << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n, a, b, c, cnt, i, j;
while (cin >> n >> a >> b >> c) {
cnt = 0;
for (i = 0; i <= b; i++) {
for (j = 0; j <= c; j++) {
if ((2 * (n - i - 2 * j)) <= a) {
cnt++;
}
}
}
cout << cnt << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(0) * 2;
const double EPS = 1e-8;
const long long MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int oo = 1e9;
const double foo = 1e30;
template <class T>
int getbit(T s, int i) {
return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
return __builtin_popcounll(s);
}
int a[100100], n;
long long solve(int l, int r) {
vector<long long> d;
int L[100100], R[100100];
stack<pair<int, int> > st;
memset(L, 0, sizeof L);
memset(R, 0, sizeof R);
d.clear();
d.push_back(oo);
for (int i = l + 1; i <= r; i++) {
int x = abs(a[i] - a[i - 1]);
d.push_back(x);
}
d.push_back(oo);
st.push(make_pair(d[0], 0));
int N = ((int)(d).size());
for (int i = 1; i < ((int)(d).size()); i++) {
while (st.top().first < d[i]) st.pop();
L[i] = st.top().second;
st.push(make_pair(d[i], i));
}
while (!st.empty()) st.pop();
st.push(make_pair(d[N - 1], N - 1));
for (int i = N - 2; i >= 1; i--) {
while (!st.empty() && st.top().first <= d[i]) st.pop();
R[i] = st.top().second;
st.push(make_pair(d[i], i));
}
long long ret = 0;
for (int i = 1; i <= N - 2; i++) {
long long l = L[i] + 1;
long long r = R[i] - 1;
l = i - l + 1;
r = r - i + 1;
long long x = (l * r) * d[i];
ret += x;
}
return ret;
}
int main() {
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
printf("%I64d\n", solve(l, r));
}
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long long MOD = (int)1e9 + 7;
const long long MOR = (int)1e9 + 31;
const long long INF = (long long)1e15 + 100;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int MAXN = (int)3e5 + 5;
const double pi = 3.14159265358979323846;
const int SZ = 1;
double Inf = 1e15;
long long n, m, k;
long long x, y, L, R;
long long a[MAXN], b[MAXN];
long long pr[4][51];
vector<pair<long long, pair<long long, long long> > > answ;
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long pw(long long x, long long k) {
if (k == 0) return 1;
if (k % 2 == 1) return x * pw(x, k - 1) % MOD;
long long b = pw(x, k / 2);
return b * b % MOD;
}
void rot() {
int r = -1, c;
for (int i = 0; i < n; i++) {
if (pr[1][i] == 0) r = 1, c = i;
}
for (int i = 0; i < n; i++) {
if (pr[2][i] == 0) r = 2, c = i;
}
for (int t = 0, i = r, j = c; t < 2 * n - 1; t++) {
if (i == 1) {
if (j + 1 < n) {
if (pr[i][j + 1])
answ.push_back({pr[i][j + 1], {i + 1, j + 1}}),
pr[i][j] = pr[i][j + 1], pr[i][j + 1] = 0;
j++;
continue;
} else {
if (pr[i + 1][j])
answ.push_back({pr[i + 1][j], {i + 1, j + 1}}),
pr[i][j] = pr[i + 1][j], pr[i + 1][j] = 0;
i++;
continue;
}
} else {
if (j - 1 >= 0) {
if (pr[i][j - 1])
answ.push_back({pr[i][j - 1], {i + 1, j + 1}}),
pr[i][j] = pr[i][j - 1], pr[i][j - 1] = 0;
j--;
continue;
} else {
if (pr[i - 1][j])
answ.push_back({pr[i - 1][j], {i + 1, j + 1}}),
pr[i][j] = pr[i - 1][j], pr[i - 1][j] = 0;
i--;
continue;
}
}
}
}
bool tryans() {
bool hav = 0, end = 0;
for (int i = 0; i < n; i++) {
if (pr[1][i] == pr[0][i] && pr[1][i] != 0)
answ.push_back({pr[1][i], {1, i + 1}}), hav = 1, pr[1][i] = 0;
if (pr[1][i] == 0)
hav = 1;
else
end = 1;
}
for (int i = 0; i < n; i++) {
if (pr[2][i] == pr[3][i] && pr[2][i] != 0)
answ.push_back({pr[2][i], {4, i + 1}}), hav = 1, pr[2][i] = 0;
if (pr[2][i] == 0)
hav = 1;
else
end = 1;
}
return hav & end;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
cin >> pr[i][j];
}
}
while (tryans()) rot();
if (answ.size()) {
cout << answ.size() << "\n";
for (auto p : answ)
cout << p.first << " " << p.second.first << " " << p.second.second
<< "\n";
} else
cout << -1;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int n,m;
int x,y,z;
int fa[100010];
int g[100010],cnt=0;
int find(int x){
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
int main() {
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++){
scanf("%d %d %d",&x,&y,&z);
int u=find(x);
int v=find(y);
if(u!=v){
if(u<v) fa[v]=u;
else fa[u]=v;
}
}
for(int i=1;i<=n;i++){
fa[i]=find(i);
if(g[fa[i]]==0){
g[fa[i]]=1;
cnt++;
}
}
printf("%d\n",cnt);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int tmp, res, n, a[20000 + 5];
void Init();
void Process();
int main() {
Init();
Process();
return 0;
}
void Init() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
res += a[i];
}
}
void Process() {
int k;
for (int i = 1; i <= n; ++i)
if ((n % i == 0) && (n / i >= 3))
for (int j = 1; j <= i; ++j) {
tmp = a[j];
k = i + j;
if (k > n) k -= n;
for (; k != j;) {
tmp += a[k];
k += i;
if (k > n) k -= n;
}
res = max(res, tmp);
}
printf("%d\n", res);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0, a;
for (int i = 0; i < n; i++) {
cin >> a;
sum += a;
}
int m;
cin >> m;
vector<char> good(1e5 + 1, false);
int l, r;
for (int i = 0; i < m; i++) {
cin >> l >> r;
for (int j = l; j <= r; j++) {
good[j] = true;
}
}
for (int i = sum; i <= 1e5; i++) {
if (good[i]) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define eps 1e-9
#define nmax 200
#define f(c,a,b) for(int c=a; c<=b; c++)
#define pi acos(-1)
using namespace std;
typedef double db;
struct P{
db x, y;
P operator - (P a){ return (P){x-a.x, y-a.y}; }
db dot(P a){ return a.x*x + a.y*y; }
P operator * (db a) { return (P){a*x, a*y}; }
P operator + (P a){ return (P){x+a.x, y+a.y}; }
P operator / (db a) { return (P) {x/a, y/a}; }
db times(P a){ return x*a.y-y*a.x; }
db le() { return sqrt( x*x+y*y ); }
db le2() { return x*x+y*y; }
P rot90(){ return (P){-y,x}; }
db rad(P a) { return atan2(times(a),dot(a)); }
void pri(){ printf("%.10lf %.10lf\n", x, y); }
void swap(P& a){
db tx = x, ty = y;
x = a.x; y = a.y;
a.x = tx; a.y = ty;
}
bool operator < (const P a) const { return (x==a.x)?(y<a.y):(x<a.x); }
bool operator == (const P a) const { return (x==a.x)&&(y==a.y); }
}po[nmax];
int sign(db x) { return (x<-eps) ? -1 : x>eps; }
db disLP(P p1, P p2, P p){ return abs( (p1-p).times(p2-p) ) / (p1-p2).le(); }
P projection(P a, P p, P q){
P tl = q - p;
db ll = tl.le();
db k = (a-p).dot(tl)/ll/ll;
return (P){ p.x + k*tl.x, p.y + k*tl.y };
}
db disSP(P p1, P p2, P p){
if((p1-p2).dot(p-p2) < eps) return (p-p2).le();
if((p2-p1).dot(p-p1) < eps) return (p-p1).le();
return disLP(p1, p2, p);
}
vector<P> cpCL(P c1, db r1, P p1, P q1){
db dis = disLP(p1, q1, c1);
dis = sqrt( r1*r1-dis*dis );
P pj = projection(c1, p1, q1);
P a1 = pj + (p1-q1) /(p1-q1).le() *dis;
P a2 = pj + (q1-p1) /(q1-p1).le() *dis;
return {a1, a2};
}
db interCT(P p1, db r1, P p2, P p3){
P tp=p1; p1=p1-p1; p2=p2-tp; p3=p3-tp;
int pd2 = sign(r1*r1-p2.le2()), pd3 = sign(r1*r1-p3.le2());
vector <P> cp = cpCL(p1, r1, p2, p3);
if(pd2>=0){
if(pd3>=0) return p2.times(p3);
return p2.times(cp[1]) + r1*r1*cp[1].rad(p3);
}else{
if(pd3>=0) return cp[0].times(p3) + r1*r1*p2.rad(cp[0]);
if(disSP(p2,p3,p1) >= r1) { return r1*r1*p2.rad(p3); }
return r1*r1*p2.rad(cp[0])+cp[0].times(cp[1])+r1*r1*cp[1].rad(p3);
}
}
int main(){
//freopen("owo.in","r",stdin);
int n;
db r, ans=0;
cin >> n >> r;
f(i,0,n-1) scanf("%lf%lf", &po[i].x, &po[i].y);
f(i,0,n-1) {
db test=interCT((P){0,0}, r, po[i], po[(i+1)%n]);
ans += test;
}
printf("%.12lf\n", ans/2);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> a(m), b(m);
vector<string> line(n);
vector<string> ans(n);
char s[15];
for (int i = 0; i < m; i++) {
scanf("%s", s);
string x = "";
for (int j = 0; j < strlen(s); j++) x += s[j];
a[i] = x;
scanf("%s", s);
x = "";
for (int j = 0; j < strlen(s); j++) x += s[j];
b[i] = x;
}
for (int i = 0; i < n; i++) {
scanf("%s", s);
string x = "";
for (int j = 0; j < strlen(s); j++) x += s[j];
line[i] = x;
}
for (int i = 0; i < n; i++) {
string x = line[i];
for (int j = 0; j < m; j++) {
if ((x == a[j]) || (x == b[j])) {
if (a[j].length() <= b[j].length()) {
ans[i] = a[j];
} else
ans[i] = b[j];
break;
}
}
}
int i;
for (i = 0; i < n - 1; i++) cout << ans[i] << " ";
cout << ans[i];
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cnt = 0;
int a[3][3];
char ans[3][3];
int main() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
ans[i][j] = '1';
}
if (a[0][0] + a[0][1] + a[1][0] & 1)
cout << '0';
else
cout << '1';
if (a[0][1] + a[0][0] + a[0][2] + a[1][1] & 1)
cout << '0';
else
cout << '1';
if (a[0][1] + a[0][2] + a[1][2] & 1)
cout << '0';
else
cout << '1';
cout << endl;
if (a[1][0] + a[0][0] + a[1][1] + a[2][0] & 1)
cout << '0';
else
cout << '1';
if (a[0][1] + a[1][0] + a[1][1] + a[1][2] + a[2][1] & 1)
cout << '0';
else
cout << '1';
if (a[0][2] + a[1][1] + a[1][2] + a[2][2] & 1)
cout << '0';
else
cout << '1';
cout << endl;
if (a[2][0] + a[1][0] + a[2][1] & 1)
cout << '0';
else
cout << '1';
if (a[2][0] + a[2][1] + a[2][2] + a[1][1] & 1)
cout << '0';
else
cout << '1';
if (a[2][2] + a[2][1] + a[1][2] & 1)
cout << '0';
else
cout << '1';
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, N, M, K, b[100005], q[100005 * 400];
bool e[100005];
long long H, a[100005], d[100005], c[25];
priority_queue<pair<int, int> > Q;
void init() {
scanf("%I64d%d%d%d", &H, &N, &M, &K);
for (int i = 1; i <= N; i++) scanf("%I64d%d", &a[i], &b[i]);
}
void doit() {
memset(d, 63, sizeof(d)), d[1] = 1;
for (int j = 1; j <= N; j++)
if (a[j] % K == 1) Q.push(pair<int, int>(b[j], -j));
for (int i = 1, t, x, y; i <= M; i++) {
scanf("%d", &t);
if (t == 1) {
memset(e, 0, sizeof(e));
memset(d, 63, sizeof(d));
scanf("%I64d", &c[++n]), q[1] = 1, d[1] = 1;
for (int l = 1, r = 1; l <= r; l++) {
int u = q[l];
e[u] = 0;
for (int u = q[l], j = 1, v; v = (u + c[j]) % K, j <= n; j++)
if (d[u] + c[j] < d[v]) {
d[v] = d[u] + c[j];
if (!e[v]) e[v] = 1, q[++r] = v;
}
}
for (; !Q.empty();) Q.pop();
for (int j = 1; j <= N; j++)
if (b[j] && a[j] >= d[a[j] % K]) Q.push(pair<int, int>(b[j], -j));
} else if (t == 2) {
scanf("%d%d", &x, &y), b[x] -= y;
if (a[x] >= d[a[x] % K]) Q.push(pair<int, int>(b[x], -x));
} else {
for (pair<int, int> x;;) {
if (Q.empty()) {
puts("0");
break;
}
x = Q.top(), Q.pop();
if (x.first == b[-x.second]) {
b[-x.second] = 0, printf("%d\n", x.first);
break;
}
}
}
}
}
int main() {
init();
doit();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, x;
vector<vector<int> > graph;
vector<bool> visited;
void dfs(int node) {
visited[node] = true;
for (__typeof((graph[node]).begin()) child = (graph[node]).begin();
child != (graph[node]).end(); ++child)
if (!visited[*child]) dfs(*child);
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
graph = vector<vector<int> >(n);
for (int _n = n, i = 0; i < _n; ++i)
for (int _n = n, j = 0; j < _n; ++j) {
cin >> x;
if (x) graph[i].push_back(j);
}
visited = vector<bool>(n);
dfs(0);
for (int _n = n, i = 0; i < _n; ++i)
if (!visited[i]) {
cout << "NO" << endl;
return 0;
}
vector<vector<int> > reversed(n);
for (int _n = n, parent = 0; parent < _n; ++parent)
for (__typeof((graph[parent]).begin()) child = (graph[parent]).begin();
child != (graph[parent]).end(); ++child)
reversed[*child].push_back(parent);
graph = reversed;
visited = vector<bool>(n);
dfs(0);
for (int _n = n, i = 0; i < _n; ++i)
if (!visited[i]) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
long long n, k;
vector<vector<pair<long long, long long>>> graph;
pair<long long, long long> DFS(long long node, long long par) {
vector<long long> wins;
long long ans = 0;
for (auto p : graph[node]) {
long long vec, weight;
tie(vec, weight) = p;
if (vec == par) continue;
long long without, with;
tie(without, with) = DFS(vec, node);
wins.push_back(max(0LL, with + weight - without));
ans += without;
}
sort(wins.rbegin(), wins.rend());
if (k > (long long)wins.size()) {
for (auto x : wins) ans += x;
return {ans, ans};
} else {
for (long long i = 0; i < k; ++i) ans += wins[i];
return {ans, ans - wins[k - 1]};
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t;
cin >> t;
while (t--) {
cin >> n >> k;
graph.assign(n, {});
for (long long i = 1; i < n; ++i) {
long long a, b, c;
cin >> a >> b >> c;
--a;
--b;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
auto p = DFS(0, -1);
cout << max(p.first, p.second) << '\n';
}
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int main(){
int n;
int num[2001] = {0};
for(int i=0;i<=1000;i++)
for(int j=0;j<=1000;j++)num[i+j]++;
while(cin >> n){
int res = 0;
for(int i=0;i<=1000;i++){
for(int j=0;j<=1000;j++){
if(n-i-j<0 || n-i-j>2000)continue;
res += num[n-i-j];
}
}
cout << res << endl;
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
long x,k,d;
int main(){
cin>>x>>k>>d;
x=abs(x);
if(x/d>=k) cout<<x-d*k<<endl;
else cout<<abs(x%d-((x/d)%2==k%2?0:d))<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void jabru() {
int n;
cin >> n;
vector<pair<int, int> > v(n);
for (long long i = 0; i < n; i++) {
int x;
cin >> x;
v[i] = make_pair(x, i);
}
sort(v.begin(), v.end());
vector<int> ans(n);
int ind = v[0].first;
for (long long i = 0; i < n; i++) {
int x = v[i].first;
ind = max(ind, x);
ans[v[i].second] = ind;
ind++;
}
for (long long i = 0; i < n; i++) cout << ans[i] << " ";
cout << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
jabru();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int inf = 1e9;
const ll inf64 = 1e18;
bool check(vector<string> a) {
int n = (int) a.size();
int m = (int) a[0].size();
for (int si = 0; si < n; si++) {
for (int sj = 0; sj < m; sj++) {
if (a[si][sj] != 'X')
continue;
vector<vector<int>> used(n, vector<int>(m));
used[si][sj] = 1;
queue<pii> q;
q.push({si, sj});
while (!q.empty()) {
auto [vi, vj] = q.front();
q.pop();
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
if (di && dj || !di && !dj)
continue;
int ti = vi + di;
int tj = vj + dj;
if (ti < 0 || ti >= n || tj < 0 || tj >= m)
continue;
if (a[ti][tj] != 'X' || used[ti][tj])
continue;
used[ti][tj] = 1;
q.push({ti, tj});
}
}
}
int V = 0, E = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'X' && !used[i][j])
return false;
if (a[i][j] != 'X')
continue;
V++;
if (i + 1 < n && a[i + 1][j] == 'X')
E++;
if (j + 1 < m && a[i][j + 1] == 'X')
E++;
}
}
if (V != E + 1)
return false;
return true;
}
}
return true;
}
int main() {
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
//#define TEST
#ifdef TEST
mt19937 rnd(42);
const int N = 50;
#endif
while (t--) {
#ifndef TEST
int n, m;
cin >> n >> m;
#else
int n = rnd() % N + 1;
int m = rnd() % N + 1;
#endif
vector<string> a(n), b;
#ifndef TEST
for (int i = 0; i < n; i++)
cin >> a[i];
#else
a.assign(n, string(m, '?'));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = '.';
if (i > 0 && a[i - 1][j] == 'X')
continue;
if (j > 0 && a[i][j - 1] == 'X')
continue;
if (i > 0 && j > 0 && a[i - 1][j - 1] == 'X')
continue;
if (i > 0 && j + 1 < m && a[i - 1][j + 1] == 'X')
continue;
a[i][j] = rnd() % 2 ? 'X' : '.';
}
}
b = a;
cout << n << " " << m << "\n";
for (auto x : b)
cout << x << "\n";
cout.flush();
#endif
for (int i = 0; i < n; i += 3)
for (int j = 0; j < m; j++)
a[i][j] = 'X';
for (int i = 0; i + 1 < n; i += 3) {
if (i + 3 < n) {
int ok = 0;
for (int j = 0; j < m; j++) {
if (a[i + 1][j] == 'X') {
a[i + 2][j] = 'X';
ok = 1;
break;
}
}
if (ok)
continue;
for (int j = 0; j < m; j++) {
if (a[i + 2][j] == 'X') {
a[i + 1][j] = 'X';
ok = 1;
break;
}
}
if (ok)
continue;
a[i + 1][0] = a[i + 2][0] = 'X';
} else if (i + 3 == n) {
for (int j = 0; j < m; j++) {
if (a[i + 2][j] == 'X')
a[i + 1][j] = 'X';
}
}
}
for (auto x : a)
cout << x << "\n";
#ifdef TEST
if (!check(a)) {
cout << "WA\n";
break;
} else {
cout << "OK" << endl;
}
#endif
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int arr[305][305];
int main() {
int i, j, k, a, b, c = 1, n, m, t, x, y;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i % 2 == 1)
arr[j][i] = c++;
else
arr[n - j + 1][i] = c++;
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) printf("%d ", arr[i][j]);
printf("\n");
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int g = 0, p = 0;
for(int i=0;i<s.size();++i){
if(s[i] == 'g')g++;
else p++;
}
cout << (g - p) / 2 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
struct Point{
double x,y;
Point(double x = 0,double y = 0) : x(x), y(y) {}
Point operator + (const Point &p)const{
return Point(x + p.x , y + p.y);
}
Point operator - (const Point &p)const{
return Point(x - p.x , y - p.y);
}
Point operator * (const double &k)const{
return Point(x * k , y * k);
}
Point operator / (const double &k)const{
return Point(x / k , y / k);
}
double norm(){
return x*x + y*y;
}
double dot(const Point &p)const{
return x*p.x + y*p.y;
}
};
typedef Point Vector;
struct Segment{
Vector s, t;
Segment(){}
Segment(Vector s, Vector t) : s(s), t(t) {}
Point projection(Point p){
Vector base = t - s;
double t = (p-s).dot(base) / base.norm();
return s + base*t;
}
};
int main(){
Point p1, p2, p3;
int q;
std::cin >> p1.x >> p1.y >> p2.x >> p2.y >> q;
Segment s = Segment(p1, p2);
for(int i = 0 ; i < q ; i++){
std::cin >> p3.x >> p3.y;
Point p = s.projection(p3);
printf("%.10f %.10f\n",p.x ,p.y);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string outputAns(string odds, string evens, vector<int> &freq) {
string out = "";
for (int i = 0; i < odds.length(); i++)
for (int j = 1; j <= freq[odds[i] - 'a']; j++) out += string(1, odds[i]);
for (int i = 0; i < evens.length(); i++)
for (int j = 1; j <= freq[evens[i] - 'a']; j++) out += string(1, evens[i]);
return out;
}
int main() {
int T;
cin >> T;
while (T--) {
string str;
cin >> str;
vector<int> freq(26, 0);
vector<bool> found(26, false);
for (int i = 0; i < str.length(); i++) {
freq[str[i] - 'a']++;
found[str[i] - 'a'] = true;
}
string odds = "", evens = "";
for (int i = 0; i < 26; i += 2)
if (found[i]) odds += string(1, (char)'a' + i);
for (int i = 1; i < 26; i += 2)
if (found[i]) evens += string(1, (char)'a' + i);
if (abs(odds[odds.length() - 1] - evens[0]) != 1)
cout << outputAns(odds, evens, freq) << endl;
else if (abs(odds[odds.length() - 1] - evens[evens.length() - 1]) != 1) {
reverse(evens.begin(), evens.end());
cout << outputAns(odds, evens, freq) << endl;
} else if (abs(odds[0] - evens[0]) != 1) {
reverse(odds.begin(), odds.end());
cout << outputAns(odds, evens, freq) << endl;
} else if (abs(odds[0] - evens[evens.length() - 1]) != 1) {
reverse(evens.begin(), evens.end());
reverse(odds.begin(), odds.end());
cout << outputAns(odds, evens, freq) << endl;
} else
cout << "No answer" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 10000000;
int f(int a, int k) {
int aa = a;
int u = 0;
while (a > k) {
a /= 2;
u++;
}
if (a == k) return u;
while (k > a) {
a *= 2;
u++;
}
if (a == k) return u;
return 1 + f(aa / 2, k);
}
int b[100005];
set<int> s;
set<int>::iterator it;
int main() {
int n, i, a, minn = oo, su, k, j;
cin >> n;
for (i = 0; i < n; i++) {
scanf("%d", &a);
s.insert(a);
b[i] = a;
}
while (s.size() != 1) {
it = s.end();
it--;
su = (*it) / 2;
s.erase(it);
s.insert(su);
}
k = *s.begin();
int pq = 18;
if (k != 1) pq = log(200000.0) / log(1.0 * k);
for (j = 0; j <= pq; j++) {
su = 0;
for (i = 0; i < n; i++) su += f(b[i], k);
if (su < minn) minn = su;
k *= 2;
}
cout << minn << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N,D;
cin >> N >> D;
cout << (2*D+N)/(2*D+1) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[1000013];
void add(int u, int v) { G[u].push_back(v); }
int dfn[1000013], low[1000013], num;
stack<int> st;
int scc[1000013], sc;
int sz[1000013];
void tarjan(int u) {
low[u] = dfn[u] = ++num;
st.push(u);
for (auto v : G[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (!scc[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++sc;
while (st.top() != u) {
scc[st.top()] = sc, sz[sc]++, st.pop();
}
scc[u] = sc, sz[sc]++, st.pop();
}
}
int n, m;
void init() {
num = sc = 0;
for (int i = 0; i <= n; i++) {
G[i].clear();
dfn[i] = scc[i] = sz[i] = 0;
}
while (!st.empty()) st.pop();
}
bool vis[1000013];
int p;
void dfs(int u) {
vis[u] = 1, p++;
for (auto v : G[u]) {
if (!vis[v]) dfs(v);
}
}
int main() {
int T, u, v;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
init();
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
if (u == v) continue;
add(u, v);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
if (sc == 1) {
printf("No\n");
continue;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (auto j : G[i]) {
if (scc[i] == scc[j]) continue;
ans = j;
break;
}
if (ans) break;
}
if (!ans) ans = 1;
for (int i = 1; i <= n; i++) vis[i] = 0;
p = 0;
dfs(ans);
printf("Yes\n");
printf("%d %d\n", p, n - p);
for (int i = 1; i <= n; i++) {
if (vis[i]) printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= n; i++) {
if (!vis[i]) printf("%d ", i);
}
printf("\n");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
set<int> query;
multiset<pair<int, int> > mySet;
void ssotQuery() {
cout << "? ";
for (auto it : query) cout << it << ' ';
cout << endl;
int pos, x;
cin >> pos >> x;
mySet.insert({pos, x});
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int cnt = 1;
for (int i = 1; i <= k; i++) query.insert(i);
ssotQuery();
query.erase(1);
query.insert(k + 1);
for (int i = 1; i <= k; i++) {
ssotQuery();
query.insert(i);
query.erase(i + 1);
}
int kolP = -1, pos = -1, A, B, ff = 0, kolA = 0, kolB = 0;
map<int, int> mp;
for (auto it : mySet) {
mp[it.second]++;
}
for (auto it : mp) {
if (ff == 0) {
ff = 1;
A = it.first;
kolA += it.second;
} else {
B = it.first;
kolB = it.second;
}
}
if (A < B)
cout << "! " << -(kolA - k - 1) << endl;
else
cout << "! " << -(kolB - k - 1) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
int a[N], b[N];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
int l = 0, r = 2000;
while (l <= r) {
int mid = (l + r) >> 1, temp = k;
bool bb = true;
for (int i = 0; i < n; i++) {
if (a[i] * mid > b[i]) {
if (a[i] * mid - b[i] > temp) {
bb = false;
break;
}
temp -= a[i] * mid - b[i];
}
}
if (bb) {
l = mid + 1;
} else {
r = mid - 1;
}
}
printf("%d", l - 1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
string s, t;
cin >> s;
int open = 0, close = 0, act = 0, i;
for (i = 0; i < s.size(); i++) {
if (s[i] == '(') {
open++;
if (open * 2 <= m)
t += s[i];
else
break;
} else {
close++;
if (close <= open)
t += s[i];
else
break;
}
}
while (close != m / 2) t += ")", close++;
cout << t << endl;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int ask(int i){
int r;
cout<<"? "<<i<<"\n";
fflush(stdout);
cin >> r;
return r;
}
int main(){
int n;
cin >> n;
if(n<=100){
int a[n];
for(int i=0;i<n;i++){
a[i]=ask(i+1);
}
if(a[0]<a[1]){
cout<<"! 1\n";
fflush(stdout);
return 0;
}
else if(a[n-1]<a[n-2]){
cout<<"! "<<n<<"\n";
fflush(stdout);
return 0;
}
else{
for(int i=1;i<n-1;i++){
if((a[i]<a[i+1])&&(a[i]<a[i-1])){
cout<<"! "<<i+1<<"\n";
fflush(stdout);
return 0;
}
}
}
}
else{
vector<int> a(n,-1);
a[0]=ask(1);
a[1]=ask(2);
a[n-1]=ask(n);
a[n-2]=ask(n-1);
if(a[0]<a[1]){
cout<<"! 1\n";
fflush(stdout);
return 0;
}
else if(a[n-1]<a[n-2]){
cout<<"! "<<n<<"\n";
fflush(stdout);
return 0;
}
int l=1;
int h=n-2;
while(l<=h){
int mid=l+(h-l)/2;
if(a[mid]==-1){a[mid]=ask(mid+1);}
if(a[mid+1]==-1){a[mid+1]=ask(mid+2);}
if(a[mid-1]==-1){a[mid-1]=ask(mid);}
if((a[mid]<a[mid+1])&&(a[mid]<a[mid-1])){
cout<<"! "<<mid+1<<"\n";
fflush(stdout);
return 0;
}
else if((a[mid]<a[mid+1])&&(a[mid]>a[mid-1])){
h=mid-1;
}
else if((a[mid]>a[mid+1])&&(a[mid]<a[mid-1])){
l=mid+1;
}
else if((a[mid]>a[mid+1])&&(a[mid]>a[mid-1])){
if(mid<n-mid){
h=mid-1;
}
else{
l=mid+1;
}
}
}
}
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
set<long long> s[4];
long long x, y, z, ans = 0, mx = 0;
int tt, qq;
cin >> tt;
for (qq = 1; qq <= tt; qq++) {
cin >> x >> y;
ans += y;
if (x == 0) {
if (y > 0) {
s[0].insert(y);
if (s[3].size()) {
z = *s[3].begin();
if (y > z) {
mx += y - z;
s[2].insert(z);
s[3].erase(z);
s[3].insert(y);
} else
s[2].insert(y);
} else
s[2].insert(y);
} else {
y = -y;
s[0].erase(y);
if (s[3].size()) {
z = *s[3].begin();
if (y >= z) {
mx -= y - *s[2].rbegin();
s[3].erase(y);
z = *s[2].rbegin();
s[3].insert(z);
s[2].erase(z);
} else {
s[2].erase(y);
}
} else
s[2].erase(y);
}
} else {
if (y > 0) {
s[1].insert(y);
s[2].insert(y);
mx += *s[2].rbegin();
s[3].insert(*s[2].rbegin());
s[2].erase(--(s[2].end()));
} else {
y = -y;
s[1].erase(y);
if (y >= *s[3].begin()) {
s[3].erase(y);
mx -= y;
} else {
mx -= *s[3].begin();
x = *s[3].begin();
s[3].erase(s[3].begin());
s[2].erase(y);
s[2].insert(x);
}
}
}
if (s[1].size() && *s[1].begin() == *s[3].begin()) {
if (s[0].size())
x = *s[0].rbegin();
else
x = 0;
cout << ans + mx - *s[1].begin() + x << endl;
} else
cout << ans + mx << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
pair<long double, long double> a, b, r, p[100005];
int n, e = 0, pos;
long double fr[100005], fa[100005], fb[100000], sm = 0, res, ra, rb, y, w;
long double far(pair<long double, long double> f,
pair<long double, long double> s) {
return sqrt((f.first - s.first) * (f.first - s.first) +
(f.second - s.second) * (f.second - s.second));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> a.first >> a.second >> b.first >> b.second >> r.first >> r.second >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
fr[i] = far(r, p[i]);
fa[i] = far(a, p[i]);
fb[i] = far(b, p[i]);
sm += (fr[i] + fr[i]);
}
ra = sm - fr[0] + fa[0], e = 0, pos = 0;
rb = sm - fr[0] + fb[0];
for (int i = 1; i < n; i++) {
if (ra > sm - fr[i] + fa[i]) e = i, ra = sm - fr[i] + fa[i];
if (rb > sm - fr[i] + fb[i]) pos = i, rb = sm - fr[i] + fb[i];
}
res = min(ra, rb);
y = ra;
w = rb;
for (int i = 0; i < n; i++) {
if (i != pos) ra = min(ra, w - fr[i] + fa[i]);
if (i != e) rb = min(rb, y - fr[i] + fb[i]);
}
res = min(res, min(ra, rb));
cout << setprecision(12) << res << endl;
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int main(){
int a,b,c;
char x;
int r=0,m=0;
while(cin>>a>>x>>b>>x>>c){
if(a*a+b*b==c*c)r++;
if(a==b)m++;
}
cout<<r<<endl
<<m<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n;
long long m;
cin >> n >> m;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long sum = std::accumulate(a.begin(), a.end(), 0);
if (sum < m) {
cout << -1 << endl;
return 0;
}
sort(a.begin(), a.end(), greater<long long>());
for (int i = 1; i <= n; ++i) {
int len = (n + i - 1) / i;
long long sum = 0;
for (int j = 0; j < len; ++j) {
for (int k = 0; k < i; ++k) {
int idx = j * i + k;
if (idx < n) {
sum += max(a[idx] - j, 0LL);
}
}
}
if (sum >= m) {
cout << i << endl;
break;
}
}
return 0;
}
| 4 |
#include<iostream>
int main(){int n;std::cin>>n;std::cout<<(n>=30?"Yes":"No");} | 0 |
#include <bits/stdc++.h>
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
long long ts, tf, t, n, arr[maxn], now, idx, ans, anss;
queue<long long> q;
int main() {
scanf("%lld%lld%lld", &ts, &tf, &t);
scanf("%lld", &n);
for (int i = 0; i < n; i++) scanf("%lld", &arr[i]);
now = ts;
idx = 0;
anss = INF;
while (arr[idx] <= now && idx < n) q.push(arr[idx++]);
while (now < tf) {
while (arr[idx] <= now && idx < n) q.push(arr[idx++]);
if (q.empty() && now + t <= tf) {
anss = 0;
ans = now;
break;
}
if (q.empty()) break;
if (now - q.front() + 1 < anss) {
anss = now - q.front() + 1;
ans = q.front() - 1;
}
q.pop();
now += t;
}
printf("%lld\n", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename TH>
void _dbg(const char* sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <typename TH, typename... TA>
void _dbg(const char* sdbg, TH h, TA... t) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, t...);
}
template <class C>
void mini(C& a4, C b4) {
a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
a4 = max(a4, b4);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> pair) {
return out << "(" << pair.first << ", " << pair.second << ")";
}
template <class A, class B, class C>
struct Triple {
A first;
B second;
C third;
bool operator<(const Triple& t) const {
if (first != t.first) return first < t.first;
if (second != t.second) return second < t.second;
return third < t.third;
}
};
template <class T>
void ResizeVec(T&, vector<long long>) {}
template <class T>
void ResizeVec(vector<T>& vec, vector<long long> sz) {
vec.resize(sz[0]);
sz.erase(sz.begin());
if (sz.empty()) {
return;
}
for (T& v : vec) {
ResizeVec(v, sz);
}
}
template <class A, class B, class C>
ostream& operator<<(ostream& out, Triple<A, B, C> t) {
return out << "(" << t.first << ", " << t.second << ", " << t.third << ")";
}
template <class T>
ostream& operator<<(ostream& out, vector<T> vec) {
out << "(";
for (auto& v : vec) out << v << ", ";
return out << ")";
}
template <class T>
ostream& operator<<(ostream& out, set<T> vec) {
out << "(";
for (auto& v : vec) out << v << ", ";
return out << ")";
}
template <class L, class R>
ostream& operator<<(ostream& out, map<L, R> vec) {
out << "(";
for (auto& v : vec) out << v << ", ";
return out << ")";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
if (0) cout << fixed << setprecision(10);
cin.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> ____left, right;
for (long long i = (1); i <= (n); ++i) {
long long y;
cin >> y;
y += 10000;
____left.push_back(y);
}
for (long long i = (1); i <= (m); ++i) {
long long y;
cin >> y;
y += 10000;
right.push_back(y);
}
long long M = 2e4 + 5;
vector<bitset<123> > bitsets(2 * M);
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (m); ++j) {
bitsets[____left[i] + right[j]][i] = 1;
bitsets[____left[i] + right[j]][n + j] = 1;
}
}
long long best = 0;
sort((bitsets).begin(), (bitsets).end(),
[&](bitset<123>& a, bitset<123>& b) { return a.count() > b.count(); });
for (long long i = 0; i < (((long long)(bitsets).size())); ++i) {
for (long long j = (i + 1); j <= (((long long)(bitsets).size()) - 1); ++j) {
if ((long long)(bitsets[i].count() + bitsets[j].count()) <= best) {
break;
}
maxi(best, (long long)(bitsets[i] | bitsets[j]).count());
}
}
cout << best << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long h, n;
long long level;
int main() {
while (cin >> h >> n) {
long long ans = 0LL;
level = 0LL;
long long tot = 1LL << h;
while (true) {
if (level == h) {
break;
}
if (ans % 2) {
if (n <= tot / 2LL) {
ans += tot;
} else {
ans++;
n -= tot / 2LL;
}
level++;
tot /= 2LL;
} else {
if (n <= tot / 2LL) {
ans++;
} else {
ans += tot;
n -= tot / 2LL;
}
level++;
tot /= 2LL;
}
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 210000;
namespace SegTree {
struct Node {
int val, cnt, addn;
bool operator<(const Node x) const { return val < x.val; }
} tree[MAXN << 2];
Node merge(Node l, Node r) {
l.addn = r.addn = 0;
if (l.val == r.val)
return (Node){l.val, l.cnt + r.cnt, 0};
else
return min(l, r);
}
void add(int nown, int v) { tree[nown].addn += v, tree[nown].val += v; }
void push_down(int nown) {
if (tree[nown].addn != 0) {
add((nown << 1), tree[nown].addn), add((nown << 1 | 1), tree[nown].addn);
tree[nown].addn = 0;
}
}
void build(int nown, int l, int r) {
if (l == r) {
tree[nown] = (Node){0, 1, 0};
} else {
build((nown << 1), l, ((l + r) >> 1));
build((nown << 1 | 1), ((l + r) >> 1) + 1, r);
tree[nown] = merge(tree[(nown << 1)], tree[(nown << 1 | 1)]);
}
}
void update(int nown, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
add(nown, v);
} else {
push_down(nown);
if (ql <= ((l + r) >> 1)) update((nown << 1), l, ((l + r) >> 1), ql, qr, v);
if (qr >= ((l + r) >> 1) + 1)
update((nown << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v);
tree[nown] = merge(tree[(nown << 1)], tree[(nown << 1 | 1)]);
}
}
int query(int n) {
Node tmp = tree[1];
if (tmp.val != 0)
return n;
else
return n - tmp.cnt;
}
} // namespace SegTree
int n, m;
struct Edge {
int to, nex;
} edge[MAXN * 2];
int ecnt = 2;
int fir[MAXN];
void addedge(int a, int b) {
edge[ecnt] = (Edge){b, fir[a]};
fir[a] = ecnt++;
}
int x[MAXN], y[MAXN];
int id[MAXN], siz[MAXN], cnt, back[MAXN];
void dfs0(int nown, int fa) {
id[nown] = ++cnt;
siz[nown] = 1;
back[cnt] = nown;
for (int nowe = fir[nown]; nowe; nowe = edge[nowe].nex) {
int v = edge[nowe].to;
if (v == fa) continue;
dfs0(v, nown);
siz[nown] += siz[v];
}
}
void init() {
scanf("%d %d", &n, &m);
for (int i = 2; i <= n; i++) {
int a, b;
scanf("%d %d", &a, &b);
addedge(a, b), addedge(b, a);
}
}
struct Node {
int x, l, r, v, o;
};
bool cmp(Node a, Node b) { return a.x < b.x; }
vector<Node> v;
void add(int a, int b) {
if (id[a] > id[b]) swap(a, b);
if (id[a] + siz[a] - 1 >= id[b] + siz[b] - 1) {
v.push_back((Node){id[a], id[a], id[a] + siz[a] - 1, 1, 0});
v.push_back((Node){id[a] + siz[a], id[a], id[a] + siz[a] - 1, -1, 0});
} else {
v.push_back((Node){id[a], id[a], id[a] + siz[a] - 1, 1, b});
v.push_back((Node){id[a] + siz[a], id[a], id[a] + siz[a] - 1, -1, b});
v.push_back((Node){id[b], id[b], id[b] + siz[b] - 1, 1, a});
v.push_back((Node){id[b] + siz[b], id[b], id[b] + siz[b] - 1, -1, a});
}
}
int ans[MAXN];
void solve() {
dfs0(1, 0);
SegTree::build(1, 1, n);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x[i], &y[i]);
add(x[i], y[i]);
}
sort(v.begin(), v.end(), cmp);
int L = 0;
for (int i = 1; i <= n; i++) {
while (L < int(v.size()) && v[L].x <= i) {
SegTree::update(1, 1, n, v[L].l, v[L].r, v[L].v);
if (v[L].o != 0) {
int t = v[L].o;
SegTree::update(1, 1, n, id[t], id[t] + siz[t] - 1, v[L].v);
}
L++;
}
ans[back[i]] = SegTree::query(n);
}
for (int i = 1; i <= n; i++) {
printf("%d ", max(0, ans[i] - 1));
}
printf("\n");
}
int main() {
init();
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, ans, ar[200004];
long long freq[200004];
long long pre[200004];
long long get_sum(long long i, long long j) {
if (i == 0) return pre[j];
return pre[j] - pre[i - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> ar[i];
freq[ar[i]]++;
}
for (int i = 1; i < 200004; i++) {
pre[i] = pre[i - 1] + freq[i];
}
sort(ar, ar + n);
for (int i = 1; i < 200004; i++) {
if (freq[i] == 0) continue;
long long res = 0;
for (int j = i; j < 200004; j += i) {
res += j * get_sum(j, min(200004 - 1, j + i - 1));
}
ans = max(ans, res);
}
cout << ans;
return 0;
}
| 6 |
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int m,n,i,j,t,y;
int e[15]={10000};
for(i=0;i<12;i++){
scanf("%d",&e[i]);
}
sort(e,e+12);
if(e[0]==e[1] && e[2]==e[3] && e[4]==e[5] && e[6]==e[7] && e[8]==e[9] && e[10] == e[11]){
printf("yes\n");
}
else{
printf("no\n");
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int maxN = 1100;
int D[maxN][maxN];
int sum[maxN][maxN], sum2[maxN][maxN];
int N, M, K;
int main() {
int i, j, k;
scanf("%d%d%d", &N, &M, &K);
for (i = 1000; i >= 1; i--) {
D[K][i] = 1;
sum[K][i] = 1000 - i + 1 + sum[K][i + 1];
sum2[K][i] = sum2[K][i + 1] + 1;
}
for (i = K - 1; i >= 0; i--) {
D[1][i] = 0;
for (j = 2; j <= 1000; j++) {
D[i][j] = sum[i + 1][1] - sum[i + 1][j - 1];
if (D[i][j] < 0) D[i][j] += mod;
int scad = sum2[i + 1][1] - sum2[i + 1][j - 1];
if (scad < 0) scad += mod;
D[i][j] = D[i][j] - (1LL * (1000 - j + 2) * scad) % mod;
if (D[i][j] < 0) D[i][j] += mod;
}
for (j = 1000; j >= 1; j--) {
sum[i][j] =
(sum[i][j] + 1LL * (1000 - j + 1) * D[i][j] + sum[i][j + 1]) % mod;
sum2[i][j] = (sum2[i][j + 1] + D[i][j]) % mod;
}
}
printf("%lld\n", (1LL * D[0][N] * D[0][M]) % mod);
return 0;
}
| 5 |
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<map>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
int n;
map<ll,int> a;
queue<pii> q;
set<int> l[100010];
int f[100010];
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
ll getid(int x,int y)
{
if(x>y)
swap(x,y);
return ll(x)*n+y;
}
void link(int x,int y)
{
l[x].insert(y);
l[y].insert(x);
ll v=getid(x,y);
a[v]++;
if(a[v]==3)
{
printf("NO\n");
exit(0);
}
if(a[v]==2)
q.push(pii(x,y));
}
int main()
{
scanf("%d",&n);
int i,x,y;
for(i=1;i<=2*n-2;i++)
{
scanf("%d%d",&x,&y);
link(x,y);
}
for(i=1;i<=n;i++)
f[i]=i;
for(i=1;i<n;i++)
{
while(1)
{
if(q.empty())
{
printf("NO\n");
return 0;
}
x=q.front().first;
y=q.front().second;
x=find(x);
y=find(y);
q.pop();
if(x!=y)
break;
}
if(l[x].size()>l[y].size())
swap(x,y);
f[x]=y;
a.erase(getid(x,y));
l[y].erase(x);
for(auto v:l[x])
{
v=find(v);
if(v==y)
continue;
a.erase(getid(x,v));
link(v,y);
l[v].erase(x);
l[x].erase(v);
}
}
printf("YES\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll tc;
cin >> tc;
while (tc--)
{
ll n, ans = LLONG_MAX, pre = 0;
cin >> n;
vector<ll> c(n);
priority_queue<ll, vector<ll>, greater<ll>> epq, opq;
for (ll i = 0; i < n; ++i)
{
cin >> c[i];
i % 2 ? opq.push(c[i]) : epq.push(c[i]);
pre += c[i];
if (i)
ans = min(ans, pre + (n - (ll)epq.size()) * epq.top() + (n - (ll)opq.size()) * opq.top());
}
cout << ans << '\n';
}
return 0;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[6000];
int Fen[6000];
void upd(int x) {
for (; x <= n; x |= (x + 1)) Fen[x]++;
}
int how(int x) {
int a = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) a += Fen[x];
return a;
}
int main() {
cin >> n;
for (int(i) = (0); i < (n); ++(i)) cin >> a[i];
int s = 0, num = 1, pr = 0;
for (int(i) = (0); i < (n); ++(i))
for (int(j) = (i + 1); j < (n); ++(j))
if (a[i] > a[j]) s++;
for (int(i) = (0); i < (n); ++(i)) {
memset(Fen, 0, sizeof(Fen));
int t = 0, delta = 0;
for (int(j) = (i + 1); j < (n); ++(j)) {
if (a[j] < a[i]) {
delta = t - (j - i - t - 1);
int z = how(n) - how(a[j]);
delta += z - (j - i - z - 1);
delta++;
if (delta == pr) num++;
if (delta > pr) {
pr = delta;
num = 1;
}
t++;
}
upd(a[j]);
}
}
cout << s - pr << " " << num;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 10;
long long a[maxn], nxt[maxn], dp[maxn], n, q;
unordered_map<long long, long long> NXT[maxn];
signed main() {
ios::sync_with_stdio(false);
cin >> q;
while (q--) {
cin >> n;
for (long long i = 0; i < n; i++) cin >> a[i];
nxt[n - 1] = -1;
for (long long i = n - 2; ~i; i--) {
if (a[i] == a[i + 1])
nxt[i] = i + 1;
else if (NXT[i + 1].count(a[i]))
nxt[i] = NXT[i + 1][a[i]] + 1;
else
nxt[i] = -1;
if (nxt[i] != -1) {
if (nxt[i] < n - 1) {
swap(NXT[i], NXT[nxt[i] + 1]);
NXT[i][a[nxt[i] + 1]] = nxt[i];
}
dp[i] += dp[nxt[i] + 1] + 1;
}
}
for (long long i = n - 2; ~i; i--) dp[i] += dp[i + 1];
cout << dp[0] << '\n';
for (long long i = 0; i <= n; i++) NXT[i].clear(), dp[i] = 0;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
ifstream fin("AAtest.in.txt");
long long t, n, k, jad[200005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> t;
while (t--) {
cin >> n >> k;
long long ma = -10000000000;
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
cin >> jad[i];
ma = max(ma, jad[i]);
}
long long ma1 = 0;
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
jad[i] = ma - jad[i];
ma1 = max(ma1, jad[i]);
}
if (k % 2) {
for (int(i) = (0); ((i)) < ((n)); ((i))++) cout << jad[i] << " ";
cout << endl;
} else {
for (int(i) = (0); ((i)) < ((n)); ((i))++) cout << ma1 - jad[i] << " ";
cout << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265358979323846;
void debn(vector<int> a) {
for (auto v : a) cout << v << ' ';
cout << endl;
}
void debnn(vector<vector<int>> a) {
for (auto v : a) {
for (auto w : v) cout << w << ' ';
cout << endl;
}
}
long long powmod(long long x, long long y, long long mod) {
long long res = 1;
while (y) {
if (y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, m, h;
cin >> n >> m >> h;
vector<vector<int>> a;
a.assign(n, vector<int>(m));
vector<int> b, c;
b.resize(m);
c.resize(n);
for (int i = 0; i < m; i++) cin >> b[i];
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] == 1) a[i][j] = min(b[j], c[i]);
debnn(a);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
if (n > m) {
swap(n, m);
}
if (n == 1) {
long long mod = m % 6;
if (mod <= 3) {
cout << (m / 6) * 6 << endl;
} else {
cout << (m / 6) * 6 + 2 * (mod - 3) << endl;
;
}
} else if (n == 2) {
if (m == 2) {
cout << 0 << endl;
} else if (m % 4 == 3) {
if (m > 7) {
cout << m * n << endl;
} else {
cout << m * n - 2 << endl;
}
} else {
cout << m * n << endl;
}
} else if (m % 2 == 1 && n % 2 == 1) {
cout << m * n - 1 << endl;
} else {
cout << m * n << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
const long double pi = acos(-1.0);
int n, a[200200];
long long sum[200200];
int read() {
int x = 0, f = 1;
char ch = getchar();
while ((ch < '0') || (ch > '9')) {
if (ch == '-') f = -1;
ch = getchar();
}
while ((ch >= '0') && (ch <= '9')) {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * f;
}
int main() {
n = read();
for (register int i = 1; i <= n; i++) {
a[i] = read();
sum[i] = sum[i - 1] + a[i];
}
long long ans = sum[n];
for (register int i = n - 1; i >= 2; i--) ans = max(ans, sum[i] - ans);
printf("%lld", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long n, k, l, mod;
struct matrix {
long long m[2][2];
matrix() { memset(m, 0, sizeof(m)); }
matrix operator*(matrix const& other) {
matrix res;
for (long long i = 0; i <= 1; i++) {
for (long long j = 0; j <= 1; j++) {
for (long long k = 0; k <= 1; k++)
res.m[i][j] = (res.m[i][j] + m[i][k] * other.m[k][j]) % mod;
}
}
return res;
}
} unit, base;
matrix mpower(matrix a, long long n) {
matrix res = unit;
while (n > 0) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
long long power(long long x, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
unit.m[0][0] = unit.m[1][1] = 1;
base.m[0][1] = base.m[1][0] = base.m[1][1] = 1;
cin >> n >> k >> l >> mod;
long long r2 = mpower(base, n + 2).m[0][1];
long long r1 = (((power(2, n) - r2) % mod) + mod) % mod;
if (l < 63 && (1ll << l) <= k) {
cout << 0;
return 0;
}
long long ans = 1;
for (long long i = 0; i <= l - 1; i++) {
if (k & (1ll << i))
ans = ans * r1 % mod;
else
ans = ans * r2 % mod;
}
cout << ans % mod;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n, m;
bool flag[26];
char color;
std::cin >> n >> m >> color;
int i, j, count = 0;
for (i = 0; i < 26; i++) flag[i] = false;
char plan[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) std::cin >> plan[i][j];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (plan[i][j] == color) {
if (i - 1 >= 0) {
if ((plan[i - 1][j] != color && plan[i - 1][j] != '.') &&
!flag[plan[i - 1][j] - 'A']) {
count++;
flag[plan[i - 1][j] - 'A'] = true;
}
}
if (j - 1 >= 0) {
if ((plan[i][j - 1] != color && plan[i][j - 1] != '.') &&
!flag[plan[i][j - 1] - 'A']) {
count++;
flag[plan[i][j - 1] - 'A'] = true;
}
}
if (i + 1 < n) {
if ((plan[i + 1][j] != color && plan[i + 1][j] != '.') &&
!flag[plan[i + 1][j] - 'A']) {
count++;
flag[plan[i + 1][j] - 'A'] = true;
}
}
if (j + 1 < m) {
if ((plan[i][j + 1] != color && plan[i][j + 1] != '.') &&
!flag[plan[i][j + 1] - 'A']) {
count++;
flag[plan[i][j + 1] - 'A'] = true;
}
}
}
}
}
std::cout << count << std::endl;
return 0;
}
| 2 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
pair<ll,ll> a[100010];
ll cls[100010];
int main(){
int n;ll t;
cin>>t>>n;
ll sum=0;cls[0]=0;
a[0]=make_pair(-1,-1);
for(int i=1;i<=n;i++){
cin>>a[i].first>>a[i].second;
sum+=a[i].second-a[i].first;
cls[i]=sum;
}
ll ans=0;
lol(i,n){
ll to=t+a[i+1].first;
int left=-1,right=n+1,mid;
while(left+1<right){
mid=(left+right)/2;
if(a[mid].second<=to)left=mid;
else right=mid;
}
ll tmp=cls[left]-cls[i];
if(left<n)tmp+=max((ll)0,to-a[left+1].first);
ans=max(tmp,ans);
}
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
if(n%2){
for(int i=1,j=2*m;m>0&&i<j;i++,j--){
cout<<i<<" "<<j<<endl;
m--;
}
}else{
for(int i=1,j=n/2;m>0&&i<j;i++,j--){
cout<<i<<" "<<j<<endl;
m--;
}
for(int i=n/2+1,j=n-1;m>0&&i<j;i++,j--){
cout<<i<<" "<<j<<endl;
m--;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> cost;
int n;
pair<int, int> a[200001];
int main() {
int i = 0;
for (;;) {
int tmp = i * i;
cost.push_back(tmp);
if (tmp > 1000000000) break;
++i;
}
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
int tar = lower_bound(cost.begin(), cost.end(), x) - cost.begin();
if (cost[tar] == x) {
if (x == 0)
a[i].first = 0, a[i].second = 2;
else
a[i].first = 0, a[i].second = 1;
} else {
assert(tar);
a[i].first = min(cost[tar] - x, x - cost[tar - 1]), a[i].second = 0;
}
}
sort(a, a + n, [&](const pair<int, int> &a, const pair<int, int> &b) {
return a.first - a.second < b.first - b.second;
});
long long ans = 0;
for (int i = 0; i < n / 2; ++i) {
ans += a[i].first;
}
for (int i = n / 2; i < n; ++i) {
ans += a[i].second;
}
printf("%lld\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, t1, t2, a[100005], b[100005];
set<long long> g[100005];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (long long i = 1; i <= m; ++i) {
cin >> t1 >> t2;
g[t1].insert(t2);
g[t2].insert(t1);
}
for (long long i = 1; i <= n; ++i) {
for (long long j = 1; j <= n; ++j) {
if (i == j) continue;
if (!g[i].count(j)) {
cout << "YES\n";
long long k = 0;
for (long long z = 1; z <= n; ++z) {
if (i != z && j != z) {
++k;
a[z] = k;
b[z] = k;
}
}
a[i] = k + 1;
a[j] = k + 2;
b[i] = k + 1;
b[j] = k + 1;
for (long long z = 1; z <= n; ++z) cout << a[z] << ' ';
cout << '\n';
for (long long z = 1; z <= n; ++z) cout << b[z] << ' ';
return 0;
}
}
}
cout << "NO\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 3e5 + 7;
long long a, b;
long long ans = llINF;
set<long long> s;
int main(int argc, char const *argv[]) {
cin >> a >> b;
long long c = a + b;
long long lim = sqrt(c);
for (int i = 1; i <= lim; i++) {
if (a % i == 0 && i <= (a / i)) s.insert(a / i);
if (b % i == 0 && i <= (b / i)) s.insert(b / i);
if (c % i == 0 && (c / i) >= *s.begin()) ans = min(ans, 2 * (i + c / i));
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long n, k, m;
long a[1010], b[1010];
int main() {
scanf("%ld %ld", &n, &k);
for (long i = 1; i <= n; i++) scanf("%ld", &b[i]);
for (long i = n; i >= 1; i--) {
m++;
long dem = 0;
for (long j = 1; j <= m; j++) {
if (dem == b[i]) {
for (long t = m; t >= j + 1; t--) a[t] = a[t - 1];
a[j] = i;
break;
}
if (a[j] >= i + k) dem++;
}
}
for (long i = 1; i <= n; i++) printf("%ld ", a[i]);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int mxm = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a[n], i;
set<long long int> x;
for (i = 0; i < n; i++) {
cin >> a[i];
x.insert(i + 1);
}
sort(a, a + n);
for (i = 0; i < n; i++) {
if (x.count(a[i])) {
x.erase(a[i]);
a[i] = 0;
}
}
long long int ans = 0;
sort(a, a + n);
for (i = 0; i < n; i++) {
if (a[i] > 0) {
if (2 * (*x.begin()) < a[i]) {
x.erase(x.begin());
ans++;
} else {
ans = -1;
break;
}
}
}
cout << (ans) << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, Q;
vector<int> g[N];
int in[N], out[N];
int T;
int D[N];
int P[N][22];
void DFS(int k, int p) {
in[k] = ++T;
P[k][0] = p;
D[k] = D[p] + 1;
for (int i = 0; i < g[k].size(); i++)
if (g[k][i] != p) DFS(g[k][i], k);
out[k] = ++T;
}
int LCA(int u, int v) {
if (in[u] <= in[v] && out[v] <= out[u]) return u;
if (in[v] <= in[u] && out[u] <= out[v]) return v;
int I = u;
for (int i = 19; i >= 0; i--) {
if (P[I][i] == 0) continue;
if (in[P[I][i]] <= in[v] && out[v] <= out[P[I][i]]) continue;
I = P[I][i];
}
return P[I][0];
}
int main() {
cin >> n >> Q;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
g[i].push_back(p);
g[p].push_back(i);
}
DFS(1, 0);
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n; j++) P[j][i] = P[P[j][i - 1]][i - 1];
while (Q--) {
int a, b, c;
cin >> a >> b >> c;
int x, y, z;
x = LCA(b, c);
y = LCA(a, c);
z = LCA(a, b);
if (x == z) {
swap(b, c);
swap(y, z);
}
if (y == z) {
swap(a, c);
swap(x, z);
}
cout << ((((D[a] - D[z] + 1) > (D[b] - D[z] + 1) ? (D[a] - D[z] + 1)
: (D[b] - D[z] + 1))) >
(D[z] + D[c] - 2 * D[x] + 1)
? (((D[a] - D[z] + 1) > (D[b] - D[z] + 1) ? (D[a] - D[z] + 1)
: (D[b] - D[z] + 1)))
: (D[z] + D[c] - 2 * D[x] + 1))
<< endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, u, v, l, k, used[100001], a[100001], w, ans = -1;
vector<pair<long long, long long> > g[100001];
int main() {
cin >> n >> m >> k;
for (i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &l);
g[u].push_back(make_pair(v, l));
g[v].push_back(make_pair(u, l));
}
for (i = 0; i < k; i++) {
scanf("%d", &a[i]);
used[a[i]] = 1;
}
for (i = 0; i < k; i++) {
for (j = 0; j < g[a[i]].size(); j++) {
w = g[a[i]][j].second;
v = g[a[i]][j].first;
if (used[v] == 0)
if (ans == -1)
ans = w;
else
ans = min(ans, w);
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, ans;
long long a[200005], d;
int main() {
scanf("%d", &n);
for (i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
for (k = 1; k <= n;) {
++ans;
for (i = k; a[i] == -1; ++i)
;
for (j = i + 1; a[j] == -1; ++j)
;
if (j > n) break;
if ((a[j] - a[i]) % (j - i) != 0) {
k = j;
continue;
}
d = (a[j] - a[i]) / (j - i);
if (a[j] - d * (j - k) <= 0) {
k = j;
continue;
}
for (k = j + 1; k <= n && a[j] + d * (k - j) > 0 &&
(a[k] == -1 || a[k] == a[j] + d * (k - j));
++k)
;
}
printf("%d\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g(200200);
int n, d, h;
int main() {
cin >> n;
cin >> d;
cin >> h;
if (n == 2) {
if (h == d && d == 1) {
cout << "1 2" << endl;
return 0;
} else {
cout << -1 << endl;
return 0;
}
}
if (d > 2 * h || d == 1) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i <= h; i++) {
cout << i << " " << i + 1 << endl;
}
if (d > h) {
cout << 1 << " " << h + 2 << endl;
for (int i = h + 2; i <= d; i++) {
cout << i << " " << i + 1 << endl;
}
}
int v = 2;
if (h == 1) {
v = 1;
}
for (int i = d + 2; i <= n; i++) {
cout << v << " " << i << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, mi, ro, t;
int si[100010], L[100010], R[100010], id[100010], tr[100010 * 4], wei[100010];
pair<int, int> c[100010 * 4];
vector<int> root;
vector<pair<int, int> > a[100010], b[100010];
long long an;
void dfs(int x, int y) {
int i, ma = 0;
si[x] = 1;
for (i = 0; i < a[x].size(); i++) {
pair<int, int> A = a[x][i];
if (A.first == y) continue;
dfs(A.first, x);
int B = si[A.first];
an += 2ll * A.second * min(B, n - B);
si[x] += B;
ma = max(ma, B);
}
ma = max(ma, n - si[x]);
if (ma < mi)
mi = ma, root.clear(), root.push_back(x);
else if (ma == mi)
root.push_back(x);
}
void Dfs(int x, int y, int z) {
int i;
L[x] = ++t;
id[x] = z;
wei[t] = x;
si[x] = 1;
for (i = 0; i < a[x].size(); i++) {
pair<int, int> A = a[x][i];
if (A.first == y) continue;
Dfs(A.first, x, !z ? A.first : z);
si[x] += si[A.first];
}
R[x] = t;
}
inline void update(int x) {
int y = x * 2, z = x * 2 + 1;
tr[x] = min(tr[y], tr[z]);
c[x] = max(c[y], c[z]);
}
void build(int q, int x, int y) {
if (x == y) {
tr[q] = wei[x];
if (id[x] == x) c[q] = make_pair(si[x] * 2, x);
return;
}
int mid = (x + y) / 2;
build(q * 2, x, mid);
build(q * 2 + 1, mid + 1, y);
update(q);
}
void into(int q, int x, int y, int k) {
if (x == y) {
tr[q] = 100010;
return;
}
int mid = (x + y) / 2;
if (k <= mid)
into(q * 2, x, mid, k);
else
into(q * 2 + 1, mid + 1, y, k);
update(q);
}
int cal(int q, int x, int y, int l, int r) {
if (l > r) return 100010;
if (l <= x && y <= r) return tr[q];
int mid = (x + y) / 2, mi = 100010;
if (l <= mid) mi = min(mi, cal(q * 2, x, mid, l, r));
if (mid < r) mi = min(mi, cal(q * 2 + 1, mid + 1, y, l, r));
return mi;
}
void jian(int q, int x, int y, int k) {
if (x == y) {
c[q].first--;
return;
}
int mid = (x + y) / 2;
if (k <= mid)
jian(q * 2, x, mid, k);
else
jian(q * 2 + 1, mid + 1, y, k);
update(q);
}
pair<int, int> Cal(int q, int x, int y, int l, int r) {
if (l > r) return make_pair(0, 0);
if (l <= x && y <= r) return c[q];
int mid = (x + y) / 2;
pair<int, int> ma;
ma = make_pair(0, 0);
if (l <= mid) ma = max(ma, Cal(q * 2, x, mid, l, r));
if (mid < r) ma = max(ma, Cal(q * 2 + 1, mid + 1, y, l, r));
return ma;
}
int main() {
scanf("%d", &n);
if (n == 1) {
printf("0\n1\n");
return 0;
}
for (i = 1; i <= n - 1; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[x].push_back(make_pair(y, z));
a[y].push_back(make_pair(x, z));
}
mi = 100010;
dfs(1, 0);
printf("%I64d\n", an);
ro = root[0];
Dfs(ro, 0, 0);
for (i = 1; i <= n; i++)
if (i == ro) {
if (root.size() == 2) {
int A = root[1];
b[i].push_back(make_pair(L[A], R[A]));
} else
b[i].push_back(make_pair(1, n));
} else {
int A = id[i];
b[i].push_back(make_pair(1, L[A] - 1));
b[i].push_back(make_pair(R[A] + 1, n));
}
build(1, 1, n);
for (i = 1; i <= n; i++) {
int mi = 100010;
for (j = 0; j < b[i].size(); j++) {
pair<int, int> A = b[i][j];
mi = min(mi, cal(1, 1, n, A.first, A.second));
}
int F = 0;
{
pair<int, int> A;
if (id[i])
A = max(Cal(1, 1, n, 1, id[i] - 1), Cal(1, 1, n, id[i] + 1, n));
else
A = c[1];
int sh = n - i + 1;
if (sh == A.first) mi = cal(1, 1, n, L[A.second], R[A.second]), F = 1;
}
printf("%d ", mi);
into(1, 1, n, L[mi]);
if (id[mi]) jian(1, 1, n, id[mi]);
if (id[i]) jian(1, 1, n, id[i]);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 10;
int n, m;
int bs;
int a[MAXN];
int cnt[MAXN];
struct Query {
int l, r, id;
bool operator<(const Query& b) const {
int b1 = l / bs, b2 = b.l / bs;
if (b1 != b2)
return b1 < b2;
else
return (b1 & 1) ? r > b.r : r < b.r;
}
} qr[MAXN];
int ans[MAXN];
int bsa, nba;
int bn1[MAXN];
inline void add(int i) {
cnt[a[i]]++;
if (cnt[a[i]] == 1) {
bn1[a[i] / bsa]++;
} else if (cnt[a[i]] == 2) {
bn1[a[i] / bsa]--;
}
}
inline void sub(int i) {
cnt[a[i]]--;
if (cnt[a[i]] == 1) {
bn1[a[i] / bsa]++;
} else if (cnt[a[i]] == 0) {
bn1[a[i] / bsa]--;
}
}
void solve() {
int l = 1, r = 0;
for (int i = 0; i < m; i++) {
while (r < qr[i].r) add(++r);
while (r > qr[i].r) sub(r--);
while (l < qr[i].l) sub(l++);
while (l > qr[i].l) add(--l);
for (int j = 0; j <= nba; j++) {
if (bn1[j] > 0) {
for (int k = j * bsa; k < (j + 1) * bsa; k++) {
if (cnt[k] == 1) {
ans[qr[i].id] = k;
goto ct;
}
}
}
}
ans[qr[i].id] = 0;
ct:;
}
}
int main() {
scanf("%d", &n);
int tmax = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
tmax = max(tmax, a[i]);
}
bsa = sqrt(tmax);
nba = tmax / bsa + 1;
scanf("%d", &m);
bs = sqrt(m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &qr[i].l, &qr[i].r);
qr[i].id = i;
}
sort(qr, qr + m);
solve();
for (int i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
long long qpow(long long x, long long k) {
return k == 0 ? 1 : 1ll * qpow(1ll * x * x % M, k >> 1) * (k & 1 ? x : 1) % M;
}
const int maxn = 1e3 + 5;
int val, n;
int findcol(int row) {
int l = 1, h = n, col;
while (l <= h) {
int mid = (l + h) / 2;
cout << "? " << row << " " << 1 << " " << row << " " << mid << endl;
cin >> val;
if (val & 1) {
col = mid;
h = mid - 1;
} else
l = mid + 1;
}
return col;
}
int findrow(int col) {
int l = 1, h = n, row;
while (l <= h) {
int mid = (l + h) / 2;
cout << "? " << 1 << " " << col << " " << mid << " " << col << endl;
cin >> val;
if (val & 1) {
row = mid;
h = mid - 1;
} else
l = mid + 1;
}
return row;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
int val;
vector<int> row, col;
for (int i = 1; i <= n; i++) {
cout << "? " << i << " " << 1 << " " << i << " " << n << endl;
cin >> val;
if (val & 1) {
row.push_back(i);
}
}
if (row.size() == 0) {
for (int i = 1; i <= n; i++) {
cout << "? " << 1 << " " << i << " " << n << " " << i << endl;
cin >> val;
if (val & 1) {
col.push_back(i);
}
}
int row1 = findrow(col[0]);
cout << "! " << row1 << " " << col[0] << " " << row1 << " " << col[1]
<< endl;
} else {
int col1 = findcol(row[0]);
int col2 = findcol(row[1]);
cout << "! " << row[0] << " " << col1 << " " << row[1] << " " << col2
<< endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> v(n);
long long int ans = 0, c = 0;
for (long long int i = 0; i < n; i++) cin >> v[i];
for (long long int i = 0; i < n; i++) {
c++;
if (v[i] <= c)
continue;
else {
ans += v[i] - c;
c = v[i];
}
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 2;
long long inf = 1ll << 60;
void solve() {
long long n;
cin >> n;
long long x[n];
for (long long i = 0; i < n; i++) {
cin >> x[i];
}
long long ma = 0, ans = 0;
for (long long i = 0; i < n; i++) {
if (x[i] >= ma) {
if (i and x[i - 1] != ma) ans += ma - x[i - 1];
ma = x[i];
} else {
if (x[i] > x[i - 1]) ans += x[i] - x[i - 1];
}
}
if (x[n - 1] != ma) ans += ma - x[n - 1];
cout << ans << '\n';
}
int main() {
long long t;
cin >> t;
while (t--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long, long long>;
const long long MOD = 1e9 + 7;
const long long INF = 1 << 29;
const long long N = 1e6 + 10;
const long long MOD1 = 1073741824;
struct IOsetup {
IOsetup() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
}
} IO;
const long long MAX_N = 1e3 + 10;
bool g[MAX_N][MAX_N];
long long n, m;
bool check(long long r, long long c) {
bool ok = g[r][c];
ok &= g[r + 1][c + 1];
ok &= g[r][c + 1];
ok &= g[r + 1][c];
return ok;
}
int32_t main() {
long long k;
cin >> n >> m >> k;
for (long long i = 0; i < k; i++) {
long long x, y;
cin >> x >> y;
g[x][y] = true;
if (check(x, y - 1) || check(x, y) || check(x - 1, y) ||
check(x - 1, y - 1)) {
cout << i + 1 << endl;
exit(0);
}
}
cout << 0 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> a[12], b[12];
set<int> the;
set<int> ca[12], cb[12];
int main(void) {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[i] = make_pair(x, y);
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
b[i] = make_pair(x, y);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i].first == b[j].first && a[i].second != b[j].second) {
the.insert(a[i].first);
ca[i].insert(a[i].first);
cb[j].insert(a[i].first);
} else if (a[i].first == b[j].second && a[i].second != b[j].first) {
the.insert(a[i].first);
ca[i].insert(a[i].first);
cb[j].insert(a[i].first);
} else if (a[i].first != b[j].first && a[i].second == b[j].second) {
the.insert(a[i].second);
ca[i].insert(a[i].second);
cb[j].insert(a[i].second);
} else if (a[i].first != b[j].second && a[i].second == b[j].first) {
the.insert(a[i].second);
ca[i].insert(a[i].second);
cb[j].insert(a[i].second);
}
}
}
if (the.size() == 0) {
cout << -1 << endl;
return 0;
}
if (the.size() == 1) {
cout << *the.begin() << endl;
return 0;
}
bool knows = true;
for (int i = 0; i < n; i++) {
if (ca[i].size() > 1) {
knows = false;
break;
}
}
for (int j = 0; j < m; j++) {
if (cb[j].size() > 1) {
knows = false;
break;
}
}
cout << (knows ? 0 : -1) << endl;
return 0;
}
| 4 |
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x) (x).begin(),(x).end()
# define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
# define mp make_pair
# define eb emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n) FOR(i,0,n)
# define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
int n, p[101], q[101], r[101], b[101];
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a%b);
}
long long lcm(long long a, long long b) {
long long g = gcd(a, b);
return a / g * b;
}
int check[200];
int calc(int i) {
int rc = r[i] ? calc(r[i]) : 1;
int bc = b[i] ? calc(b[i]) : 1;
int num = lcm(p[i] * rc, q[i] * bc);
return num / p[i] + num / q[i];
}
int main() {
while (cin >> n&&n) {
for (int i = 0; i < 200; i++)check[i] = 0;
for (int i = 0; i < 101; i++)p[i] = q[i] = r[i] = b[i] = 0;
for (int i = 1; i <= n; i++) {
cin >> p[i] >> q[i] >> r[i] >> b[i];
check[r[i]] = check[b[i]] = true;
}
for (int i = 1; i <= n; i++) {
if (!check[i]) {
cout << calc(i) << endl;
break;
}
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char b; cin >> b;
cout << (b == 'A' ? 'T' : b == 'C' ? 'G' : b == 'G' ? 'C' : b == 'T' ? 'A' : ' ');
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pi>;
using si = set<int>;
using sl = set<ll>;
using qi = queue<int>;
using ql = queue<ll>;
template <class T>
bool uin(T& a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T& a, T b) {
return a < b ? (a = b, true) : false;
}
template <class T>
bool uin(T& u, T& v, T a, T b) {
return v - u > b - a ? (u = a, v = b, true) : false;
}
template <class T>
bool uax(T& u, T& v, T a, T b) {
return v - u < b - a ? (u = a, v = b, true) : false;
}
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
void re(ld& x) {
string t;
re(t);
x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
template <class T>
void re(complex<T>& x) {
T a, b;
re(a, b);
x = cd(a, b);
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.f, p.s);
}
template <class T>
void re(vector<T>& a) {
for (int i = 0; i < (((int)(a).size())); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = 0; i < (SZ); ++i) re(a[i]);
}
} // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
} // namespace output
using namespace output;
inline int bs(bool (*valid)(int), int l, int r, bool order) {
l--, r++;
if (!order) swap(l, r);
while (abs(r - l) > 1) {
int mid = (l + r) >> 1;
if (valid(mid))
r = mid;
else
l = mid;
}
valid(r);
return r;
}
struct dsu {
vector<int> p;
dsu(int n) { p.resize(n + 1); }
inline int get(int x) { return p[x] ? p[x] = get(p[x]) : x; }
inline bool mrg(int x, int y) {
return get(x) == get(y) ? false : p[get(x)] = get(y);
}
};
const int M = 1e9 + 7;
const ll lnf = 1e18 + 3;
const int inf = 1e9 + 3;
const int nax = 2e5 + 5;
typedef decay<decltype(M)>::type T;
struct mi {
T val;
explicit operator T() const { return val; }
mi() { val = 0; }
mi(const ll& v) {
val = (-M <= v && v <= M) ? v : v % M;
if (val < 0) val += M;
}
friend void pr(const mi& a) { pr(a.val); }
friend void re(mi& a) {
ll x;
re(x);
a = mi(x);
}
friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; }
friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; }
mi operator-() const { return mi(-val); }
mi& operator+=(const mi& m) {
if ((val += m.val) >= M) val -= M;
return *this;
}
mi& operator-=(const mi& m) {
if ((val -= m.val) < 0) val += M;
return *this;
}
mi& operator*=(const mi& m) {
val = (ll)val * m.val % M;
return *this;
}
friend mi pow(mi a, ll p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend mi inv(const mi& a) {
assert(a != 0);
return pow(a, M - 2);
}
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
vi invs, fac, ifac;
void genFac(int SZ) {
invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ);
invs[1] = fac[0] = ifac[0] = 1;
for (int i = (2); i <= (SZ - 1); ++i)
invs[i] = M - (ll)M / i * invs[M % i] % M;
for (int i = (1); i <= (SZ - 1); ++i) {
fac[i] = (ll)fac[i - 1] * i % M;
ifac[i] = (ll)ifac[i - 1] * invs[i] % M;
}
}
ll comb(int a, int b) {
if (a < b || b < 0) return 0;
return (ll)fac[a] * ifac[b] % M * ifac[a - b] % M;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int test_case;
cin >> test_case;
while (test_case--) {
int n;
re(n);
vi a(n, 0);
re(a);
int q = 0, p = n - 1;
while (a[q] >= q) q++;
q--;
while (a[p] >= n - 1 - p) p--;
p++;
ps(q >= p ? "YES" : "NO");
}
}
| 2 |
#include<cstdio>
#include<vector>
using namespace std;
int n;
const int MAXN=1e5+5;
vector<int>neigh[MAXN];
int cnt[MAXN],L[MAXN],P[MAXN];
void Traverse(int v){
cnt[v]=1;
for(int i=0;i<neigh[v].size();i++){
int u=neigh[v][i];
if(P[v]==u)continue;
L[u]=L[v]+1;P[u]=v;
Traverse(u);
cnt[v]+=cnt[u];
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int a,b;scanf("%d%d",&a,&b);
neigh[a].push_back(b);
neigh[b].push_back(a);
}
Traverse(1);
int up=(L[n]-1)/2;
int v=n;
while(up--)v=P[v];
if(cnt[v]>=n-cnt[v])printf("Snuke\n");
else printf("Fennec\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 222222;
char ss[maxn];
int cnt[33], tot, Q[33];
int main() {
scanf("%s", ss + 1);
int N = strlen(ss + 1);
for (int i = 1; i <= N; ++i) ++cnt[ss[i] - 'a'];
for (int i = 0; i < 26; ++i)
if (cnt[i] & 1) Q[++tot] = i;
int uuu = tot >> 1;
for (int i = 1; i <= uuu; ++i) ++cnt[Q[i]], --cnt[Q[tot - i + 1]];
if (tot & 1)
--cnt[uuu = Q[tot - uuu]];
else
uuu = -1;
for (int i = 0; i < 26; ++i)
for (int j = cnt[i] >> 1; j; --j) putchar('a' + i);
if (~uuu) putchar('a' + uuu);
for (int i = 25; ~i; --i)
for (int j = cnt[i] >> 1; j; --j) putchar('a' + i);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T x) {
return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<T> &x) {
s << "[";
for (auto it : x) {
s << it << ", ";
}
s << "]";
return s;
}
template <typename T>
ostream &operator<<(ostream &s, const set<T> &x) {
s << "{";
for (auto it : x) {
s << it << ", ";
}
s << "}";
return s;
}
template <typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
s << "(" << x.first << ", " << x.second << ")";
return s;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
int main() {
int n, k;
cin >> n >> k;
if (n == 4 || k < n + 1) {
cout << -1 << "\n";
return 0;
}
int a, b, c, d;
cin >> a >> b >> c >> d;
set<int> used = {a, b, c, d};
vector<int> line;
for (int i = 1; i <= n; i++) {
if (used.count(i) == 0) {
line.push_back(i);
}
}
cout << a << " " << c << " ";
for (auto x : line) cout << x << " ";
cout << d << " " << b << "\n";
cout << c << " " << a << " ";
for (auto x : line) cout << x << " ";
cout << b << " " << d << "\n";
return 0;
}
| 2 |
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main(){
long long x,k,d,s;
cin>>x>>k>>d;
x=abs(x);
s=min(k,x/d);
if((k-s)%2==0){
cout<<x-s*d<<endl;
}else{
cout<<(1+s)*d-x<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500000;
const int MOD = int(1E9) + 7;
struct mint {
int x;
mint(int _x = 0) : x(_x) {}
friend mint operator+(mint a, mint b) {
return a.x + b.x >= MOD ? a.x + b.x - MOD : a.x + b.x;
}
friend mint operator-(mint a, mint b) {
return a.x - b.x < 0 ? a.x - b.x + MOD : a.x - b.x;
}
friend mint operator*(mint a, mint b) { return 1LL * a.x * b.x % MOD; }
friend void operator+=(mint &a, mint b) { a = a + b; }
friend void operator-=(mint &a, mint b) { a = a - b; }
friend void operator*=(mint &a, mint b) { a = a * b; }
};
const mint INV2 = (MOD + 1) >> 1;
mint ipw2[MAXN + 10];
void init() {
ipw2[0] = 1;
for (int i = 1; i <= MAXN + 5; i++) ipw2[i] = ipw2[i - 1] * INV2;
}
struct edge {
int to;
edge *nxt;
} edges[2 * MAXN + 5], *adj[MAXN + 5], *ecnt = edges;
void addedge(int u, int v) {
edge *p = (++ecnt);
p->to = v, p->nxt = adj[u], adj[u] = p;
p = (++ecnt);
p->to = u, p->nxt = adj[v], adj[v] = p;
}
mint sum[MAXN + 5], deg[MAXN + 5], siz[MAXN + 5], all;
vector<int> v[MAXN + 5];
int n, m, tot;
int dfn[MAXN + 5], stk[MAXN + 5], dcnt, tp;
void dfs(int x, int f) {
dfn[x] = (++dcnt), stk[++tp] = x;
for (edge *p = adj[x]; p; p = p->nxt) {
if (p->to == f) continue;
if (!dfn[p->to])
dfs(p->to, x);
else if (dfn[p->to] < dfn[x]) {
tot++;
for (int i = tp;; i--) {
v[tot].push_back(stk[i]);
if (stk[i] == p->to) break;
}
siz[tot] = v[tot].size(), all += ipw2[v[tot].size()];
for (int i = 0; i < v[tot].size(); i++)
sum[v[tot][i]] += ipw2[v[tot].size()];
}
}
stk[tp--] = 0;
}
mint get1() {
mint ret = 0, cnt = 1LL * m * (m - 1) % MOD;
for (int i = 1; i <= n; i++) {
ret += ipw2[1] + (n - 1) * ipw2[2];
ret -= (deg[i] * ipw2[2] + (m - deg[i]) * ipw2[3]) * 2;
ret += deg[i] * (deg[i] - 1) * ipw2[3];
cnt -= deg[i] * (deg[i] - 1);
}
ret += cnt * ipw2[4] + m * ipw2[2];
for (int i = 1; i <= tot; i++) {
int s = v[i].size();
ret += (siz[i] * ipw2[s] + (n - siz[i]) * ipw2[s + 1]) * 2;
mint p = m - s, q = all - ipw2[s];
for (int j = 0; j < v[i].size(); j++) {
int x = v[i][j];
ret -= (deg[x] - 2) * ipw2[s + 1] * 2;
p -= deg[x] - 2;
ret += (sum[x] - ipw2[s]) * ipw2[s - 1];
q -= (sum[x] - ipw2[s]);
}
ret -= (s * ipw2[s] + p * ipw2[s + 2]) * 2;
ret += ipw2[s] + q * ipw2[s];
}
return ret;
}
mint get2() {
mint ret = 0;
ret += n * ipw2[1];
ret -= m * ipw2[2];
for (int i = 1; i <= tot; i++) ret += ipw2[v[i].size()];
return ret;
}
int main() {
init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v), deg[u] += 1, deg[v] += 1;
}
dfs(1, 0);
mint x = get1(), y = get2();
printf("%d\n", (x - y * y).x);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
cin >> tc;
while (tc--) {
long long n, m;
cin >> n >> m;
long long cnt = n / m, tmp = 0, sum = 0;
set<long long> st;
vector<int> v;
for (int i = 0; i < n; i++) {
tmp += m;
tmp %= 10;
if (st.find(tmp) != st.end()) break;
v.push_back(tmp);
st.insert(tmp);
sum += tmp;
}
long long ans = (cnt / (1LL * st.size())) * sum;
cnt = (cnt % (1LL * st.size()));
for (int i = 0; i < cnt; i++) ans += v[i];
cout << ans << endl;
}
}
| 3 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[1100];
bool b[1100000];
int main()
{
int n;
cin>>n;
memset(b,0,sizeof(b));
long long maxx=999999;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[a[i]]=1;
}
int l=0;
for(int i=1;i<=n;i++)
{
int x=a[i];
for(int j=i+1;j<=n;j++)
{
x*=10;
x+=a[j];
if(x>999999)
break;
b[x]=1;
}
}
for(int i=0;i<=maxx;i++)
{
if(!b[i])
{
cout<<i<<endl;
break;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& a, C b) {
a = min(a, b);
}
template <class C>
void maxi(C& a, C b) {
a = max(a, b);
}
const long long mod = 1e9 + 7;
struct nodo {
void upd(int val, int x, int y) { v = val; }
nodo comb(nodo& izq, nodo& der) {
v = max(izq.v, der.v);
return *this;
}
int v;
};
struct segmentTreeLazy {
segmentTreeLazy(int _n) {
n = _n;
tree = vector<nodo>(4 * n);
}
void build(vector<int>& a) { build(a, 1, 0, n); }
void build(vector<int>& a, int v, int l, int r) {
if (l + 1 == r) {
tree[v].upd(a[l], l, r);
return;
}
int m = (l + r) / 2;
build(a, 2 * v, l, m);
build(a, 2 * v + 1, m, r);
tree[v].comb(tree[2 * v], tree[2 * v + 1]);
}
void upd(int i, int val) { upd(i, val, 1, 0, n); }
void upd(int i, int val, int v, int x, int y) {
if (i < x or y <= i) return;
if (x + 1 == y) {
tree[v].upd(val, x, y);
return;
}
int m = (x + y) / 2;
upd(i, val, 2 * v, x, m);
upd(i, val, 2 * v + 1, m, y);
tree[v].comb(tree[2 * v], tree[2 * v + 1]);
}
nodo get(int l, int r) { return get(l, r, 1, 0, n); }
nodo get(int l, int r, int v, int x, int y) {
if (r <= x or l >= y) return nodo();
if (l <= x and r >= y) return tree[v];
int m = (x + y) / 2;
nodo izq = get(l, r, 2 * v, x, m);
nodo der = get(l, r, 2 * v + 1, m, y);
return nodo().comb(izq, der);
}
int trav(int l, int x) { return trav(l, x, 1, 0, n); }
int trav(int l, int k, int v, int x, int y) {
if (y <= l) return n;
if (tree[v].v < k) return n;
if (x + 1 == y) return x;
int m = (x + y) / 2;
int izq = trav(l, k, 2 * v, x, m);
if (izq != n) return izq;
return trav(l, k, 2 * v + 1, m, y);
}
vector<nodo> tree;
int n;
};
void controversial_rounds() {
int n;
string s;
cin >> n >> s;
segmentTreeLazy st(n);
vector<int> nxt1(n + 1), nxt0(n + 1);
nxt1[n] = n;
nxt0[n] = n;
for (int(i) = n - 1; i >= 0; i--) {
if (s[i] == '1')
nxt1[i] = i;
else
nxt1[i] = nxt1[i + 1];
}
for (int(i) = n - 1; i >= 0; i--) {
if (s[i] == '0')
nxt0[i] = i;
else
nxt0[i] = nxt0[i + 1];
}
vector<int> a(n);
for (int i = 0; i < n; i++) a[i] = max(nxt0[i], nxt1[i]) - i;
st.build(a);
vector<int> res(n + 1);
for (int(x) = 1; x <= n; x++) {
int l = 0;
l = st.trav(l, x);
while (l < n) {
l += x;
l = st.trav(l, x);
res[x]++;
}
}
for (int(i) = 1; i <= n; i++) cout << res[i] << ' ';
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
controversial_rounds();
}
| 6 |
#include <iostream>
using namespace std;
int main(void){
long q, h, s, d, n;
cin >> q >> h >> s >> d >>n;
if(2*q < h) h = 2*q;
if(2*h < s) s = 2*h;
if(2*s < d) d = 2*s;
cout << (n/2)*d + (n%2)*s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)
#define int long long
//using ll = long long;
using P = pair<int,int>;
ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }
template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
bool ch[100010];
vector<P> G[100010];
bool used[100010];
vector<int> ans;
bool isOK = true;
int dfs(int v,int num){
used[v] = true;
int sum = ch[v];
for(auto to : G[v]){
if(used[to.first]) continue;
sum += dfs(to.first,to.second);
}
if(sum % 2){
if(num == -1) isOK = false;
else ans.push_back(num);
}
return sum % 2;
}
signed main(){
int n,m,a[100010],b[100010],l[200010],r[200010];
cin >> n >> m;
vector<P> vec;
for(int i = 0;i < n;i++){
cin >> a[i] >> b[i];
vec.emplace_back(a[i],b[i]);
}
sort(all(vec));
a[0] = b[0] = 0;
a[n + 1] = INF;
b[n + 1] = 0;
for(int i = 0;i < n;i++){
a[i + 1] = vec[i].first;
b[i + 1] = vec[i].second;
}
for(int i = 0;i <= n;i++){
ch[i] = (b[i] != b[i + 1]);
}
for(int i = 0;i < m;i++){
cin >> l[i] >> r[i];
l[i] = lower_bound(a,a + n + 2,l[i]) - a - 1;
r[i] = upper_bound(a,a + n + 2,r[i]) - a - 1;
G[l[i]].emplace_back(r[i],i);
G[r[i]].emplace_back(l[i],i);
}
for(int i = 0;i < n;i++){
if(used[i]) continue;
dfs(i,-1);
}
if(!isOK) cout << -1 << endl;
else{
cout << ans.size() << endl;
if(ans.size()){
sort(all(ans));
for(int i = 0;i < ans.size();i++) cout << (i ? " " : "") << ans[i] + 1;
}
cout << endl;
}
} | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.