solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m, h;
vector<int> aux, aux2;
int vec[102][102];
int vis[102];
int vis2[102];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> h;
for (int i = 0; i < m; i++) {
int a;
cin >> a;
aux.push_back(a);
}
for (int i = 0; i < n; i++) {
int a;
cin >> a;
aux2.push_back(a);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> vec[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vec[i][j] != 0) {
if (aux[j] == aux2[i]) {
vis[i] = aux2[i];
vis2[j] = aux[j];
vec[i][j] = aux[j];
} else {
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (vec[i][j] != 0) {
if (aux[j] != aux2[i]) {
if (!vis2[j]) {
int maxi = 0;
int maxi2 = 0;
if (aux[j] > aux2[i])
continue;
else
vec[i][j] = aux[j], vis2[j] = aux[j];
} else if (!vis[i]) {
if (aux2[i] > aux[j])
continue;
else
vec[i][j] = aux2[i], vis[i] = aux[i];
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << vec[i][j] << " ";
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[8];
int main() {
int n, s = 0;
cin >> n;
for (int i = 1; i <= 7; i++) {
cin >> a[i];
}
for (int i = 1; i <= 7; i++) {
s += a[i];
if (s >= n) {
cout << i;
return 0;
}
if (i == 7) {
i = 0;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
if (a.first == b.first) return a.second < b.second;
return (a.first < b.first);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long x, y, t, c;
cin >> x >> y;
if (y == 0) {
cout << "No" << endl;
return 0;
}
if (y == 1) {
if (x > 0) {
cout << "No" << endl;
return 0;
}
}
t = y - 1;
t = x - t;
if (t % 2 == 0 && t >= 0) {
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
}
| 1 |
#include<iostream>
using namespace std;
int main(){
while(true){
double a[100000], data, total=0;
cin >> data;
if(data == 0) break;
for(int i=0; i<data; i++){
cin >> a[i];
total += a[i];
}
double ave = total/data;
int n=0;
for(int i=0; i<data; i++){
if(a[i]<=ave) n++;
}
cout << n << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int Y[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int X[] = {0, 0, -1, 1, -1, 1, -1, 1};
int directions = 4;
long long power(long long a, long long n) {
if (a == 0) return 0;
if (a == 1 || n == 0) return 1;
if (n == 1) return a % 1000000007;
long long t = power(a, n / 2);
t = t * t % 1000000007;
if (n & 1) return t * a % 1000000007;
return t;
}
void keywords() {
return;
int x;
scanf("%d", &x);
printf("lol");
cin >> x;
cout << "lol" << endl;
while (1) continue;
while (1) break;
}
int popcount(long long a) {
int c = 0;
while (a) {
c++;
a -= a & -a;
}
return c;
}
void factorize(int a) {}
void add(vector<int>& tree, int idx, int val, int maxval) {
for (; idx <= maxval; idx += idx & -idx) {
tree[idx] += val;
}
}
int read(vector<int>& tree, int idx) {
long long sum = 0;
for (; idx > 0; idx -= idx & -idx) {
sum += tree[idx];
}
return sum;
}
struct node2 {
int id, val;
node2() {
static int ctx = 1;
id = ctx++;
};
node2(int a, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0) {
val = a;
}
};
struct comp2 {
bool operator()(int a, int b) { return b < a; }
};
bool cmp2(int a, int b) { return b < a; }
struct node {
int id, val;
node() {
static int ctx = 1;
id = ctx++;
};
node(int a, int b = 0, int c = 0, int d = 0, int e = 0, int f = 0) {
val = a;
}
};
struct comp {
bool operator()(int a, int b) { return b < a; }
};
bool cmp(int a, int b) { return b < a; }
int n, m, a, b, d, k, h, w, x, y, p, q, t, ans, res, ma, mi, T,
act = 0, pas = 1, cur, flag, now;
int uu, vv, ww, l, r, L = 11;
int dp, dp2, cnt;
char c, s[100109], e[19], nuc[] = {'A', 'C', 'G', 'T'};
double f, z;
vector<int> tree[4][12][12], vec;
set<int> sett;
map<int, int> mapp;
void print() {
for (int i = 0; i < 4; i++) {
for (int len = 1; len < L; len++) {
for (int pos = 1; pos < len + 1; pos++) {
for (int j = 1; j < n / len + 2; j++)
cout << read(tree[i][len][pos], j) << " ";
cout << endl;
}
cout << endl;
}
cout << "----------------------------------------------" << endl;
}
}
void print2() {}
void input() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s + 1 >> q;
n = strlen(s + 1);
}
inline void build(char nuc, int len, int pos, vector<int>& tree) {
for (int i = 0; i * len + pos <= n; i++) {
if (s[i * len + pos] == nuc) add(tree, i + 1, 1, tree.size() - 2);
}
}
inline void init() {
for (int i = 0; i < 4; i++) {
for (int len = 1; len < L; len++) {
for (int pos = 1; pos < len + 1; pos++) {
tree[i][len][pos].resize(100109 / len, 0);
build(nuc[i], len, pos, tree[i][len][pos]);
}
}
}
}
inline void query(int l, int r, char e[]) {
int m = strlen(e);
int l2;
int r2;
ans = 0;
for (int len = 0; len < min(m, r - l + 1); len++) {
p = ((l + len) % m ? (l + len) % m : m);
l2 = (l + len - 1) / m + 1;
r2 = (r - 1) / m + ((r % m ? r % m : m) >= p);
for (int i = 0; i < 4; i++) {
if (e[len] == nuc[i])
ans += read(tree[i][m][p], r2) - read(tree[i][m][p], l2 - 1);
}
}
cout << ans << endl;
}
void update(int x, char c) {
if (s[x] == c) return;
for (int i = 0; i < 4; i++) {
int val = (nuc[i] == s[x] ? -1 : nuc[i] == c ? 1 : 0);
if (!val) continue;
for (int len = 1; len < L; len++) {
int pos = (x % len == 0 ? len : x % len);
int idx = (x - 1) / len + 1;
add(tree[i][len][pos], idx, val, tree[i][len][pos].size() - 2);
}
}
s[x] = c;
}
void solve() {
init();
while (q--) {
cin >> t;
if (t == 1) {
cin >> x >> c;
update(x, c);
}
if (t == 2) {
cin >> l >> r >> e + 1;
query(l, r, e + 1);
}
}
}
void output() {}
int main() {
input();
solve();
output();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int N;
bool firstMass[3000000];
bool secondMass[3000000];
void input() {
cin >> N;
char c;
for (int i = 0; i < 2 * N; i++) {
cin >> c;
if (c == '1')
firstMass[i] = true;
else
firstMass[i] = false;
}
for (int i = 0; i < 2 * N; i++) {
cin >> c;
if (c == '1')
secondMass[i] = true;
else
secondMass[i] = false;
}
}
int solve() {
bool first = true;
int oneone = 0, onezero = 0, zeroone = 0, zerozero = 0;
for (int i = 0; i < 2 * N; i++) {
if (firstMass[i] == true && secondMass[i] == true) {
oneone++;
}
if (firstMass[i] == true && secondMass[i] == false) {
onezero++;
}
if (secondMass[i] == true && firstMass[i] == false) {
zeroone++;
}
if (firstMass[i] == false && secondMass[i] == false) {
zerozero = 0;
}
}
int firstScore = 0, secondScore = 0;
for (int i = 0; i < 2 * N; i++) {
if (first) {
if (oneone != 0) {
oneone--;
firstScore++;
} else if (onezero != 0) {
onezero--;
firstScore++;
} else if (zeroone != 0) {
zeroone--;
}
}
if (!first) {
if (oneone != 0) {
oneone--;
secondScore++;
} else if (zeroone != 0) {
zeroone--;
secondScore++;
} else if (onezero != 0) {
onezero--;
}
}
first = !first;
}
if (firstScore > secondScore) return 1;
if (firstScore < secondScore) return 2;
return 0;
}
int main() {
input();
int ans = solve();
if (ans == 1) cout << "First";
if (ans == 2) cout << "Second";
if (ans == 0) cout << "Draw";
}
| 1 |
#include<cstdio>
#include<algorithm>
#include<functional>
using namespace std;
int main(void)
{
int n,m,i,j,mx,x,a[1000];
while(1) {
scanf("%d %d",&n,&m);
if(n==0 && m==0) break;
for(i=0;i<n;i++) scanf("%d",&a[i]);
mx=0;
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(a[i]+a[j]<=m) {
mx=max(mx,a[i]+a[j]);
}
}
}
if(mx!=0) printf("%d\n",mx);
else printf("NONE\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
Node *ch[2];
int r, s, v;
Node(int _v = 0) : v(_v) {
ch[0] = ch[1] = NULL;
r = rand();
s = 1;
}
void up() {
s = 1;
if (ch[0] != NULL) s += ch[0]->s;
if (ch[1] != NULL) s += ch[1]->s;
}
};
struct Treap {
Node *root;
Treap() { root = NULL; }
void rot(Node *&o, int d) {
Node *k = o->ch[d ^ 1];
o->ch[d ^ 1] = k->ch[d];
k->ch[d] = o;
o->up();
k->up();
o = k;
}
void ins(Node *&o, int x) {
if (o == NULL) {
o = new Node(x);
return;
}
int d = x >= o->v;
ins(o->ch[d], x);
if (o->r < o->ch[d]->r) rot(o, d ^ 1);
o->up();
}
void del(Node *&o, int x) {
if (o == NULL) return;
if (o->v == x) {
if (o->ch[0] != NULL && o->ch[1] != NULL) {
int d = o->ch[0]->r > o->ch[1]->r;
rot(o, d);
del(o->ch[d], x);
} else {
Node *u = o;
if (o->ch[0] == NULL)
o = o->ch[1];
else
o = o->ch[0];
delete u;
}
} else {
int d = x > o->v;
del(o->ch[d], x);
}
if (o != NULL) o->up();
}
void insert(int x) { ins(root, x); }
void erase(int x) { del(root, x); }
int query(int k) {
Node *t = root;
int ans = 0;
while (1) {
if (t == NULL) return ans;
if (t->v >= k) {
ans++;
if (t->ch[1] != NULL) ans += t->ch[1]->s;
t = t->ch[0];
} else
t = t->ch[1];
}
}
};
const int maxn = 200200;
int head[maxn], tol, val[maxn];
struct Edge {
int next, to;
} edge[3 * maxn];
void addedge(int u, int v) {
edge[tol].to = v;
edge[tol].next = head[u];
head[u] = tol++;
}
map<int, int> mp[maxn];
Treap *col[maxn];
vector<pair<int, int> > query[maxn];
int ans[maxn];
void dfs(int u, int fa) {
mp[u][val[u]] = 1;
col[u]->insert(1);
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (v == fa) continue;
dfs(v, u);
if (mp[u].size() < mp[v].size()) {
std::swap(col[u], col[v]);
std::swap(mp[u], mp[v]);
}
for (map<int, int>::iterator it = mp[v].begin(); it != mp[v].end(); it++) {
if (mp[u].count(it->first) == 0) {
mp[u][it->first] = it->second;
col[u]->insert(it->second);
} else {
col[u]->erase(mp[u][it->first]);
mp[u][it->first] += it->second;
col[u]->insert(mp[u][it->first]);
}
}
}
for (vector<pair<int, int> >::iterator it = query[u].begin();
it != query[u].end(); it++)
ans[it->first] = col[u]->query(it->second);
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof(head));
tol = 0;
for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
addedge(a, b);
addedge(b, a);
}
for (int i = 1; i <= n; i++) query[i].clear();
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
query[a].push_back(make_pair(i, b));
}
for (int i = 1; i <= n; i++) mp[i].clear();
for (int i = 1; i <= n; i++) col[i] = new Treap();
dfs(1, -1);
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin>>n;
double x,s=0;
string u;
for(int i=0; i<n; i++){
cin>>x>>u;
if(u=="JPY") s+=x;
else s+=380000*x;
}
printf("%-10.5lf\n",s);
} | 0 |
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
struct mat{
ll a[3][3];
friend mat operator *(mat a,mat b){
mat c;memset(c.a,0,sizeof(c.a));
for(int i=0;i<3;i++){
for(int p=0;p<3;p++){
for(int j=0;j<3;j++) c.a[i][p]=(c.a[i][p]+a.a[i][j]*b.a[j][p])%mod;
}
}
return c;
}
}dan,dan1;
int n,m,a[100005];
mat quick_pow(mat a,int b){
mat ans;memset(ans.a,0,sizeof(ans.a));
ans.a[0][0]=ans.a[1][1]=ans.a[2][2]=1;
for(;b;b>>=1,a=a*a)
if(b&1) ans=ans*a;
return ans;
}
int main(){
dan.a[0][0]=2;
dan.a[0][1]=2;
dan.a[0][2]=1;
dan.a[1][0]=1;
dan.a[1][1]=1;
dan.a[2][0]=1;
dan.a[2][1]=2;
dan.a[2][2]=1;
dan1.a[0][0]=1;
dan1.a[1][0]=1;
dan1.a[1][1]=1;
dan1.a[2][0]=1;
dan1.a[2][1]=2;
dan1.a[2][2]=1;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d",&a[i]);
mat ans;memset(ans.a,0,sizeof(ans.a));
ans.a[0][0]=ans.a[1][1]=ans.a[2][2]=1;
for(int i=1;i<=m;i++){
ans=quick_pow(dan,a[i]-a[i-1]-1)*ans;
ans=dan1*ans;
}
ans=quick_pow(dan,n-a[m]-1)*ans;
dan.a[0][0]--;
ans=dan*ans;
printf("%lld",ans.a[0][0]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int64_t a[200005];
signed main() {
int64_t t;
cin >> t;
while (t--) {
int64_t n, i;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i];
int64_t c = 0, f = 0, fl = 0;
a[0] = 1000000000;
int64_t cou = 0;
for (i = 1; i <= n; i++) {
if (a[i] != i) fl = 1;
if (a[i] == i && a[i - 1] != i - 1) cou++;
}
if (fl == 0)
cout << "0"
<< "\n";
else if (cou == 0)
cout << "1"
<< "\n";
else if (cou == 1 && (a[1] == 1 || a[n] == n))
cout << cou << "\n";
else if (cou == 2 && (a[1] == 1 && a[n] == n))
cout << "1"
<< "\n";
else
cout << "2"
<< "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, s, pcount = 0, vis[500][500];
char arr[500][500];
int dfs(int i, int j) {
vis[i][j] = 1;
int count = 1;
if (i > 0 && vis[i - 1][j] == 0) count += dfs(i - 1, j);
if (i < n - 1 && vis[i + 1][j] == 0) count += dfs(i + 1, j);
if (j > 0 && vis[i][j - 1] == 0) count += dfs(i, j - 1);
if (j < m - 1 && vis[i][j + 1] == 0) count += dfs(i, j + 1);
if (count == 1 && pcount < k) {
pcount++;
arr[i][j] = 'X';
return count - 1;
}
return count;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
string st;
for (int i = 0; i < n; i++) {
cin >> st;
for (int j = 0; j < m; j++) arr[i][j] = st[j];
}
int flag = 0;
for (int i = 0; i < n && flag == 0; i++) {
for (int j = 0; j < m && flag == 0; j++) {
if (arr[i][j] == '.') {
flag = 1;
for (int g = 0; g < n; g++)
for (int h = 0; h < m; h++)
if (arr[g][h] == '.')
vis[g][h] = 0;
else
vis[g][h] = 1;
int tcount = dfs(i, j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << arr[i][j];
cout << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 10;
const long long INF = 1e9;
long long n, ans[maxn], fen[maxn], dp[maxn], a[maxn];
vector<pair<pair<long long, long long>, long long> > poi;
long long ask(long long l, long long r) {
if (r < l) return 0;
if (l > n - 1 || l < 0 || r < 0 || r > n - 1) return 0;
r++;
l++;
long long anss = 0;
while (r >= l) {
if ((r - (r & -r) + 1) >= l) {
anss = max(anss, fen[r]);
r -= (r & -r);
continue;
}
anss = max(anss, a[r]);
r--;
}
return anss;
}
void asg(long long idx, long long va) {
for (++idx; idx <= n; idx += (idx & -idx)) fen[idx] = max(va, fen[idx]);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (long long i = 1; i <= n; i++) {
long long x, h;
cin >> x >> h;
poi.push_back({{x, h}, i});
}
sort(poi.begin(), poi.end());
for (long long i = n - 1; i >= 0; i--) {
long long x = poi[i].first.first, h = poi[i].first.second,
ii = poi[i].second;
long long ind1 = lower_bound(poi.begin(), poi.end(),
make_pair(make_pair(x + 1, -INF), -INF)) -
poi.begin();
long long ind2 = lower_bound(poi.begin(), poi.end(),
make_pair(make_pair(x + h, -INF), -INF)) -
poi.begin();
ind2--;
dp[ii] = max(i, ask(ind1, ind2));
ans[ii] = dp[ii] - i + 1;
a[i + 1] = dp[ii];
asg(i, dp[ii]);
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int minn(long long int a, long long int b) {
if (a > b) return b;
return a;
}
long long int maxx2(long long int a, long long int b) {
if (a > b) return a;
return b;
}
long long int maxx(long long int a, long long int b, long long int c) {
return maxx2(a, maxx2(b, c));
}
long long int gcdd(long long int a, long long int b) {
if (b == 0)
return a;
else
return gcdd(b, a % b);
}
long long int dsum(long long int n) {
long long int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
long long int poww(long long int a, long long int b) {
if (b == 0) return 1;
if (b == 1)
return a;
else
return ((poww(a, b / 2) % 998244353) * (poww(a, b / 2) % 998244353) *
(poww(a, b % 2) % 998244353)) %
998244353;
}
long long int temp[200001] = {0};
long long int bs(long long int x, long long int l, long long int r,
long long int n) {
if (l == r) {
return l;
}
long long int mid = (l + r) / 2;
if (temp[mid] == x) return mid;
if (temp[mid] < x && temp[mid + 1] > x) return mid;
if (temp[mid] < x)
return bs(x, mid, r, n);
else
return bs(x, l, mid, n);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int count = 0;
for (long long int i = n - 1; i >= 0; i--) {
for (long long int j = 0; j < i; j++) {
if (a[j] == a[i]) a[j] = 0;
}
if (a[i] != 0) count++;
}
cout << count << endl;
for (long long int i = 0; i < n; i++) {
if (a[i] != 0) cout << a[i] << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int num = 1;
struct node {
int to;
int next;
} road[400010];
int head[200010];
int low[100010], dfn[100010], cut[200010], tot = 0;
int cnt = 0, vis[100010], belong[100010];
int pre[200010][20], deep[200010];
void build(int from, int to) {
road[++num].next = head[from];
road[num].to = to;
head[from] = num;
}
void tarjan(int x, int in_edge) {
low[x] = dfn[x] = ++tot;
for (int i = head[x]; i; i = road[i].next) {
int y = road[i].to;
if (!dfn[y]) {
tarjan(y, i);
low[x] = min(low[x], low[y]);
if (dfn[x] < low[y]) {
cut[i] = cut[i ^ 1] = 1;
}
} else if (i != (in_edge ^ 1))
low[x] = min(low[x], dfn[y]);
}
}
void dfs(int x) {
vis[x] = 1;
belong[x] = cnt;
for (int i = head[x]; i; i = road[i].next) {
if (cut[i]) continue;
int y = road[i].to;
if (vis[y]) continue;
dfs(y);
}
}
void dfs2(int x, int fa) {
pre[x][0] = fa;
deep[x] = deep[fa] + 1;
for (int i = 1; i <= 18; i++) {
pre[x][i] = pre[pre[x][i - 1]][i - 1];
}
for (int i = head[x]; i; i = road[i].next) {
int y = road[i].to;
if (y == fa) continue;
dfs2(y, x);
}
}
int lca(int x, int y) {
if (deep[x] < deep[y]) swap(x, y);
int k = deep[x] - deep[y];
for (int i = 0; i <= 18; i++) {
if ((k >> i) & 1) x = pre[x][i];
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (pre[x][i] != pre[y][i]) {
x = pre[x][i];
y = pre[y][i];
}
}
return pre[x][0];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
build(a, b);
build(b, a);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i, 0);
}
cnt = n;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
cnt++;
dfs(i);
}
}
for (int x = 1; x <= n; x++) {
for (int i = head[x]; i; i = road[i].next) {
int y = road[i].to;
if (belong[x] == belong[y]) continue;
build(belong[x], belong[y]);
}
}
dfs2(n + 1, 0);
scanf("%d", &k);
while (k--) {
int s, l;
scanf("%d%d", &s, &l);
printf("%d\n", deep[belong[s]] + deep[belong[l]] -
2 * deep[lca(belong[s], belong[l])]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0;
bool flg = false;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') flg = true;
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return flg ? -x : x;
}
int ans[310][310];
int a[310][310];
int n, m;
void init() {}
void solve() {
n = read();
m = read();
bool flg = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
ans[i][j] = 0;
if (i > 1) ans[i][j]++;
if (i < n) ans[i][j]++;
if (j > 1) ans[i][j]++;
if (j < m) ans[i][j]++;
if (ans[i][j] < read()) flg = 0;
}
if (!flg) return puts("NO"), void();
puts("YES");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) printf("%d%c", ans[i][j], " \n"[j == m]);
}
int main() {
for (int T = read(); T--;) init(), solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
const int s = 30;
int n, d, x;
int a[maxn], b[maxn], q[maxn], to[maxn];
int getNextX() {
x = (x * 37LL + 10007) % 1000000007;
return x;
}
void initAB() {
int i;
for (i = 0; i < n; i = i + 1) {
a[i] = i + 1;
}
for (i = 0; i < n; i = i + 1) {
swap(a[i], a[getNextX() % (i + 1)]);
}
for (i = 0; i < n; i = i + 1) {
if (i < d)
b[i] = 1;
else
b[i] = 0;
}
for (i = 0; i < n; i = i + 1) {
swap(b[i], b[getNextX() % (i + 1)]);
}
}
int main() {
cin >> n >> d >> x;
initAB();
for (int i = 0; i < n; i++) to[a[i]] = i;
for (int i = 0; i < n; i++)
if (b[i]) q[++q[0]] = i;
for (int i = 0; i < n; i++) {
int j;
for (j = n; j >= n - s; j--) {
if (j > 0 && i >= to[j] && b[i - to[j]]) {
printf("%d\n", j);
break;
}
}
if (j < n - s) {
int Max = 0;
for (j = 1; j <= q[0] && q[j] <= i; j++) {
Max = max(Max, a[i - q[j]]);
}
printf("%d\n", Max);
}
}
return 0;
}
| 2 |
#include<iostream>
#include<algorithm>
using namespace std;
int N,A[100];
int ans=1e9;
int main()
{
cin>>N;
for(int i=0;i<N;i++)cin>>A[i];
for(int X=-100;X<=100;X++)
{
int now=0;
for(int i=0;i<N;i++)now+=(X-A[i])*(X-A[i]);
ans=min(ans,now);
}
cout<<ans<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int n, x[N], y[N], sx, sy, cnt1, cnt2, cnt3, cnt4, mx;
int main() {
scanf("%d%d%d", &n, &sx, &sy);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x[i], &y[i]);
x[i] -= sx;
y[i] -= sy;
}
for (int i = 1; i <= n; i++) {
if (y[i] >= 1) cnt1++;
if (y[i] <= -1) cnt2++;
if (x[i] >= 1) cnt3++;
if (x[i] <= -1) cnt4++;
}
mx = max({cnt1, cnt2, cnt3, cnt4});
printf("%d\n", mx);
if (cnt1 == mx) {
printf("%d %d\n", sx, sy + 1);
return 0;
}
if (cnt2 == mx) {
printf("%d %d\n", sx, sy - 1);
return 0;
}
if (cnt3 == mx) {
printf("%d %d\n", sx + 1, sy);
return 0;
}
if (cnt4 == mx) {
printf("%d %d\n", sx - 1, sy);
return 0;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, b, c, mn;
struct node {
int x, d;
};
int mem1[200001];
int mem2[200001];
vector<node> v[200001];
stack<node> q;
queue<int> ans;
void f(int x, int p) {
for (int i = 0; i < v[x].size(); i++) {
int nx = v[x][i].x;
int nd = v[x][i].d;
if (nx != p) {
c += nd;
mem1[nx] = mem1[x] + 1;
mem2[nx] = mem2[x] + nd;
f(nx, x);
}
}
}
int main() {
mn = 1e9;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d%d", &b, &c);
v[b].push_back({c, 0});
v[c].push_back({b, 1});
}
c = 0;
f(1, 0);
mn = c;
ans.push(1);
for (int i = 2; i <= n; i++) {
if (c + mem1[i] - 2 * mem2[i] < mn) {
mn = c + mem1[i] - 2 * mem2[i];
while (!ans.empty()) ans.pop();
ans.push(i);
} else if (c + mem1[i] - 2 * mem2[i] == mn)
ans.push(i);
}
printf("%d\n", mn);
while (!ans.empty()) {
printf("%d ", ans.front());
ans.pop();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, l, r, s, c[1000];
double d, ans, f[110][100010], sum[100010];
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ '0');
c = getchar();
}
return x * f;
}
int main() {
n = read();
m = read();
d = 1.0 / (m - 1);
for (long long i = 1; i <= n; i++) {
c[i] = read();
s += c[i];
}
f[0][0] = sum[0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = i; j <= i * m; j++) {
l = max(i - 1, j - m);
r = min((i - 1) * m, j - 1);
f[i][j] = d * sum[r];
if (l) f[i][j] -= d * sum[l - 1];
if (l <= j - c[i] && j - c[i] <= r) f[i][j] -= d * f[i - 1][j - c[i]];
}
for (long long j = 1; j <= i * m; j++) sum[j] = sum[j - 1] + f[i][j];
}
for (int j = n; j < s; j++) ans += f[n][j];
printf("%.16lf", ans * (m - 1) + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int main() {
int cc;
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) cin >> a[i];
int ans = -0x3f3f3f3f;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) {
vector<int> ss, pp;
int sum = 0;
for (int k = i; k <= j; k++) {
sum += a[k];
ss.push_back(a[k]);
}
for (int k = 1; k < i; k++) pp.push_back(a[k]);
for (int k = j + 1; k <= n; k++) pp.push_back(a[k]);
sort(ss.begin(), ss.end());
sort(pp.begin(), pp.end());
int l = 0, r = pp.size() - 1, p = m;
while ((p--) && l < ss.size() && r >= 0) {
if (ss[l] < pp[r]) {
sum -= ss[l];
sum += pp[r];
l++;
r--;
}
}
ans = max(ans, sum);
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool isCoprime(int64_t a, int64_t b) {
if (a > b) {
swap(a, b);
}
if (a == 1) {
return true;
}
if (b % a == 0) {
return false;
}
for (int64_t d = 2; d <= b - a; ++d) {
if (a % d == 0 and (b % d == 0 or b % (b / d) == 0)) {
return false;
}
}
return true;
}
int main() {
int64_t lft;
cin >> lft;
int64_t rgt;
cin >> rgt;
for (int64_t i = lft; i <= rgt; ++i) {
for (int64_t j = i + 1; j <= rgt; ++j) {
for (int64_t k = j + 1; k <= rgt; ++k) {
if (isCoprime(i, j) and isCoprime(j, k) and not isCoprime(i, k)) {
cout << i << " " << j << " " << k << endl;
return 0;
}
}
}
}
cout << -1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = 0;
int x = 0, flag = 1;
while (!isdigit(ch)) {
ch = getchar();
if (ch == '-') flag = -1;
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * flag;
}
struct edge {
int to, nxt;
} e[440000 * 2];
int num, head[440000];
inline void add(int x, int y) {
e[++num] = {y, head[x]};
head[x] = num;
}
int n, m, q, root, cnt, p[440000], g[440000], f[440000], st[440000], lg[440000],
nxt[440000], w[440000][24];
void dfs(int x, int fa) {
if (x != root) st[++cnt] = x;
if (cnt >= n)
w[x][0] = st[cnt - n + 1];
else
w[x][0] = 1e9 + 7;
for (int i = head[x]; i != -1; i = e[i].nxt) {
int to = e[i].to;
if (to == fa) continue;
dfs(to, x);
}
cnt--;
}
int query(int l, int r) {
int k = lg[r - l + 1];
return min(w[l][k], w[r - (1 << k) + 1][k]);
}
int main() {
n = read();
m = read();
q = read();
root = m + 1;
num = -1;
memset(head, -1, sizeof(head));
for (int i = 1; i <= n; i++) p[i] = read(), f[i] = root;
for (int i = 1; i <= n; i++) nxt[p[i]] = p[(i % n) + 1];
for (int i = 1; i <= m; i++) g[i] = read();
for (int i = m; i >= 1; i--) {
int x = g[i];
add(f[nxt[x]], i), f[x] = i;
}
dfs(root, 0);
for (int i = 1; i <= m; i++)
lg[i] = lg[i - 1] + ((1 << (lg[i - 1] + 1)) == i);
for (int k = 1; (1 << k) <= m; k++)
for (int i = 1; i + (1 << k) - 1 <= m; i++)
w[i][k] = min(w[i][k - 1], w[i + (1 << (k - 1))][k - 1]);
for (int i = 1; i <= q; i++) {
int l = read(), r = read();
printf("%d", query(l, r) <= r);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int infinity = 9e18;
string getString(string s) {
if (s.size() == 0) return "-1";
int i = 0;
while (i < s.size() && s[i] == '0') i++;
if (i == s.size())
return "0";
else
return s.substr(i, s.size() - i);
}
string get(string s, int x, int y) {
string str;
for (int i = 0; i < s.size(); i++) {
if (i != x && i != y) str += s[i];
}
return str;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string str;
cin >> str;
vector<int> one, two;
int n = str.size();
int sum = 0;
for (int i = n - 1; i >= 0; i--) {
int k = (str[i] - '0');
sum += k;
k = k % 3;
if (k == 1) {
one.push_back(i);
} else if (k == 2) {
two.push_back(i);
}
}
int x = -1;
int y = -1;
if (sum % 3 == 0) {
cout << getString(str);
} else if (sum % 3 == 1) {
if (one.size()) {
x = one[0];
if (x == 0 && str.size() > 1 && str[1] == '0' && two.size() > 1) {
x = two[0];
y = two[1];
str = get(str, x, y);
cout << getString(str);
} else {
str = get(str, x, y);
cout << getString(str);
}
} else if (two.size() > 1) {
x = two[0];
y = two[1];
str = get(str, x, y);
cout << getString(str);
} else {
cout << -1;
}
} else {
if (two.size()) {
x = two[0];
if (x == 0 && str.size() > 1 && str[1] == '0' && one.size() > 1) {
x = one[0];
y = one[1];
str = get(str, x, y);
cout << getString(str);
} else {
str = get(str, x, y);
cout << getString(str);
}
} else if (one.size() > 1) {
x = one[0];
y = one[1];
str = get(str, x, y);
cout << getString(str);
} else {
cout << -1;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int getbit(long long x, int y) { return (x >> y) & 1; }
long long getoff(long long x, int y) { return x ^ ((1ll) << y); }
using namespace std;
int n, k, pos, p[410], c[410][410];
vector<int> vt[410];
vector<int> f[410][410];
void dfs(int u, int cha) {
for (int i = 0; i <= n; ++i) f[u][i].resize(vt[u].size() + 1);
for (int i = 0; i <= n; ++i)
for (int e = 0; e <= (int)vt[u].size(); ++e) f[u][i][e] = 1e9;
f[u][1][0] = 0;
if (vt[u].size() == 1 && cha != -1) {
return;
}
int e = 0;
for (int v : vt[u]) {
if (v == cha) continue;
p[v] = u;
dfs(v, u);
++e;
int sz = vt[v].size() - 1;
for (int i = n; i >= 1; --i) {
f[u][i][e] = f[u][i][e - 1] + 1;
for (int j = 1; j < i; ++j)
f[u][i][e] = min(f[u][i][e], f[u][i - j][e - 1] + f[v][j][sz]);
}
}
}
void tv(int u, int cha, int sl) {
reverse(vt[u].begin(), vt[u].end());
int e = vt[u].size() - 1;
for (int v : vt[u]) {
if (v == cha) continue;
int luu = 0;
int sz = vt[v].size() - 1;
for (int j = 1; j < sl; ++j)
if (f[u][sl][e] == f[u][sl - j][e - 1] + f[v][j][sz]) {
luu = j;
break;
}
if (luu == 0)
cout << c[u][v] << " ";
else {
tv(v, u, luu);
sl -= luu;
}
--e;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
c[u][v] = i;
c[v][u] = i;
vt[u].emplace_back(v);
vt[v].emplace_back(u);
}
vt[1].emplace_back(-1);
p[1] = -1;
dfs(1, -1);
int mi = 1e9;
for (int i = 1; i <= n; ++i)
if (pos == 0 || mi > f[i][k][vt[i].size() - 1] + (i > 1)) {
pos = i;
mi = f[i][k][vt[i].size() - 1] + (i > 1);
}
cout << mi << '\n';
tv(pos, p[pos], k);
if (pos > 1) cout << c[p[pos]][pos];
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
bool f;
int n, i, j;
int card[9];
bool judge (int a, int b, int c){
if (a == b && b == c) return true;
else if (a + 1 == b && a + 2 == c) return true;
return false;
}
int win(){
sort(card, card + 9);
do{
f = 1;
f = judge(card[0], card[1], card[2]);
if (f == true) f = judge(card[3], card[4], card[5]);
if (f == true) f = judge(card[6], card[7], card[8]);
if (f == 1) return 1;
}while (next_permutation(card, card+9));
return 0;
}
signed main(){
cin >> n;
string color;
for (i = 0; i < n; i++){
for (j = 0; j < 9; j++){
cin >> card[j];
}
for (j = 0; j < 9; j++){
cin >> color;
if (color == "G") card[j] += 10;
else if (color == "B") card[j] += 20;
}
int ans = win();
cout << ans << endl;
/*sort(card, card + 9);
f = true;
f = judge(card[0], card[1], card[2]);
if (f == true) f = judge(card[3], card[4], card[5]);
if (f == true) f = judge(card[6], card[7], card[8]);
cout << (f ? 1 : 0) << endl;*/
}
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
using namespace std;
const long long INF = 1000000000000000;
int main() {
int a, b, q; cin >> a >> b >> q;
long long s[100010], t[100010];
s[0] = t[0] = -INF;
for (int i = 1; i <= a; i++) {
cin >> s[i];
}
for (int i = 1; i <= b; i++) {
cin >> t[i];
}
s[a + 1] = INF; t[b + 1] = INF;
long long ans[100010];
for (int i = 0; i < q; i++) {
ans[i] = INF;
}
for (int i = 0; i < q; i++) {
long long x; cin >> x;
int sh = lower_bound(s, s + a + 2, x) - s;
int tem = lower_bound(t, t + b + 2, x) - t;
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ans[i] = min(ans[i], abs(s[sh - j] - x) + abs(t[tem - k] - s[sh - j]));
ans[i] = min(ans[i], abs(t[tem - k] - x) + abs(s[sh - j] - t[tem - k]));
}
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
} | 0 |
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
int main()
{
for(;;){
int n, k;
cin >> n >> k;
if(n == 0)
return 0;
vector<bool> card(n+1, false);
for(int i=0; i<k; ++i){
int a;
cin >> a;
card[a] = true;
}
int ret = 0;
int l1 = 0;
int l2 = 0;
for(int i=1; i<=n; ++i){
if(card[i]){
++ l1;
++ l2;
}else{
l2 = l1 + 1;
l1 = 0;
}
ret = max(ret, l1);
if(card[0])
ret = max(ret, l2);
}
cout << ret << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N;
cin >> N;
set<int> s;
map<int, int> m, mm;
for (int i = 0; i < N; ++i) {
int x;
cin >> x;
s.insert(x);
m[x]++;
}
if (s.size() != 2) {
cout << "NO" << '\n';
return 0;
}
for (auto& x : m) mm[x.second]++;
for (auto& x : mm) {
if (x.second % 2) {
cout << "NO" << '\n';
return 0;
}
}
cout << "YES" << '\n';
for (auto it = s.begin(); it != s.end(); ++it) cout << *it << " ";
cout << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const long long MD = md * 100;
const int xn = 2e3 + 10;
const int xm = 4e6 + 10;
const int SQ = 320;
const int sq = 1e3 + 10;
const long long inf = 1e18 + 10;
long long power(long long a, long long b) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2) % md
: power(a * a % md, b / 2) % md));
}
int n, m, Root = 1, ans, Par[xn], sz[xn], par[xn], a[xn], SZ[xn], t;
vector<int> adj[xn], G[xn], bach;
pair<int, int> E[xm];
bool Mark[xn], dp[xn][xn];
void DFS2(int v) {
Mark[v] = true;
SZ[v] = sz[v];
if (par[v] == Root) bach.push_back(v);
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (Mark[u]) continue;
par[u] = v;
DFS2(u);
SZ[v] += SZ[u];
}
t += sz[v] * SZ[v];
}
void Solve() {
memset(par, 0, sizeof par);
memset(Mark, false, sizeof Mark);
memset(a, 0, sizeof a);
memset(SZ, 0, sizeof SZ);
memset(dp, false, sizeof dp);
bach.clear();
t = 0;
DFS2(Root);
ans = max(ans, t);
for (int i = 0; i < bach.size(); i++) {
int v = bach[i];
if (i == 0) {
dp[i][0] = true;
dp[i][SZ[v]] = true;
continue;
}
for (int j = 0; j < n; j++) {
if (dp[i - 1][j]) dp[i][j] = true;
if (SZ[v] <= j && dp[i - 1][j - SZ[v]]) dp[i][j] = true;
}
}
int pnt = bach.size() - 1;
if (pnt == -1) return;
for (int i = 0; i < n; i++) {
if (!dp[pnt][i]) continue;
ans = max(ans, t + i * (SZ[Root] - sz[Root] - i));
}
}
int get_root(int v) {
if (v == Par[v]) return v;
return Par[v] = get_root(Par[v]);
}
void Merge(int v, int u) {
v = get_root(v);
u = get_root(u);
if (v == u) return;
swap(v, u);
Par[u] = v;
sz[v] += sz[u];
}
void DFS(int v) {
Mark[v] = true;
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i];
if (u == par[v]) continue;
if (Mark[u]) {
int x = v;
while (get_root(x) != get_root(u)) x = get_root(x), Merge(x, par[x]);
continue;
}
par[u] = v;
DFS(u);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
E[i] = {v, u};
}
for (int i = 1; i <= n; i++) Par[i] = i, sz[i] = 1;
DFS(Root);
for (int i = 0; i < m; i++) {
int v = E[i].first, u = E[i].second;
v = get_root(v);
u = get_root(u);
if (v == u) continue;
G[v].push_back(u);
G[u].push_back(v);
}
for (int v = 1; v <= n; v++) {
if (get_root(v) != v) continue;
Root = v;
Solve();
}
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
struct I_Node {
int x = 0;
int k = 0;
long long int subtract = 0;
};
struct T_Node {
int start;
int end;
int height;
int root;
};
I_Node segtree[4 * 300010];
T_Node tree[300010];
int roots[300010];
int name[300010];
int name_inv[300010];
int curr_name = 0;
std::vector<std::vector<int>> graph;
void dfs_name(int curr, int height) {
int next_node;
name[curr] = curr_name;
name_inv[curr_name] = curr;
tree[curr].start = curr_name;
tree[curr].height = height;
curr_name += 1;
for (int i = 0; i < graph[curr].size(); i++) {
next_node = graph[curr][i];
if (name[next_node] == -1) {
dfs_name(next_node, height + 1);
}
}
tree[curr].end = curr_name - 1;
}
void get_roots(int node_start, int node_end, int pos) {
if (node_start == node_end) {
roots[node_start] = pos;
} else {
int mid = (node_end + node_start) / 2;
get_roots(node_start, mid, pos * 2);
get_roots(mid + 1, node_end, pos * 2 + 1);
}
}
long long int query(int pos, int height) {
long long int ans = 0;
ans += segtree[pos].x;
ans -= (long long int)segtree[pos].k * (long long int)height;
ans += segtree[pos].subtract;
ans %= 1000000007;
if (ans < 0) {
ans += 1000000007;
}
if (pos != 1) {
ans += query(pos / 2, height);
}
ans %= 1000000007;
if (ans < 0) {
ans += 1000000007;
}
return ans;
}
void propogate(int node_start, int node_end, int update_start, int update_end,
int pos, int x, int k, int height) {
if (node_start > update_end or node_end < update_start) {
return;
}
if (update_start <= node_start and update_end >= node_end) {
segtree[pos].x += x;
segtree[pos].k += k;
segtree[pos].subtract += (long long int)height * (long long int)k;
segtree[pos].x %= 1000000007;
segtree[pos].k %= 1000000007;
segtree[pos].subtract %= 1000000007;
return;
}
int mid = (node_end + node_start) / 2;
propogate(node_start, mid, update_start, update_end, pos * 2, x, k, height);
propogate(mid + 1, node_end, update_start, update_end, pos * 2 + 1, x, k,
height);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int N;
int Q;
std::cin >> N;
int P;
for (int i = 0; i < N; i++) {
std::vector<int> a;
graph.emplace_back(a);
name[i] = -1;
}
for (int i = 1; i < N; i++) {
std::cin >> P;
graph[i].emplace_back(P - 1);
graph[P - 1].emplace_back(i);
}
dfs_name(0, 1);
get_roots(0, N - 1, 1);
std::cin >> Q;
int query_type;
int v;
int x;
int k;
int update_start;
int update_end;
long long int ans;
for (int i = 0; i < Q; i++) {
std::cin >> query_type;
if (query_type == 1) {
std::cin >> v;
std::cin >> x;
std::cin >> k;
v -= 1;
update_start = tree[v].start;
update_end = tree[v].end;
propogate(0, N - 1, update_start, update_end, 1, x, k, tree[v].height);
} else {
std::cin >> v;
v -= 1;
int new_name = name[v];
ans = 0;
if (tree[v].root != 1) {
ans += query(roots[new_name], tree[v].height);
}
ans %= 1000000007;
if (ans < 0) {
ans += 1000000007;
}
std::cout << ans << std::endl;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int N, M;
vector<vector<int>> occur;
string S;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M >> S;
occur.assign(M, vector<int>(M, 0));
for (int i = 0; i + 1 < N; i++) {
int x = S[i] - 'a';
int y = S[i + 1] - 'a';
occur[x][y]++;
occur[y][x]++;
}
vector<int> dp(1 << M, INF);
dp[0] = 0;
for (int mask = 0; mask < 1 << M; mask++) {
int mask_sum = 0;
for (int a = 0; a < M; a++)
if (mask >> a & 1)
for (int b = 0; b < M; b++)
if ((mask >> b & 1) == 0) mask_sum += occur[a][b];
for (int i = 0; i < M; i++)
dp[mask | 1 << i] = min(dp[mask | 1 << i], dp[mask] + mask_sum);
}
cout << dp[(1 << M) - 1] << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300007;
const int IT = 500000;
struct xorshift {
unsigned x, y, z, w;
xorshift() : x(123456789), y(328932), z(7777777), w(898329832) {}
unsigned dsljhfdskjfdshkjfds() {
unsigned t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = w ^ (w >> 19) ^ t ^ (t >> 8);
}
unsigned dsljhfdskjfdshkjfds(unsigned a) { return dsljhfdskjfdshkjfds() % a; }
int dsljhfdskjfdshkjfds(int a, int b) {
return a + dsljhfdskjfdshkjfds(b - a + 1);
}
} rng;
int n, a[N], g;
bool have[N];
int i1, i2, i3, i4, i5, i6;
int dsljhfdskjfdshkjfds(int a, int b) { return rng.dsljhfdskjfdshkjfds(a, b); }
int gcd(int a, int b) {
int r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
int gcd(int a, int b, int c) { return gcd(a, gcd(b, c)); }
int gcd(int a, int b, int c, int d) { return gcd(gcd(a, b), gcd(c, d)); }
int gcd(int a, int b, int c, int d, int e) {
return gcd(gcd(a, b, c), gcd(d, e));
}
int gcd(int a, int b, int c, int d, int e, int f) {
return gcd(gcd(a, b), gcd(c, d), gcd(e, f));
}
bool try2() {
int i;
for (i = 1; i + 1 <= n; i++) {
if (gcd(a[i], a[i + 1]) == 1) return true;
}
if (n <= 1000) {
for (i1 = 1; i1 <= n; i1++) {
for (i2 = i1 + 1; i2 <= n; i2++) {
if (gcd(a[i1], a[i2]) == 1) return true;
}
}
} else {
for (i = 1; i <= IT; i++) {
i1 = dsljhfdskjfdshkjfds(1, n - 1);
i2 = dsljhfdskjfdshkjfds(i1 + 1, n);
if (gcd(a[i1], a[i2]) == 1) return true;
}
}
return false;
}
bool try3() {
int i;
for (i = 1; i + 2 <= n; i++) {
if (gcd(a[i], a[i + 1], a[i + 2]) == 1) return true;
}
if (n <= 70) {
for (i1 = 1; i1 <= n; i1++) {
for (i2 = i1 + 1; i2 <= n; i2++) {
for (i3 = i2 + 1; i3 <= n; i3++) {
if (gcd(a[i1], a[i2], a[i3]) == 1) return true;
}
}
}
} else {
for (i = 1; i <= IT; i++) {
i1 = dsljhfdskjfdshkjfds(1, n - 2);
i2 = dsljhfdskjfdshkjfds(i1 + 1, n - 1);
i3 = dsljhfdskjfdshkjfds(i2 + 1, n);
if (gcd(a[i1], a[i2], a[i3]) == 1) return true;
}
}
return false;
}
bool try4() {
int i;
for (i = 1; i + 3 <= n; i++) {
if (gcd(a[i], a[i + 1], a[i + 2], a[i + 3]) == 1) return true;
}
if (n <= 30) {
for (i1 = 1; i1 <= n; i1++) {
for (i2 = i1 + 1; i2 <= n; i2++) {
for (i3 = i2 + 1; i3 <= n; i3++) {
for (i4 = i3 + 1; i4 <= n; i4++) {
if (gcd(a[i1], a[i2], a[i3], a[i4]) == 1) return true;
}
}
}
}
} else {
for (i = 1; i <= IT; i++) {
i1 = dsljhfdskjfdshkjfds(1, n - 3);
i2 = dsljhfdskjfdshkjfds(i1 + 1, n - 2);
i3 = dsljhfdskjfdshkjfds(i2 + 1, n - 1);
i4 = dsljhfdskjfdshkjfds(i3 + 1, n);
if (gcd(a[i1], a[i2], a[i3], a[i4]) == 1) return true;
}
}
return false;
}
bool try5() {
int i;
for (i = 1; i + 4 <= n; i++) {
if (gcd(a[i], a[i + 1], a[i + 2], a[i + 3], a[i + 4]) == 1) return true;
}
if (n <= 15) {
for (i1 = 1; i1 <= n; i1++) {
for (i2 = i1 + 1; i2 <= n; i2++) {
for (i3 = i2 + 1; i3 <= n; i3++) {
for (i4 = i3 + 1; i4 <= n; i4++) {
for (i5 = i4 + 1; i5 <= n; i5++) {
if (gcd(a[i1], a[i2], a[i3], a[i4], a[i5]) == 1) return true;
}
}
}
}
}
} else {
for (i = 1; i <= IT; i++) {
i1 = dsljhfdskjfdshkjfds(1, n - 4);
i2 = dsljhfdskjfdshkjfds(i1 + 1, n - 3);
i3 = dsljhfdskjfdshkjfds(i2 + 1, n - 2);
i4 = dsljhfdskjfdshkjfds(i3 + 1, n - 1);
i5 = dsljhfdskjfdshkjfds(i4 + 1, n);
if (gcd(a[i1], a[i2], a[i3], a[i4], a[i5]) == 1) return true;
}
}
return false;
}
bool try6() {
int i;
for (i = 1; i + 5 <= n; i++) {
if (gcd(a[i], a[i + 1], a[i + 2], a[i + 3], a[i + 4], a[i + 5]) == 1)
return true;
}
if (n <= 10) {
for (i1 = 1; i1 <= n; i1++) {
for (i2 = i1 + 1; i2 <= n; i2++) {
for (i3 = i2 + 1; i3 <= n; i3++) {
for (i4 = i3 + 1; i4 <= n; i4++) {
for (i5 = i4 + 1; i5 <= n; i5++) {
for (i6 = i5 + 1; i6 <= n; i6++) {
if (gcd(a[i1], a[i2], a[i3], a[i4], a[i5], a[i6]) == 1)
return true;
}
}
}
}
}
}
} else {
for (i = 1; i <= IT; i++) {
i1 = dsljhfdskjfdshkjfds(1, n - 5);
i2 = dsljhfdskjfdshkjfds(i1 + 1, n - 4);
i3 = dsljhfdskjfdshkjfds(i2 + 1, n - 3);
i4 = dsljhfdskjfdshkjfds(i3 + 1, n - 2);
i5 = dsljhfdskjfdshkjfds(i4 + 1, n - 1);
i6 = dsljhfdskjfdshkjfds(i5 + 1, n);
if (gcd(a[i1], a[i2], a[i3], a[i4], a[i5], a[i6]) == 1) return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
have[a[i]] = true;
}
if (have[1]) {
printf("1\n");
return 0;
}
for (i = 1; i <= 300000; i++)
if (have[i]) {
for (j = i + i; j <= 300000; j += i) {
have[j] = false;
}
}
n = 0;
for (i = 1; i <= 300000; i++)
if (have[i]) {
a[++n] = i;
}
for (i = 1; i <= n; i++) {
g = gcd(g, a[i]);
}
if (g > 1) {
printf("-1\n");
return 0;
}
if (try2()) {
printf("2\n");
} else if (try3()) {
printf("3\n");
} else if (try4()) {
printf("4\n");
} else if (try5()) {
printf("5\n");
} else if (try6()) {
printf("6\n");
} else {
printf("7\n");
}
return 0;
}
| 6 |
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long ll;
//using ll=long long;
using vll=vector<ll>;
ll nCr(int n, int r)
{
ll ret=1;
if(r*2>n) r=n-r;
for(int i=0;i<r;i++)
{
ret*=(n-i);
ret/=(i+1);
}
return ret;
}
int main(void)
{
int n, a, b;
while(scanf("%d%d%d", &n, &a, &b)==3)
{
vll v(n);
for(int i=0;i<n;i++) scanf("%lld", &v[i]);
sort(v.begin(), v.end(), greater<ll>());
if(v[0]!=v[a-1]) b=a;
ll sum=0;
for(int i=0;i<a;i++) sum+=v[i];
int l, r;
for(r=a-1;r<n;r++)
{
if(v[r]<v[a-1]) break;
}
for(l=a;l>0;l--)
{
if(v[l-1]>v[a-1]) break;
}
ll ans=0;
for(int i=a-1;i<min(r,b);i++)
{
ans+=nCr(r-l, i-l+1);
}
printf("%.8f\n", (double)sum/a);
printf("%lld\n", ans);
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
#define lowbit(x) x&(-x)
#define ll long long
#define mod 998244353
using namespace std;
int x[200001];int y[200001];int lsh[200001];int cnt;ll ans=0; int n;
struct point {
int x,y;
bool operator<(const point &gv)const{
return gv.x<x;}
}p[200001];ll tree[200001];ll tree2[200001];
int query(int x1){int ans=0;
for(;x1;x1-=lowbit(x1))ans+=tree[x1];return ans;
}
int query2(int x1){int ans=0;
for(;x1;x1-=lowbit(x1))ans+=tree2[x1];return ans;
}
void add(int x1,int rt){
for(;x1<=n;x1+=lowbit(x1)) tree[x1]+=rt;
}
void add2(int x1,int rt){
for(;x1<=n;x1+=lowbit(x1)) tree2[x1]+=rt;
}
inline ll power(ll a,ll b)
{
ll ans=1;
for(;b;b>>=1,a=a*a%mod)
if(b&1)ans=ans*a%mod;
return ans;
}
int main(){memset(tree,0,sizeof(tree));
cin>>n;
for(int i=1; i<=n; i++) {
scanf("%d%d",&x[i],&y[i]);
lsh[i] = x[i];
}
sort(lsh+1 , lsh+n+1);
cnt = unique(lsh+1 , lsh+n+1) - lsh - 1;
for(int i=1; i<=n; i++)
x[i] = lower_bound(lsh+1 , lsh+cnt+1 ,x[i]) - lsh;
for(int i=1; i<=n; i++) {
lsh[i] = y[i];
}
sort(lsh+1 , lsh+n+1);
cnt = unique(lsh+1 , lsh+n+1) - lsh - 1;
for(int i=1; i<=n; i++)
y[i] = lower_bound(lsh+1 , lsh+cnt+1 ,y[i]) - lsh;
for(int i=1;i<=n;i++){
p[i].x=x[i];
p[i].y=y[i];
}
sort(p+1,p+n+1);for(int i=1;i<=n;i++)tree2[i]=i&(-i);
for(int i=1;i<=n;i++){
add2(p[i].y,-1);
int A1=query(p[i].y);int B1=query(n)-query(p[i].y);int C1=query2(p[i].y);int D1=query2(n)-query2(p[i].y);
ll A2=power(2,A1),B2=power(2,B1),C2=power(2,C1),D2=power(2,D1);
ans=(ans+(A2-1)%mod*(B2)%mod*(C2)%mod*(D2-1)%mod)%mod;
ans=(ans+(A2)%mod*(B2-1)%mod*(C2-1)%mod*(D2)%mod)%mod;
ans=(ans-(A2-1)%mod*(B2-1)%mod*(C2-1)%mod*(D2-1)%mod)%mod;
ans=(ans+power(2,n-1))%mod;
add(p[i].y,1);
}cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define pb push_back
#define PII pair <int,int>
#define PLL pair <ll,ll>
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(),v.end()
#define lt 2*n
#define rt 2*n+1
#define clft 2*n,st,mid
#define crgt 2*n+1,mid+1,ed
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define eps 1e-9
#define PI acos(-1.0)
#define MAX 100010
#define INF 2000000000
/******************************************************************************************/
#define mod1 1000000007
#define mod2 1000000123
#define base 100001
vector <ll> ed[MAX];
ll has1[MAX],has2[MAX];
map <PLL,ll> M;
void dfs(ll u,ll p)
{
ll i,v,h1 = 0,h2 = 0;
for(i=0;i<ed[u].size();i++){
v = ed[u][i];
if(v==p) continue;
dfs(v,u);
h1 += has1[v];
h2 += has2[v];
h1 %= mod1;
h2 %= mod2;
}
has1[u] = (h1*base + 1)%mod1;
has2[u] = (h2*base + 1)%mod2;
M[mp(h1,h2)]++;
}
int main()
{
//ios_base::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ll n,i,u,v;
cin >> n;
for(i=1;i<n;i++){
cin >> u >> v;
ed[u].pb(v);
ed[v].pb(u);
}
dfs(1,0);
ll ans = 0;
map <PLL,ll> :: iterator it = M.begin();
while(it!=M.end()){
ans += (it->second*(it->second-1))/2;
it++;
}
cout << ans << endl;
return 0;
} | 0 |
#include <iostream>
#include <cstdio>
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
int N;
int64 S,X[MAXN],P[MAXN],ans;
void Solve() {
scanf("%d%lld",&N,&S);
for(int i = 1 ; i <= N ; ++i) {
scanf("%lld%lld",&X[i],&P[i]);
}
int L = 1,R = N;
int dir = 0;
while(1) {
if(X[L] >= S) {ans += X[R] - S;break;}
if(X[R] <= S) {ans += S - X[L];break;}
if(P[L] >= P[R]) {
if(dir != 1) {dir = 1;ans += X[R] - X[L];}
P[L] += P[R];R--;
}
else {
if(dir != 2) {dir = 2;ans += X[R] - X[L];}
P[R] += P[L];L++;
}
}
printf("%lld\n",ans);
}
int main() {
Solve();
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char str[5050];
short lcp[5050][5050], f[5050][5050];
int ans[5050];
int main() {
scanf("%s", str + 1);
int len = strlen(str + 1);
for (int i = len; i >= 1; --i) {
for (int j = 1; j <= len; ++j) {
if (str[i] == str[j]) {
lcp[i][j] = lcp[i + 1][j - 1] + 1;
} else {
lcp[i][j] = 0;
}
}
}
for (int i = 1; i <= len; ++i) {
f[i][i] = 1;
for (int j = i + 1; j <= len; ++j) {
int l = j - i + 1;
if (lcp[i][j] > l / 2) {
f[i][j] = f[i][i + l / 2 - 1] + 1;
}
}
}
for (int i = 1; i <= len; ++i) {
for (int j = i; j <= len; ++j) {
ans[f[i][j]]++;
}
}
for (int i = len; i >= 0; --i) {
ans[i] += ans[i + 1];
}
for (int i = 1; i <= len; ++i) {
cout << ans[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long n, i, j;
string s1, s2;
cin >> s1 >> s2;
scanf("%lld", &n);
string s3[n], s4[n];
for (i = 0; i < n; i++) {
cin >> s3[i] >> s4[i];
}
string tm1, tm2;
tm1 = s1;
tm2 = s2;
for (i = 0; i < n; i++) {
cout << tm1 << " " << tm2 << endl;
if (s3[i] == tm1) {
tm1 = s4[i];
} else if (s3[i] == tm2) {
tm2 = s4[i];
}
}
cout << tm1 << " " << tm2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 255;
const int RIGHT = 1;
const int DOWN = 2;
int nowarn;
int n, m, a[MAXN][MAXN];
int ALLST, EMPTYST, GRIDST, UDST, LRST;
map<int, int> f[2];
void add_to(int cur, int S, int S2) {
f[cur ^ 1][S2] = (f[cur ^ 1][S2] + f[cur][S]) % MOD;
}
void update(int cur, int x, int y) {
for (map<int, int>::iterator it = f[cur].begin(); it != f[cur].end(); ++it) {
int S = it->first;
if (a[x][y] == 1) {
int S2 = ((S << 2) & GRIDST) | (S & EMPTYST);
add_to(cur, S, S2);
} else {
int S2 = ((S << 2) & GRIDST) | (S & EMPTYST) | RIGHT | DOWN;
add_to(cur, S, S2);
if ((!(S & LRST) && !(S & UDST)) || (y == 1 && !(S & UDST))) {
if (S & EMPTYST) continue;
S2 = ((S << 2) & GRIDST) | EMPTYST;
add_to(cur, S, S2);
} else {
S2 = ((S << 2) & GRIDST) | (S & EMPTYST);
if ((S & LRST) && y != 1) S2 |= RIGHT;
if (S & UDST) S2 |= DOWN;
add_to(cur, S, S2);
}
}
}
}
void solve() {
int cur = 0;
f[cur][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
f[cur ^ 1].clear();
update(cur, i, j);
cur ^= 1;
}
int ans = 0;
for (map<int, int>::iterator it = f[cur].begin(); it != f[cur].end(); ++it)
ans = (ans + it->second) % MOD;
printf("%d\n", ans);
}
int main() {
nowarn = scanf("%d%d", &n, &m);
if (n > m) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
char ch;
nowarn = scanf(" %c", &ch);
a[i][j] = (ch == '.' ? 0 : 1);
}
} else {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
char ch;
nowarn = scanf(" %c", &ch);
a[j][i] = (ch == '.' ? 0 : 1);
}
swap(n, m);
}
ALLST = (1 << (m * 2 + 1)) - 1;
EMPTYST = 1 << m * 2;
GRIDST = EMPTYST - 1;
UDST = EMPTYST >> 1;
LRST = 1;
solve();
return 0;
}
| 6 |
#include<iostream>
using namespace std;
int main()
{
int n, r, k;
cin>>n>>r;
if(n>=10)
cout<<r;
else
cout<<r+100*(10-n)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
map<pair<int, pair<int, int> >, bool> v;
int n = s.size();
int i;
int x = 0, y = 0;
long long ans = 0;
for (i = 0; i < n; i++) {
if (s[i] == 'N') {
if (v[{1, {x, y}}])
ans++;
else {
ans += 5;
v[{1, {x, y}}] = 1;
}
y++;
v[{3, {x, y}}] = 1;
}
if (s[i] == 'E') {
if (v[{2, {x, y}}])
ans++;
else {
ans += 5;
v[{2, {x, y}}] = 1;
}
x++;
v[{4, {x, y}}] = 1;
}
if (s[i] == 'S') {
if (v[{3, {x, y}}])
ans++;
else {
ans += 5;
v[{3, {x, y}}] = 1;
}
y--;
v[{1, {x, y}}] = 1;
}
if (s[i] == 'W') {
if (v[{4, {x, y}}])
ans++;
else {
ans += 5;
v[{4, {x, y}}] = 1;
}
x--;
v[{2, {x, y}}] = 1;
}
}
cout << ans << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
template <typename T, typename U>
inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
if (x < y) x = y;
}
int main() {
int i, j, k;
string s1, s2, s3, s4, s5;
s1 = "Danil";
s2 = "Olya";
s3 = "Slava";
s4 = "Ann";
s5 = "Nikita";
string s;
cin >> s;
int x = s1.size();
int y = s.size();
int cnt1 = 0;
int chk = -1;
if (x > y)
cnt1 = 0;
else {
for (i = 0; i < y; i += 1) {
chk = 1;
if (y - i + 1 < x)
break;
else {
k = i;
for (j = 0; j < x; j += 1) {
if (s1[j] != s[k]) {
chk = -1;
break;
}
k += 1;
}
if (chk == 1) cnt1 += 1;
}
}
}
x = s2.size();
y = s.size();
int cnt2 = 0;
chk = -1;
if (x > y)
cnt2 = 0;
else {
for (i = 0; i < y; i += 1) {
chk = 1;
if (y - i + 1 < x)
break;
else {
k = i;
for (j = 0; j < x; j += 1) {
if (s2[j] != s[k]) {
chk = -1;
break;
}
k += 1;
}
if (chk == 1) cnt2 += 1;
}
}
}
x = s3.size();
y = s.size();
int cnt3 = 0;
chk = -1;
if (x > y)
cnt3 = 0;
else {
for (i = 0; i < y; i += 1) {
chk = 1;
if (y - i + 1 < x)
break;
else {
k = i;
for (j = 0; j < x; j += 1) {
if (s3[j] != s[k]) {
chk = -1;
break;
}
k += 1;
}
if (chk == 1) cnt3 += 1;
}
}
}
x = s4.size();
y = s.size();
int cnt4 = 0;
chk = -1;
if (x > y)
cnt4 = 0;
else {
for (i = 0; i < y; i += 1) {
chk = 1;
if (y - i + 1 < x)
break;
else {
k = i;
for (j = 0; j < x; j += 1) {
if (s4[j] != s[k]) {
chk = -1;
break;
}
k += 1;
}
if (chk == 1) cnt4 += 1;
}
}
}
x = s5.size();
y = s.size();
int cnt5 = 0;
chk = -1;
if (x > y)
cnt5 = 0;
else {
for (i = 0; i < y; i += 1) {
chk = 1;
if (y - i + 1 < x)
break;
else {
k = i;
for (j = 0; j < x; j += 1) {
if (s5[j] != s[k]) {
chk = -1;
break;
}
k += 1;
}
if (chk == 1) cnt5 += 1;
}
}
}
int sum = cnt1 + cnt2 + cnt3 + cnt4 + cnt5;
if (sum == 1)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);
template <class T, class S>
inline void add(T& a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T& a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T& a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T& a, S b) {
return a > b ? a = b, true : false;
}
int n, m, x[N];
pair<int, int> hole[N];
long long dp[2][N];
int c;
long long sum[N];
long long *f = dp[0], *g = dp[1];
void solve(int L, int R, int l, int r) {
if (l > r) return;
int mid = l + r >> 1, p = R;
for (int i = max(L, mid - c); i <= min(R, mid); i++)
if (g[i] + sum[mid] - sum[i] < f[mid])
f[mid] = g[i] + sum[mid] - sum[i], p = i;
solve(L, p, l, mid - 1);
solve(p, R, mid + 1, r);
}
int main() {
scanf("%d%d", &n, &m);
int tot = 0;
for (int i = 1; i <= n; i++) scanf("%d", &x[i]);
for (int i = 1; i <= m; i++)
scanf("%d%d", &hole[i].first, &hole[i].second), tot += hole[i].second;
if (tot < n) return puts("-1"), 0;
sort(x + 1, x + 1 + n);
sort(hole + 1, hole + 1 + m);
memset(f, INF, (n + 1) * sizeof(long long));
f[0] = 0;
int tmp = 0;
for (int i = 1; i <= m; i++) {
swap(f, g);
tmp += hole[i].second;
memset(f, INF, (n + 1) * sizeof(long long));
c = hole[i].second;
for (int j = 1; j <= n; j++)
sum[j] = sum[j - 1] + abs(x[j] - hole[i].first);
solve(0, n, 0, n);
}
printf("%lld\n", f[n]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
map<string, int> record;
struct player {
string ratings;
string name;
int scr;
void getratings(int bnum) {
if (bnum * 2 > n) {
ratings = "noob";
return;
}
if (bnum * 5 > n) {
ratings = "random";
return;
}
if (bnum * 10 > n) {
ratings = "average";
return;
}
if (bnum * 100 > n) {
ratings = "hardcore";
return;
}
ratings = "pro";
}
};
vector<player> ans;
bool cmpscr(player a, player b) { return a.scr > b.scr; }
bool cmpname(player a, player b) { return a.name < b.name; }
int main() {
cin >> n;
string name;
for (int i = 0; i < n; i++) {
int scr;
cin >> name >> scr;
record[name] = max(record[name], scr);
}
int pln = record.size();
ans.resize(pln);
map<string, int>::iterator i;
int cnt = 0;
for (i = record.begin(); i != record.end(); i++) {
ans[cnt].name = i->first;
ans[cnt].scr = i->second;
cnt++;
}
sort(ans.begin(), ans.end(), cmpscr);
int bnum = 0;
n = pln;
for (int i = 0; i < cnt; i++) {
if (i > 0 && ans[i].scr != ans[i - 1].scr) bnum = i;
ans[i].getratings(bnum);
}
sort(ans.begin(), ans.end(), cmpname);
cout << cnt << endl;
for (int i = 0; i < cnt; i++)
cout << ans[i].name << " " << ans[i].ratings << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long n, a, b, x, y, t, m, c, p;
string s;
bool bole, bolo;
int main() {
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'C') {
if (p) {
m++;
p = 0;
}
c++;
if (c == 5) {
m++;
c = 0;
}
}
if (s[i] == 'P') {
if (c) {
m++;
c = 0;
}
p++;
if (p == 5) {
m++;
p = 0;
}
}
}
if (p || c) m++;
cout << m << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, m, x, i, flag = 0;
cin >> a >> m;
x = a;
for (i = 1; i <= (2 * m); i++) {
x += x % m;
if (x % m == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << "No";
else
cout << "Yes";
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 1 << 20;
long long lazy[N << 1], val[N << 1], sum[N << 1], mul[N << 1], mn[N << 1],
mnpos[N << 1];
void pd(int i, int l, int r) {
if (lazy[i] != -1e18) {
val[i] = lazy[i];
sum[i] = lazy[i] * mul[i];
if (0 <= sum[i])
mn[i] = 0, mnpos[i] = l;
else
mn[i] = sum[i], mnpos[i] = r;
if (i < N) {
lazy[i << 1] = lazy[i];
lazy[i << 1 | 1] = lazy[i];
}
lazy[i] = -1e18;
}
}
void upd(int a, int b, int i, int l, int r, int v) {
pd(i, a, b);
if (l <= a && b <= r) {
lazy[i] = v;
pd(i, a, b);
return;
}
if (r <= a || b <= l) return;
int md = (a + b) >> 1;
upd(a, md, i << 1, l, r, v);
upd(md, b, i << 1 | 1, l, r, v);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
if (mn[i << 1] <= sum[i << 1] + mn[i << 1 | 1])
mn[i] = mn[i << 1], mnpos[i] = mnpos[i << 1];
else
mn[i] = sum[i << 1] + mn[i << 1 | 1], mnpos[i] = mnpos[i << 1 | 1];
}
void upd(int l, int r, int v) { upd(0, N, 1, l, r, v); }
long long qry(int a, int b, int i, int l, int r) {
pd(i, a, b);
if (l <= a && b <= r) return sum[i];
if (r <= a || b <= l) return 0;
int md = (a + b) >> 1;
long long L = qry(a, md, i << 1, l, r);
long long R = qry(md, b, i << 1 | 1, l, r);
return L + R;
}
long long qry(int l, int r) { return qry(0, N, 1, l, r); }
struct YYY {
long long sum, mn;
YYY() {}
YYY(long long a, long long b) : sum(a), mn(b) {}
};
YYY yyy[N << 1];
void qryyy(int a, int b, int i, int l, int r) {
pd(i, a, b);
if (l <= a && b <= r) {
yyy[i] = YYY(sum[i], mn[i]);
return;
}
if (r <= a || b <= l) {
yyy[i] = YYY(0, 0);
return;
}
int md = (a + b) >> 1;
qryyy(a, md, i << 1, l, r);
qryyy(md, b, i << 1 | 1, l, r);
yyy[i].sum = yyy[i << 1].sum + yyy[i << 1 | 1].sum;
if (yyy[i << 1].mn < yyy[i << 1].sum + yyy[i << 1 | 1].mn)
yyy[i].mn = yyy[i << 1].mn;
else
yyy[i].mn = yyy[i << 1].sum + yyy[i << 1 | 1].mn;
}
void qryyy(int l, int r) { qryyy(0, N, 1, l, r); }
int qryans(int a, int b, int i, int l, int r, long long v) {
pd(i, a, b);
if (r <= a || b <= l) return -1;
int md = (a + b) >> 1;
if (i < N) {
pd(i << 1, a, md);
pd(i << 1 | 1, md, b);
long long lsum, rsum, lmn, rmn;
if (l <= a && b <= r) {
lsum = sum[i << 1];
lmn = mn[i << 1];
rsum = sum[i << 1 | 1];
rmn = mn[i << 1 | 1];
} else {
lsum = yyy[i << 1].sum;
lmn = yyy[i << 1].mn;
rsum = yyy[i << 1 | 1].sum;
rmn = yyy[i << 1 | 1].mn;
}
if (lmn + v <= 0) return qryans(a, md, i << 1, l, r, v);
if (lsum + rmn + v <= 0) return qryans(md, b, i << 1 | 1, l, r, v + lsum);
return -1;
} else
return mn[i] + v <= 0 ? mnpos[i] : -1;
}
int qryans(int l, int r, int v) { return qryans(0, N, 1, l, r, v); }
long long qrys(int a, int b, int i, int x) {
pd(i, a, b);
if (a + 1 == b) return val[i];
int md = (a + b) >> 1;
if (x < md) return qrys(a, md, i << 1, x);
return qrys(md, b, i << 1 | 1, x);
}
long long qrys(int x) { return qrys(0, N, 1, x); }
struct query {
int tp, A, B, C;
query() {}
query(int a, int b, int c, int d) : tp(a), A(b), B(c), C(d) {}
};
vector<query> vquery;
vector<int> vtime;
struct event {
int t, s;
event() {}
event(int a, int b) : t(a), s(b) {}
bool operator<(const event &x) const { return t < x.t; }
};
set<event> events;
int getId(int tme) {
return lower_bound(vtime.begin(), vtime.end(), tme) - vtime.begin();
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int tp;
scanf("%d", &tp);
if (tp == 1) {
int t, s;
scanf("%d%d", &t, &s);
vquery.push_back(query(tp, t, s, -1));
vtime.push_back(t);
}
if (tp == 2) {
int t;
scanf("%d", &t);
vquery.push_back(query(tp, t, -1, -1));
}
if (tp == 3) {
int l, r, v;
scanf("%d%d%d", &l, &r, &v);
vquery.push_back(query(tp, l, r, v));
vtime.push_back(l);
vtime.push_back(r);
}
}
vtime.push_back(0);
vtime.push_back(1e9);
sort(vtime.begin(), vtime.end());
vtime.erase(unique(vtime.begin(), vtime.end()), vtime.end());
for (int i = 0; i < (N << 1); ++i) lazy[i] = -1e18;
for (int i = 0; i + 1 < vtime.size(); ++i)
mul[i + N] = vtime[i + 1] - vtime[i];
for (int i = N - 1; ~i; --i) mul[i] = mul[i << 1] + mul[i << 1 | 1];
for (int i = 0; i < n; ++i) {
int tp = vquery[i].tp;
if (tp == 1) {
int t = vquery[i].A;
int s = vquery[i].B;
set<event>::iterator it, jt;
events.insert(event(t, s));
it = events.find(event(t, s));
int nxt = 1e9;
jt = it;
if (jt != events.end()) {
++jt;
if (jt != events.end()) {
nxt = jt->t;
}
}
int L = getId(t);
int R = getId(nxt);
upd(L, R, s);
}
if (tp == 2) {
int t = vquery[i].A;
set<event>::iterator it, jt, kt;
it = events.lower_bound(event(t, -1e9));
int s = it->s;
it = events.erase(it);
int lsts = 0;
jt = it;
if (jt != events.begin()) {
--jt;
lsts = jt->s;
}
int nxt = 1e9;
if (it != events.end()) {
nxt = it->t;
}
int L = getId(t);
int R = getId(nxt);
upd(L, R, lsts);
}
if (tp == 3) {
int l = getId(vquery[i].A);
int r = getId(vquery[i].B);
int v = vquery[i].C;
set<event>::iterator it = events.lower_bound(event(vtime[l], -1e9));
int fst = 1e9;
if (it != events.end()) fst = it->t;
fst = getId(fst);
int fsts = qrys(l);
upd(l, fst, 0);
double ans = -1;
if (!v)
ans = vtime[l];
else {
qryyy(l, r);
int x = qryans(l, r, v);
if (~x) {
--x;
ans = vtime[x];
long long lft = qry(l, x);
long long nws = -qrys(x);
if (lft + v) ans += 1.0 * (lft + v) / nws;
if (ans > vtime[r]) ans = -1;
}
}
printf("%.7f\n", ans);
upd(l, fst, fsts);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define vc vector<ll>
#define um unordered_map<ll,ll>
#define ll long long
#define rep(i,a,b) for(__typeof(a) i=a; i<b; i++)
#define si(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define ss(a) a.second
#define ff(a) a.first
#define pb push_back
#define repit(i,a) for(auto i=a.begin(); i!=a.end(); i++)
#define take(arr,n) for(int i=0;i<n;i++)cin>>arr[i]
#define out(arr,n) for(int i=0;i<n;i++)cout<<arr[i]<<' '; cout<<endl
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define reprev(i,b,a) for(__typeof(b) i=b; i>=a; i--)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
#define doublearray vector<vector<int>>
#define mod (int)(1000000007)
#define MAX (ll)1e18
using namespace std;
ll min(ll a, ll b){
return a<b?a:b;
}
ll max(ll a, ll b){
return a>b?a:b;
}
ll gcd(ll a, ll b){
if(b==0) return a;
return gcd(b, a%b);
}
ll power(ll a, ll b){
ll ans = 1;
while(b){
if(b&1){
b--; ans = (ans*a)%mod;
}
b/=2; a = (a*a)%mod;
}
return ans;
}
// int vis[400001];
// int countCycles(vector<vector<int>> &gp, int i, int viscur[]){
// if(viscur[i]) return 1;
// if(vis[i]) return 0;
// vis[i] = 1; viscur[i] = 1;
// int cntcyc = 0;
// rep(j,0,si(gp[i])){
// cntcyc += countCycles(gp, gp[i][j], viscur);
// }
// viscur[i] = 0;
// return cntcyc;
// }
ll findceil(ll a, ll b){
if(a%b == 0){
return (a/b);
}
else{
return (a/b+1);
}
}
int main(){
fastio;
int q; cin>>q;
while(q--){
int n,k; cin>>n>>k;
ll a[k], t[k];
take(a,k); take(t,k);
unordered_map<ll,ll>hash;
map<ll,ll>mp;
rep(i,0,k){
hash[a[i]] = t[i];
mp[(t[i]+(a[i]-1))]++;
}
ll ans[n+1];
ll lftMin = -1;
rep(i,1,n+1){
if(lftMin == -1){
ans[i] = ((mp.begin()->first)-(i-1));
}
else if(mp.empty()){
ans[i] = lftMin+1;
lftMin++;
}
else{
ans[i] = min((mp.begin()->first)-(i-1), lftMin+1);
lftMin++;
}
if(hash.find(i) != hash.end()){
mp[hash[i]+i-1]--;
if(mp[hash[i]+i-1] == 0){
mp.erase(hash[i]+i-1);
}
if(lftMin == -1){
lftMin = hash[i];
}
else
lftMin = min(lftMin, hash[i]);
}
}
rep(i,1,n+1) cout<<ans[i]<<' ';
cout<<endl;
}
return 0;
} | 5 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int x = 0, y = 0;
cin >> x >> y;
cout << min({x, y, (x + y) / 3}) << "\n";
}
int main() {
int t = 0;
cin >> t;
while (t--) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long* input(long long n) {
long long* arr = new long long[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
return arr;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
signed main() {
long long n;
cin >> n;
long long i, a[n + 1], b[n + 1];
for (i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
for (i = 1; i <= n; i++) {
if (i <= n / 2 || a[i] < b[n - i + 1])
cout << "1";
else
cout << "0";
}
cout << endl;
for (i = 1; i <= n; i++) {
if (i <= n / 2 || b[i] < a[n - i + 1])
cout << "1";
else
cout << "0";
}
}
| 2 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<unordered_map>
#include<functional>
#include<map>
#include<iomanip>
#include<limits>
#include<unordered_set>
#include<cmath>
#include <numeric>
#include <array>
#include<utility>
#include <complex>
#define M_PI 3.141592653589793238
using namespace std;
long long p9 = 998244353;
long long p1 = 1000000007;
#define upperbound(v,val) upper_bound(v.begin(),v.end(),val)-v.begin()
#define lowerbound(v,val) lower_bound(v.begin(),v.end(),val)-v.begin()
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp(a,b) make_pair(a,b)
#define pin pair<int,int>
#define qin pair<pin,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define fcout cout << fixed << setprecision(15)
#define rev(s) reverse(s.begin(),s.end())
#define lower(h,val) (lower_bound(h.begin(),h.end(),val)-h.begin())
#define upper(h,val) (upper_bound(h.begin(),h.end(),val)-h.begin())
#define vveb V<veb>
#define omajinai cin.tie(0);ios::sync_with_stdio(false);
#define endl "\n"
#define pb push_back
vel kai;
vel inv_kai;
vel inv;
int root(int x, vel& pa) {
if (pa[x] == -1) { return x; }
int ans = root(pa[x], pa); pa[x] = ans;
return ans;
}
bool mar(int x, int y, vel& pa) {
x = root(x, pa);
y = root(y, pa);
if (x != y) { pa[x] = y; }
return (x != y);
}
int gcd(int x, int y) {
if (x < y) { return gcd(y, x); }
if (y == 0) { return x; }
return gcd(y, x % y);
}
int lcm(int x, int y) {
x = abs(x); y = abs(y);
return x * (y / gcd(x, y));
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
void make_inv(int max_inv, int p) {
inv = vel(max_inv + 1, 1);
for (int i = 2; i <= max_inv; i++) {
inv[i] = p - ((p / i) * inv[p % i]) % p;
}
}
void make_kai(int max_kai, int p) {
kai = vel(max_kai + 1, 1);
inv_kai = kai;
make_inv(max_kai, p);
rep(i, max_kai) {
kai[i + 1] = kai[i] * (i + 1); kai[i + 1] %= p;
inv_kai[i + 1] = inv_kai[i] * inv[i + 1]; inv_kai[i + 1] %= p;
}
}
int com(int n, int r, int p) {
if ((n < 0) || (r < 0) || (r > n)) { return 0; }
int ans = (kai[n] * inv_kai[r]) % p;
return (ans * inv_kai[n - r]) % p;
}
int per(int n, int r, int p) {
if ((n < 0) || (r < 0) || (r > n)) { return 0; }
return (kai[n] * inv_kai[n - r]) % p;
}
vel dijk(V<V<pin>> way, int st, int inf) {
int n = way.size();
vel dist(n, inf); dist[st] = 0;
priority_queue<pin, vector<pin>, greater<pin>> pq;
pq.push(mkp(0, st));
veb is_checked(n, false);
while (!pq.empty()) {
pin x = pq.top(); pq.pop();
int pot = x.second;
if (!is_checked[pot]) {
is_checked[pot] = true;
for (auto y : way[pot]) {
int nex_dist = x.first + y.second;
int nex_pot = y.first;
if (dist[nex_pot] > nex_dist) {
dist[nex_pot] = nex_dist;
pq.push(mkp(nex_dist, y.first));
}
}
}
}
return dist;
}
V<V<pin>> make_w(vvel v) {
int n = v.size();
V<V<pin>> ret(n);
rep(i, n) {
for (int x : v[i]) {
ret[i].push_back(mkp(x, 1));
}
}
return ret;
}
void make_tree(vvel& chi, vel& par, int n) {
V<V<pin>> way(n);
rep(i, n - 1) {
int a, b; cin >> a >> b; a--; b--;
way[a].push_back(mkp(b, 1));
way[b].push_back(mkp(a, 1));
}
vel dist = dijk(way, 0, n + 1);
par = vel(n, -1);
chi = vvel(n);
rep(i, n) {
for (auto nex : way[i]) {
int pot = nex.first;
if (dist[pot] > dist[i]) { chi[i].push_back(pot); }
else { par[i] = pot; }
}
}
}
void pri(vel& v) {
if (v.size() == 0) { return; }
cout << v[0];
rep(i, v.size() - 1) { cout << " " << v[i + 1]; }
cout << endl;
return;
}
int modpow(int a, int n, int p) {
if (n == 0) { return 1; }
int m = n / 2;
int x = modpow(a, n / 2, p);
x *= x; x %= p;
if (n % 2 == 1) { x *= a; x %= p; }
return x;
}
#define ui long long
vvel disj_min(vel& v) {
int n = v.size();
vvel ret(22, vel(n));
ret[0] = v;
rep(i, 21) {
rep(j, n) {
int nex = j + (1 << i);
if (nex < n) {
ret[i + 1][j] = min(ret[i][j], ret[i][nex]);
}
else {
ret[i + 1][j] = ret[i][j];
}
}
}
return ret;
}
int find_min(vvel& dv, int l, int r) {
int i = 21;
while (l + (1 << i) > r) {
i--;
}
while (i >= 0) {
if (dv[i][l] > dv[i][r - (1 << i)]) {
l = r - (1 << i);
}
else {
r = l + (1 << i);
}
i--;
}
return l;
}
V<V<pin>> dbl(V<pin>& v) {
V<V<pin>> ans(20, V<pin>(v));
int n = v.size();
rep(i, 19) {
rep(j, n) {
ans[i + 1][j].first = ans[i][ans[i][j].first].first;
ans[i + 1][j].second = max(ans[i][j].second, ans[i][ans[i][j].first].second);
}
}
return ans;
}
int lca(int s, int t, int diff, V<V<pin>>& pa) {
if (diff < 0) { return lca(t, s, -diff, pa); }
int ans = 0;
rep(i, 19) {
if ((diff & (1 << i)) != 0) {
mmax(ans, pa[i][s].second);
s = pa[i][s].first;
}
}
for (int i = 19; i >= 0; i--) {
if (pa[i][s] != pa[i][t]) {
mmax(ans, pa[i][s].second);
s = pa[i][s].first;
mmax(ans, pa[i][t].second);
t = pa[i][t].first;
}
}
if (s != t) {
mmax(ans, pa[0][s].second);
mmax(ans, pa[0][t].second);
}
return ans;
}
void alp(int n, vel& pr) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
pr.push_back(i);
while (n % i == 0) { n /= i; }
}
}
if (n != 1) { pr.push_back(n); }
}
vel dx = { 0,0,-1,1,1,-1 };
vel dy = { 1,-1,0,0,1,1 };
#define all(a) a.begin(),a.end()
template<typename T>
void mk_uni(V<T>& a) {
std::sort(a.begin(), a.end());
a.erase(std::unique(a.begin(), a.end()), a.end());
}
signed main() {
omajinai;
int p = p1;
int n, m; cin >> n >> m;
vvel dp(n + 1, vel(m + 1, 0));
for (int i = 1; i <= m; i++) {
dp[1][i] = modpow(2, i, p);
}
for (int i = 2; i <= n; i++) {
int sum = 0;
for (int j = 1; j <= m; j++) {
dp[i][j] += sum;
dp[i][j] += dp[i - 1][j] * (j + 1);
dp[i][j] %= p;
sum *= 2;
sum += dp[i - 1][j] * j;
sum %= p;
}
}
cout << dp[n][m] << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
const int N = 600100;
using namespace std;
int a[N];
long long sum1[N];
long long sum2[N];
long long pre_sum1[N];
long long pre_sum2[N];
long long inc_sum1[N];
long long inc_sum2[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> a[2 * n - i];
for (int i = 1; i <= n + n; i++) pre_sum1[i] = pre_sum1[i - 1] + a[i];
for (int i = 1; i <= n + n; i++)
pre_sum2[i] = pre_sum2[i - 1] + a[2 * n - i + 1];
for (int i = 1; i <= n + n; i++) sum1[i] = 1ll * i * a[i];
for (int i = 1; i <= n + n; i++) sum2[i] = 1ll * i * a[2 * n - i + 1];
for (int i = 1; i <= n + n; i++) inc_sum1[i] = inc_sum1[i - 1] + sum1[i];
for (int i = 1; i <= n + n; i++) inc_sum2[i] = inc_sum2[i - 1] + sum2[i];
long long res = 0;
for (int i = 1; i <= n + n; i++) res += 1ll * (i - 1) * a[i];
long long alt = 0;
for (int i1 = 1, i2 = 2 * n, timer = 0; i1 <= n; i1++, i2--, timer++) {
if (i1 & 1) {
alt += 1ll * timer * a[i1];
timer++;
alt += 1ll * timer * a[i2];
} else {
alt += 1ll * timer * a[i2];
timer++;
alt += 1ll * timer * a[i1];
}
long long curr_res = alt;
if (i1 + 1 == i2) {
res = max(res, curr_res);
continue;
}
if (!(i1 & 1))
curr_res += 1ll * (timer - i1) * (pre_sum1[i2 - 1] - pre_sum1[i1]) +
(inc_sum1[i2 - 1] - inc_sum1[i1]);
else
curr_res += 1ll * (timer - i1) * (pre_sum2[i2 - 1] - pre_sum2[i1]) +
(inc_sum2[i2 - 1] - inc_sum2[i1]);
res = max(res, curr_res);
}
cout << res << '\n';
return 0;
}
| 3 |
#include<cstdio>
using namespace std;
int n;
int a[100]={1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
int main(){
scanf("%d",&n);
printf("%d",a[n-1]);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> di[N];
int s[8];
int main() {
for (int i = (int)(1); i <= (int)(N - 1); i++)
for (int j = (int)(1); j <= (int)((N - 1) / i); j++) di[i * j].push_back(i);
int T;
scanf("%d", &T);
while (T--) {
int x, y, z, ans = 0;
scanf("%d%d%d", &x, &y, &z);
memset(s, 0, sizeof(s));
for (int i = (int)(0); i <= (int)(di[x].size() - 1); i++) {
int fl = 1;
if (y % di[x][i] == 0) fl |= 2;
if (z % di[x][i] == 0) fl |= 4;
s[fl]++;
}
for (int i = (int)(0); i <= (int)(di[y].size() - 1); i++) {
if (x % di[y][i] == 0) continue;
int fl = 2;
if (z % di[y][i] == 0) fl |= 4;
s[fl]++;
}
for (int i = (int)(0); i <= (int)(di[z].size() - 1); i++) {
if (x % di[z][i] == 0) continue;
if (y % di[z][i] == 0) continue;
s[4]++;
}
for (int i = (int)(1); i <= (int)(7); i++)
for (int j = (int)(i); j <= (int)(7); j++)
for (int k = (int)(j); k <= (int)(7); k++) {
if ((i | j | k) != 7) continue;
if ((i != 1) + (j != 1) + (k != 1) <= 1) continue;
if ((i != 2) + (j != 2) + (k != 2) <= 1) continue;
if ((i != 4) + (j != 4) + (k != 4) <= 1) continue;
if (i != j && i != k && j != k)
ans += s[i] * s[j] * s[k];
else if (i == k)
ans += s[i] * (s[i] + 1) * (s[i] + 2) / 6;
else if (i == j)
ans += s[i] * (s[i] + 1) / 2 * s[k];
else
ans += s[i] * s[j] * (s[j] + 1) / 2;
}
printf("%d\n", ans);
}
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
unsigned long long Total;
unsigned long long Array[200001];
unsigned long long Brray[200001];
unsigned long long Crray[200001];
unsigned long long Count[101];
int main(void) {
register unsigned long long i, j;
cin >> Total;
for (i = 1; i <= Total; i++) {
cin >> Brray[i];
}
for (i = 1; i <= Total; i++) {
cin >> Crray[i];
}
register unsigned long long Sum;
Sum = 0;
for (i = 1; i <= Total; i++) {
Sum += Brray[i];
}
for (i = 1; i <= Total; i++) {
Sum += Crray[i];
}
if (Sum % (Total * 2) != 0) {
goto Impossible;
}
Sum /= (Total * 2);
for (i = 1; i <= Total; i++) {
Array[i] = Brray[i] + Crray[i] - Sum;
if (Array[i] % Total != 0) {
goto Impossible;
}
Array[i] /= Total;
for (j = 1; j <= 63; j++) {
if ((Array[i] & (unsigned long long)pow(2, j - 1)) != 0) {
Count[j]++;
}
}
}
for (i = 1; i <= Total; i++) {
register unsigned long long Temp;
Temp = 0;
for (j = 1; j <= 63; j++) {
if ((Array[i] & (unsigned long long)pow(2, j - 1)) != 0) {
Temp += Count[j] * (unsigned long long)pow(2, j - 1);
}
}
if (Temp != Brray[i]) {
goto Impossible;
}
Temp = Sum;
for (j = 1; j <= 63; j++) {
if ((Array[i] & (unsigned long long)pow(2, j - 1)) != 0) {
Temp += (Total - Count[j]) * (unsigned long long)pow(2, j - 1);
}
}
if (Temp != Crray[i]) {
goto Impossible;
}
}
for (i = 1; i <= Total; i++) {
cout << Array[i] << ' ';
}
cout << endl;
return 0;
Impossible:
cout << -1 << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int arr[100][100];
int vis[100][100];
int n;
int in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < n; }
int edge(int r, int c) { return !in(r, c) || (arr[r][c] == 1); }
int main() {
cin >> n;
pair<int, int> topLeft4(-1, -1), bottomRight4(-1, -1);
int nums[5] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char c;
cin >> c;
arr[i][j] = c - '0';
nums[arr[i][j]]++;
if (arr[i][j] == 4) {
if (topLeft4.first == -1) {
topLeft4 = make_pair(i, j);
}
bottomRight4 = make_pair(i, j);
}
}
}
int good = topLeft4.first != -1;
if (nums[4] != (bottomRight4.first - topLeft4.first + 1) *
(bottomRight4.second - topLeft4.second + 1)) {
good = 0;
}
for (int i = topLeft4.first; i <= bottomRight4.first; i++) {
for (int j = topLeft4.second; j <= bottomRight4.second; j++) {
if (arr[i][j] != 4) {
good = 0;
}
}
}
good &= edge(topLeft4.first - 1, topLeft4.second - 1) &&
edge(topLeft4.first - 1, bottomRight4.second + 1) &&
edge(bottomRight4.first + 1, topLeft4.second - 1) &&
edge(bottomRight4.first + 1, bottomRight4.second + 1);
for (int i = topLeft4.first - 1; i <= bottomRight4.first + 1; i++) {
for (int j = topLeft4.second - 1; j <= bottomRight4.second + 1; j++) {
if (((i == topLeft4.first - 1) && (j == topLeft4.second - 1)) ||
((i == topLeft4.first - 1) && (j == bottomRight4.second + 1)) ||
((i == bottomRight4.first + 1) && (j == topLeft4.second - 1)) ||
((i == bottomRight4.first + 1) && (j == bottomRight4.second + 1))) {
;
} else if ((i == topLeft4.first - 1) || (i == bottomRight4.first + 1) ||
(j == topLeft4.second - 1) || (j == bottomRight4.second + 1)) {
good &= !in(i, j) || (arr[i][j] == 2);
}
if (in(i, j)) {
vis[i][j] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!vis[i][j] && arr[i][j]) {
good = 0;
}
}
}
if (good) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ld = long double;
using pii = pair<int, int>;
template <class F>
void bleh(int i, int n, F f) {
f(i);
poop(i + 1, n, f);
}
template <class F>
void poop(int i, int n, F f) {
return (i == n) ? void() : bleh(i, n, f);
}
int main() {
int n;
cin >> n;
int oo = (int)1e9;
vector<vector<int>> d(n, vector<int>(n, oo));
poop(0, n, [&](int i) { d[i][i] = 0; });
poop(0, n, [&](int i) { poop(0, n, [&](int j) { cin >> d[i][j]; }); });
poop(0, n, [&](int k) {
poop(0, n, [&](int i) {
poop(0, n, [&](int j) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); });
});
});
int ans = 0;
poop(0, n, [&](int i) {
poop(0, n, [&](int j) { ans = (d[i][j] < oo) ? max(ans, d[i][j]) : ans; });
});
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e9;
const static ll MOD = 1e9+7;
int main(){
ll N; cin >> N;
vector<ll> T(N), A(N), V(N);
for(int i = 0; i < N; i++) cin >> T[i];
for(int i = 0; i < N; i++) cin >> A[i];
V[0] = 1;
if(T[N-1] != A[0]){cout << 0 << endl; return 0;}
for(int i = 1; i < N; i++){
if(T[i-1] != T[i]) {
V[i] = 1;
if(T[i] > A[i]){ cout << 0 << endl; return 0;}
}
else V[i] = T[i];
}
V[N-1] = 1;
for(int i = N-2; i >= 0; i--){
if(A[i+1] != A[i]) {
V[i] = 1;
if(A[i] > T[i]){ cout << 0 << endl; return 0;}
}
else { if(V[i] != 1) V[i] = min(V[i], A[i]); }
}
ll ans = 1;
for(int i = 0; i < N; i++){
ans *= V[i];
ans%= MOD;
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
long long int i, arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n, greater<int>());
long long int max = arr[0], min = 0, ans = 0;
for (i = 1; i <= k; i++) {
max += arr[i];
arr[i] = 0;
}
sort(arr, arr + n);
min = arr[0];
ans = max - min;
cout << ans << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
string ans = "0000000000";
for (int i = 0; i < n; ++i) {
if (s[i] == 'L') {
for (int j = 0; j < 10; ++j) {
if (ans[j] == '0') {
ans[j] = '1';
break;
}
}
} else if (s[i] == 'R') {
for (int j = 9; j >= 0; --j) {
if (ans[j] == '0') {
ans[j] = '1';
break;
}
}
} else {
ans[s[i] - '0'] = '0';
}
}
cout << ans << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char n[5] = "";
for (int i = 0; i < 4; i++)
cin >> n[i];
sort(n, n + 4);
cout << (string(n) == "1479" ? "YES" : "NO") << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int h, m, nQ;
const int MAX_N = int(2e5) + 10;
set<int> emptySet[MAX_N];
vector<int> seqs[MAX_N];
int id[MAX_N], idx[MAX_N], cnt[MAX_N];
int nId;
int findNext(set<int>& s, int me) {
set<int>::iterator it = s.lower_bound(me);
if (it == s.end())
return *s.begin();
else
return *it;
}
map<int, int> where;
int main() {
cin >> h >> m >> nQ;
memset(id, -1, sizeof id);
nId = 0;
for (int i = 0; i < h; ++i) {
if (id[i] == -1) {
int x = i, cur = 0;
while (id[x] == -1) {
id[x] = nId;
idx[x] = cur++;
seqs[nId].push_back(x);
x += m;
x %= h;
}
cnt[nId] = cur;
nId++;
}
}
for (int i = 0; i < h; ++i) {
emptySet[id[i]].insert(idx[i]);
}
long long ans = 0;
for (int i = 0; i < nQ; ++i) {
char cmd;
scanf(" ");
scanf("%c", &cmd);
if (cmd == '+') {
int me, hs;
scanf("%d%d", &me, &hs);
int at = idx[hs];
int nxt = findNext(emptySet[id[hs]], at);
if (at <= nxt)
ans += nxt - at;
else {
ans += cnt[id[hs]] + nxt - at;
}
where[me] = seqs[id[hs]][nxt];
emptySet[id[hs]].erase(nxt);
} else {
int me;
scanf("%d", &me);
int at = where[me];
emptySet[id[at]].insert(idx[at]);
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int U, a[10], C[100][100];
int cur[100], nxt[100];
int solve2() {
int i, j, k, ret = 0;
int L = 0;
for (i = 0; i < 10; i++) L += a[i];
memset(cur, 0x00, sizeof(cur));
cur[0] = 1;
for (i = 0; i < 10; i++) {
memset(nxt, 0x00, sizeof(nxt));
for (j = 0; j <= U; j++) {
for (k = a[i]; j + k <= U; k++) {
nxt[j + k] =
(nxt[j + k] + (long long)cur[j] * C[j + k][k]) % 1000000007;
}
}
memcpy(cur, nxt, sizeof(cur));
}
for (i = L; i <= U; i++) {
ret += cur[i];
if (ret >= 1000000007) ret -= 1000000007;
}
return ret;
}
bool ZERO() {
for (int i = 1; i < 10; i++)
if (a[i] > 0) return false;
return a[0] <= 1;
}
int solve() {
int i, j, k, ret = 0;
U--;
for (i = 1; i < 10; i++) {
j = a[i];
a[i] = max(a[i] - 1, 0);
k = solve2();
a[i] = j;
if ((ret += k) >= 1000000007) ret -= 1000000007;
}
return ret;
}
void input() {
scanf("%d", &U);
for (int i = 0; i < 10; i++) scanf("%d", a + i);
}
void build() {
for (int i = 0; i < 100; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % 1000000007;
}
}
int main() {
build();
input();
printf("%d\n", solve());
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int long long n, i, a, b[100005], ans, ans1;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> a;
for (i = 1; i <= n; i++) {
cin >> b[i];
}
if (n == 1) {
cout << 0;
return 0;
}
sort(b + 1, b + n + 1);
ans = min(abs(a - b[1]), abs(b[n - 1] - a)) + (b[n - 1] - b[1]);
ans1 = min(abs(a - b[2]), abs(b[n] - a)) + (b[n] - b[2]);
cout << min(ans, ans1) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int n, m, t, q;
vector<int> G[N];
map<int, int> M;
set<int> S;
struct p {
int x, y;
};
int a[N], ans[N];
int main() {
scanf("%d%d%d", &n, &m, &t);
int sum = 0;
for (int i = (1); i <= (m); i++) scanf("%d", &a[i]), sum += a[i];
int id = 0;
for (int i = (1); i <= (m); i++) {
if (id + t + sum - 1 >= n) {
int v = n;
for (int j = (m); j >= (i); j--) {
for (int k = (1); k <= (a[j]); k++) {
ans[v] = j;
v--;
}
}
id = n + 1;
break;
} else {
for (int j = (id + t); j <= (id + t + a[i] - 1); j++) ans[j] = i;
id = id + t + a[i] - 1;
}
sum = sum - a[i];
}
if (n - id < t) {
printf("YES\n");
for (int i = (1); i <= (n); i++) printf("%d ", ans[i]);
} else
printf("NO\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int arr[1005][1005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, tmp = 0;
cin >> n;
for (int i = 1; i <= n / 4; i++) {
for (int j = 1; j <= n / 4; j++) {
for (int x = 1; x <= 4; x++) {
for (int y = 1; y <= 4; y++) {
arr[(i - 1) * 4 + x][(j - 1) * 4 + y] = tmp;
tmp++;
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j < n)
cout << arr[i][j] << ' ';
else
cout << arr[i][j] << '\n';
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n], x[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int cnt = 1, temp = 0;
int prim[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int j = 0; j < 11; j++) {
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
if (a[i] % prim[j] == 0) {
a[i] = 0;
x[i] = cnt;
temp = 2;
}
}
}
if (temp == 2) cnt++;
temp = 0;
}
cout << cnt - 1 << endl;
for (int i = 0; i < n; i++) {
cout << x[i] << " ";
}
cout << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
const int M=300005;
const int N=998244353;
int n;
ll m,x,y,a,b,k,fact[M],inv[M];
int modpow(int x,int p,int m){
if(p==0)return 1;
int k=modpow(x,p/2,m)%m;
k=(1LL*k*k)%m;
if(p%2==1)k=(1LL*k*x)%m;
return k;
}
int stuff(int a,int b){
ll x=fact[n]*inv[a]%N*inv[n-a]%N;
ll y=fact[n]*inv[b]%N*inv[n-b]%N;
return (x*y)%N;
}
int main()
{
cin>>n>>a>>b>>k;
fact[0]=inv[0]=1;
for(int i=1;i<=n;i++){
fact[i]=i*fact[i-1]%N;
inv[i]=modpow(fact[i],N-2,N);
}
for(int i=0;i<=n;i++){
if((k-i*a)%b==0&&(k-i*a)/b<=n&&k-i*a>=0){
x+=stuff(i,(k-i*a)/b);
x%=N;
}
}
cout<<x;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, p[100100], cnt, flag[100100], inv[100100];
void shai() {
for (long long i = 2; i <= n; i++) {
if (flag[i] == 0) p[++cnt] = i;
for (long long j = 1; j <= cnt && i * p[j] <= n; j++) {
flag[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}
int main() {
scanf("%lld", &n);
if (n == 1) {
printf("YES\n");
printf("1\n");
return 0;
}
if (n == 2) {
printf("YES\n");
printf("1\n2\n");
return 0;
}
if (n == 3) {
printf("YES\n");
printf("1\n2\n3\n");
return 0;
}
if (n == 4) {
printf("YES\n");
printf("1\n3\n2\n4\n");
return 0;
}
shai();
if (flag[n] == 0)
printf("YES\n");
else {
printf("NO\n");
return 0;
}
inv[1] = 1;
for (long long i = 2; i <= n - 1; i++) {
inv[i] = ((n - n / i) * inv[n % i]) % n;
}
printf("1\n");
for (long long i = 2; i <= n - 1; i++) {
long long ans = i * inv[i - 1];
ans %= n;
printf("%lld\n", ans);
}
printf("%lld\n", n);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int num[maxn];
vector<pair<int, int> > edges[maxn];
int dfs(int v, int dad, long long dis) {
if (dis > num[v]) return 0;
int res = 1;
for (int i = 0; i < edges[v].size(); i++) {
int vv = edges[v][i].first;
int ww = edges[v][i].second;
if (vv == dad) continue;
res += dfs(vv, v, max(dis + ww, 0LL));
}
return res;
}
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) edges[i].clear();
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
for (int i = 1; i <= n - 1; i++) {
int v, w;
scanf("%d %d", &v, &w);
edges[i + 1].push_back(make_pair(v, w));
edges[v].push_back(make_pair(i + 1, w));
}
printf("%d\n", n - dfs(1, -1, 0));
}
}
| 3 |
#include<cstdio>
const int N = 1000000;
int prime[N] = {0},num_prime = 0;
int isNotPrime[N] = {1, 1};
int main()
{
int n;
for(long i=2; i<(2<<17); i++)
{
if(!isNotPrime[i])
prime[num_prime++]=i;
for(long j = 0 ; j < num_prime && i * prime[j] < N ; j ++)
{
isNotPrime[i * prime[j]] = 1;
if( !(i % prime[j] ) )
break;
}
}
while(~scanf("%d",&n),n)
{
int cnt = 0;
for(int i=0; prime[i]<n; i++)
for(int j=i; prime[j]<n; j++)
if(prime[i] + prime[j] == n)
cnt++;
printf("%d\n",cnt);
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll N;
vector<P> A; //(活性度、番号)
vector<vector<ll>> dp(2010, vector<ll>(2010, 0));
ll res = 0;
int main(){
cin >> N;
ll a;
for(ll i = 1; i <= N; i++){ cin >> a; A.push_back({a, i}); }
sort(A.begin(), A.end());
reverse(A.begin(), A.end()); //活性度大きい順
for(int i = 0; i <= N; i++){ //活性度の高い順かつ合計
ll ac = A[i].first, n = A[i].second; //活性度とナンバー
for(int j = 0; j <= i; j++){
int k = i - j;
dp[j+1][k] = max(dp[j+1][k], dp[j][k] + ac * abs(j+1-n));
dp[j][k+1] = max(dp[j][k+1], dp[j][k] + ac * abs(N-k-n));
}
}
for(int i = 0; i <= N; i++) res = max(res, dp[i][N-i]);
cout << res << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
int get(int n) {
int r = 0;
while (n) {
r += n % 10;
n /= 10;
}
return r;
}
int main() {
int n;
cin >> n;
set<int> r;
for (int i = n; i >= max(0, n - 200); i--) {
if (i + get(i) == n) r.insert(i);
}
cout << r.size() << endl;
for (int i : r) cout << i << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int N = 100010;
long long t[N];
long long Ans[N];
long long sum[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &t[i]);
std::sort(t + 1, t + n + 1);
std::reverse(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + t[i];
for (int i = 2; i <= n; i++) Ans[1] += t[i] * (i - 1);
int q;
scanf("%d", &q);
while (q--) {
int k;
scanf("%d", &k);
if (Ans[k] != 0) {
printf("%I64d\n", Ans[k]);
continue;
}
long long ans = 0;
long long tmp = 1, d = 0;
for (int i = 1; i <= n; i++) {
if (i + tmp - 1 > n) {
ans += d * (sum[n] - sum[i - 1]);
break;
}
ans += d * (sum[i + tmp - 1] - sum[i - 1]);
d++;
i += tmp - 1;
tmp *= k;
}
Ans[k] = ans;
printf("%I64d", Ans[k]);
if (q) putchar(' ');
}
puts("");
scanf(" ");
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
for (int i = 0; i < 111; i++) {
s = s.substr(1) + s[0];
if (s == t) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char arr[62];
void def() {
int k = 0, l = 0;
for (int i = 0; i < 62; i++) {
if (i < 26)
arr[i] = 'A' + i;
else if (i < 52)
arr[i] = 'a' + k++;
else
arr[i] = '0' + l++;
}
}
int main() {
def();
int t;
cin >> t;
while (t--) {
int r, c, k, i, j, ct = 0, rice, l = 0;
cin >> r >> c >> k;
char ch[r][c], ans[r][c];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
cin >> ch[i][j];
}
}
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if (ch[i][j] == 'R') ct++;
}
}
rice = ct / k;
if (ct % k != 0) rice++;
int last = (rice * k) - ct;
last = k - last;
ct = 0;
bool ck = true;
for (i = 0; i < r; i++) {
if (i % 2 == 0) {
for (j = 0; j < c; j++) {
if (ch[i][j] == 'R') ct++;
ans[i][j] = arr[l];
if (ct == rice && l < k - 1) {
l++;
ct = 0;
}
if (l == last && ck == true) {
rice--;
ck = false;
}
}
} else {
for (j = c - 1; j >= 0; j--) {
if (ch[i][j] == 'R') ct++;
ans[i][j] = arr[l];
if (ct == rice && l < k - 1) {
l++;
ct = 0;
}
if (l == last && ck == true) {
rice--;
ck = false;
}
}
}
}
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
cout << ans[i][j];
}
cout << '\n';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
bool vis[maxn];
vector<pair<int, int> > g;
bool dfs(int node, int t) {
if (vis[node]) return false;
if (node == t) return true;
vis[node] = true;
bool ans = false;
for (int i = 0; i < (int)g.size(); i++) {
int l_atual = g[node].first;
int r_atual = g[node].second;
int l_to = g[i].first;
int r_to = g[i].second;
if ((l_atual < r_to && l_atual > l_to) ||
(r_atual < r_to && r_atual > l_to)) {
ans |= dfs(i, t);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while (n--) {
int t;
cin >> t;
if (t == 1) {
int l, r;
cin >> l >> r;
g.push_back({l, r});
} else {
memset(vis, 0, sizeof(vis));
int s, e;
cin >> s >> e;
s--;
e--;
cout << (dfs(s, e) ? "YES" : "NO") << '\n';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
pair<long long, long long> par[MAXN];
vector<pair<long long, long long>> edge;
;
long long n, ans;
void input();
bool is_lucky(long long a);
long long get_par(long long u);
void merge(long long u, long long v);
void merge1(long long u, long long v);
void solve();
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
input();
solve();
return 0;
}
void input() {
cin >> n;
for (long long i = 0; i < n; i++) par[i] = {-1, 0};
for (long long i = 0; i < n - 1; i++) {
long long v, u, w;
cin >> v >> u >> w;
v--;
u--;
if (!is_lucky(w))
merge(v, u);
else
edge.push_back({v, u});
}
}
bool is_lucky(long long a) {
while (a > 0) {
if (a % 10 != 4 && a % 10 != 7) return false;
a /= 10;
}
return true;
}
long long get_par(long long u) {
return par[u].first < 0 ? u : par[u].first = get_par(par[u].first);
}
void merge(long long u, long long v) {
u = get_par(u), v = get_par(v);
if (u == v) return;
if (par[u].first > par[v].first) swap(u, v);
par[u].first += par[v].first;
par[v].first = u;
}
void merge1(long long u, long long v) {
u = get_par(u), v = get_par(v);
if (u == v) return;
ans += ((-par[v].first) * ((-par[v].first) - 1)) * (-par[u].first);
ans += ((-par[u].first) * ((-par[u].first) - 1)) * (-par[v].first);
ans += 2 * par[v].second * (-par[u].first);
ans += 2 * par[u].second * (-par[v].first);
par[u].second += 2 * (-par[u].first) * (-par[v].first);
par[u].second += par[v].second;
par[v].second = 0;
par[u].first += par[v].first;
par[v].first = u;
}
void solve() {
for (auto i : edge) merge1(i.first, i.second);
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int gi() {
int res = 0, w = 1;
char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar();
return res * w;
}
const int MAX_N = 1e6 + 5;
int N, flag, a[MAX_N], b[MAX_N];
long long sa[MAX_N], sb[MAX_N];
pair<int, int> buc[MAX_N];
void print(int l, int r) {
printf("%d\n", r - l + 1);
for (int i = l; i <= r; i++) printf("%d ", i);
putchar('\n');
}
int main() {
N = gi();
for (int i = 1; i <= N; i++) a[i] = gi(), sa[i] = sa[i - 1] + a[i];
for (int i = 1; i <= N; i++) b[i] = gi(), sb[i] = sb[i - 1] + b[i];
if (sa[N] > sb[N]) swap(a, b), swap(sa, sb), flag = 1;
for (int i = 1, j = 1; i <= N; i++) {
while (sa[i] > sb[j]) ++j;
if (sa[i] == sb[j]) {
if (flag)
print(1, j), print(1, i);
else
print(1, i), print(i, j);
return 0;
}
int v = sb[j] - sa[i];
if (!buc[v].first)
buc[v] = make_pair(i, j);
else {
if (flag)
print(buc[v].second + 1, j), print(buc[v].first + 1, i);
else
print(buc[v].first + 1, i), print(buc[v].second + 1, j);
return 0;
}
}
puts("xgzc ak ioi");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, p;
int main() {
long long l, r, i, j, b, ans;
cin >> n >> l >> r >> k;
p = (r - l + n) % n + 1;
if (k - p < 0) {
cout << -1 << endl;
return 0;
}
if (k - p == 0) {
cout << n - p + 1 << endl;
return 0;
}
if (k - p <= p) {
cout << min(n, n + k - 2ll * p + 1) << endl;
return 0;
}
if (n <= 4e7) {
ans = -1;
for (i = n; i >= 0; i--) {
long long a = (k - p) % (n + i);
if (a >= max(0ll, i - n + p - 1) && a <= min(i, p)) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
}
ans = -1;
for (b = max(1ll, (k - p) / (2ll * n)); b <= (k - p) / n; b++) {
long long x = k - p - n * b;
if (p <= n - p + 1) {
long long aa;
aa = min(p, x / b);
if (aa >= x / (b + 1)) ans = max(ans, aa);
aa = min(n - p + 1, x / b);
if (aa >= p && aa >= (x - p) / b) ans = max(ans, aa);
aa = min(n, (x + n - p + 1) / (b + 1));
if (aa >= n - p + 1 && aa >= (x - p) / b) ans = max(ans, aa);
} else {
long long aa;
aa = min(n - p + 1, x / b);
if (aa >= x / (b + 1)) ans = max(ans, aa);
aa = min(p, (x + n - p + 1) / (b + 1));
if (aa >= n - p + 1 && aa >= x / (b + 1)) ans = max(ans, aa);
aa = min(n, (x + n - p + 1) / (b + 1));
if (aa >= p && aa >= (x - p) / b) ans = max(ans, aa);
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using ll = long long;
const ll N = 2 * 1e5 + 5;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const ll SZ = 101;
const double eps = 1e-9;
using namespace std;
void solve() {
ll n, k;
cin >> n;
vector<pair<ll, ll> > a(n);
vector<ll> pfx(n, 0);
for (ll(i) = 0; i < (n); i++) cin >> a[i].first, a[i].second = i;
cin >> k;
sort((a).begin(), (a).end());
for (ll(i) = 0; i < (n); i++) {
pfx[i] = a[i].first;
if (i) pfx[i] += pfx[i - 1];
}
ll l = 0, r = k - 1, ans = 0;
ll mul = -(k - 1);
for (ll(i) = 0; i < (k); i++) {
ans += mul * a[i].first, mul += 2;
}
pair<ll, pair<ll, ll> > xx = {ans, {0, k - 1}};
for (ll i = 1, j = k; i < n && j < n; ++i, ++j) {
ans += (k - 1) * a[i - 1].first;
ans -= 2 * pfx[j - 1] - 2 * pfx[i - 1];
ans += (k - 1) * a[j].first;
xx = min(xx, {ans, {i, j}});
}
for (ll f = (xx.second.first); f <= (xx.second.second); f++)
cout << a[f].second + 1 << " ";
}
int32_t main(int32_t argc, char *argv[]) {
double t1 = clock();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll TC = 1, t = 0;
while (t++ < TC) {
solve();
}
if (false)
cerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, c;
cin >> n >> a >> b >> c;
cout << max(min(min(a, b) * (n - 1), min(a, b) + (n - 2) * c), 0);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
const int inf = 1e9;
int n, m, high[N], nxt[N];
int visit[N * 2];
vector<pair<int, int> > E;
vector<int> G[N];
struct Dinic {
int source, sink, n;
struct edge {
int to, c, f;
edge(int to, int c, int f) : to(to), c(c), f(f) {}
};
vector<edge> E;
vector<int> G[N * 4];
void add_edge(int u, int v, int c) {
G[u].push_back(E.size());
E.push_back(edge(v, c, 0));
G[v].push_back(E.size());
E.push_back(edge(u, 0, 0));
}
int nxt[N * 4], dis[N * 4];
bool bfs() {
for (int i = 0; i <= n; ++i) dis[i] = inf;
queue<int> qu;
dis[source] = 0;
qu.push(source);
while (qu.size()) {
int u = qu.front();
qu.pop();
for (int i = 0; i < G[u].size(); ++i) {
int id = G[u][i];
if (E[id].c > E[id].f && dis[E[id].to] == inf) {
dis[E[id].to] = dis[u] + 1;
qu.push(E[id].to);
}
}
}
return dis[sink] != inf;
}
int dfs(int u, int flow) {
if (u == sink || !flow) return flow;
for (int &i = nxt[u]; i < G[u].size(); ++i) {
int id = G[u][i];
if (dis[E[id].to] != dis[u] + 1) continue;
int nxt_flow = dfs(E[id].to, min(flow, E[id].c - E[id].f));
if (nxt_flow) {
E[id].f += nxt_flow;
E[id ^ 1].f -= nxt_flow;
return nxt_flow;
}
}
return 0;
}
int maxflow() {
int res = 0;
while (bfs()) {
for (int i = 0; i <= n; ++i) nxt[i] = 0;
while (int flow = dfs(source, inf)) res += flow;
}
return res;
}
} flow;
void dfs(int u) {
for (int i = 0; i < G[u].size(); ++i) {
int id = G[u][i];
if (high[E[id].first]) continue;
high[E[id].first] = high[u] + 1;
visit[id >> 1] = 1;
nxt[E[id].first] = (id ^ 1);
dfs(E[id].first);
}
}
void pre(int num, int &cnt, int u, int v) {
if (high[u] > high[v]) swap(u, v);
while (high[v] > high[u]) {
visit[nxt[v] >> 1] = -1;
flow.add_edge(num, E[nxt[v]].second, 1);
cnt++;
v = E[nxt[v]].first;
}
while (u != v) {
visit[nxt[v] >> 1] = visit[nxt[u] >> 1] = -1;
flow.add_edge(num, E[nxt[v]].second, 1);
flow.add_edge(num, E[nxt[u]].second, 1);
cnt++;
v = E[nxt[v]].first;
cnt++;
u = E[nxt[u]].first;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
G[u].push_back(E.size());
E.push_back(pair<int, int>(v, c));
G[v].push_back(E.size());
E.push_back(pair<int, int>(u, c));
}
high[1] = 1;
dfs(1);
int num = m;
flow.source = 0, flow.sink = m + m + 1, flow.n = m + m + 1;
for (int i = 1; i <= m; ++i) flow.add_edge(i, flow.sink, 1);
for (int i = 0; i < m; ++i) {
if (!visit[i]) {
int cnt = 0;
num++;
visit[i] = -1;
flow.add_edge(num, E[i * 2].second, 1);
pre(num, cnt, E[i * 2].first, E[i * 2 + 1].first);
flow.add_edge(flow.source, num, cnt);
}
}
for (int i = 0; i < m; ++i) {
if (visit[i] != -1) {
flow.add_edge(flow.source, ++num, 1);
flow.add_edge(num, E[i * 2].second, 1);
}
}
cout << flow.maxflow();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n;
char x;
cin >> n >> t;
if (t == 10 && n == 1) {
cout << "-1";
return 0;
}
if (t == 10 && n > 1) {
for (int i = 1; i <= n - 1; i++) {
if (i >= 10) {
cout << i % 10;
} else {
cout << i;
}
}
cout << 0;
return 0;
} else {
for (int i = 1; i <= n; i++) {
cout << t;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int suma[100005], sumb[100005];
int a[100005], n;
struct Node {
int s, t;
} ans[100005];
int num;
bool cmp(Node x, Node y) { return x.s < y.s; }
int main() {
while (scanf("%d", &n) != EOF) {
memset(suma, 0, sizeof(suma));
memset(sumb, 0, sizeof(sumb));
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
suma[i] = suma[i - 1];
sumb[i] = sumb[i - 1];
if (a[i] == 1)
suma[i]++;
else
sumb[i]++;
}
suma[n + 1] = suma[n];
sumb[n + 1] = sumb[n];
if (a[n] == 1) {
num = 0;
for (int tempt = 1; tempt <= suma[n]; tempt++) {
bool flag = false;
int tempa, tempb;
int numa, numb;
int lasta, lastb;
tempa = tempb = 0;
lasta = lastb = 0;
while (true) {
numa = lower_bound(suma + 1, suma + n + 1, lasta + tempt) - suma;
numb = lower_bound(sumb + 1, sumb + n + 1, lastb + tempt) - sumb;
if (numa < numb) {
tempa++;
lasta = suma[numa];
lastb = sumb[numa];
} else {
tempb++;
lasta = suma[numb];
lastb = sumb[numb];
}
if (min(numa, numb) >= n) {
if (tempa > tempb && numa == n) {
flag = true;
}
break;
}
}
if (flag) {
ans[num].t = tempt;
ans[num].s = tempa;
num++;
}
}
} else {
num = 0;
for (int tempt = 1; tempt <= sumb[n]; tempt++) {
bool flag = false;
int tempa, tempb;
int numa, numb;
int lasta, lastb;
tempa = tempb = 0;
lasta = lastb = 0;
while (true) {
numa = lower_bound(suma + 1, suma + n + 1, lasta + tempt) - suma;
numb = lower_bound(sumb + 1, sumb + n + 1, lastb + tempt) - sumb;
if (numa < numb) {
tempa++;
lasta = suma[numa];
lastb = sumb[numa];
} else {
tempb++;
lasta = suma[numb];
lastb = sumb[numb];
}
if (min(numa, numb) >= n) {
if (tempb > tempa && numb == n) {
flag = true;
}
break;
}
}
if (flag) {
ans[num].t = tempt;
ans[num].s = tempb;
num++;
}
}
}
sort(ans, ans + num, cmp);
printf("%d\n", num);
for (int i = 0; i < num; i++) printf("%d %d\n", ans[i].s, ans[i].t);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using std::swap;
const int maxn = 610, maxm = 200010;
int N, n, m, P;
int fs[maxn], oth[maxm], next[maxm];
int id[maxn], od[maxn];
bool v[maxn];
int d[maxn][maxn], a[maxn][maxn];
void sol(int x) {
if (v[x]) return;
v[x] = 1;
int i, j, t;
for (i = fs[x]; i; i = next[i]) {
sol(t = oth[i]);
for (j = 0; j < n; ++j)
if (d[x][j] += d[t][j], d[x][j] > P) d[x][j] -= P;
}
}
void pre() {
int i, x, y;
scanf("%d%d%d", &N, &m, &P);
for (i = 1; i <= m; ++i) {
scanf("%d%d", &x, &y);
oth[i] = x, next[i] = fs[y], fs[y] = i;
++od[x], ++id[y];
}
for (i = 1; i <= N; ++i)
if (!id[i]) d[i][n++] = 1, v[i] = 1;
for (m = 0, i = 1; i <= N; ++i)
if (!od[i]) sol(i), memcpy(a[m++], d[i], sizeof(d[i]));
}
inline int pow(int x, int y) {
int e = 1;
while (y) {
if (y & 1) e = (long long)e * x % P;
x = (long long)x * x % P;
y >>= 1;
}
return e;
}
int gauss() {
int s = 1, i, j, k, t;
for (i = 0; i < n; ++i) {
for (j = i; j < n && !a[j][i]; ++j)
;
if (j == n) return 0;
if (j != i)
for (s = P - s, k = 0; k < n; ++k) swap(a[i][k], a[j][k]);
s = (long long)s * a[i][i] % P, t = pow(a[i][i], P - 2);
for (j = i; j < n; ++j) a[i][j] = (long long)a[i][j] * t % P;
for (j = 0; j < n; ++j)
if (i != j)
for (k = n - 1; k >= i; --k)
a[j][k] = (a[j][k] - (long long)a[j][i] * a[i][k] % P + P) % P;
}
return s;
}
int main() {
if (0) {
freopen("t.in", "r", stdin);
freopen("t.out", "w", stdout);
}
pre();
printf("%d", gauss());
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
ifstream fin(
""
".in");
ofstream fout(
""
".out");
const int MAX_N = int(2e5) + 20;
int main() {
ios_base::sync_with_stdio(false);
vector<pair<long long, long long>> seg(MAX_N);
map<long long, long long> cnt;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
seg[i].first = l;
seg[i].second = r;
cnt[l] = 0;
cnt[r + 1] = 0;
}
for (int i = 0; i < n; i++) {
long long l = seg[i].first;
long long r = seg[i].second;
cnt[l]++;
auto it = cnt.find(r + 1);
if (it != cnt.end()) {
it->second--;
}
}
for (auto i = cnt.begin(); i != cnt.end();) {
long long prev = (*i).second;
(*++i).second += prev;
}
vector<long long> ans(MAX_N, 0);
for (auto i = cnt.begin(); i != cnt.end();) {
long long key = i->first;
long long value = i++->second;
if (i != cnt.end()) {
ans[value] += i->first - key;
}
}
for (int k = 1; k <= n; k++) {
cout << ans[k] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, t1, ans[1005], cnt;
bool used[1005];
char ch;
int main() {
scanf("%d%c", &t1, &ch);
while (ch == ',') {
used[t1] = true;
scanf("%d%c", &t1, &ch);
}
used[t1] = true;
for (int i = 1; i <= 1000; ++i) {
if (used[i]) {
for (int j = i; used[j]; ++j) {
ans[i] = j;
used[j] = false;
}
}
}
cnt = 0;
for (int i = 1; i <= 1000; ++i) {
if (ans[i]) {
++cnt;
if (cnt != 1) printf(",");
if (ans[i] == i)
printf("%d", i);
else
printf("%d-%d", i, ans[i]);
}
}
printf("\n");
return 0;
}
| 3 |
#include "bits/stdc++.h"
#define rep(i, a) for (ll i = 0; i < (a); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FORV(a, A) for (auto &a : A)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
void Main() {
ll n;
cin >> n;
vector<ll> C(n);
rep(i, n) cin >> C[i];
sort(C.begin(), C.end());
rep(i, n - 1)(C[i + 1] += C[i]) %= MOD;
ll ans = C[n - 1];
rep(i, n)(ans += C[i]) %= MOD;
rep(i, 2 * n - 2)(ans *= 2) %= MOD;
cout << ans << endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int t, x, y, n, k;
int main() {
cin >> t;
while (t--) {
cin >> x >> y >> n;
k = (n / x * x + y > n) ? n / x - 1 : n / x;
cout << k * x + y << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 10;
vector<int> g[N];
vector<long long> w[N];
vector<long long> p[N];
long long sum[N];
long long minw[N];
long long maxw[N];
bool ok = true;
map<pair<int, int>, long long> delta;
void dfs2(int v, int par) {
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to != par) {
dfs2(to, v);
long long low = minw[to];
if (low > p[v][i]) {
ok = false;
break;
}
long long d = min(p[v][i] - low, w[v][i] - 1);
low += w[v][i] - d;
long long high = maxw[to];
high = min(high, p[v][i]);
high += w[v][i];
minw[v] += low;
maxw[v] += high;
}
}
}
void dfs3(int v, int par, long long extra) {
assert(extra >= 0 && extra + minw[v] <= maxw[v]);
for (int i = 0; i < g[v].size(); i++) {
int to = g[v][i];
if (to != par) {
long long low = minw[to];
assert(low <= p[v][i]);
long long d = min(p[v][i] - low, w[v][i] - 1);
long long dec = min(d, extra);
extra -= dec;
delta[pair<int, int>(min(v, to), max(v, to))] = d - dec;
long long there = min(p[v][i] - d + dec, maxw[to]) - minw[to];
dfs3(to, v, min(extra, there));
extra -= min(extra, there);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n - 1), y(n - 1);
vector<long long> wht(n - 1), st(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> x[i] >> y[i] >> wht[i] >> st[i];
x[i]--;
y[i]--;
g[x[i]].push_back(y[i]);
g[y[i]].push_back(x[i]);
w[x[i]].push_back(wht[i]);
w[y[i]].push_back(wht[i]);
p[x[i]].push_back(st[i]);
p[y[i]].push_back(st[i]);
}
dfs2(0, 0);
if (!ok) {
cout << -1 << endl;
return 0;
}
dfs3(0, 0, maxw[0] - minw[0]);
cout << n << "\n";
for (int i = 0; i < n - 1; i++) {
auto cur = pair<int, int>(min(x[i], y[i]), max(y[i], x[i]));
cout << x[i] + 1 << " " << y[i] + 1 << " " << wht[i] - delta[cur] << " "
<< st[i] - delta[cur] << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 3e3 + 5, MXX = 23;
const long long mod = 1e9 + 7, inf = 1e18 + 6;
vector<int> G[MX];
long long dp[MX][MX], sz[MX], par[MXX][MX], hig[MX], mxa, n;
void dfs(int u) {
for (auto v : G[u])
if (v != par[0][u]) {
par[0][v] = u;
hig[v] = hig[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
int anc(int u, int dis) {
for (int i = 0; i < MXX; ++i)
if (dis & 1 << i) u = par[i][u];
return u;
}
int dis(int u, int v) {
int ans = 0;
if (hig[v] < hig[u]) swap(u, v);
ans += hig[v] - hig[u];
v = anc(v, hig[v] - hig[u]);
if (v == u) return ans;
for (int i = MXX - 1; i >= 0; i--)
if (par[i][u] != par[i][v])
ans += (1 << i + 1), u = par[i][u], v = par[i][v];
return ans + 2;
}
void lca(int u, int v) {
if (hig[v] < hig[u]) swap(u, v);
if (u == anc(v, hig[v] - hig[u])) {
int x = par[0][v];
int y = anc(v, hig[v] - hig[u] - 1);
dp[u][v] = sz[v] * (n - sz[y]);
dp[u][v] += max(dp[u][x], dp[v][y]);
dp[v][u] = dp[u][v];
mxa = max(mxa, dp[u][v]);
return;
}
int x = par[0][v];
int y = par[0][u];
dp[u][v] = sz[u] * sz[v];
dp[u][v] += max(dp[u][x], dp[v][y]);
dp[v][u] = dp[u][v];
mxa = max(mxa, dp[u][v]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(9);
cin >> n;
for (int i = 0; i < n + 5; ++i) sz[i] = 1;
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
u--, v--;
G[u].push_back(v), G[v].push_back(u);
}
dfs(0);
for (int i = 1; i < MXX; ++i)
for (int t = 0; t < n; ++t) par[i][t] = par[i - 1][par[i - 1][t]];
vector<pair<int, pair<int, int>>> ord;
for (int i = 0; i < n; ++i)
for (int t = i + 1; t < n; ++t)
ord.push_back(make_pair(dis(i, t), make_pair(i, t)));
sort(ord.begin(), ord.end());
for (auto x : ord) lca(x.second.first, x.second.second);
cout << mxa << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.14159265359;
vector<long long> primes;
void GeneratingPrimes(int top) {
vector<bool> p(top + 1, 1);
p[0] = p[1] = 0;
for (int i = 2; i <= sqrt(top); i++) {
if (p[i]) {
for (int j = i * i; j <= top; j += i) {
p[j] = 0;
}
}
}
for (int i = 0; i < top + 1; i++)
if (p[i]) primes.push_back(i);
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void EXTgcd(long long a, long long b, int& x0, int& y0) {
if (b == 0) {
x0 = 1, y0 = 0;
return;
}
EXTgcd(b, a % b, x0, y0);
long long x1 = y0;
long long y1 = x0 - (a / b) * y0;
x0 = x1;
y0 = y1;
}
long long power(long long x, long long p, long long m) {
if (!p) return 1;
if (p % 2) return ((x % m) * (power(x, p - 1, m) % m)) % m;
x %= m;
x *= x;
x %= m;
p /= 2;
return (x * (power(x, p, m)) % m) % m;
}
double dist(double a, double b, double x, double y) {
return sqrt(pow(a - x, 2) + pow(b - y, 2));
}
double dist3(double a, double b, double c, double x, double y, double z) {
return sqrt(pow(a - x, 2) + pow(b - y, 2) + pow(c - z, 2));
}
int xadd[9] = {1, -1, 0, 1, -1, 0, 1, -1, 0};
int yadd[9] = {1, -1, 0, -1, 0, 1, 0, 1, -1};
long long const N = 3e5 + 10;
long long mod = 1e9;
long long n, k;
int main() {
cin >> n >> k;
long long lo = 0, hi = k - 1, mid, ans = -1;
while (lo <= hi) {
mid = (lo + hi) / 2;
long long s = mid * (k + k - mid + 1) / 2 - mid + 1;
if (s >= n) {
if (ans == -1) ans = mid;
ans = min(ans, mid);
hi = mid - 1;
} else {
lo = mid + 1;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int solve() {
int n, i, j, k, m;
cin >> n;
vector<int> v(n);
for (i = 0; i < n; ++i) cin >> v[i];
set<int> st(v.begin(), v.end());
v.clear();
for (int x : st) v.push_back(x);
sort(v.begin(), v.end());
int flg = 1;
for (i = 0; i < v.size() - 1; ++i) {
int x = i, p = v.size();
for (; p >= 1; p /= 2) {
while (x + p < v.size() && v[x + p] < 2 * v[i]) x += p;
}
++x;
if (x != i + 1) flg = 0;
}
if (flg)
cout << "NO\n";
else
cout << "YES\n";
return 0;
;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
return 0;
;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 20;
int tree[2 * MAXN];
int tree_min[2 * MAXN];
int tree_max[2 * MAXN];
void update(int v, int vl, int vr, int tl, int tr, int val) {
if (vl > tr || tl > vr) {
return;
}
if (tl <= vl && tr >= vr) {
tree[v] += val;
return;
}
int vm = (vl + vr) / 2;
tree[2 * v] += tree[v];
tree[2 * v + 1] += tree[v];
tree[v] = 0;
update(2 * v, vl, vm, tl, tr, val);
update(2 * v + 1, vm + 1, vr, tl, tr, val);
tree_max[v] =
max(tree_max[2 * v] + tree[2 * v], tree_max[2 * v + 1] + tree[2 * v + 1]);
tree_min[v] =
min(tree_min[2 * v] + tree[2 * v], tree_min[2 * v + 1] + tree[2 * v + 1]);
}
void update(int pos, int val) { update(1, 0, MAXN - 1, pos, MAXN - 1, val); }
int get_min() { return tree_min[1] + tree[1]; }
int get_max() { return tree_max[1] + tree[1]; }
int get_val(char f, char s) {
int val = 0;
if (f == '(') {
--val;
}
if (f == ')') {
++val;
}
if (s == '(') {
++val;
}
if (s == ')') {
--val;
}
return val;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
string current;
for (int i = 0; i < n; ++i) {
current.push_back('A');
}
int pos = 0;
int balance = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == 'R') {
++pos;
} else if (s[i] == 'L') {
if (pos > 0) {
--pos;
}
} else {
update(pos, get_val(current[pos], s[i]));
balance += get_val(current[pos], s[i]);
current[pos] = s[i];
}
if (balance != 0 || get_min() <= -1) {
cout << -1;
} else {
cout << get_max();
}
cout << " ";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int __ = 505;
int b[__][10005];
int fi[__];
int xorxy(int n, int m) {
int i = 1;
for (int j = 1; i <= n && j <= m; j++) {
int x = 0;
for (int k = i; k <= n; k++)
if (b[k][j]) {
x = k;
break;
}
if (!x) continue;
for (int k = j; k <= m; k++) swap(b[i][k], b[x][k]);
for (int k = 1; k <= n; k++) {
if (k == i || !b[k][j]) continue;
for (int l = j; l <= m; l++) b[k][l] ^= b[i][l];
}
fi[i] = j;
i++;
}
return i - 1;
}
int a[3][__], ans[__][__];
bool vis[__];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = (1); i <= (n); ++i) scanf("%d", &a[1][i]);
for (int i = (1); i <= (m); ++i) scanf("%d", &a[2][i]);
int idx = 0, z = n * m + 1;
for (int i = (1); i <= (n); ++i) {
++idx;
for (int j = (1); j <= (m); ++j) b[idx][(i - 1) * m + j] = 1;
b[idx][n * m + 1] = a[1][i];
}
for (int j = (1); j <= (m); ++j) {
++idx;
for (int i = (1); i <= (n); ++i) b[idx][(i - 1) * m + j] = 1;
b[idx][n * m + 1] = a[2][j];
}
int x = xorxy(idx, z);
for (int i = (1); i <= (idx); ++i) {
bool flag = false;
for (int j = (1); j <= (z - 1); ++j)
if (b[i][j]) {
flag = true;
break;
}
if (!flag && b[i][z]) return !printf("NO\n");
}
for (int i = (1); i <= (idx); ++i)
if (fi[i] && b[i][fi[i]])
ans[(fi[i] - 1) / m + 1][(fi[i] - 1) % m + 1] = b[i][z];
puts("YES");
for (int i = (1); i <= (n); ++i)
for (int j = (1); j <= (m); ++j) printf("%d%c", ans[i][j], " \n"[j == m]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
const int MAX = 4 * 1000 * 1000 + 47;
const int MIN = 2 * 1000 * 100 + 47;
string A[MIN];
string s;
int U[MIN];
int U1[MIN];
struct node {
int next[2];
int ind;
};
struct Bor {
node T[MAX];
int sz = MAX;
void init() {
for (int i = (0); i < (sz); i++) {
T[i].next[0] = T[i].next[1] = -1;
T[i].ind = -1;
}
sz = 1;
}
void add(const string& s, int ind) {
int v = 0;
for (int i = (0); i < ((int)s.size()); i++) {
int nex = s[i] - '0';
if (T[v].next[nex] == -1) {
T[v].next[nex] = sz++;
}
v = T[v].next[nex];
}
T[v].ind = ind;
}
int getSame(const string& s) {
int v = 0;
for (int i = (0); i < ((int)s.size()); i++) {
int nex = s[i] - '0';
if (T[v].next[nex] == -1) {
return -1;
}
v = T[v].next[nex];
}
return T[v].ind;
}
} B;
void solve() {
B.init();
int n;
cin >> n;
for (int i = (0); i < (n); i++) {
cin >> A[i];
}
int C[4] = {0, 0, 0, 0};
for (int i = (0); i < (n); i++) {
U[i] = (A[i][0] - '0') * 2 + (A[i][(int)A[i].size() - 1] - '0');
C[U[i]]++;
}
for (int i = (0); i < (n); i++) {
if (U[i] == 1 || U[i] == 2) {
s = A[i];
reverse(s.begin(), s.end());
int ind = B.getSame(s);
U1[ind] = U1[i] = ind != -1;
B.add(A[i], i);
}
}
if (C[1] == 0 && C[2] == 0) {
if (C[0] != 0 && C[3] != 0) {
cout << -1 << "\n";
return;
}
cout << 0 << "\n"
<< "\n";
return;
}
int k = -1;
int type = -1;
if (C[1] > C[2]) {
k = (C[1] - C[2] - 1 + 1) / 2;
type = 1;
} else {
k = (C[2] - C[1] - 1 + 1) / 2;
type = 2;
}
cout << k << endl;
for (int i = (0); i < (n); i++) {
if (k == 0) break;
if (U[i] == type && !U1[i]) {
cout << i + 1 << " ";
k--;
}
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, m, j;
cin >> n >> m;
int a1[n];
int a2[m];
for (i = 0; i < n; i++) {
cin >> a1[i];
}
for (i = 0; i < m; i++) {
cin >> a2[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (a1[i] == a2[j]) {
cout << a1[i] << " ";
}
}
}
cout << endl;
return 0;
}
| 1 |