solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
long long ans[200111];
long long diff[200111];
int main() {
int n;
scanf("%d", &n);
long long prv = 0, ind = 1;
for (int i = 1; i <= n / 2; i++) {
scanf("%lld", diff + i);
long long r = ind;
bool found = 0;
for (ind; ind * ind - prv * prv <= 1e13; ind++) {
while (r * r - ind * ind < diff[i]) r++;
if (r * r - ind * ind == diff[i]) {
ans[2 * i - 1] = ind * ind - prv * prv;
ans[2 * i] = diff[i];
found = 1;
prv = r;
ind = r + 1;
break;
}
}
if (!found) {
printf("No\n");
return 0;
}
}
printf("Yes\n");
for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main(){
while(1){
string g;
cin >> g;
if(g=="#"){
break;
}
int y,m,d;
cin >> y >> m >> d;
if((y==31&&m>=5)||(y>=32)){
cout << "? " << y-30 << " " << m << " " << d << endl;
}else{
cout << g << " " << y << " " << m << " " << d << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
const int manx = 3e5 + 5;
;
long long a[manx], dp[manx];
int main() {
long long n, m, ans = -0x3f3f3f3f;
n = read(), m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
dp[0] = max((long long)0, dp[0] + a[i]);
dp[1] = max(dp[0], dp[1] + a[i] * m);
dp[2] = max(dp[1], dp[2] + a[i]);
ans = max(ans, dp[2]);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> ch[200005];
int vis[200005], g, sub[200005][2], arr1[200005], arr2[200005], ap[200005];
void dfs(int now) {
int i, j, k;
for (i = 0; i < ch[now].size(); i++) {
j = ch[now][i];
if (vis[j] > 0) continue;
vis[j] = g;
dfs(j);
}
}
int main() {
int i, j, k, a, b, c, d = 0, n, t, gp = 0, s, m, ans = 0;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
ch[a].push_back(b);
ch[b].push_back(a);
}
for (i = 1; i <= n; i++) {
if (vis[i] == 0) {
g++;
vis[i] = g;
dfs(i);
}
}
for (i = 1; i <= n; i++) {
if (sub[vis[i]][0] == 0) {
sub[vis[i]][0] = i;
}
sub[vis[i]][1] = i;
}
for (i = 1; i <= g; i++) {
arr1[sub[i][0]] = i;
arr2[sub[i][1]] = i;
}
c = 0;
for (i = 1; i <= n; i++) {
if (arr1[i] == 0 && arr2[i] == 0) continue;
if (arr1[i]) {
gp++;
c++;
ap[arr1[i]] = 1;
}
if (arr2[i]) {
gp--;
ap[arr2[i]] = 0;
}
if (gp == 0) {
ans += c - 1;
c = 0;
}
}
printf("%d", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = int(1e9 + 7);
int n, m, first[2222][2222], g[2222][2222];
int main() {
scanf("%d%d", &n, &m);
for (int w = 2; w <= m; ++w) first[1][w] = g[1][w] = 1;
for (int h = 2; h <= n; ++h) {
int sum = 0, total = 0;
for (int w = 2; w <= m; ++w) {
(sum += first[h - 1][w]) %= mod;
total = (1LL * first[h - 1][w] * w + total) % mod;
first[h][w] = (1LL * sum * (w + 1) - total + mod) % mod;
}
sum = 0, total = 0;
for (int w = 2; w <= m; ++w) {
(sum += g[h - 1][w]) %= mod;
total = (1LL * g[h - 1][w] * w + total) % mod;
g[h][w] = (1LL * sum * (w + 1) - total + mod + h) % mod;
}
}
int res = 0;
for (int h = 1; h <= n; ++h) {
int r = n - h + 1;
int sum = 0, total = 0;
for (int w = 2; w <= m; ++w) {
int cur = (1LL * sum * (w + 1) - total + mod + r) % mod;
cur = (1LL * cur * (m - w + 1)) % mod;
res = (1LL * cur * first[h][w] + res) % mod;
(sum += g[r - 1][w]) %= mod;
total = (1LL * g[r - 1][w] * w + total) % mod;
}
}
cout << res << endl;
return 0;
}
| 4 |
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#define DIV 1000000007
using namespace std;
long long N, M;
//
map<long long, long long> num;
//
map<long long, long long> modnum;
//
map<long long, set<long long> > modset;
int main(){
cin >> N >> M;
for(int i = 0; i < N; i++){
long long tmp;
cin >> tmp;
num[tmp]++;
modnum[tmp%M]++;
modset[tmp%M].insert(tmp);
}
long long ans = 0;
set<long long> done;
for(auto mod: modnum){
long long f = mod.first;
long long s = (M - mod.first)%M;
if(done.count(f) != 0){
continue;
}
done.insert(f);
done.insert(s);
if(f == s){
ans += modnum[f]/2;
}else{
if(modnum[f] > modnum[s]){
swap(f, s);
}
for(auto inter: modset[s]){
while(num[inter] > 1 && modnum[f] <= modnum[s] - 2){
num[inter] -= 2;
modnum[s] -= 2;
ans++;
//cout << "make pair with " << inter << endl;
}
}
long long nn = min(modnum[f], modnum[s]);
//cout << "make " << f << " and " << s << " num = " << nn<< endl;
ans += nn;
modnum[f] -= nn;
modnum[s] -= nn;
}
}
cout << ans << endl;
}
| 0 |
#include <cstdio>
using namespace std;
int main(){
char s,t; scanf("%c %c",&s,&t);
printf("%c\n",((s=='H'?1:0)^(t=='H'?1:0)?'D':'H'));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool ss(int a, int b) { return a > b; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a[200005], ans[200005];
pair<int, int> b[200005];
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
cin >> b[i].first;
b[i].second = i;
}
sort(a + 1, a + n + 1, ss);
sort(b + 1, b + n + 1);
for (int i = 1; i <= n; ++i) ans[b[i].second] = a[i];
for (int i = 1; i <= n; ++i) cout << ans[i] << ' ';
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
struct point{
double x,y;
point(){};
point(double x,double y):x(x),y(y){}
point operator + (point p){
return point(x+p.x,y+p.y);
}
point operator - (point p){
return point(x-p.x,y-p.y);
}
point operator * (double k){
return point(x*k,y*k);
}
point operator / (double k){
return point(x/k,y/k);
}
};
double dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
double norm(point p)
{
return p.x*p.x+p.y*p.y;
}
double ABS(point p)
{
return sqrt(norm(p));
}
point project(point a,point b,point c)
{
point base=b-a;
double r=dot(base,c-a)/norm(base);
return a+(base*r);
}
pair<point,point>getcrosspoints(point a,int r,point b,point c)
{
point pr=project(b,c,a);
point e=(b-c)/ABS(b-c);
double base=sqrt(r*r*1.0-norm(pr-a));
e=e*base;
return make_pair(pr+e,pr-e);
}
int main()
{
point a;
int temp;
cin>>a.x>>a.y>>temp;
int q;
cin>>q;
point c,d;
for(int i=0;i<q;i++){
cin>>c.x>>c.y>>d.x>>d.y;
pair<point,point>res=getcrosspoints(a,temp,c,d);
if(res.first.x>res.second.x){
swap(res.first,res.second);
}else if(res.first.x==res.second.x){
if(res.first.y>res.second.y){
swap(res.first,res.second);
}
}
printf("%.8f %.8f %.8f %.8f\n",res.first.x,res.first.y,res.second.x,res.second.y);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int a[5][1005] = {0}, ind[5][1005] = {0};
for (int i = 0; i < k; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
ind[i][a[i][j]] = j;
}
}
int ans[5005] = {0};
for (int i = 1; i <= n; i++) {
int flag = 0;
for (int j = 1; j < i; j++) {
int cur = ind[0][a[0][i]], flag = 0, temp = 5000;
for (int k = 0; k < 5; k++) {
if (ind[k][a[0][i]] < ind[k][a[0][j]]) {
flag = 1;
}
temp = min(temp, ans[j]);
}
if (flag == 0) {
ans[i] = max(ans[i], temp + 1);
}
}
ans[i] = max(1, ans[i]);
}
int t = 1;
for (int i = 1; i <= n; i++) {
t = max(t, ans[i]);
}
cout << t << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int t,x,n;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
n*=2;
int j=0,o=0;
for(int i=0;i<n;++i)
{
scanf("%d",&x);
if(x%2==1)++j;
else ++o;
}
if(j==o)puts("Yes");
else puts("No");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
long long n, k;
long long ans = 0;
vector<vector<long long> > g;
pair<long long, vector<long long> > dfs(long long v, long long pr,
long long h = 0) {
vector<long long> d(k, 0);
long long sz = 1;
d[h % k] = 1;
for (long long i = 0; i < g[v].size(); i++) {
if (g[v][i] == pr) continue;
pair<long long, vector<long long> > kek = dfs(g[v][i], v, h + 1);
sz += kek.first;
ans += kek.first * (n - kek.first);
for (long long j = 0; j < k; j++) {
for (long long y = 0; y < k; y++) {
ans += ((k - ((y + j - ((2 * h) % k) + k) % k) + k) % k) * d[j] *
kek.second[y];
}
}
for (long long j = 0; j < k; j++) d[j] += kek.second[j];
}
return {sz, d};
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
g.resize(n);
for (long long i = 1; i < n; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0, 0);
cout << ans / k;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool possible(long long x, vector<long long> a) {
long long n = (long long)(a).size();
if (x == 0) {
for (long long i = 1; i < n; i++) {
if (a[i] < a[i - 1]) return false;
}
return true;
}
for (long long i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
long long diff = a[i - 1] - a[i];
long long ma = 1ll << (x)-1;
if (diff > ma)
return false;
else
a[i] = a[i - 1];
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T;
cin >> T;
while (T--) {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
long long prev = a[0];
long long diff = 0;
for (long long i = 1; i < n; i++) {
if (a[i] < prev) {
diff = max(diff, prev - a[i]);
} else
prev = a[i];
}
if (diff > 0) {
cout << (long long)log2(diff) + 1 << endl;
} else {
cout << 0 << endl;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[51][51];
int i, j, m, n;
cin >> m >> n;
for (i = 0; i < m; i = i + 2) {
for (j = 0; j < n; j++) {
str[i][j] = '#';
}
}
for (i = 1; i < m; i = i + 4) {
for (j = 0; j < n - 1; j++) {
str[i][j] = '.';
str[i][n - 1] = '#';
}
}
for (i = 3; i < m; i = i + 4) {
for (j = 1; j < n; j++) {
str[i][j] = '.';
str[i][0] = '#';
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
cout << str[i][j];
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k, ans, sum, ti, count, i;
cin >> n >> k;
if (k >= (n / 2)) {
ans = (n * (n - 1)) / 2;
cout << ans << endl;
return 0;
}
sum = 0;
ans = n - 1;
count = 0;
for (i = k; i >= 1; i--) {
sum = sum + ans;
ans--;
count++;
}
ti = n - 2 * count;
sum = sum + ti * count;
ti = count;
ti--;
count--;
while (ti--) {
sum += count;
count--;
}
cout << sum;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, n;
cin >> n;
pair<long long int, long long int> p[n];
for (i = 0; i < n; i++) {
cin >> p[i].first;
}
for (i = 0; i < n; i++) {
cin >> p[i].second;
}
sort(p, p + n);
priority_queue<long long int> q;
long long int sum = 0, val = 0, ans = 0;
for (i = 0; i <= n; i++) {
while (q.size() != 0 && (i == n || p[i].first > val)) {
sum -= q.top();
q.pop();
ans += sum;
val++;
}
if (i == n) break;
val = p[i].first;
sum += p[i].second;
q.push(p[i].second);
}
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int A[4];
cin >> A[0] >> A[1] >> A[2] >> A[3];
sort(A,A+4);
if (A[0]==1 and A[1]==4 and A[2]==7 and A[3]==9){
cout << "YES";
}
else{
cout << "NO";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 20;
const int ALP = 30;
const int BASE = 31337;
const double EPS = 1e-9;
const double PI = 3.1415926535;
int n, m;
long long kol[N];
set<pair<long long, int> > S;
vector<pair<long long, pair<int, int> > > edg;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
kol[x] -= z;
kol[y] += z;
}
for (int i = 1; i <= n; i++) {
if (kol[i] == 0) continue;
S.insert({kol[i], i});
}
while (!S.empty()) {
int x = S.rbegin()->second;
S.erase({kol[x], x});
int y = S.begin()->second;
S.erase({kol[y], y});
long long z = min(kol[x], -kol[y]);
edg.push_back({z, {y, x}});
kol[x] -= z, kol[y] += z;
if (kol[x] != 0) S.insert({kol[x], x});
if (kol[y] != 0) S.insert({kol[y], y});
}
printf("%d\n", (int)edg.size());
for (pair<long long, pair<int, int> > A : edg)
printf("%d %d %lld\n", A.second.first, A.second.second, A.first);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 20, mm = 400;
int cnt[maxn];
int us[maxn];
int main() {
int m, r, t;
while (~scanf("%d%d%d", &m, &t, &r)) {
if (t < r) {
printf("-1\n");
for (int i = 0; i < m; i++) scanf("%d", &t);
continue;
}
memset(cnt, 0, sizeof cnt);
memset(us, 0, sizeof us);
int ans = 0;
for (int i = 0; i < m; i++) {
int w;
scanf("%d", &w);
int l = w - 1;
while (cnt[w] < r) {
if (us[l + mm] == 0) {
for (int j = l + 1; j <= l + t; j++) cnt[j]++;
us[l + mm] = 1;
ans++;
}
l--;
}
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
unordered_map<int,int>cnt;
int main()
{
int N,M;
scanf("%d",&N);
bool ok = true;
int tmp;
for (int i=1;i<=N;i++)scanf("%d",&tmp),cnt[tmp]++;
scanf("%d",&M);
for (int i=1;i<=M;i++){scanf("%d",&tmp);if (cnt[tmp]-- == 0) ok = false;}
puts(ok?"YES":"NO");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
class SCCDecomp {
int n;
vvi adj;
public:
vvi scc;
SCCDecomp(int n) : n(n), adj(n) {}
void addArc(int a, int b) {
adj[a].push_back(b);
}
int run() {
vi num(n), low(n);
stack<int> S;
vi inS(n);
int t = 0;
function<void(int)> visit = [&](int v) {
low[v] = num[v] = ++t;
S.push(v);
inS[v] = 1;
for (int s: adj[v]) {
if (!num[s]) {
visit(s);
low[v] = min(low[v], low[s]);
} else if (inS[s]) {
low[v] = min(low[v], num[s]);
}
}
if (low[v] == num[v]) {
scc.push_back(vi());
while (1) {
int w = S.top();
S.pop();
inS[w] = false;
scc.back().push_back(w);
if (v == w) {
break;
}
}
}
};
for (int i = 0; i < n; i++) {
if (num[i] == 0) {
visit(i);
}
}
return scc.size();
}
};
int main() {
int v, e;
cin >> v >> e;
SCCDecomp s(v);
for (int i = 0; i < e; i++) {
int a, b;
cin >> a >> b;
s.addArc(a, b);
}
int n = s.run();
vi scc(v);
for (int i = 0; i < n; i++) {
for (int v: s.scc[i]) {
scc[v] = i;
}
}
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
cout << (scc[a] == scc[b]) << endl;
}
}
| 0 |
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
int main() {
int l;
string s;
while (cin >> s >> l, l) {
int cnt = 0;
vector<string>v;
while (s.size() < l)s = '0' + s;
v.push_back(s);
while (1) {
string big(s), small(s);
sort(small.begin(), small.end());
sort(big.begin(), big.end(), greater<int>());
s = to_string(stoi(big) - stoi(small));
while (s.size() < l)s = '0' + s;
v.push_back(s);
int f = distance(v.begin(), find(v.begin(), v.end(), s));
if (f!=v.size()-1) {
cout << f << " " << stoi(s) << " " << v.size()-1-f << endl;
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename item>
item abs(item i) {
if (i < 0) i = -i;
return i;
}
long long int mod = 1000000000 + 7;
;
template <class typea>
typea POW(typea b, typea p) {
typea res = 1;
while (p) {
if (p & 1) {
res *= b;
res %= mod;
}
b *= b;
b %= mod;
p /= 2;
}
return res;
}
int tc = 0;
double arr[3 * 100000 + 10];
long long int n, k, m;
int main() {
cin >> n >> k >> m;
for (int i = 1; i <= n; i++) cin >> arr[i];
sort(arr + 1, arr + 1 + n);
reverse(arr + 1, arr + 1 + n);
long long int last;
if (m >= n)
last = 1, m -= (n - 1);
else
last = n - m, m = 0;
double ans = 0.0;
double sum = 0;
for (int i = 1; i <= last; i++) sum += arr[i];
long long int temp = min(m, last * k);
sum += temp;
m -= temp;
ans = sum / last;
while (++last <= n) {
sum += arr[last];
m++;
temp = min(m, k);
sum += temp;
m -= temp;
ans = max(sum / last, ans);
}
cout << fixed << setprecision(12) << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010;
struct DSU {
int par[MAXN];
DSU() {
for (int i = 1; i < MAXN; i++) par[i] = i;
}
int get(int x) {
if (par[x] == x) return x;
return par[x] = get(par[x]);
}
int join(int x, int y) {
if (get(x) == get(y)) return 0;
par[get(x)] = get(y);
return 1;
}
} dsu;
int n, m, k, u, v, w, x, y, t, a, b;
long long ans;
int P[MAXN];
pair<long long, pair<int, int>> E[MAXN];
vector<pair<int, int>> G[MAXN];
long long dist[MAXN];
int par[MAXN];
bool mark[MAXN];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
pq;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
G[u].push_back({v, w});
G[v].push_back({u, w});
E[i] = {w, {u, v}};
}
memset(dist, 63, sizeof(dist));
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d", &P[i]);
pq.push({dist[P[i]] = 0, P[i]});
par[P[i]] = P[i];
}
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (mark[u]) continue;
mark[u] = 1;
for (pair<int, int> p : G[u]) {
int v = p.first, w = p.second;
if (dist[u] + w < dist[v]) {
pq.push({dist[v] = dist[u] + w, v});
par[v] = par[u];
}
}
}
for (int i = 0; i < m; i++) {
long long w = E[i].first, u = E[i].second.first, v = E[i].second.second;
E[i] = {w + dist[v] + dist[u], {par[u], par[v]}};
}
sort(E, E + m);
for (int i = 0; i < m; i++) {
long long w = E[i].first, u = E[i].second.first, v = E[i].second.second;
ans += w * dsu.join(u, v);
}
printf("%lld\n", ans + dist[1]);
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct line{
int l,r;
bool friend operator <(line i,line j){
return (i.l==j.l)?i.r<j.r:i.l<j.l;
}
}a[N];
int pre[N],nxt[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i].l>>a[i].r;
sort(a+1,a+n+1);
pre[0]=nxt[n+1]=1e9;
for(int i=1;i<=n;++i)
pre[i]=min(pre[i-1],a[i].r);
for(int i=n;i>=1;--i)
nxt[i]=min(nxt[i+1],a[i].r);
int ans=0;
for(int i=1;i<n;++i)
ans=max(ans,max(pre[i]-a[i].l+1,0)+max(nxt[i+1]-a[n].l+1,0));
int L=a[n].l,R=pre[n];
for(int i=1;i<=n;++i)
ans=max(ans,max(R-L+1,0)+a[i].r-a[i].l+1);
cout<<ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int f[2010];
int find(int x) {
if (f[x] == x) return x;
return f[x] = find(f[x]);
}
int n, m, k;
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) f[i] = i;
int cnt = n;
for (int i = 1; i <= n; i++) {
int l = i;
int r = l + k - 1;
if (r > n) break;
while (l < r) {
int x = find(l);
int y = find(r);
if (x != y) cnt--;
f[x] = y;
l++;
r--;
}
}
long long ans = 1ll;
for (int i = 1; i <= cnt; i++) ans = ans * (long long)m % 1000000007;
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9, C = 26;
int id;
struct Node {
int link[C];
int dp;
void clear() { memset(link, -1, sizeof(link)); }
} u[N];
void insert(char *s) {
int top = 0;
for (int i = 0; s[i]; ++i) {
int c = s[i] - 'a';
if (u[top].link[c] == -1) {
u[id].clear();
u[top].link[c] = id++;
}
top = u[top].link[c];
}
}
void dfs(int x) {
bool leaf = 1;
int dp = 1;
for (int i = 0; i < C; ++i) {
int to = u[x].link[i];
if (to == -1) continue;
dfs(to);
leaf = 0;
dp &= u[to].dp;
}
if (leaf) {
u[x].dp = 0;
} else {
if (dp == 0)
u[x].dp = 1;
else
u[x].dp = 0;
}
}
void dfs2(int x) {
bool leaf = 1;
int dp = 1;
for (int i = 0; i < C; ++i) {
int to = u[x].link[i];
if (to == -1) continue;
dfs2(to);
leaf = 0;
dp &= u[to].dp;
}
if (leaf) {
u[x].dp = 1;
} else {
if (dp == 0)
u[x].dp = 1;
else
u[x].dp = 0;
}
}
char s[N];
int main() {
u[(id = 0)++].clear();
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i) {
scanf("%s", s);
insert(s);
}
dfs(0);
if (!u[0].dp) {
puts("Second");
} else {
dfs2(0);
if (u[0].dp)
puts("First");
else {
if (k & 1)
puts("First");
else
puts("Second");
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
const double EULER = 0.57721566490153286060651209;
const double PI = 3.14159265358979323846264338328;
const double E = 2.718281828459045235360287471352;
using namespace std;
long long a[100005];
bool b[100005];
long long c[100005];
int cnt;
const long long nega = -2e18;
const long long posi = 2e18;
int main() {
long long res;
int n, k;
cin.sync_with_stdio(false);
cin >> n >> k;
cin.get();
char ch;
for (int i = 0; i < n; i++) {
if (cin.peek() == '?') {
b[i] = true;
cin.get();
cin.get();
} else {
ch = cin.get();
bool neg = ch == '-';
if (neg) ch = cin.get();
res = 0;
while (ch != ' ' && ch != '\n') {
res *= 10;
res += ch - '0';
ch = cin.get();
}
a[i] = (neg) ? -res : res;
}
}
for (int i = 0; i < k; i++) {
cnt = 0;
int len = 0, last;
for (int j = i; j < n; j += k) {
len++;
if (j + k >= n) {
last = j;
}
if (b[j]) {
cnt++;
} else {
if (cnt == 0) {
if (j >= k && a[j] <= a[j - k]) {
puts("Incorrect sequence");
return 0;
}
} else {
long long l = (j < (cnt + 1) * k) ? nega : a[j - cnt * k - k];
long long r = a[j];
if (r - l <= cnt) {
puts("Incorrect sequence");
return 0;
} else {
if (l >= -cnt / 2) {
for (int p = j - cnt * k; p < j; p += k) {
a[p] = ++l;
}
} else if (r <= cnt / 2) {
for (int p = j - k; p >= j - cnt * k; p -= k) {
a[p] = --r;
}
} else {
l = -cnt / 2 - 1;
for (int p = j - cnt * k; p < j; p += k) {
a[p] = ++l;
}
}
}
}
cnt = 0;
}
}
if (cnt) {
int j = last + k;
long long l = (len == cnt) ? nega : a[j - cnt * k - k];
if (l >= -cnt / 2) {
for (int p = j - cnt * k; p < j; p += k) {
a[p] = ++l;
}
} else {
l = -cnt / 2 - 1;
for (int p = j - cnt * k; p < j; p += k) {
a[p] = ++l;
}
}
}
}
for (int i = 0; i < n - 1; i++) {
cout << a[i] << ' ';
}
cout << a[n - 1] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, A[100005], B[100005], cev, flag, son, uc;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
B[i] = A[i];
}
sort(B + 1, B + n + 1);
for (int i = 1; i <= n; i++) {
if (A[i] == B[i]) {
uc++;
}
}
for (int i = 1; i <= n; i++) {
if (A[i] != B[i]) {
cev = i;
break;
}
}
for (int i = n; i >= cev; i--) {
if (A[i] != B[i]) {
flag = i;
break;
}
}
reverse(A + cev, A + flag + 1);
for (int i = 1; i <= n; i++) {
if (A[i] == B[i]) {
son++;
}
}
if (uc == n) {
printf("yes\n1 1");
return 0;
}
if (son == n) {
printf("yes\n%d %d", cev, flag);
} else {
printf("no");
}
return 0;
}
| 2 |
// B - ATCoder
#include <bits/stdc++.h>
using namespace std;
int main(){
string T="ACGT", S; cin>>S;
int maxi = 0, now=0;
for(int i=0; i<S.size(); ++i){
now = T.find(S[i]) < T.size()? now+1: 0;
maxi = max(maxi, now);
}
cout<< maxi <<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int n, ans;
int data[4][maxn][maxn];
int cal() {
int chi[2][4], ret = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
int sum = 0;
for (int ii = 0; ii < n; ii++) {
for (int jj = 0; jj < n; jj++) {
int res = (ii + jj) % 2;
if (i == 0) {
if (data[j][ii][jj] != res) sum++;
} else {
if (data[j][ii][jj] == res) sum++;
}
}
}
chi[i][j] = sum;
}
}
int A[4], B[4];
for (int i = 0; i < 4; i++) {
A[i] = abs(chi[0][i] - chi[1][i]);
B[i] = i;
}
for (int i = 3; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (A[j] < A[j + 1]) {
int tmp = A[j];
A[j] = A[j + 1];
A[j + 1] = tmp;
tmp = B[j];
B[j] = B[j + 1];
B[j + 1] = tmp;
}
}
}
int ones = 0, zeros = 0;
if (chi[0][B[0]] < chi[1][B[0]]) {
zeros++;
ret += chi[0][B[0]];
} else {
ones++;
ret += chi[1][B[0]];
}
if (chi[0][B[1]] < chi[1][B[1]]) {
zeros++;
ret += chi[0][B[1]];
} else {
ones++;
ret += chi[1][B[1]];
}
if (ones == 2) {
ret += chi[0][B[2]];
ret += chi[0][B[3]];
} else if (zeros == 2) {
ret += chi[1][B[2]];
ret += chi[1][B[3]];
} else {
if (chi[0][B[2]] < chi[1][B[2]]) {
ret += chi[0][B[2]];
ret += chi[1][B[3]];
} else {
ret += chi[1][B[2]];
ret += chi[0][B[3]];
}
}
return ret;
}
int main(void) {
cin >> n;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
char x;
cin >> x;
while (x != '0' && x != '1') cin >> x;
data[i][j][k] = x - '0';
}
}
}
cout << cal() << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
string s;
cin >> n >> k >> s;
vector<vector<int>> g(k, vector<int>(200, 0));
for (int i = 0; i < n; i++) g[i % k][s[i]]++;
for (int i = 0, cnt = 0; i < k; i++) {
int sum = 0, mx = 0;
for (char c = 'a'; c <= 'z'; c++) {
sum += g[i][c] + g[k - i - 1][c];
mx = max(mx, g[i][c] + g[k - i - 1][c]);
}
cnt += sum - mx;
if (i == k - 1) cout << cnt / 2 << '\n';
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
x = 0;
int f = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = 1;
if (ch == EOF) return;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch - '0');
ch = getchar();
}
if (f) x = -x;
}
const int MAXN = 200011;
struct node {
long long v;
int ls, rs, pr, siz;
} T[MAXN];
int root, Lrt, Rrt, tot = 0;
inline int addnode(long long v) {
T[++tot].v = v;
T[tot].ls = T[tot].rs = 0;
T[tot].pr = rand();
T[tot].siz = 1;
return tot;
}
inline void pullup(int x) { T[x].siz = T[T[x].ls].siz + T[T[x].rs].siz + 1; }
inline void split(int rt, int& x, int& y, long long k) {
if (!rt) {
x = y = 0;
return;
}
if (T[rt].v >= k) {
y = rt;
split(T[rt].ls, x, T[y].ls, k);
} else {
x = rt;
split(T[rt].rs, T[x].rs, y, k);
}
pullup(rt);
}
inline int merge(int x, int y) {
if (x < 1 || y < 1) {
return x < 1 ? y : x;
}
if (T[x].pr < T[y].pr) {
T[x].rs = merge(T[x].rs, y);
pullup(x);
return x;
} else {
T[y].ls = merge(x, T[y].ls);
pullup(y);
return y;
}
}
inline void insert(long long v) {
split(root, Lrt, Rrt, v);
root = merge(merge(Lrt, addnode(v)), Rrt);
}
long long a[MAXN], s[MAXN];
int n;
long long m, ans;
int main() {
scanf("%d%lld", &n, &m);
--m;
insert(0);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
s[i] = s[i - 1] + a[i];
int Lrt, Rrt;
split(root, Lrt, Rrt, s[i] - m);
ans += T[Rrt].siz;
root = merge(Lrt, Rrt);
insert(s[i]);
}
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
void dbg(const char* fmt, ...) {}
int main() {
int t1, t2, t3;
int k;
int n1, n2, n3;
std::multiset<int> free1, free2, free3;
scanf("%d", &k);
scanf("%d%d%d", &n1, &n2, &n3);
for (int i = 0; i < n1; i++) free1.insert(0);
for (int i = 0; i < n2; i++) free2.insert(0);
for (int i = 0; i < n3; i++) free3.insert(0);
scanf("%d%d%d", &t1, &t2, &t3);
t2 += t1;
t3 += t2;
int ans = 0;
for (int i = 0; i < k; i++) {
int s1 = (*free1.begin());
free1.erase(free1.begin());
int s2 = (*free2.begin()) - t1;
free2.erase(free2.begin());
int s3 = (*free3.begin()) - t2;
free3.erase(free3.begin());
int start_time = std::max(s1, std::max(s2, s3));
free1.insert(start_time + t1);
free2.insert(start_time + t2);
free3.insert(start_time + t3);
ans = std::max(ans, start_time + t3);
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
double x, y;
Point(double a = 0, double b = 0) {
x = a;
y = b;
}
};
struct Line {
double a, b, c, angle;
Point p1, p2;
Line(Point s, Point e) {
a = s.y - e.y;
b = e.x - s.x;
c = s.x * e.y - e.x * s.y;
angle = atan2(e.y - s.y, e.x - s.x);
p1 = s;
p2 = e;
}
Line() {}
};
struct Segment {
Point s, e;
Segment(Point a, Point b) {
s = a;
e = b;
}
Segment(double x1, double y1, double x2, double y2) {
s = Point(x1, y1);
e = Point(x2, y2);
}
Segment() {}
};
Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); }
Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
Point operator*(Point a, double k) { return Point(a.x * k, a.y * k); }
Point operator/(Point a, double k) { return Point(a.x / k, a.y / k); }
double len(Point a) { return sqrt(a.x * a.x + a.y * a.y); }
double Cross(Point& sp, Point& ep, Point& op) {
return (sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y);
}
double Cross(Point a, Point b) { return a.x * b.y - b.x * a.y; }
double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }
double max(double a, double b) {
if (a < b)
return b;
else
return a;
}
double min(double a, double b) {
if (a > b)
return b;
else
return a;
}
int epssgn(double x) {
if (fabs(x) < 1e-8)
return 0;
else
return x < 0 ? -1 : 1;
}
double dis(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double PointToSegDist(Point A, Point B, Point C) {
if (dis(A, B) < 1e-8) return dis(B, C);
if (epssgn(Dot(B - A, C - A)) < 0) return dis(A, C);
if (epssgn(Dot(A - B, C - B)) < 0) return dis(B, C);
return fabs(Cross(B - A, C - A)) / dis(B, A);
}
const int maxn = 1005;
int n;
Point point[maxn];
int main() {
scanf("%d", &n);
double ans = 1e12;
for (int i = 0; i < n; i++) {
double x, y;
scanf("%lf%lf", &x, &y);
point[i] = Point(x, y);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || (i + 1) % n == j || (i - 1 + n) % n == j) continue;
if ((j - 1 + n) % n != i)
ans = min(
ans,
PointToSegDist(point[i], point[j], point[(j - 1 + n) % n]) / 2.0);
if ((j + 1) % n != i)
ans = min(ans,
PointToSegDist(point[i], point[j], point[(j + 1) % n]) / 2.0);
if ((i - 1 + n) % n != j)
ans = min(
ans,
PointToSegDist(point[i], point[j], point[(i - 1 + n) % n]) / 2.0);
if ((i + 1) % n != j)
ans = min(ans,
PointToSegDist(point[i], point[j], point[(i + 1) % n]) / 2.0);
}
}
printf("%.8f\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int a, x, y;
while (scanf(" %d %d %d", &a, &x, &y) == 3) {
if (x <= -100 || x >= 100 || y % a == 0) {
printf("-1\n");
continue;
}
double width;
if (y < a) {
width = double(a) / 2;
if (x > -width && x < width)
printf("%d\n", 1);
else
printf("-1\n");
} else if (y / a % 2 == 0) {
if (x > -a && x < 0)
printf("%d\n", 1 + 3 * (y / a / 2) - 1);
else if (x > 0 && x < a)
printf("%d\n", 1 + 3 * (y / a / 2));
else
printf("-1\n");
} else {
width = double(a) / 2;
if (x > -width && x < width)
printf("%d\n", 1 + 3 * (y / a / 2) + 1);
else
printf("-1\n");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
char x = 'c';
vector<char> a;
cin >> n >> k;
if (k > n) {
cout << -1;
return 0;
}
if (n == 1) {
cout << "a";
return 0;
}
if (k == 1 && n != 1) {
cout << -1;
return 0;
}
for (long long i = 0; i < n - k + 2; i++) {
if (i % 2 == 0)
a.push_back('a');
else
a.push_back('b');
}
for (long long i = 2; i < k; i++) {
a.push_back(x);
x++;
}
for (long long i = 0; i < n; i++) cout << a[i];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 1000;
puts("2000");
for (int i = 1; i <= n; ++i) printf("%d 1 %d 2\n", i, i);
for (int i = n; i > 0; --i) printf("%d 1 %d 2\n", i, i);
}
| 4 |
//#pragma GCC optimize("trapv")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 733, mod = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, k, mat[maxn][maxn];
vi g[maxn];
int c[2];
int col[maxn];
void dfs(int v, int cc) {
col[v] = cc+1;
c[cc]++;
for(int i : g[v]) {
if(col[v]==col[i]) cout << -1, exit(0);
if(col[i]) continue;
dfs(i, cc^1);
}
}
bitset<100000> bp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int f, t, i = 0; i < m; i++) {
cin >> f >> t;
mat[f][t] = mat[t][f] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = i+1; j <= n; j++) {
if(mat[i][j]) continue;
g[i].pb(j), g[j].pb(i);
++k;
}
int a = 0, b = 0;
bp.set(0);
for(int i = 1; i <= n; i++) {
if(col[i]) continue;
c[0] = c[1] = 0;
dfs(i, 0);
bp = (bp<<c[0])|(bp<<c[1]);
}
int ans = 1<<30;
for(int i = 0; i <= n; i++) if(bp.test(i)) ans = min(ans, m - (i*(n-i) - k));//, cout << i << "\n";
cout << ans;
}
| 0 |
#include <bits/stdc++.h>
#define il inline
const int N=2e5+5,M=1e6;
int n,P[N],Q[N],A[N],B[N],lev[N],q[M],h,t;
int S,T,to[M],nx[M],cap[M],hd[N],it[N],sze=1;
il void add(int u,int v,int c){to[++sze]=v,nx[sze]=hd[u],cap[sze]=c,hd[u]=sze;}
il void ins(int u,int v,int c){add(u,v,c),add(v,u,0);}
il int bfs()
{
memset(lev,0,4*T+4),q[h=t=lev[S]=1]=S; int i,u,v;
while (h<=t) for (i=hd[u=q[h++]]; i; i=nx[i])
if (cap[i]&&!lev[v=to[i]]) lev[v]=lev[u]+1,q[++t]=v;
return lev[T];
}
il int dfs(int u,int f)
{
if (u==T) return f; int v,res=0,z;
for (int &i=it[u]; i; i=nx[i])
if (lev[v=to[i]]==lev[u]+1&&cap[i]&&(z=dfs(v,std::min(f-res,cap[i]))))
{
cap[i]-=z,cap[i^1]+=z,res+=z;
if (res==f) break;
}
return res;
}
il int dinic()
{
int res=0,f;
while (bfs())
{
memcpy(it,hd,4*T+4);
while (f=dfs(S,1e9)) res+=f;
}
return res;
}
int main()
{
scanf("%d",&n),S=n+n+1,T=S+1; int i,j,s=0;
for (i=1; i<=n; i++) scanf("%d",P+i),P[i]++,A[i]=i;
for (i=1; i<=n; i++) scanf("%d",Q+i),Q[i]++,B[i]=i;
for (i=1; i<=n; i++)
{
if (A[i]==i){for (j=P[i]; j!=i; j=P[j]) A[j]=i;}
if (B[i]==i){for (j=Q[i]; j!=i; j=Q[j]) B[j]=i;}
}
for (i=1; i<=n; i++) B[i]+=n;
for (i=1; i<=n; i++)
if (P[i]==i&&Q[i]==i) s++;
else if (P[i]==i) ins(B[i],T,1);
else if (Q[i]==i) ins(S,A[i],1);
else if (P[i]!=Q[i]) ins(B[i],A[i],1);
else ins(A[i],B[i],1),ins(B[i],A[i],1);
printf("%d",n-dinic()-s);
return 0;
}
| 0 |
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
#include<climits>
#include<queue>
using namespace std;
#define INF INT_MAX
struct edge{ int to,cost; };
typedef pair<int,int> P;//firstは最短距離、secondは頂点の番号
vector<edge> G[3003];
int d[3003];
int n,m,k;
int solve(int s){
int ret=0;
for(int i=0;i<G[s].size();i++){
edge e=G[s][i];
int a=(((e.cost+d[e.to]-d[s])*10/2)+5)/10;//一番長い距離への差を算出
ret=max(ret,a+d[s]);
//cout << "最高地..."<<ret << " 謎の値..." <<a << endl;
}
return ret;
}
void dijkstra(int s){
// greater<P>を指定することでfirstが小さい順に取り出せるようにする
priority_queue<P, vector<P>, greater<P> > que;
d[s]=0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top(); que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
//cout << "before : basyo:"<< e.to << " cost:" << d[e.to] << endl;
if(d[e.to] > d[v] + e.cost){
d[e.to]=d[v] + e.cost;
que.push(P(d[e.to],e.to));
}
//cout << "after : basyo:"<< e.to << " cost:" << d[e.to] << endl;
}
}
// cout << s << "..."<< d[s] << endl;
}
int main(){
cin >> n >> m >> k;
for(int i=0;i<m;i++){
int a,b,l;
edge bl;
cin >> a >> b >> bl.cost;
bl.to=b;
G[a].push_back(bl);
bl.to=a;
G[b].push_back(bl);
}
fill(d,d+n+1,INF);
for(int i=0;i<k;i++){
int s;
cin >> s;
dijkstra(s);
}
// cout << "koko" << endl;
vector <int> md;
int mx=0;
/*for(int i=1;i<=n;i++){
if(mx==d[i]) md.push_back(i);
if(mx<d[i]){
md.clear();
md.push_back(i);
mx=d[i];
}
// cout << i << "..." << d[i] << endl;
}
int res=0;
for(int i=0;i<md.size();i++)
res=max(res,solve(md[i]));
*/
int res=0;
for(int i=1;i<=n;i++)
res=max(res,solve(i));
cout << res << endl;
// cout << q.top() ;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
multiset<int64_t> S;
if (n & 1) {
n--;
cout << n / 2 << endl;
while (n) {
S.insert(2);
n = n - 2;
}
auto itr = S.end();
itr--;
S.erase(itr);
S.insert(3);
} else {
cout << n / 2 << endl;
while (n) {
S.insert(2);
n = n - 2;
}
}
for (auto itr = S.begin(); itr != S.end(); itr++) {
cout << (*itr) << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
int sgn(int x) {
if (x > 0) return 1;
if (x == 0) return 0;
return -1;
}
signed main() {
int x, y;
cin >> x >> y;
int x1, x2, y1, y2;
int ans = abs(x) + abs(y);
x1 = ans * sgn(x), y1 = 0;
x2 = 0;
y2 = ans * sgn(y);
if (tie(x1, y1) > tie(x2, y2)) {
swap(x1, x2);
swap(y1, y2);
}
cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n';
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
vector <int> dr[1000100],db[1000100];
int fa[1000100],dep[1000100],ans;
void prework(int s)
{
for (int k=0;k<db[s].size();k++) if (db[s][k]!=fa[s])
{
fa[db[s][k]]=s;
dep[db[s][k]]=dep[s]+1;
prework(db[s][k]);
}
}
bool check(int u,int v)
{
if (dep[u]<dep[v]) swap(u,v);
if (fa[fa[u]]==v) return true;
if (fa[u]==fa[v]) return true;
if (fa[u]==v) return true;
if (u==v) return true;
return false;
}
void dfs(int s,int pre,int d)
{
ans=max(ans,dep[s]);
if (dep[s]<=d) return;
for (int k=0;k<dr[s].size();k++) if (dr[s][k]!=pre)
{
if (!check(dr[s][k],s)) {printf("-1\n");exit(0);}
dfs(dr[s][k],s,d+1);
}
}
int main()
{
int n,b,r;scanf("%d%d%d",&n,&b,&r);
for (int i=1;i<n;i++)
{
int u,v;scanf("%d%d",&u,&v);
dr[u].push_back(v);dr[v].push_back(u);
}
for (int i=1;i<n;i++)
{
int u,v;scanf("%d%d",&u,&v);
db[u].push_back(v);db[v].push_back(u);
}
prework(r);
dfs(b,0,0);
printf("%d\n",ans<<1);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1123456;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
void vout(T s) {
cout << s << "\n";
exit(0);
}
long long bp(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res *= a;
a *= a;
n >>= 1;
}
return res;
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> a(n), b(n);
long long ra, rb;
ra = rb = 0;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
long long sc = 0;
for (int i = 0; i < n; i++) {
sc += b[i];
ra += max(0ll, a[i] - m * sc);
}
sc = 0;
for (int i = n - 1; i >= 0; i--) {
sc += b[i];
rb += max(0ll, a[i] - m * sc);
}
if (ra > rb) vout("Limak");
if (rb > ra) vout("Radewoosh");
vout("Tie");
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, b;
int result, result1, r, n, q[10], num, t, c;
getline(cin, s);
int len = s.size();
memset(q, 0, sizeof(q));
result = 0;
for (int i = 0; i < len; i++) {
num = s[i];
for (int j = 0; j < 8; j++) {
q[j] = num % 2;
num >>= 1;
}
result1 = 0;
for (int j = 0; j < 8; j++) result1 = result1 * 2 + q[j];
r = result - result1;
cout << (256 + r) % 256 << endl;
result = result1;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int first;
int n;
cin >> n;
int i;
vector<int> nums(n);
for (i = 0; i < n; ++i) {
cin >> nums[i];
}
int minDiff = abs(nums[n - 1] - nums[0]);
int idx = n - 1;
for (i = 0; i < n - 1; ++i) {
int t = abs(nums[i + 1] - nums[i]);
if (t < minDiff) {
minDiff = t;
idx = i;
}
}
if (idx == n - 1)
cout << n << ' ' << 1 << endl;
else
cout << idx + 1 << ' ' << idx + 2 << endl;
return 0;
}
| 1 |
#include <cstdio>
#include <map>
using namespace std;
int main(){
int i,n,x,y;
for(;scanf("%d",&n),n;printf(i==n?"NO COLOR\n":"%d\n",x)){
map<int,int> m;
for(i=0;i<n;i++){
scanf("%d",&x);
m[x]++;
if(m[x]>n/2){for(;getchar()-10;);i=0;break;}
}
}
}
| 0 |
#include <bits/stdc++.h>
int main() {
long long int a, b, c, t, d, i;
scanf("%lld", &t);
for (i = 1; i <= t; i++) {
scanf("%lld%lld%lld", &a, &b, &c);
d = (a + b + c) / 2;
printf("%lld\n", d);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline void err(istringstream *iss) {}
template <typename T, typename... Args>
void err(istringstream *iss, const T &$val, const Args &...args) {
string $name;
*iss >> $name;
if ($name.back() == ',') $name.pop_back();
cout << $name << " = " << $val << "; ", err(iss, args...);
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
int n, m, K, T, Q, cn;
int dist[65][65][65], d[65][65][65];
int main() {
cin.tie(NULL);
scanf("%d%d%d", &n, &m, &Q);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
scanf("%d", &dist[i][j][k]);
}
}
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
dist[i][k][l] = min(dist[i][k][l], dist[i][k][j] + dist[i][j][l]);
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
d[i][j][0] = 1000000007;
for (int k = 0; k < m; k++) {
d[i][j][0] = min(d[i][j][0], dist[k][i][j]);
}
}
}
for (int i = 1; i <= 65 - 1; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
d[j][k][i] = d[j][k][i - 1];
for (int l = 0; l < n; l++) {
d[j][k][i] = min(d[j][k][i], d[j][l][0] + d[l][k][i - 1]);
}
}
}
}
while (Q--) {
int s, t;
scanf("%d%d%d", &s, &t, &K);
printf("%d\n", d[--s][--t][min(n, K)]);
}
return 0;
}
| 4 |
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int N, M;
int p[100005];
ll dist[100005];
int find(int a){
if(a==p[a]) return a;
int pa=p[a];
p[a]=find(pa);
dist[a]+=dist[pa];
return p[a];
}
bool merge(int a,int b,int d){
int pa=find(a), pb=find(b);
if(pa==pb){
return dist[a]==dist[b]+d;
}
else{
p[pa]=pb;
dist[pa]=d-dist[a]+dist[b];
return 1;
}
}
int main(){
scanf("%d %d",&N,&M);
for(int i=1;i<=N;i++)
p[i]=i;
while(M--){
int L,R,D; scanf("%d %d %d",&L,&R,&D);
if(!merge(L,R,D)) return !printf("No");
}
puts("Yes");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int kase;
cin >> kase;
while (kase--) {
int n, m, p;
cin >> n >> m;
while (m--) {
if (!m)
cout << n << endl;
else {
cout << (p = pow(10, (int)log10(n - m))) << " ";
n -= p;
}
}
}
}
| 4 |
#include <iostream>
using namespace std;
int main() {
int g;
int x, y;
cin >> x >> y;
for (int i = 1; i <= 1000; i++) {
if (x % i == 0 && y % i == 0) {
g = i;
}
}
cout << x + y - g + 1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) { return (a > b ? a : b); }
int min(int a, int b) { return ((a < b) ? a : b); }
int gcd(int a, int b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
int main() {
int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
if (n == 1 || m == 1)
cout << "YES\n";
else if (n == 2 && m == 2)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int a,b,c; cin >> a >> b >> c;
for (int i=0;i<120;++i){
int t=60*i+c;
if (t%(a+b)<=a){
cout << t << '\n';
return 0;
}
}
cout << -1 << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long ceil(long long p, long long q) {
if (p % q == 0) return (p / q);
return (p / q) + 1;
}
int main(int argc, char const *argv[]) {
int k, n, s, p;
cin >> k >> n >> s >> p;
cout << ceil(k * ceil(n, s), p) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, tot;
int fat[1008611], ans[1008611];
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; ++i) scanf("%d", &fat[i]);
int now = n;
while (now != 1) {
ans[++tot] = now;
now = fat[now];
}
ans[++tot] = now;
reverse(ans + 1, ans + tot + 1);
for (int i = 1; i <= tot; ++i)
printf("%d%c", ans[i], (i == tot ? '\n' : ' '));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, d, ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d%d%d%d", &a, &b, &c, &d), ans += (c - a + 1) * (d - b + 1);
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3 * 1e5 + 10;
struct visiter {
int index, want, get;
};
vector<visiter> vis;
bool cmp1(visiter v1, visiter v2) { return v1.index < v2.index; }
bool cmp2(visiter v1, visiter v2) { return v1.want < v2.want; }
int main(void) {
int n;
cin >> n;
vis.resize(n);
for (int i = 0; i < n; i++) {
vis[i].index = i;
cin >> vis[i].want;
}
sort(vis.begin(), vis.end(), cmp2);
int cur = 1;
for (int i = 0; i < n; i++) {
cur = max(cur, vis[i].want);
vis[i].get = cur;
cur++;
}
sort(vis.begin(), vis.end(), cmp1);
for (int i = 0; i < n; i++) {
cout << vis[i].get << ((i == n - 1) ? '\n' : ' ');
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5;
const long long M = 20;
const long long SQ = 320;
const long long INF = 1e16;
const long long MOD = 1e9 + 7;
int n;
int x[N], y[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
int t;
cin >> t >> t;
int ans = 0;
for (int i = 0; i < n; i++) {
int p1 = (i - 1 + n) % n, p2 = (i + 1) % n;
if (y[i] == y[p1]) {
if ((x[p1] < x[i] && y[p2] > y[i]) || (x[p1] > x[i] && y[p2] < y[i]))
ans++;
} else {
if ((y[p1] < y[i] && x[p2] < x[i]) || (y[p1] > y[i] && x[p2] > x[i]))
ans++;
}
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int INF = (int)1e9 + 5;
const int K = 6;
const int N = 100100;
int n;
int a[K];
int b[N];
int ans = INF;
int solve(int m) {
if (b[0] - a[0] < m) return INF;
int res = 0;
for (int i = 0; i < K; i++) {
int p = n;
if (i + 1 < K) p = lower_bound(b, b + n, m + a[i + 1]) - b;
if (p > 0) res = max(res, b[p - 1] - a[i]);
}
return res - m;
}
int main() {
startTime = clock();
for (int i = 0; i < K; i++) scanf("%d", &a[i]);
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
sort(a, a + K);
sort(b, b + n);
for (int i = 0; i < K; i++)
for (int j = 0; j < n; j++) ans = min(ans, solve(b[j] - a[i]));
printf("%d\n", ans);
return 0;
}
| 1 |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[305][305];
char name[50];
int cost[305];
int v[305],b[305],f[305];
int n,m;
int solve(int s[],int i,int c){
int res,sk,nc;
if(i==n) return 0;
if(dp[i][c]>=0) return dp[i][c];
res= solve(s,i+1,c);
for(nc=cost[i],sk=s[i];c>=nc;nc+=cost[i],sk+=s[i]){
res=max(res,solve(s,i+1,c-nc)+sk);
}
return dp[i][c]=res;
}
int main(void){
int ans;
while(scanf("%d %d",&n,&m)!=EOF){
getchar();
for(int i=0;i<n;i++){
fgets(name,sizeof(name),stdin);
scanf("%d %d %d %d",&cost[i],&v[i],&b[i],&f[i]);
getchar();
}
ans=0;
memset(dp,-1,sizeof(dp));
ans=max(ans,solve(v,0,m));
memset(dp,-1,sizeof(dp));
ans=max(ans,solve(b,0,m));
memset(dp,-1,sizeof(dp));
ans=max(ans,solve(f,0,m));
printf("%d\n",ans);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, d;
cin >> n >> a >> d;
vector<int> t(n), v(n);
for (int(i) = (int)(0); (i) < (int)(n); ++(i)) cin >> t[i] >> v[i];
double p, q, r;
vector<double> s;
for (int(i) = (int)(0); (i) < (int)(n); ++(i)) {
double r = double(v[i]) / a;
p = sqrt(2. * double(d) / a);
if (r < p) {
q = (double(d) - 0.5 * a * r * r) / v[i] + r;
s.push_back(q + t[i]);
} else {
s.push_back(p + t[i]);
}
}
for (int(i) = (int)(0); (i) < (int)(n); ++(i)) {
if (i > 0 && s[i] <= s[i - 1]) s[i] = s[i - 1];
cout.setf(ios::fixed, ios::floatfield);
cout.precision(9);
cout << s[i] << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<int, vector<pair<int, int>>> adj;
vector<long long> dis(100010, 1e16), parent(100010);
priority_queue<int> q;
int v, e, from, to, w;
void print_path(int node) {
if (node != 1) print_path(parent[node]);
cout << node << " ";
}
void dijkstra(int node) {
q.push(node), dis[node] = 0;
while (!q.empty()) {
int cur = q.top();
q.pop();
for (int i = 0; i < adj[cur].size(); i++) {
int child = adj[cur][i].first, W = adj[cur][i].second;
if (dis[cur] + W < dis[child]) {
dis[child] = dis[cur] + W;
q.push(child);
parent[child] = cur;
}
}
}
}
int main() {
cin >> v >> e;
for (int i = 1; i <= e; i++) {
cin >> from >> to >> w;
adj[from].push_back({to, w});
adj[to].push_back({from, w});
}
dijkstra(1);
if (dis[v] == 1e16)
cout << "-1";
else
print_path(v);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
string ss[10];
string ans = "";
for (int i = 0; i < 10; i++) {
cin >> ss[i];
}
for (int i = 0; i < 80; i += 10) {
string k = s.substr(i, 10);
if (k == ss[0])
ans += '0';
else if (k == ss[1])
ans += '1';
else if (k == ss[2])
ans += '2';
else if (k == ss[3])
ans += '3';
else if (k == ss[4])
ans += '4';
else if (k == ss[5])
ans += '5';
else if (k == ss[6])
ans += '6';
else if (k == ss[7])
ans += '7';
else if (k == ss[8])
ans += '8';
else if (k == ss[9])
ans += '9';
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
long long int power(long long int s, int t) {
long long int ans = 1;
for (int i = 1; i <= t; i++) ans *= s, ans %= 1000000007;
return ans;
}
int dist[100010];
std::vector<std::pair<int, int> > V[100010];
void func(int s, int t) {
if (dist[s] > 0) return;
dist[s] = t;
for (int i = 0; i < V[s].size(); i++) func(V[s][i].first, V[s][i].second + t);
}
int main() {
int a, b;
scanf("%d%d", &a, &b);
for (int i = 1; i <= b; i++) {
int c, d, e;
scanf("%d%d%d", &c, &d, &e);
V[c].push_back(std::make_pair(d, 1 - e));
V[d].push_back(std::make_pair(c, 1 - e));
}
int C = 0;
for (int i = 1; i <= a; i++) {
if (dist[i] == 0) {
func(i, 1);
C++;
}
}
for (int i = 1; i <= a; i++) {
for (int j = 0; j < V[i].size(); j++) {
if ((dist[i] + dist[V[i][j].first] + V[i][j].second) % 2 == 1) {
printf("0");
return 0;
}
}
}
printf("%lld", power(2, C - 1));
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int a, int b) { return a + b < mod ? a + b : a + b - mod; }
int mul(int a, int b) { return int(1LL * a * b % mod); }
const int N = 2005;
int dp[N][N];
int f(int n, int len) {
if (len < 2 or n < 0) return 0;
if (n == 0) return 1;
int &ans = dp[n][len];
if (~ans) return ans;
ans = f(n - 1, len);
ans = add(ans, f(n, len - 1));
ans = add(ans, f(n, len - 1));
ans = add(ans, mod - f(n, len - 2));
return ans;
}
int pref[N][N];
int pref2[N][N];
int main() {
memset(dp, -1, sizeof dp);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
pref[i][j] = f(i, j);
if (i > 0) pref[i][j] = add(pref[i][j], pref[i - 1][j]);
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == 0) {
pref2[i][j] = f(i, j);
} else {
pref2[i][j] = pref2[i - 1][j];
pref2[i][j] = add(pref2[i][j], f(i, j - 1));
pref2[i][j] = add(pref2[i][j], f(i, j - 1));
pref2[i][j] = add(pref2[i][j], mod - f(i, j - 2));
}
}
}
int n, m;
scanf("%d %d", &n, &m);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int len = 2; len <= m; len++) {
int tmp = mul(pref2[n - i][len], pref[i - 1][len]);
tmp = mul(tmp, m - len + 1);
ans = add(ans, tmp);
}
}
printf("%d\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long a[100001];
long long b[100001];
int main() {
int n, i;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
if (a[n - 1] < a[n - 2] + a[n - 3]) {
cout << "YES\n";
swap(a[n - 1], a[n - 2]);
for (i = 0; i < n; i++) cout << a[i] << " ";
} else
cout << "NO\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int infinito = 1000000000;
int n;
int f[1002];
int m[1002][101][2];
int ej[1002][101][2];
list<char> sol;
vector<list<char>::iterator> genera(int i, int j, int k) {
vector<list<char>::iterator> vi;
if (i == n - 1) {
for (int paso = 0; paso < f[i]; paso++) {
sol.push_back('A');
sol.push_back('R');
sol.push_back('A');
sol.push_back('L');
sol.push_back('A');
vi.push_back(--sol.end());
}
return vi;
}
int i1 = i + 1;
int j1 = ej[i][j][k];
int k1 = 0;
if (k == 1 and m[i1][j1][1] < m[i1][j1][0]) k1 = 1;
vector<list<char>::iterator> viaux = genera(i1, j1, k1);
list<char>::iterator it;
it = sol.begin();
for (; j > j1; j--) {
sol.insert(it, 'A');
sol.insert(it, 'R');
sol.insert(it, 'A');
sol.insert(it, 'L');
sol.insert(it, 'A');
it--;
vi.push_back(it);
it++;
}
sol.insert(it, 'A');
sol.insert(it, 'R');
for (int paso = j1 - j; paso < j1; paso++) {
it = viaux[paso];
vi.push_back(it);
sol.insert(it, 'L');
if (paso < j1 - 1 or k == 1) {
it++;
sol.insert(it, 'A');
sol.insert(it, 'R');
}
}
return vi;
}
int main() {
cin >> n;
int tope = 0;
int maximof = 0;
for (int i = 0; i < n; i++) {
cin >> f[i];
tope = max(tope, f[i]);
if (f[i] > 0) maximof = i;
}
tope++;
n = maximof + 1;
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
for (int j = 0; j < tope; j++) {
if (j != f[i])
m[i][j][0] = m[i][j][1] = infinito;
else {
m[i][j][0] = 5 * f[i];
m[i][j][1] = infinito;
}
}
} else {
for (int j = 0; j < tope; j++) {
m[i][j][0] = m[i][j][1] = infinito;
if (j >= f[i]) {
int i1 = i + 1;
for (int j1 = 0; j1 < tope; j1++) {
int n0 = 0, n1 = 0;
int jj = j;
if (jj > j1) {
n0 += 5 * (jj - j1);
n1 += 5 * (jj - j1);
jj = j1;
}
if (jj == 0)
n0 = infinito;
else
n0 += m[i1][j1][0] + 3 * jj;
n1 += min(m[i1][j1][0], m[i1][j1][1]) + 3 * jj + 2;
if (n0 <= m[i][j][0]) {
m[i][j][0] = n0;
ej[i][j][0] = j1;
}
if (n1 <= m[i][j][1]) {
m[i][j][1] = n1;
ej[i][j][1] = j1;
}
}
}
}
}
}
{
int jmin, kmin;
int minimo = infinito;
for (int j = 0; j < tope; j++) {
if (minimo > m[0][j][0]) {
minimo = m[0][j][0];
jmin = j;
kmin = 0;
}
if (minimo > m[0][j][1]) {
minimo = m[0][j][1];
jmin = j;
kmin = 1;
}
}
genera(0, jmin, kmin);
for (list<char>::iterator it = sol.begin(); it != sol.end(); it++)
cout << *it;
cout << endl;
}
}
| 5 |
#include <iostream>
#include <vector>
using namespace std;
long long ope_cnt(vector<long long> a, int n, bool plus) {
long long sum = 0, ret = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (plus && sum <= 0) {
ret += 1 - sum;
sum = 1;
}
else if (!plus && sum >= 0) {
ret += 1 + sum;
sum = -1;
}
plus = !plus;
}
return ret;
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
long long ans_p = ope_cnt(a, n, true);
long long ans_m = ope_cnt(a, n, false);
cout << min(ans_p, ans_m) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
string s1, s2, s3;
s1 = s;
s2 = s;
s3 = s;
for (int i = 0; i < s.size(); ++i) {
s1[i] = 'a' + (i / (26 * 26));
s2[i] = 'a' + (i / 26) % 26;
s3[i] = 'a' + i % 26;
}
cout << "? " << s1 << endl;
cout.flush();
cin >> s1;
cout << "? " << s2 << endl;
cout.flush();
cin >> s2;
cout << "? " << s3 << endl;
cout.flush();
cin >> s3;
string ans = s;
for (int i = 0; i < s.size(); ++i) {
int b = (s1[i] - 'a') * (26 * 26) + (s2[i] - 'a') * (26) + (s3[i] - 'a');
ans[b] = s[i];
}
cout << "! " << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void nextPlease() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
int di[] = {1, 1, -1, -1, 2, 2, -2, -2};
int dj[] = {2, -2, 2, -2, 1, -1, 1, -1};
long long n, m, cx, cy;
bool valid(long long x, long long y) {
return (x > 0 && x <= n && y > 0 && y <= m);
}
int main() {
nextPlease();
cin >> n >> m;
cin >> cx >> cy;
int k;
cin >> k;
int idx = k;
vector<long long> steps(k);
while (k--) {
long long dx, dy;
cin >> dx >> dy;
long long l = 0, r = max(n, m);
while (l <= r) {
long long mid = l + (r - l) / 2;
if (valid(cx + dx * mid, cy + dy * mid)) {
l = mid + 1;
steps[idx - k - 1] = mid;
} else
r = mid - 1;
}
cx = cx + dx * steps[idx - k - 1];
cy = cy + dy * steps[idx - k - 1];
}
long long res = 0;
for (int i = 0; i < steps.size(); ++i) {
res += steps[i];
}
cout << res;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int l[100000], r[100000];
int main() {
long long n, sum = 0;
memset(l, 0, sizeof(l));
memset(r, 0, sizeof(r));
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
sort(l, l + n);
sort(r, r + n);
for (int i = 0; i < n; i++) {
sum += max(l[i], r[i]);
}
sum += n;
cout << sum << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
size_t push(const std::vector<size_t>& v, size_t id, size_t val) {
while (id < v.size() && v[id] < val) {
id += 1;
}
return id;
}
size_t solve(const std::vector<size_t>& m, const std::vector<size_t>& l,
const std::vector<size_t>& r) {
size_t l_ptr = push(l, 0, m[0]);
size_t r_ptr = push(r, 0, m[0]);
size_t result = 0;
for (size_t m_ptr = 1; m_ptr < m.size(); ++m_ptr) {
const size_t l_next = push(l, l_ptr, m[m_ptr]);
const size_t r_next = push(r, r_ptr, m[m_ptr]);
if (l_next - l_ptr != r_next - r_ptr) {
result += 1;
}
l_ptr = l_next;
r_ptr = r_next;
}
return result;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t n;
std::cin >> n;
std::vector<std::vector<size_t>> ps(n);
for (size_t i = 0; i < n; ++i) {
size_t k;
std::cin >> k;
ps[i].resize(k);
for (size_t j = 0; j < k; ++j) {
std::cin >> ps[i][j];
}
sort(ps[i].begin(), ps[i].end());
}
size_t result = 0;
for (size_t i = 0; i < n; ++i) {
result += solve(ps[i], ps[(i + 1) % n], ps[(i + n - 1) % n]);
}
std::cout << result << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(2007);
struct DD {
struct Node {
long long cnt, x, y, sum;
Node *l, *r;
Node(long long x_) {
x = sum = x_;
cnt = 1;
y = rnd();
l = r = NULL;
}
} * root;
void print_tree(Node *t) {
if (!t) return;
cout << t->x << ", ";
if (t->l)
cout << "l : " << t->l->x << ", ";
else
cout << "l : NULL, ";
if (t->r)
cout << "r : " << t->r->x;
else
cout << "r : NULL";
cout << '\n';
print_tree(t->l);
print_tree(t->r);
}
void print_list(Node *t) {
if (!t) return;
print_list(t->l);
cout << t->x << ' ';
print_list(t->r);
}
long long sum(Node *t) {
if (!t)
return 0;
else
return t->sum;
}
long long cnt(Node *t) {
if (!t)
return 0;
else
return t->cnt;
}
void relax(Node *t) {
t->sum = sum(t->l) + sum(t->r) + t->x;
t->cnt = cnt(t->l) + cnt(t->r) + 1;
}
Node *merge(Node *l, Node *r) {
if (!l) return r;
if (!r) return l;
if (l->y < r->y) {
l->r = merge(l->r, r);
relax(l);
return l;
} else {
r->l = merge(l, r->l);
relax(r);
return r;
}
}
pair<Node *, Node *> split(Node *t, long long x) {
if (!t) return {NULL, NULL};
if (t->x < x) {
auto tmp = split(t->r, x);
t->r = tmp.first;
relax(t);
return make_pair(t, tmp.second);
} else {
auto tmp = split(t->l, x);
t->l = tmp.second;
relax(t);
return make_pair(tmp.first, t);
}
}
pair<Node *, Node *> splitcnt(Node *t, long long k) {
if (!t) return {NULL, NULL};
if (k <= cnt(t->l)) {
auto tmp = splitcnt(t->l, k);
t->l = tmp.second;
relax(t);
return make_pair(tmp.first, t);
} else {
auto tmp = splitcnt(t->r, k - cnt(t->l) - 1);
t->r = tmp.first;
relax(t);
return make_pair(t, tmp.second);
}
}
void init() { root = NULL; }
void add(long long x) {
auto t = split(root, x);
root = merge(merge(t.first, new Node(x)), t.second);
}
void del(long long x) {
auto t1 = split(root, x);
auto t2 = split(t1.second, x + 1);
root = merge(merge(t1.first, merge(t2.first->l, t2.first->r)), t2.second);
}
long long sum_less(long long x) {
auto t = split(root, x);
long long ans = sum(t.first);
root = merge(t.first, t.second);
return ans;
}
long long sum_pref(long long k) {
auto t = splitcnt(root, k);
long long ans = sum(t.first);
root = merge(t.first, t.second);
return ans;
}
long long cnt() { return cnt(root); }
void print_tree() {
print_tree(root);
cout << '\n';
}
void print_list() {
print_list(root);
cout << '\n';
}
};
const long long N = 3e5, INF = 1e18;
vector<pair<long long, long long> > g[N], cur[N];
long long par[N], up[N];
DD a[N];
void dfs(long long u, long long p) {
par[u] = p;
for (auto e : g[u])
if (e.first != p) {
up[e.first] = e.second;
a[u].add(e.second);
dfs(e.first, u);
}
}
vector<long long> v[N];
bool used[N];
long long dp[N][2];
void solve(long long u, long long x) {
vector<long long> c;
long long sum1 = 0;
for (auto e : cur[u]) {
solve(e.first, x);
c.push_back(dp[e.first][0] + e.second - dp[e.first][1]);
sum1 += dp[e.first][1];
}
long long w = (long long)g[u].size() - x - 1;
sort(c.begin(), c.end());
long long sum = 0;
dp[u][0] = dp[u][1] = INF;
for (long long i = 0; i <= c.size(); ++i) {
for (long long j = 0; j < 2; ++j)
if (w - i + j <= a[u].cnt()) {
dp[u][j] =
min(dp[u][j], sum1 + sum + a[u].sum_pref(max(0ll, w - i + j)));
}
if (i < c.size()) sum += c[i];
}
}
long long print[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (long long i = 0; i < N; ++i) a[i].init();
long long n;
cin >> n;
for (long long i = 0; i < n - 1; ++i) {
long long u, v, c;
cin >> u >> v >> c;
g[u].push_back(make_pair(v, c));
g[v].push_back(make_pair(u, c));
}
dfs(1, 0);
for (long long i = 1; i <= n; ++i) v[g[i].size()].push_back(i);
vector<long long> in;
for (long long x = n - 1; x >= 0; --x) {
for (long long u : v[x + 1]) {
if (par[u]) a[par[u]].del(up[u]);
in.push_back(u);
used[u] = 1;
}
for (long long u : in) cur[u].clear();
for (long long u : in)
if (used[par[u]]) {
cur[par[u]].push_back(make_pair(u, up[u]));
}
for (long long u : in)
if (!used[par[u]]) {
solve(u, x);
long long add = 0;
if (u == 1)
add = dp[u][1];
else
add = min(dp[u][1], dp[u][0] + up[u]);
print[x] += add;
}
}
for (long long i = 0; i < n; ++i) cout << print[i] << ' ';
cout << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
if (n <= 3) {
cout << "NO" << endl;
return 0;
}
if (n == 4) {
cout << "YES" << endl;
printf("1 * 2 = 2\n");
printf("2 * 3 = 6\n");
printf("6 * 4 = 24\n");
return 0;
}
if (n == 5) {
cout << "YES" << endl;
printf("2 - 1 = 1\n");
printf("4 - 1 = 3\n");
printf("3 + 5 = 8\n");
printf("3 * 8 = 24\n");
return 0;
}
if ((n - 4) % 2 == 0) {
cout << "YES" << endl;
for (int i = 5; i <= n; i += 2)
cout << i + 1 << " - " << i << " = 1" << endl;
printf("1 * 2 = 2\n");
printf("2 * 3 = 6\n");
printf("6 * 4 = 24\n");
for (int i = 5; i <= n; i += 2) cout << "1 * 24 = 24" << endl;
return 0;
}
if ((n - 5) % 2 == 0) {
cout << "YES" << endl;
for (int i = 6; i <= n; i += 2)
cout << i + 1 << " - " << i << " = 1" << endl;
printf("2 - 1 = 1\n");
printf("4 - 1 = 3\n");
printf("3 + 5 = 8\n");
printf("3 * 8 = 24\n");
for (int i = 6; i <= n; i += 2) cout << "1 * 24 = 24" << endl;
return 0;
}
return 0;
}
| 3 |
#include <cstdio>
#include <vector>
#include <algorithm>
int main() {
int N, C;
scanf("%d %d", &N, &C);
std::vector<int> prog(100010);
for (int i=0; i<N; ++i) {
int s, t, c;
scanf("%d %d %d", &s, &t, &c);
if (prog[s]>>c & 1) {
prog[s] ^= 1 << c;
} else {
prog[s-1] ^= 1 << c;
}
if (prog[t-1]>>c & 1) {
prog[t-1] ^= 1 << c;
} else {
prog[t] ^= 1 << c;
}
}
for (int i=1; i<=100000; ++i)
prog[i] ^= prog[i-1];
int res=0;
for (int i=0; i<=100000; ++i)
res = std::max(res, __builtin_popcount(prog[i]));
printf("%d\n", res);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long check(int cur_op, vector<char> ops, vector<long long> nums) {
if (nums.size() == 1) {
return nums[0];
}
long long cur_res = 0, min_res = -1, number;
vector<long long> new_nums;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i + 1; j < nums.size(); ++j) {
if (ops[cur_op] == '+') {
number = nums[i] + nums[j];
} else {
number = nums[i] * nums[j];
}
new_nums.clear();
new_nums.push_back(number);
for (int k = 0; k < nums.size(); ++k) {
if (k != i && k != j) new_nums.push_back(nums[k]);
}
cur_res = check(cur_op + 1, ops, new_nums);
if (min_res == -1 || min_res > cur_res) min_res = cur_res;
}
}
return min_res;
}
int main() {
vector<long long> nums;
nums.assign(4, 0);
vector<char> ops;
ops.assign(3, ' ');
cin >> nums[0] >> nums[1] >> nums[2] >> nums[3];
cin >> ops[0] >> ops[1] >> ops[2];
cout << check(0, ops, nums);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using std::make_pair;
using std::pair;
const int N = 1000 + 10;
std::vector<std::tuple<int, int, int>> edges;
std::vector<int> neighbors[N];
int weights[N], size[N], father[N], n;
namespace union_set {
int ancestor[N], size[N];
void init(int n) {
for (int i = 1; i <= n; ++i) {
ancestor[i] = i;
size[i] = 1;
}
}
int get_ancestor(int u) {
return ancestor[u] == u ? u : ancestor[u] = get_ancestor(ancestor[u]);
}
void merge(int u, int v) {
u = get_ancestor(u);
v = get_ancestor(v);
if (size[u] < size[v]) {
std::swap(u, v);
}
size[u] += size[v];
ancestor[v] = u;
}
} // namespace union_set
void dfs(int u, int f) {
size[u] = 1;
father[u] = f;
for (auto v : neighbors[u]) {
if (v == f) continue;
dfs(v, u);
size[u] += size[v];
}
}
void paint(int u, int f, int set_root, int coeff) {
int s = 0;
for (auto v : neighbors[u]) {
if (v == f) continue;
if (set_root == -1 || union_set::get_ancestor(v) == set_root) {
edges.push_back(std::make_tuple(u, v, (s + 1) * coeff));
paint(v, u, -1, coeff);
s += size[v];
}
}
}
void run(int case_id = 0) {
std::cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
std::cin >> u >> v;
neighbors[u].push_back(v);
neighbors[v].push_back(u);
}
if (n == 1) return;
if (n == 2) {
std::cout << "1 2 1" << std::endl;
return;
}
dfs(1, -1);
int root = 0, max_size = 0;
for (int u = 1; u <= n; ++u) {
int temp = n - size[u];
for (auto v : neighbors[u]) {
if (v == father[u]) continue;
if ((temp) < (size[v])) (temp) = (size[v]);
}
if (root == 0 || temp < max_size) {
root = u;
max_size = temp;
}
}
dfs(root, -1);
union_set::init(n);
std::priority_queue<pair<int, int>, std::vector<pair<int, int>>,
std::greater<pair<int, int>>>
heap;
for (auto v : neighbors[root]) {
heap.push(make_pair(size[v], v));
}
while (heap.size() > 2) {
auto p = heap.top();
heap.pop();
auto q = heap.top();
heap.pop();
union_set::merge(p.second, q.second);
heap.push(make_pair(p.first + q.first, p.second));
}
int root_u = union_set::get_ancestor(heap.top().second);
heap.pop();
int root_v = union_set::get_ancestor(heap.top().second);
heap.pop();
int size_u = 0;
for (auto v : neighbors[root]) {
if (union_set::get_ancestor(v) == root_u) {
size_u += size[v];
}
}
paint(root, -1, root_u, 1);
paint(root, -1, root_v, 1 + size_u);
for (auto e : edges) {
int u = std::get<0>(e);
int v = std::get<1>(e);
int w = std::get<2>(e);
std::cout << u << " " << v << " " << w << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
run();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-12;
const int N = 32;
bool zero(double x) { return fabs(x) < eps; }
int sgn(double x) {
if (x < -eps) return -1;
if (x > eps) return 1;
return 0;
}
struct Point {
double x, y;
Point(double _x = 0, double _y = 0) { x = _x, y = _y; }
void output() { printf("%f %f\n", x, y); }
} top[512][2], bot[512][2], p[32];
struct Segment {
Point s, e;
double angle;
void get_angle() { angle = atan2(e.y - s.y, e.x - s.x); }
};
int m;
double h, f;
double xmul(Point p0, Point p1, Point p2) {
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
Point Get_Intersect(Segment s1, Segment s2) {
double u = xmul(s1.s, s1.e, s2.s), v = xmul(s1.e, s1.s, s2.e);
Point t;
t.x = (s2.s.x * v + s2.e.x * u) / (u + v);
t.y = (s2.s.y * v + s2.e.y * u) / (u + v);
return t;
}
bool cmp(Segment s1, Segment s2) {
if (s1.angle > s2.angle)
return true;
else if (zero(s1.angle - s2.angle) && xmul(s2.s, s2.e, s1.e) > -eps)
return true;
return false;
}
void HalfPlaneIntersect(Segment seg[], int n) {
sort(seg, seg + n, cmp);
int tmp = 1;
for (int i = 1; i < n; i++)
if (!zero(seg[i].angle - seg[tmp - 1].angle)) seg[tmp++] = seg[i];
n = tmp;
Segment deq[N];
deq[0] = seg[0];
deq[1] = seg[1];
int head = 0, tail = 1;
for (int i = 2; i < n; i++) {
while (head < tail && xmul(seg[i].s, seg[i].e,
Get_Intersect(deq[tail], deq[tail - 1])) < -eps)
tail--;
while (head < tail && xmul(seg[i].s, seg[i].e,
Get_Intersect(deq[head], deq[head + 1])) < -eps)
head++;
deq[++tail] = seg[i];
}
while (head < tail && xmul(deq[head].s, deq[head].e,
Get_Intersect(deq[tail], deq[tail - 1])) < -eps)
tail--;
while (head < tail && xmul(deq[tail].s, deq[tail].e,
Get_Intersect(deq[head], deq[head + 1])) < -eps)
head++;
if (head == tail) return;
for (int i = head; i < tail; i++) p[m++] = Get_Intersect(deq[i], deq[i + 1]);
if (tail > head + 1) p[m++] = Get_Intersect(deq[head], deq[tail]);
}
double Get_area(Point p[], int &n) {
double area = 0;
for (int i = 1; i < n - 1; i++) area += xmul(p[0], p[i], p[i + 1]);
return fabs(area) / 2.0;
}
double solve(int i, int j) {
int n = 0;
Segment seg[N];
seg[n].s = Point(0, f);
seg[n].e = top[i][0];
seg[n++].get_angle();
seg[n].s = top[i][1], h, seg[n].e = Point(0, f);
seg[n++].get_angle();
seg[n].s = Point(0, -f);
seg[n].e = bot[j][1];
seg[n++].get_angle();
seg[n].s = bot[j][0], seg[n].e = Point(0, -f);
seg[n++].get_angle();
seg[n].s = Point(0, h), seg[n].e = Point(-1000, h);
seg[n++].get_angle();
seg[n].s = Point(-1000, 0), seg[n].e = Point(0, 0);
seg[n++].get_angle();
m = 0;
HalfPlaneIntersect(seg, n);
return Get_area(p, m);
}
int main() {
int n;
while (scanf("%d%lf%lf", &n, &h, &f) != EOF) {
double ans = 0;
for (int i = 0; i < n; i++) {
double tl, tr;
scanf("%lf%lf", &tl, &tr);
top[i][0] = Point(tl, h);
top[i][1] = Point(tr, h);
bot[i][0] = Point(tl, -h);
bot[i][1] = Point(tr, -h);
ans += ((tr - tl) + (tr - tl) * (f + h) / (f - h)) * h;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
ans -= solve(i, j);
}
printf("%.10f\n", ans * 2);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000003;
long long n, k;
int pow_mod(int x, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = 1LL * ans * x % MOD;
x = 1LL * x * x % MOD;
k >>= 1;
}
return ans;
}
int zi, mu;
long long a = 0;
int main() {
scanf("%lld%lld", &n, &k);
if (n <= 60) {
long long ans = 1;
for (int i = 0; i < n; i++) ans *= 2;
if (k > ans) {
printf("1 1\n");
return 0;
}
}
int tot = 1;
int tmp = pow_mod(2, n % (MOD - 1));
for (long long i = 1; i < k; i++) {
long long cnt = 1, ii = i;
while (ii % 2 == 0) {
cnt *= 2;
ii /= 2;
}
int aa = 1LL * (tmp - i + MOD) % MOD * pow_mod(cnt % MOD, MOD - 2) % MOD;
tot = 1LL * tot * aa % MOD;
if (tot == 0) break;
}
long long cnt = 0;
long long kk = k - 1;
while (kk) {
cnt += kk / 2;
kk /= 2;
}
n %= (MOD - 1);
k--;
k %= (MOD - 1);
cnt %= (MOD - 1);
int mi = (1LL * n * k % (MOD - 1) - cnt + MOD - 1) % (MOD - 1);
int mu = pow_mod(2, mi);
printf("%d %d\n", (mu - tot + MOD) % MOD, mu);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 7;
const int Q = 1e9 + 7;
inline int pls(int x, int y) { return ((long long)x + y) % Q; }
inline int mns(int x, int y) { return pls(x, Q - y); }
inline int mul(int x, int y) { return 1LL * x * y % Q; }
inline int ri() {
int x = 0;
bool f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = 0;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
return f ? x : -x;
}
int n, L, R;
char str[M];
int d[M], s[M];
int pw[M];
int fac[M], ifac[M];
int inv[M];
int f[M];
inline int binom(int x, int y) {
if (x < y) return 0;
return mul(fac[x], mul(ifac[y], ifac[x - y]));
}
int calc(int m) {
if (m < 0) return 0;
int i, res = 0;
for (f[0] = 1, i = 1; i <= n; i++)
f[i] = mns(mul(2, f[i - 1]), binom(i - 1, m));
for (i = 1; i <= n; i++) res = pls(res, mul(mul(d[i], pw[n - i]), f[i - 1]));
for (i = 0; i <= n - 2; i++)
res = pls(res, mul(mul(s[n - i - 1], pw[i]),
mns(f[n - i - 2], binom(n - i - 2, m))));
return res;
}
void init() {
int i;
for (i = 1; i <= n; i++) d[i] = str[i] - '0';
for (s[0] = 0, i = 1; i <= n; i++) s[i] = pls(d[i], s[i - 1]);
for (pw[0] = 1, i = 1; i <= n; i++) pw[i] = mul(pw[i - 1], 10);
for (inv[1] = 1, i = 2; i <= n; i++) inv[i] = mul(inv[Q % i], Q - Q / i);
for (fac[0] = 1, i = 1; i <= n; i++) fac[i] = mul(fac[i - 1], i);
for (ifac[0] = 1, i = 1; i <= n; i++) ifac[i] = mul(ifac[i - 1], inv[i]);
}
int main() {
int i;
n = ri();
L = ri();
R = L;
scanf("%s", str + 1);
init();
printf("%d\n", mns(calc(R), calc(L - 1)));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int Data[400100];
int T, N;
int main() {
scanf("%d", &T);
while (T--) {
int G = 1, S = 0, B = 0;
scanf("%d", &N);
for (register int i = 1; i <= N; ++i) scanf("%d", &Data[i]);
if (N < 10) {
printf("0 0 0\n");
continue;
}
int pos = 2;
while (pos <= N / 2 && Data[pos] == Data[pos - 1]) {
++G;
++pos;
}
if (Data[pos] == Data[pos - 1]) {
printf("0 0 0\n");
continue;
}
while (pos <= N / 2 && S <= G) {
++S;
++pos;
}
while (pos <= N / 2 && Data[pos] == Data[pos - 1]) {
++S;
++pos;
}
if (S <= G) {
printf("0 0 0\n");
continue;
}
while (pos <= N / 2 && B <= G) {
++B;
++pos;
}
while (pos <= N / 2 && Data[pos] != Data[N / 2 + 1]) {
++B;
++pos;
}
if (B <= G) {
printf("0 0 0\n");
continue;
}
if (G + S + B > N / 2) {
printf("0 0 0\n");
continue;
}
if (Data[G] == Data[G + 1]) {
printf("0 0 0\n");
continue;
}
if (Data[G + S] == Data[G + S + 1]) {
printf("0 0 0\n");
continue;
}
if (Data[G + S + B] == Data[G + S + B + 1]) {
printf("0 0 0\n");
continue;
}
if (G == 0 || S == 0 || B == 0) {
printf("0 0 0\n");
continue;
}
printf("%d %d %d\n", G, S, B);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > g, G;
int dp[5000][2501][2];
int in[5000];
int last[5000], u[5000], v[5000], nxt[5000], e;
void addEdge(int a, int b) {
u[e] = a;
v[e] = b;
nxt[e] = last[a];
last[a] = e;
++e;
}
int DFS(int u, int p) {
int ret = 0, me = 1;
for (int i = 0; i < g[u].size(); ++i)
if (g[u][i] != p) {
addEdge(u, g[u][i]);
ret += DFS(g[u][i], u);
me = 0;
}
return in[u] = ret + me;
}
int calc(int e, int rem, int prv, int suffix) {
if (e == -1) {
if (rem && !prv || !rem && prv) return 100;
return 0;
}
int &ret = dp[e][rem][prv];
if (ret != -1) return ret;
ret = 10000;
suffix -= in[v[e]];
for (int take = max(0, rem - suffix); take <= rem && take <= in[v[e]];
++take) {
int ret1 = calc(last[v[e]], take, 1, in[v[e]]) + (prv == 0);
if (nxt[e] != -1) ret1 += calc(nxt[e], rem - take, prv, suffix);
int ret2 = calc(last[v[e]], take, 0, in[v[e]]) + (prv == 1);
if (nxt[e] != -1) ret2 += calc(nxt[e], rem - take, prv, suffix);
ret = min(ret, ret1);
ret = min(ret, ret2);
}
return ret;
}
int main() {
scanf("%d", &n);
g.resize(n);
if (n == 2) {
puts("1");
return 0;
}
for (int i = 1, a, b; i < n; ++i) {
scanf("%d%d", &a, &b);
--a;
--b;
g[a].push_back(b);
g[b].push_back(a);
}
int leaf = 0, root;
for (int i = 0; i < n; ++i)
if (g[i].size() == 1)
++leaf;
else
root = i;
assert(leaf % 2 == 0);
memset(last, -1, sizeof(last));
DFS(root, -1);
memset(dp, -1, sizeof(dp));
printf("%d\n", min(calc(last[root], leaf / 2, 0, in[root]),
calc(last[root], leaf / 2, 1, in[root])));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long llinf = (long long)9e18;
const int N = (int)1e7 + 111;
const long double PI = (long double)acos(-1);
int main() {
long long ans = 0;
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
ans ^= (long long)x;
}
vector<long long> first(n + 1);
first[0] = 0;
for (int i = 1; i <= n; i++) first[i] = first[i - 1] ^ (long long)i;
for (int i = 2; i <= n; i++) ans ^= (n / i) % 2 * first[i - 1] ^ first[n % i];
printf("%lld", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m;
b /= 2;
a = ((a % m) * (a % m)) % m;
}
return ans % m;
}
long long a[505][505];
long long b[505];
long long ans[505];
bool vis[505];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++) cin >> a[i][j];
for (long long i = 1; i <= n; i++) cin >> b[i];
for (long long k = n; k >= 1; k--) {
long long node = b[k];
vis[node] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
if (i != node && j != node && i != j)
a[i][j] = min(a[i][j], a[i][node] + a[node][j]);
}
}
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++) {
if (vis[i] && vis[j]) ans[k] += a[i][j];
}
}
for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int res = 0;
int dp[n];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[j] == '.') continue;
if (j < n && s[j] == 'R') {
if (s[j + 1] == '.') dp[j + 1]++;
}
if (j > 0 && s[j] == 'L') {
if (s[j - 1] == '.') dp[j - 1]--;
}
}
for (int j = 0; j < n; j++) {
if (dp[j] > 0) s[j] = 'R';
if (dp[j] < 0) s[j] = 'L';
}
}
for (int j = 0; j < n; j++) {
if (!dp[j] && s[j] == '.') res++;
}
cout << res << endl;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
long long n,x[111111],p[111111],y[111111],siz[111111],cir[111111],m,k,sum;
bool vis[111111];
int main(){
scanf("%lld",&n);
for(int i=1;i<=n;i++)scanf("%lld",&x[i]),p[i]=i,y[i]=x[i]-x[i-1];
scanf("%lld%lld",&m,&k);
for(int i=1,a;i<=m;i++)scanf("%lld",&a),swap(p[a],p[a+1]);
for(int i=1,c,pos,cnt=0;i<=n;i++)if(!vis[i]){
++cnt,c=0,pos=i;
while(!vis[pos])vis[pos]=1,cir[pos]=cnt,++c,pos=p[pos];
siz[cnt]=c;
}
for(int i=1;i<=n;i++){
int t=k%siz[cir[i]],pos=i;
while(t--)pos=p[pos];
printf("%lld.0\n",y[pos]+sum),sum+=y[pos];
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long cnt = 0;
long long sum = 0;
sort(a, a + n);
for (long long i = 0; i < n; i++) {
if (sum <= a[i]) {
cnt++;
sum += a[i];
}
}
cout << cnt << endl;
return 0;
}
| 4 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
/* do your best */
int main() {
int n; cin >> n;
vector<int> a(n);
vector<int> cnt(3, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i] % 3]++;
}
if (cnt[0] == n) {
cout << 1 << endl;
return 0;
}
lint ans = cnt[0];
if (cnt[1] > cnt[2]) {
swap(cnt[1], cnt[2]);
}
ans += cnt[1] + min(cnt[1] + 3, cnt[2]);
cout << ans << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int x;
cin>>x;
int now=0;
for(int i=0;i<x;i++){
now*=10;
now+=7;
now=now%x;
if(now==0){
cout<<i+1<<endl;
return 0;
}
}cout<<-1<<endl;
}
| 0 |
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int a[6] = {0};
while(n--) {
double h;
cin >> h;
a[max(0, min(5, ((int)(h*10)-1600)/50))] += 1;
}
for(int i = 0; i < 6; ++i) {
cout << i+1 << ":" << string(a[i], '*') << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n], ans[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
int l = 0, r = n - 1;
for (int i = n - 1; i >= 0; i--) {
if (i % 2 == 0) {
ans[i] = arr[l];
l++;
} else {
ans[i] = arr[r];
r--;
}
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << endl;
}
}
| 2 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[30];
signed main() {
string w;
cin>>w;
for(int i=0;i<w.size();i++){
a[w[i]-'a']++;
}
for(int i=0;i<26;i++){
if(a[i]%2==1){
cout<<"No"<<endl;
return 0;
}
}
cout<<"Yes"<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
inline int read() {
int weight = 0, symbol = 1;
char in = getchar();
while (!isdigit(in)) {
if (in == '-') symbol = -1;
in = getchar();
}
while (isdigit(in)) {
weight = weight * 10 + in - '0';
in = getchar();
}
return symbol * weight;
}
inline void write(int x) {
int s[20], len = 0;
if (x < 0) putchar('-'), x = -x;
do s[++len] = x % 10, x /= 10;
while (x);
while (len) putchar('0' | s[len--]);
putchar(32);
}
void Max(int &x, int y) { x = (y > x) ? y : x; }
int T, n;
int a[MAXN];
int Mx;
vector<int> pos[MAXN];
int Ans[MAXN];
void clearify() {
for (int i = 1; i <= Mx; i++) pos[i].clear();
for (int i = 1; i <= n; i++) Ans[i] = 0;
Mx = 0;
}
signed main() {
T = read();
while (T--) {
n = read();
for (int i = 1; i <= n; i++) {
a[i] = read(), pos[a[i]].push_back(i);
Max(Mx, a[i]);
}
int pin = n + 1;
for (int i = 1; i <= Mx; i++) {
if (!pos[i].size()) continue;
int res = 0, sz = pos[i].size();
Max(res, pos[i][0]);
for (int j = 1; j < sz; j++) Max(res, pos[i][j] - pos[i][j - 1]);
Max(res, n + 1 - pos[i][sz - 1]);
while (pin > res) Ans[--pin] = i;
}
for (int i = 1; i <= n; i++) write(Ans[i] ? Ans[i] : -1);
putchar(10);
clearify();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int mins[3];
int big = 2000000000;
mins[0] = big;
mins[1] = big;
mins[2] = big;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == mins[0] || a[i] == mins[1]) continue;
if (a[i] < mins[0]) {
mins[2] = mins[1];
mins[1] = mins[0];
mins[0] = a[i];
} else if (a[i] < mins[1]) {
mins[2] = mins[1];
mins[1] = a[i];
} else if (a[i] < mins[2]) {
mins[2] = a[i];
}
}
long long n1 = 0, n2 = 0, n3 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == mins[0]) n1++;
if (a[i] == mins[1]) n2++;
if (a[i] == mins[2]) n3++;
}
long long ans = 0;
if (n1 >= 3) {
ans = n1 * (n1 - 1) * (n1 - 2) / 6;
} else if (n1 + n2 >= 3) {
if (n1 == 2) {
ans = n2;
} else
ans = n2 * (n2 - 1) / 2;
} else
ans = n1 * n2 * n3;
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 65537;
int degree[N];
int xorsum[N];
int n;
queue<int> q;
int ans = 0;
queue<pair<int, int> > edges;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d %d", °ree[i], &xorsum[i]);
if (degree[i] == 1) {
q.push(i);
}
}
while (!q.empty()) {
int node = q.front();
q.pop();
if (!degree[node]) {
continue;
}
int next = xorsum[node];
xorsum[node] = 0;
edges.push(make_pair(node, next));
ans++;
xorsum[next] ^= node;
degree[next]--;
if (degree[next] == 1) {
q.push(next);
}
}
printf("%d\n", ans);
while (!edges.empty()) {
printf("%d %d\n", edges.front().first, edges.front().second);
edges.pop();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class Read>
void in(Read &x) {
x = 0;
int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
f |= (ch == '-');
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = f ? -x : x;
return;
}
int n, m, top;
long long f[1 << 20][25], d[25][25], a[1 << 20], k[1 << 20], ans;
inline int lowbit(int x) { return x & -x; }
int main() {
in(n);
in(m);
for (int i = 1; i <= m; i++) {
int x, y;
in(x);
in(y);
d[x][y] = d[y][x] = 1;
}
for (int i = 1; i <= n; i++) f[1 << i - 1][i] = 1;
for (int s = 1; s < (1 << n); s++) {
int i = lowbit(s);
for (int j = 1; j <= n; j++) {
if (f[s][j] == 0) continue;
for (int k = 1; k <= n; k++) {
if (!d[j][k]) continue;
if (i > (1 << k - 1)) continue;
if (!(s & (1 << k - 1)))
f[s | (1 << k - 1)][k] = f[s | (1 << k - 1)][k] + f[s][j];
else if (i == (1 << k - 1))
ans += f[s][j];
}
}
}
printf("%lld\n", (ans - m) >> 1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 7;
int n, m;
long long a[MAX_N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
cin >> m;
long long w, h, minh = 0;
for (int i = 1; i <= m; i++) {
cin >> w >> h;
long long hh = max(minh, a[w]);
cout << hh << endl;
minh = hh + h;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e+5 + 50;
long long int a[N], ans, tans[N], fans;
int n;
int nxt[100 * N][3], nt;
void insert(long long int val) {
int i = 0;
vector<int> dig;
for (int j = 0; j < 50; j++, val /= 2) dig.push_back(val % 2);
reverse(dig.begin(), dig.end());
for (auto it : dig) {
if (nxt[i][it] == 0) nxt[i][it] = ++nt;
i = nxt[i][it];
}
nxt[i][2]++;
}
void deletenode(long long int val) {
stack<int> st;
int i = 0;
vector<int> dig;
for (int j = 0; j < 50; j++, val /= 2) dig.push_back(val % 2);
reverse(dig.begin(), dig.end());
for (auto it : dig) {
st.push(i);
i = nxt[i][it];
}
nxt[i][2]--;
while (!st.empty()) {
auto it = st.top();
if (nxt[i][0] == 0 && nxt[i][1] == 0 && nxt[i][2] == 0) {
if (nxt[it][0] == i)
nxt[it][0] = 0;
else
nxt[it][1] = 0;
} else
break;
i = it;
st.pop();
}
}
long long int findmax(long long int val) {
long long int ans = 0, tans = 0;
int i = 0;
vector<int> dig;
for (int j = 0; j < 50; j++, val /= 2) dig.push_back(val % 2);
reverse(dig.begin(), dig.end());
for (auto it : dig) {
ans = ans * 2LL;
if (nxt[i][1 - it] != 0) {
ans++;
i = nxt[i][1 - it];
} else if (nxt[i][it] != 0) {
i = nxt[i][it];
}
}
return ans;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
tans[i] = (tans[i - 1] ^ a[i]);
ans = max(ans, tans[i]);
}
for (int i = 0; i <= n; i++) insert(tans[i]);
long long int fans = 0;
for (int i = n; i > 0; i--) {
fans = (fans ^ a[i]);
deletenode(tans[i]);
ans = max(ans, findmax(fans));
}
cout << ans;
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.