solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <iostream>
using namespace std;
int main(){
int n, m, c, ans, tmp;
int a, b[21];
cin >> n >> m >> c;
for (int i = 0; i < m; ++i) cin >> b[i];
ans = 0;
for (int i = 0; i < n; ++i){
tmp = c;
for (int j = 0; j < m; ++j){
cin >> a;
tmp += a*b[j];
}
if (tmp > 0) ans++;
}
cout << ans;
} | 0 |
#include<bits/stdc++.h>
#define N 200005
using namespace std;
int a[N],i,j,k,l,s,n,m,Last,b[N];
long long ans;
int main() {
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (i=1;i<=n;i++) if (a[i]>=i) return puts("-1"),0;
for (i=1;i<=n;i++) if (a[i]-a[i-1]>=2) return puts("-1"),0;
for (i=n;i;i--) {
if (a[i]==Last-1) Last--;
else Last=a[i],ans+=a[i];
}
printf("%lld\n",ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[2 * 65];
long long cp[2 * 65];
long long power[2 * 65];
long long encode(long long x) { return x + 64; }
long long decode64(long long x) { return x - 64; }
long long convToBinary(long long n) {
string s = "";
long long ans = 0;
while (n) {
if (n % 2 == 1) {
s += "1";
ans++;
} else
s += "0";
n = n / 2;
}
for (long long i = -63; i < 64; i++) a[encode(i)] = 0;
for (long long i = 0; i < s.length(); i++) {
a[encode(i)] = s[i] - '0';
}
return ans;
}
void printAns(long long i) {
cout << "Yes\n";
long long sum = 0, ans = 0;
for (long long j = i; j > -64; j--) {
sum = sum + cp[encode(j)];
if (cp[encode(j)] > 0)
while (cp[encode(j)]--) {
ans = ans + power[j];
printf("%d ", j);
}
}
cout << endl;
cout << ans << " " << sum << endl;
}
void cop() {
for (long long i = -63; i < 64; i++) {
cp[encode(i)] = a[encode(i)];
}
}
int main() {
power[0] = (long long)1;
for (long long i = 1; i < 64; i++) power[i] = (long long)2 * power[i - 1];
long long n, k;
cin >> n >> k;
long long countOf1 = convToBinary(n);
if (countOf1 > k) {
cout << "No\n";
return 0;
}
cout << "Yes\n";
for (int i = 63; i >= -63; i--) {
if (countOf1 + a[encode(i)] <= k) {
countOf1 += a[encode(i)];
a[encode(i - 1)] += 2 * a[encode(i)];
a[encode(i)] = 0;
} else
break;
}
multiset<int> mset;
for (int i = 63; i >= -63; i--) {
if (a[encode(i)] > 0) {
while (a[encode(i)]--) {
mset.insert(i);
}
}
}
for (multiset<int>::const_iterator i(mset.begin()), end(mset.end()); i != end;
++i) {
}
while (countOf1 < k) {
int minm = *mset.begin();
mset.erase(mset.begin());
mset.insert(minm - 1);
mset.insert(minm - 1);
countOf1++;
}
vector<int> v;
for (multiset<int>::const_iterator i(mset.begin()), end(mset.end()); i != end;
++i) {
v.push_back(*i);
}
for (int i = v.size() - 1; i >= 0; i--) cout << v[i] << " ";
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000009;
map<pair<int, int>, int> in, id;
set<int> S;
int X[100010], Y[100010];
bool check(pair<int, int> P) {
int x = P.first, y = P.second + 1;
for (int dx = x - 1; dx <= x + 1; dx++) {
if (in.count(make_pair(dx, y)) && in[make_pair(dx, y)] == 1) return 0;
}
return 1;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
X[i] = x, Y[i] = y;
in[make_pair(x, y)] = 0, id[make_pair(x, y)] = i;
}
for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();
it++) {
int x = it->first.first, y = it->first.second - 1;
for (int dx = x - 1; dx <= x + 1; dx++) {
if (in.count(make_pair(dx, y))) {
in[it->first]++;
}
}
}
for (map<pair<int, int>, int>::iterator it = in.begin(); it != in.end();
it++) {
if (check(it->first)) S.insert(id[it->first]);
}
long long ans = 0;
for (int it = 0; it < n; it++) {
if (it & 1) {
int v = *S.begin();
S.erase(S.begin());
int y = Y[v] + 1;
id.erase(make_pair(X[v], Y[v]));
in[make_pair(X[v], Y[v])] = 0;
for (int x = X[v] - 1; x <= X[v] + 1; x++) {
if (!id.count(make_pair(x, y))) continue;
in[make_pair(x, y)]--;
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
ans = (ans * n + v) % mod;
} else {
int v = *(--S.end());
S.erase(--S.end());
int y = Y[v] + 1;
id.erase(make_pair(X[v], Y[v]));
in[make_pair(X[v], Y[v])] = 0;
for (int x = X[v] - 1; x <= X[v] + 1; x++) {
if (!id.count(make_pair(x, y))) continue;
in[make_pair(x, y)]--;
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
y--;
for (int x = X[v] - 2; x <= X[v] + 2; x++) {
if (!id.count(make_pair(x, y))) continue;
if (check(make_pair(x, y)))
S.insert(id[make_pair(x, y)]);
else
S.erase(id[make_pair(x, y)]);
}
ans = (ans * n + v) % mod;
}
}
cout << ans << endl;
return 0;
}
| 2 |
//#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <bitset>
#include <queue>
#include <tuple>
#include <map>
#include <random>
#include <chrono>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0);
#define sqrt sqrtl
#define pb push_back
typedef long long ll;
typedef long double ld;
const ll SZ = 1e5 + 100, INF = 1e9 + 100, K = 53;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<vector<pair<ll, ll>>> gr;
ll n, m;
ll dist[SZ];
ll d2[SZ][53];
struct vrt {
ll type, v, c;
};
set<tuple<ll, ll, ll, ll>> s;
void djikstra() {
s.insert({ 0, 0, 0, 0 });
for (int i = 1; i < n; i++) {
dist[i] = INF;
s.insert({ dist[i], 0, i, 0 });
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < K; j++) {
d2[i][j] = INF;
vrt a = { 1, i, j };
s.insert({ d2[i][j], 1, i, j });
}
}
while (!s.empty()) {
ll d = get<0>(*s.begin());
vrt v = { get<1>(*s.begin()), get<2>(*s.begin()), get<3>(*s.begin()) };
s.erase(s.begin());
if (d >= INF) continue;
if (v.type == 0) {
for (auto to : gr[v.v]) {
if (d2[to.first][to.second] > d) {
tuple<ll, ll, ll, ll> u = { d2[to.first][to.second], 1, to.first, to.second };
s.erase(u);
d2[to.first][to.second] = d;
u = { d2[to.first][to.second], 1, to.first, to.second };
s.insert(u);
}
}
}
else {
for (auto to : gr[v.v]) {
ll cost = (to.second + v.c) * (to.second + v.c);
if (dist[to.first] > d + cost) {
tuple<ll, ll, ll, ll> u = { dist[to.first], 0, to.first, 0 };
s.erase(u);
dist[to.first] = d + cost;
u = { dist[to.first], 0, to.first, 0 };
s.insert(u);
}
}
}
}
}
int main() {
fastInp;
cin >> n >> m;
gr.resize(n);
for (int i = 0; i < m; i++) {
ll u, v, c;
cin >> u >> v >> c;
u--; v--;
gr[u].push_back({ v, c });
gr[v].push_back({ u, c });
}
djikstra();
for (int i = 0; i < n; i++) {
if (dist[i] < INF) {
cout << dist[i] << " ";
}
else {
cout << "-1 ";
}
}
return 0;
} | 5 |
#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;
// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//
const int MAX = 200010;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;
ll fexp(ll b, ll e) {
if(e == 0) return 1;
ll res = fexp(b, e/2);
res = (res * res ) % MOD;
if(e%2) res = (res * b) % MOD;
return res;
}
int32_t main() {
sws;
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
map<int,int> ind;
vector<bool> vis(n+1, 0);
vi v(n), w(n);
forn(i, n) {
cin >> v[i];
ind[v[i]] = i;
}
forn(i, n)
cin >> w[i];
int qt = 0;
forn(i, n) {
if(vis[i]) continue;
int j = i;
while(!vis[j]) {
vis[j] = 1;
int prox = w[j];
j = ind[prox];
}
qt++;
}
ll res = fexp(2, qt);
cout << res << endl;
}
// int overflow, array bounds special cases (n=1?)
// do smth instead of nothing and stay organized - divide in subproblems?
// WRITE STUFF DOWN DON'T GET STUCK ON ONE APPROACH
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, k, x;
std::cin >> n >> k >> x;
int ans = k * x;
for (int i = 0; i < n - k; i++) {
int temp;
std::cin >> temp;
ans += temp;
}
std::cout << ans << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[510];
vector<pair<int, int> > edges;
vector<int> leaf, nonleaf;
int d = 0;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
if (a[i] == 1)
leaf.push_back(i);
else
nonleaf.push_back(i);
}
d = nonleaf.size() - 1 + min((int)leaf.size(), 2);
int reserved = -1;
if (!leaf.empty()) reserved = leaf.back(), leaf.pop_back();
for (int i = 0; i < (int)nonleaf.size() - 1; i++) {
int k = nonleaf[i];
while (!leaf.empty() && a[k] > 1) {
edges.push_back({leaf.back(), k});
leaf.pop_back();
a[k]--;
}
a[k]--;
a[nonleaf[i + 1]]--;
edges.push_back({k, nonleaf[i + 1]});
}
int tp = 501;
if (!nonleaf.empty()) tp = nonleaf.back();
if (reserved != -1) leaf.push_back(reserved);
while (!leaf.empty() && a[tp] > 0) {
edges.push_back({leaf.back(), tp});
leaf.pop_back();
a[tp]--;
}
if (leaf.empty()) {
printf("YES %d\n", d);
printf("%d\n", (int)edges.size());
for (auto k : edges) {
printf("%d %d\n", k.first, k.second);
}
} else {
printf("NO\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void print() { pr("\n"); }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
print(ts...);
}
} // namespace output
using namespace output;
bool query(int pos) {
cout << "? " << pos + 1 << endl;
fflush(stdout);
char ans;
cin >> ans;
return ans == 'Y';
}
void reset() {
cout << "R" << endl;
fflush(stdout);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N, K;
cin >> N >> K;
int ans = 0;
vector<bool> seen(N);
if (K > 1) K /= 2;
for (int l = K; l < N; l += K) {
for (int b = 0; b < l; b += K) {
for (int i = (b); i < (b + K); ++i) seen[i] = seen[i] | query(i);
for (int i = (l); i < (l + K); ++i) seen[i] = seen[i] | query(i);
reset();
}
}
for (int i = (0); i < (N); ++i) ans += !seen[i];
cout << "! " << ans << endl;
fflush(stdout);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct timer {
time_t start;
timer() { start = clock(); }
~timer() {
cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
}
};
int n, s[100010], K;
vector<pair<int, int> > event[100010];
struct segtree {
int l, r;
segtree *cl, *cr;
int val, neg;
segtree(int l, int r) : l(l), r(r) {
neg = 0;
if (l + 1 == r) {
val = 0;
return;
}
cl = new segtree(l, l + r >> 1);
cr = new segtree(l + r >> 1, r);
val = cl->val + cr->val;
}
void flip(int L, int R) {
if (L <= l && r <= R) {
val = r - l - val;
neg ^= 1;
return;
}
if (neg) down();
if (L < l + r >> 1) cl->flip(L, R);
if (R > l + r >> 1) cr->flip(L, R);
val = cl->val + cr->val;
}
long long query(int L, int R) {
if (L <= l && r <= R) {
return val;
}
if (neg) down();
long long res = 0;
if (L < l + r >> 1) res += cl->query(L, R);
if (R > l + r >> 1) res += cr->query(L, R);
return res;
}
inline void down() {
cl->val = cl->r - cl->l - cl->val;
cl->neg ^= 1;
cr->val = cr->r - cr->l - cr->val;
cr->neg ^= 1;
neg = 0;
}
} * root;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> K;
for (int i = 0; i < (n); i++) cin >> s[i];
sort(s, s + n);
int a, b, A, B;
for (int i = 0; i < (K); i++) {
cin >> a >> b;
A = lower_bound(s, s + n, a) - s;
B = upper_bound(s, s + n, b) - s;
if (A >= B) continue;
event[A].push_back(make_pair(A, B));
event[B].push_back(make_pair(A, B));
}
long long ans = (long long)n * (n - 1) * (n - 2) / 6;
root = new segtree(0, n);
for (int i = 0; i < (n); i++) {
if (i > 0) {
root->flip(i - 1, i);
}
for (int j = 0; j < (((int)event[i].size())); j++) {
root->flip(event[i][j].first, event[i][j].second);
}
long long win = root->query(0, n) - root->query(i, i + 1);
ans -= win * (win - 1) / 2;
}
cout << ans;
}
| 5 |
#include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-7)
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
#define equals(a,b) (fabs((a)-(b)) < EPS)
using namespace std;
class Point{
public:
double x,y;
Point(double x = 0,double y = 0): 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 a){return Point(a*x,a*y);}
Point operator / (double a){return Point(x/a,y/a);}
bool operator < (const Point& p) const{ return !equals(x,p.x)?x<p.x:(!equals(y,p.y)&&y<p.y); }
bool operator == (const Point& p)const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; }
};
struct Segment{
Point p1,p2;
Segment(Point p1 = Point(),Point p2 = Point()):p1(p1),p2(p2){}
bool operator == (const Segment& p)const { return ( p.p1 == p1 && p.p2 == p2 ) || ( p.p1 == p2 && p.p2 == p1 ); }
};
typedef Point Vector;
typedef Segment Line;
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 a){ return a.x*a.x+a.y*a.y; }
double abs(Point a){ return sqrt(norm(a)); }
int ccw(Point p0,Point p1,Point p2){
Point a = p1-p0;
Point b = p2-p0;
if(cross(a,b) > EPS)return COUNTER_CLOCKWISE;
if(cross(a,b) < -EPS)return CLOCKWISE;
if(dot(a,b) < -EPS)return ONLINE_BACK;
if(norm(a) < norm(b))return ONLINE_FRONT;
return ON_SEGMENT;
}
bool intersectSS(Line s, Line t) {
return ccw(s.p1,s.p2,t.p1)*ccw(s.p1,s.p2,t.p2) <= 0 &&
ccw(t.p1,t.p2,s.p1)*ccw(t.p1,t.p2,s.p2) <= 0;
}
Point crosspoint(Line l,Line m){
double A = cross(l.p2-l.p1,m.p2-m.p1);
double B = cross(l.p2-l.p1,l.p2-m.p1);
if(abs(A) < EPS && abs(B) < EPS){
vector<Point> vec;
vec.push_back(l.p1),vec.push_back(l.p2),vec.push_back(m.p1),vec.push_back(m.p2);
sort(vec.begin(),vec.end());
assert(vec[1] == vec[2]);
return vec[1];
}
if(abs(A) < EPS)assert(false);
return m.p1 + (m.p2-m.p1)*(B/A);
}
double cross3p(Point p,Point q,Point r){ return (r.x-q.x) * (p.y -q.y) - (r.y - q.y) * (p.x - q.x); }
bool collinear(Point p,Point q,Point r){ return fabs(cross3p(p,q,r)) < EPS; }
bool ccwtest(Point p,Point q,Point r){ return cross3p(p,q,r) > 0; }
bool onSegment(Point p,Point q,Point r){
return collinear(p,q,r) && equals(sqrt(pow(p.x-r.x,2)+pow(p.y-r.y,2)) + sqrt(pow(r.x-q.x,2) + pow(r.y-q.y,2) ),sqrt(pow(p.x-q.x,2)+pow(p.y-q.y,2)) ) ;
}
double getArea(vector<Point>& vec){
double sum = 0;
for(int i=0;i<(int)vec.size();i++) sum += cross(vec[i],vec[(i+1)%vec.size()]);
return fabs(sum)/2.0;
}
int N;
vector<Point> ps;
Point vir;
bool comp_dist(const Point& a,const Point& b) { return !equals(abs(vir-a),abs(vir-b)) && abs(vir-a) < abs(vir-b); }
void makeSegments(vector<Segment> &segs){
vector<Point> vp;
rep(i,N-1) {
vp.clear();
vp.push_back(ps[i]);
vp.push_back(ps[i+1]);
Segment cur_seg = Segment(ps[i],ps[i+1]);
rep(j,N-1) if( i != j ) {
Segment seg = Segment(ps[j],ps[j+1]);
if( equals(cross(cur_seg.p1-cur_seg.p2,seg.p1-seg.p2),0.0) ) {
if( onSegment(cur_seg.p1,cur_seg.p2,seg.p1) ) vp.push_back(seg.p1);
if( onSegment(cur_seg.p1,cur_seg.p2,seg.p2) ) vp.push_back(seg.p2);
} else if( intersectSS(seg,cur_seg) ) vp.push_back(crosspoint(cur_seg,seg));
}
vir = ps[i];
sort(vp.begin(),vp.end(),comp_dist);
vp.erase(unique(vp.begin(),vp.end()),vp.end());
rep(j,(int)vp.size()-1) segs.push_back(Segment(vp[j],vp[j+1]));
}
}
vector<Point> angle_ps;
struct Data{
int from,to,rev;
bool operator < (const Data & a)const{
Vector vec_a = angle_ps[a.to] - angle_ps[a.from];
Vector vec_b = angle_ps[to] - angle_ps[from];
double arg_a = atan2(vec_b.y,vec_b.x);
double arg_b = atan2(vec_a.y,vec_a.x);
return !equals(arg_a,arg_b) && arg_a < arg_b;
}
};
vector<vector<Data> > G;
vector<Point> vp;
void makeGraph(const vector<Segment> &segs){
map<Point,int> mp;
int V = 0;
rep(i,segs.size()) if( !mp.count(segs[i].p1) ) mp[segs[i].p1] = V++;
if( !mp.count(segs.back().p2) ) mp[segs.back().p2] = V++;
vp.resize(V);
for(map<Point,int>::iterator it = mp.begin(); it != mp.end(); it++){
vp[it->second] = it->first;
}
angle_ps = vp;
G.resize(V,vector<Data>());
rep(i,segs.size()) {
int s = mp[segs[i].p1], t = mp[segs[i].p2];
G[s].push_back((Data){s,t,-1});
G[t].push_back((Data){t,s,-1});
}
rep(i,V) sort(G[i].begin(),G[i].end());
rep(i,V) rep(j,(int)G[i].size()) if( G[i][j].rev == -1 ) {
int to = G[i][j].to;
rep(k,(int)G[to].size())if( G[to][k].to == i ){
G[i][j].rev = k;
G[to][k].rev = j;
break;
}
}
}
const int MAX_V = 5000;
bool visited[MAX_V][MAX_V];
bool getPath(int start_node,int node,int edge,vector<Point> &buf){
if( visited[node][edge] ){
if( !buf.empty() && node == start_node ) return true;
return false;
}
visited[node][edge] = true;
int next_node = G[node][edge].to;
int next_edge = (G[node][edge].rev+1)%((int)G[next_node].size());
buf.push_back(vp[G[node][edge].from]);
if(getPath(start_node,next_node,next_edge,buf)) return true;
else return visited[node][edge] = false;
}
void compute(){
vector<Segment> segs;
segs.clear();
makeSegments(segs);
makeGraph(segs);
segs.clear();
ps.clear();
angle_ps.clear();
int V = vp.size();
rep(i,V) rep(j,V) visited[i][j] = false;
double area = 0;
rep(i,V) rep(j,(int)G[i].size()) if( !visited[i][j] ){
vector<Point> buf;
if( getPath(i,i,j,buf) && !buf.empty() ) area += getArea(buf);
}
printf("%.10lf\n",area/2.0);
}
int main(){
scanf("%d",&N);
ps.resize(N);
rep(i,N) scanf("%lf %lf",&ps[i].x,&ps[i].y);
compute();
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 9;
const int M = 200;
const int MOD = 998244353;
int N, dp[maxn][M + 9][2];
inline void Mod(int &x) {
if (x >= MOD) x -= MOD;
if (x < 0) x += MOD;
}
void upd(int i, int val) {
Mod(dp[i][val][0] = dp[i - 1][val - 1][0] + dp[i - 1][val - 1][1]);
Mod(dp[i][val][1] = dp[i - 1][M][1] - dp[i - 1][val - 1][1]);
Mod(dp[i][val][1] += dp[i - 1][val][0] - dp[i - 1][val - 1][0]);
}
int main() {
scanf("%d", &N);
for (int j = 0; j <= M; j++) dp[0][j][1] = 1;
for (int i = 1, val; i <= N; i++) {
scanf("%d", &val);
if (val == -1)
for (int j = 1; j <= M; j++) upd(i, j);
else
upd(i, val);
for (int j = 1; j <= M; j++) {
Mod(dp[i][j][0] += dp[i][j - 1][0]);
Mod(dp[i][j][1] += dp[i][j - 1][1]);
}
}
printf("%d\n", dp[N][M][1]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void re() {}
template <typename T, typename... args>
void re(T &x, args &...tail) {
cin >> x;
re(tail...);
}
void pr() {}
template <typename T>
void pr(const T &x) {
cout << x;
}
template <typename T, typename... args>
void pr(const T &x, const args &...tail) {
cout << x << " ";
pr(tail...);
}
template <typename... args>
void prln(const args &...tail) {
pr(tail...);
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int l[n], r[n];
for (long long int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
sort(l, l + n);
sort(r, r + n);
cout << max(0ll, l[n - 1] - r[0]) << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[100005];
int mod = 1000000007;
long long a[2005][4005];
long long b[2005][4005];
int main() {
int n, m;
scanf("%d%d", &n, &m);
scanf("%s", s);
int mn = 0;
int sum = 0;
for (int i = 0; i < m; i++) {
if (s[i] == '(')
sum++;
else
sum--;
mn = min(sum, mn);
}
n -= m;
if (sum > n || -mn > n) {
printf("0\n");
return 0;
}
mn = -mn;
a[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (a[i][j] > 0) {
a[i + 1][j + 1] += a[i][j];
if (a[i + 1][j + 1] >= mod) a[i + 1][j + 1] -= mod;
if (j > 0) {
a[i + 1][j - 1] += a[i][j];
if (a[i + 1][j - 1] >= mod) a[i + 1][j - 1] -= mod;
}
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = mn; j <= n; j++) {
if (j + sum <= n - i) {
b[n - i][j + sum] += a[i][j];
if (b[n - i][j + sum] >= mod) b[n - i][j + sum] -= mod;
}
}
}
for (int i = n; i > 0; i--) {
for (int j = 0; j <= i; j++) {
if (b[i][j] > 0) {
b[i - 1][j + 1] += b[i][j];
if (b[i - 1][j + 1] >= mod) b[i - 1][j + 1] -= mod;
if (j > 0) {
b[i - 1][j - 1] += b[i][j];
if (b[i - 1][j - 1] >= mod) b[i - 1][j - 1] -= mod;
}
}
}
}
printf("%I64d\n", b[0][0]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
int main() {
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while (t--) {
long long a, b, c;
std::cin >> a >> b >> c;
long long s = a + b + c;
long long t = (s + 1) / 2;
if (std::max({a, b, c}) > t) {
std::cout << "No" << std::endl;
} else {
std::cout << "Yes" << std::endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<long long, long long> m;
int a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
m[a[i]] = i + 1;
}
int b[n];
int l = 1, r = n;
int ans[n];
for (long long i = 0; i < n; i++) {
cin >> b[i];
int pos = m[b[i]];
if (pos < l)
ans[i] = 0;
else {
ans[i] = pos - l + 1;
l = pos + 1;
}
}
for (long long i = 0; i < n; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int INF = (1LL << 58);
const bool DEBUG = 0;
const int NODES_LIMIT = 100005;
int nodes, edges;
std::vector<int> adj[2][NODES_LIMIT];
bool vis[NODES_LIMIT];
int color[NODES_LIMIT], globalColor = 1;
int componentSize[NODES_LIMIT];
bool hasCycle[NODES_LIMIT];
int doColor(int node, int thisColor) {
vis[node] = 1;
color[node] = thisColor;
int i, limit = adj[0][node].size();
int ret = 0;
for (i = 0; i < limit; i++) {
int v = adj[0][node][i];
if (!vis[v]) ret += doColor(v, thisColor);
}
return ret + 1;
}
bool recStack[NODES_LIMIT];
bool detectCycle(int node) {
int limit = adj[1][node].size();
vis[node] = 1;
recStack[node] = 1;
for (int i = 0; i < limit; i++) {
int v = adj[1][node][i];
if (!vis[v]) {
if (detectCycle(v)) return 1;
} else if (recStack[v] == 1)
return 1;
}
recStack[node] = 0;
return 0;
}
int main() {
int i, j;
scanf("%d", &nodes);
scanf("%d", &edges);
for (i = 0; i < edges; i++) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
x--;
y--;
adj[0][x].push_back(y);
adj[0][y].push_back(x);
adj[1][x].push_back(y);
}
for (i = 0; i < nodes; i++) {
if (!vis[i]) {
componentSize[globalColor] = doColor(i, globalColor);
globalColor++;
}
}
memset(vis, 0, sizeof(vis));
for (i = 0; i < nodes; i++)
if (!vis[i]) hasCycle[color[i]] |= detectCycle(i);
int ans = 0;
for (i = 1; i < globalColor; i++)
ans += (componentSize[i] - 1) + (int)hasCycle[i];
printf("%d\n", ans);
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define N 100
using namespace std;
int main() {
int n,p[N+1],m[N+1][N+1];
cin>>n;
for (int i=1;i<=n;i++) cin>>p[i-1]>>p[i];
for (int i=1;i<=n;i++) m[i][i]=0;
for (int l=2;l<=n;l++) {
for (int i=1;i<=n-l+1;i++) {
int j=i+l-1;
m[i][j]=(1<<21);
for (int k=i;k<=j-1;k++) {
m[i][j]=min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
}
}
}
cout<<m[1][n]<<endl;
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int main() {
int N;
cin>>N;
int dt=0,dx=0,dy=0;
bool f=true;
for (int i=0; i<N; i++) {
int t,x,y;
cin>>t>>x>>y;
dt=abs(t-dt),dx=abs(x-dx),dy=abs(y-dy);
if (dt<dx+dy || (abs(dx+dy-dt))%2!=0) {
f=false;
}
}
cout<<(f?"Yes":"No")<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> d[1000000], f[1000000];
int lastt, cur, n, i, j, k, ans, num, last, x, l;
int g1[1000][1000], g[1000][1000];
int fix[1000000], c[1000000], h[1000000];
queue<int> q[4];
int main() {
cin >> n;
for (i = 1; i <= n; i++) cin >> c[i];
for (i = 1; i <= n; i++) {
cin >> l;
for (j = 0; j < l; j++) {
cin >> x;
g1[i][x] = 1;
}
}
cur = 1000000000;
for (lastt = 3; lastt >= 1; lastt--) {
last = lastt;
ans = 0;
for (i = 1; i <= n; i++) fix[i] = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) g[i][j] = g1[i][j];
while (1) {
for (i = 1; i <= n; i++) {
d[i].second = i;
d[i].first = 0;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) d[i].first += g[i][j];
l = 0;
sort(d + 1, d + n + 1);
for (i = 1; i <= n; i++)
if (d[i].first > 0)
break;
else if (fix[d[i].second] == 0) {
l++;
q[c[d[i].second]].push(d[i].second);
}
if (l == 0) break;
if (q[last].empty()) {
last = (last + 1) % 3;
if (last == 0) last = 3;
ans++;
continue;
}
while (!q[last].empty()) {
fix[q[last].front()] = 1;
for (i = 1; i <= n; i++) {
g[i][q[last].front()] = 0;
g[q[last].front()][i] = 0;
}
q[last].pop();
}
}
cur = min(cur, ans);
}
cout << cur + n << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int hr = s[0] - '0';
hr = hr * 10 + s[1] - '0';
if (hr >= 12) hr -= 12;
int min = s[3] - '0';
min = min * 10 + s[4] - '0';
double b = min * 6;
double a = ((double)hr + (double)min / 60) * 30;
printf("%.9lf %.9lf", a, b);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int va, vb, vc;
int A, B, C;
vector<int> as, bs, cs;
bool dp[33][2][11][11][11];
struct state {
int L, c, A, B, C, aa, bb, cc;
state() {}
state(int L, int c, int A, int B, int C, int aa, int bb, int cc)
: L(L), c(c), A(A), B(B), C(C), aa(aa), bb(bb), cc(cc) {}
};
state pre[33][2][11][11][11];
void cal(int alen, int blen, int clen) {
memset(dp, 0, sizeof(dp));
dp[0][0][0][0][0] = 1;
int L = clen;
for (int i = 0; i < L; i++)
for (int cc = 0; cc < 2; cc++)
for (int a = 0; a <= A; a++)
for (int b = 0; b <= B; b++)
for (int c = 0; c <= C; c++)
if (dp[i][cc][a][b][c])
for (int ca = 0; ca < 10; ca++)
for (int cb = 0; cb < 10; cb++) {
int CC = ca + cb + cc, ncc = 0;
if (CC >= 10) CC -= 10, ncc = 1;
if (i >= alen && ca != 0) continue;
if (i >= blen && cb != 0) continue;
if (i >= clen && CC != 0) continue;
int pa = a + ((a < A && as[a] == ca) ? 1 : 0);
int pb = b + ((b < B && bs[b] == cb) ? 1 : 0);
int pc = c + ((c < C && cs[c] == CC) ? 1 : 0);
dp[i + 1][ncc][pa][pb][pc] = 1;
pre[i + 1][ncc][pa][pb][pc] =
state(i, cc, a, b, c, ca, cb, CC);
}
if (dp[L][0][A][B][C]) {
int l = L, c = 0, aa = A, bb = B, cc = C;
string sa = "", sb = "", sc = "";
while (l > 0) {
state cur = pre[l][c][aa][bb][cc];
l = cur.L, c = cur.c, aa = cur.A, bb = cur.B, cc = cur.C;
if (l < alen) sa.push_back(cur.aa + '0');
if (l < blen) sb.push_back(cur.bb + '0');
if (l < clen) sc.push_back(cur.cc + '0');
}
cout << sa << "+" << sb << "=" << sc << endl;
exit(0);
}
}
int main() {
scanf("%d+%d=%d", &va, &vb, &vc);
while (va) {
as.push_back(va % 10);
va /= 10;
}
while (vb) {
bs.push_back(vb % 10);
vb /= 10;
}
while (vc) {
cs.push_back(vc % 10);
vc /= 10;
}
A = as.size(), B = bs.size(), C = cs.size();
for (int sum = 0;; sum++)
for (int alen = A; alen <= sum; alen++)
for (int blen = B; blen + alen <= sum; blen++) {
int clen = sum - alen - blen;
if (clen < C) break;
int tmp = clen - max(alen, blen);
if (tmp != 0 && tmp != 1) continue;
cal(alen, blen, clen);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 5;
struct Edge {
int v, nxt;
Edge() {}
Edge(int _v, int _nxt) : v(_v), nxt(_nxt) {}
};
Edge E[MAXN * 2];
int head[MAXN], ans[MAXN], sub[MAXN], mxsub[MAXN];
int tot = 0, fuc, n, x, y;
void addedge(int u, int v) {
E[tot] = Edge(v, head[u]);
head[u] = tot++;
}
void dfs1(int now, int fa) {
sub[now] = 1;
mxsub[now] = 1;
int tmp = 1;
for (int i = head[now]; ~i; i = E[i].nxt) {
int v = E[i].v;
if (v != fa) {
dfs1(v, now);
sub[now] += sub[v];
mxsub[now] = max(mxsub[now], mxsub[v]);
tmp += sub[v];
}
}
if (tmp <= fuc) {
mxsub[now] = tmp;
}
}
void dfs2(int now, int fa, int subfa, int mxfa) {
int cnt = subfa > fuc;
int id;
if (cnt) {
id = -1;
}
int tmp = n;
int mx1, mx2;
int id1, id2;
mx1 = mx2 = -1;
for (int i = head[now]; ~i; i = E[i].nxt) {
int v = E[i].v;
if (v != fa) {
if (id1 == -1 || mx1 < mxsub[v]) {
mx2 = mx1;
id2 = id1;
mx1 = mxsub[v];
id1 = v;
} else if (id2 == -1 || mx2 < mxsub[v]) {
mx2 = mxsub[v];
id2 = v;
}
}
}
for (int i = head[now]; ~i; i = E[i].nxt) {
int v = E[i].v;
if (v != fa) {
dfs2(v, now, sub[now] - sub[v] + subfa,
max(tmp - sub[v] <= fuc ? tmp - sub[v] : 0,
max(mxfa, id1 == v ? mx2 : mx1)));
if (sub[v] > fuc) {
cnt++;
id = v;
}
}
}
if (cnt > 1) {
ans[now] = 0;
} else if (cnt == 0) {
ans[now] = 1;
} else if (id != -1) {
ans[now] = (sub[id] - mxsub[id] <= fuc);
} else {
ans[now] = (subfa - mxfa <= fuc);
}
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
fuc = n / 2;
dfs1(1, 1);
dfs2(1, 1, 0, 0);
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 211111;
int a[N];
double weakness(double x, int n) {
double sum = 0;
double last_min = 0;
double last_max = 0;
double ans_min = 0;
double ans_max = 0;
for (int i = 0; i < n; i++) {
sum += a[i] - x;
ans_max = max(ans_max, sum - last_min);
ans_min = min(ans_min, sum - last_max);
last_min = min(last_min, sum);
last_max = max(last_max, sum);
}
return max(ans_max, -ans_min);
}
int main() {
ios ::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
double left = -20000.0;
double right = 20000.0;
for (int t = 0; t < 200; t++) {
double mid1 = (left * 2 + right) / 3;
double mid2 = (left + right * 2) / 3;
double w1 = weakness(mid1, n);
double w2 = weakness(mid2, n);
if (w1 > w2)
left = mid1;
else
right = mid2;
}
cout << setprecision(12) << weakness(left, n) << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
multiset<int> Q;
cin >> n;
long long ans = 0;
long long times = 0;
bool b = false;
int count = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (Q.size() == 0 || (*Q.begin()) > x) {
Q.insert(x);
} else {
ans += x - *Q.begin();
if (b) {
times += 2;
count++;
if (count == 2) {
b = false;
count = 0;
}
} else
b = true;
Q.erase(Q.begin());
Q.insert(x);
Q.insert(x);
}
}
cout << ans << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int visited[200005], par[200005];
int a[200005];
int n, cur_max;
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) scanf("%d", &a[i]);
cur_max = n;
if (a[1] == cur_max) cur_max--;
visited[a[1]] = 1;
for (int i = 2; i < n; i++) {
if (visited[a[i]]) {
par[cur_max] = a[i - 1];
visited[cur_max] = 1;
while (visited[cur_max]) cur_max--;
} else if (a[i] == cur_max) {
par[cur_max] = a[i - 1];
visited[cur_max] = 1;
while (visited[cur_max]) cur_max--;
} else
par[a[i]] = a[i - 1];
visited[a[i]] = 1;
}
printf("%d\n", a[1]);
for (int i = 1; i <= n; i++) {
if (!par[i] && i != a[1]) par[i] = a[n - 1];
if (par[i]) printf("%d %d\n", i, par[i]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
long long int N, M, K, T, H, W, L, R;
class Add_Segment_Tree {
vector<long long int> v;
long long int ret;
int num;
long long int Update(int place) {
if (place >= v.size() / 2) {
return v[place];
}
v[place] = Update(place * 2) + Update(place * 2 + 1);
return v[place];
}
public:
Add_Segment_Tree(int n) {
n++;
num = 1;
while (num < n * 2) num *= 2;
v.resize(num, 0);
}
void Add(int place, long long int num, bool update) {
place += v.size() / 2;
v[place] += num;
if (!update) return;
place /= 2;
while (place) {
v[place] = v[place * 2] + v[place * 2 + 1];
place /= 2;
}
}
void TopDown() { Update(1); }
long long int Sum(int a, int b) {
ret = 0;
b++;
for (a += num / 2, b += num / 2; a < b; a >>= 1, b >>= 1) {
if (a & 1) ret += v[a++];
if (b & 1) ret += v[--b];
}
return ret;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> T;
while (T--) {
cin >> N >> K;
vector<int> v(N);
vector<int> w(K);
vector<int> place(N + 1);
for (auto &i : v) cin >> i;
for (int i = 0; i < N; i++) {
place[v[i]] = i;
}
set<int> st;
for (auto &i : w) cin >> i;
reverse(w.begin(), w.end());
vector<int> l(K);
vector<int> r(K);
Add_Segment_Tree asg(N + 1);
for (auto i : w) {
asg.Add(place[i], 1, false);
}
st.insert(-1);
st.insert(N);
asg.TopDown();
long long int ans = 1;
for (auto i : w) {
auto it = st.lower_bound(place[i]);
auto pv = prev(it);
int nx = *it - place[i];
nx -= asg.Sum(place[i], *it - 1);
int bf = place[i] - *pv;
bf -= asg.Sum(*pv + 1, place[i]);
asg.Add(place[i], -1, true);
st.insert(place[i]);
if (nx > 0 && bf > 0)
ans *= 2;
else if (nx == 0 && bf == 0)
ans = 0;
ans %= MOD;
}
cout << ans % MOD << endl;
}
}
| 2 |
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
#define cauto const auto&
#else
#endif
using namespace std;
namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
void mainmain(){
int n;
while(cin>>n,n){
vector<pii> v;
rep(i,n){
vector<pii> tt;
int id;
cin>>id;
int t;
int cnt=1;
while(1){
cin>>t;
if(cnt==10){
int a=t;
if(a==10){
tt.PB(mkp(a,cnt));
cin>>t;
tt.PB(mkp(t,cnt));
cin>>t;
tt.PB(mkp(t,cnt));
break;
}
else{
tt.PB(mkp(t,cnt));
cin>>t;
tt.PB(mkp(t,cnt));
if(a+t==10){
cin>>t;
tt.PB(mkp(t,cnt));
}
break;
}
}
else if(t==10){
tt.PB(mkp(t,cnt));
}
else{
tt.PB(mkp(t,cnt));
cin>>t;
tt.PB(mkp(t,cnt));
}
cnt++;
}
int tmp=0;
// cout<<"id = "<<id<<endl;
// rep(i,tt.size()){
// cout<<tt[i].F<<" "<<tt[i].S<<endl;
// }
// cout<<endl;
rep(i,tt.size()){
if(tt[i].S==10){
tmp+=tt[i].F;
continue;
}
if(tt[i].F==10){
tmp+=10+tt[i+1].F+tt[i+2].F;
continue;
}
if(tt[i].F+tt[i+1].F==10){
tmp+=10+tt[i+2].F;
i++;
}
else{
tmp+=tt[i].F+tt[i+1].F;
i++;
}
}
v.PB(mkp(-tmp,id));
}
sort(ALL(v));
rep(i,n) cout<<v[i].S<<" "<<-v[i].F<<endl;
}
}
}
int main() try{
mainmain();
}
catch(...){
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
vector<int> cut;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, x, y;
string s;
cin >> n >> x >> y >> s;
s = '1' + s;
int cnt = 0;
for (int i = 0; i < int(n); ++i)
if (s[i] == '1' && s[i + 1] == '0') cnt++;
if (cnt == 0) return cout << 0, 0;
long long int ans = 1e16;
for (int i = 0; i < int(cnt); ++i) {
long long int sum = 1LL * i * x + 1LL * (cnt - i) * y;
ans = min(ans, sum);
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool query(int x, int y) {
printf("? %d %d\n", x, y);
fflush(stdout);
char str[5];
scanf("%s", str);
return str[0] == 'x';
}
int main() {
char str[10];
while (scanf("%s", str)) {
if (str[0] != 's') break;
int l = 1, r = 0;
while (!query(l, 2 * l)) l = 2 * l;
r = 2 * l;
while (l < r) {
int m = ((l + r) >> 1);
if (query(l - 1, m))
r = m;
else
l = m + 1;
}
printf("! %d\n", l);
fflush(stdout);
}
return 0;
}
| 4 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define sortr(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define max 100005
#define nil -1
int main() {
int n,a,b,c;
cin >> n>>a>>b>>c;
cout << n - (a + b) + c << endl;
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int n, m;
int w[100][100];
int p[50][50];
int b;
bool input() {
cin >> n >> m;
if (!n && !m) {
return false;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> w[i][j];
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
cin >> p[i][j];
}
}
return true;
}
void rotate() {
int tmp[50][50];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
tmp[i][j] = p[i][j];
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
p[i][j] = tmp[m - 1 - j][i];
}
}
}
bool check(int r, int c) {
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m; ++j) {
if (p[i][j] == -1) {
continue;
}
if (p[i][j] != w[r + i][c + j]) {
return false;
}
}
}
return true;
}
void solve() {
b = n * n;
for (int i = 0; i < 4; ++i) {
bool found = false;
int dr = -1;
int dc = -1;
for (int r = 0; r < m; ++r) {
for (int c = 0; c < m; ++c) {
if (p[r][c] != -1) {
dr = r;
dc = c;
found = true;
break;
}
}
if (found) {
break;
}
}
found = false;
for (int r = 0; r <= n - m; ++r) {
for (int c = 0; c <= n - m; ++c) {
int bb = (r + dr) * n + c + dc;
if (check(r, c)) {
if (b > bb) {
b = bb;
}
found = true;
break;
}
}
if (found) {
break;
}
}
rotate();
}
if (b == n * n) {
cout << "NA" << endl;
return;
} else {
cout << b % n + 1 << " " << b / n + 1 << endl;
}
}
int main() {
while (input()) {
solve();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 17;
string s, x;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> s;
reverse(s.begin(), s.end());
while (s.size() and s.back() == '0') s.pop_back();
reverse(s.begin(), s.end());
while (s.size()) {
if (s.back() == '0') x += '0';
s.pop_back();
}
if (x.size() > 5)
cout << "yes";
else
cout << "no";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct wie {
long long s, pref, suf;
};
vector<vector<wie> > T(17);
int l, size;
wie Merge(wie a, wie b, int wlk) {
wie wyn;
wyn.s = a.s + b.s;
wyn.s -= (1 + a.suf) * a.suf / 2;
wyn.s -= (1 + b.pref) * b.pref / 2;
wyn.s += (1 + (a.suf + b.pref)) * (a.suf + b.pref) / 2;
if (a.pref < wlk)
wyn.pref = a.pref;
else
wyn.pref = a.pref + b.pref;
if (b.suf < wlk)
wyn.suf = b.suf;
else
wyn.suf = b.suf + a.suf;
return wyn;
}
void Stworz(int n) {
int p = 1;
while (p < n) p *= 2;
l = p;
size = 2 * p - 1;
for (int i = 0; i < 17; ++i)
for (int j = 0; j <= size; ++j) T[i].push_back({0, 0, 0});
}
void Zmien(int nr, int a, int x) {
a += l;
T[nr][a] = {x, x, x};
a /= 2;
int wlk = 1;
while (a != 0) {
T[nr][a] = Merge(T[nr][2 * a], T[nr][2 * a + 1], wlk);
a /= 2;
wlk *= 2;
}
}
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<long long> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
Stworz(n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < 17; ++j)
if ((a[i] & (1 << j)) > 0) Zmien(j, i, 1);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x;
for (int j = 0; j < 17; ++j) {
if ((y & (1 << j)) > 0)
Zmien(j, x, 1);
else
Zmien(j, x, 0);
}
long long odp = 0;
for (int j = 0; j < 17; ++j) odp += T[j][1].s * (1 << j);
cout << odp << endl;
}
return 0;
}
| 5 |
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main() {
while(true) {
int W, Q;
cin >> W >> Q;
if(!W && !Q) break;
vector<bool> wall(W+1, true);
wall[W] = false;
map<int, pair<int, int> > catpos;
while(Q--) {
char c;
cin >> c;
if(c == 's') {
int id, w;
cin >> id >> w;
for(int i = 0; i < W; ++i) {
if(!wall[i]) continue;
bool ok = true;
for(int j = i; j < i+w; ++j) {
if(!wall[j]) {
i = j;
ok = false;
break;
}
}
if(ok) {
for(int j = i; j < i+w; ++j) {
wall[j] = false;
}
catpos[id] = make_pair(i, w);
cout << i << endl;
goto next;
}
}
cout << "impossible" << endl;
}
else {
int id;
cin >> id;
int i = catpos[id].first;
int w = catpos[id].second;
for(int j = 0; j < w; ++j) {
wall[i+j] = true;
}
}
next:
;
}
cout << "END" << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int d[305], deg[1005];
vector<pair<int, int> > vec;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> d[i];
int l = 1, r = n, q = d[n] + 1, lst = 0;
for (int i = 1; i <= q; i++) {
if (l <= r && q - i + deg[i] == d[r]) {
lst = 1;
r--;
} else if (l <= r && deg[i] == d[l]) {
lst = 0;
l++;
}
if (lst) {
for (int j = i + 1; j <= q; j++) {
deg[i]++;
deg[j]++;
vec.push_back(make_pair(i, j));
}
}
}
cout << vec.size() << endl;
for (int i = 0; i < vec.size(); i++)
printf("%d %d\n", vec[i].first, vec[i].second);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:128000000")
long long n, k;
vector<vector<pair<int, int> > > upstream, downstream;
vector<pair<int, int> > totalup, totaldown;
vector<int> allnumbers;
vector<int> digits;
void generate_upstream(int pos, int value, vector<pair<int, int> > &a) {
allnumbers.push_back(value);
if (pos == 7) {
a.push_back(pair<int, int>((int)(value % n), value));
digits.pop_back();
return;
}
if (pos & 1) {
for (int i = digits.back() + 1; i < 10; ++i) {
digits.push_back(i);
generate_upstream(pos + 1, value * 10 + i, a);
}
} else {
for (int i = 0; i < digits.back(); ++i) {
digits.push_back(i);
generate_upstream(pos + 1, value * 10 + i, a);
}
}
digits.pop_back();
}
void generate_downstream(int pos, int value, vector<pair<int, int> > &a) {
allnumbers.push_back(value);
if (pos == 6) {
upstream[0].push_back(pair<int, int>((int)(value % n), value));
}
if (pos == 7) {
a.push_back(pair<int, int>((int)(value % n), value));
digits.pop_back();
return;
}
if (!(pos & 1)) {
for (int i = digits.back() + 1; i < 10; ++i) {
digits.push_back(i);
generate_downstream(pos + 1, value * 10 + i, a);
}
} else {
for (int i = 0; i < digits.back(); ++i) {
digits.push_back(i);
generate_downstream(pos + 1, value * 10 + i, a);
}
}
digits.pop_back();
}
inline int get_total_leftmost(vector<pair<int, int> > &a, int md, int ind) {
pair<int, int> cur(md, (ind << 26));
int res = (int)a.size();
int l = 0, r = res - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid] >= cur) {
res = mid;
r = mid - 1;
continue;
} else
l = mid + 1;
}
if (res == (int)a.size()) return 0;
if (a[res].first != md) return 0;
return (a[res].second & 67108863);
}
inline int get_total_rightmost(vector<pair<int, int> > &a, int md, int ind) {
pair<int, int> cur(md, (ind << 26) | 67108863);
int res = -1;
int l = 0, r = (int)a.size() - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid] <= cur) {
res = mid;
l = mid + 1;
continue;
} else
r = mid - 1;
}
if (res == -1) return 0;
if (a[res].first != md) return 0;
return (a[res].second & 67108863);
}
inline int get_leftmost(vector<pair<int, int> > &a, int md) {
int res = -1;
int l = 0, r = (int)a.size() - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid].first == md) {
res = mid;
r = mid - 1;
continue;
}
if (a[mid].first > md)
r = mid - 1;
else
l = mid + 1;
}
return res;
}
inline int get_rightmost(vector<pair<int, int> > &a, int md) {
int res = -1;
int l = 0, r = (int)a.size() - 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid].first == md) {
res = mid;
l = mid + 1;
continue;
}
if (a[mid].first > md)
r = mid - 1;
else
l = mid + 1;
}
return res;
}
inline int get_count(vector<pair<int, int> > &a, int md) {
int left_ind = get_leftmost(a, md);
if (left_ind == -1) return 0;
int right_ind = get_rightmost(a, md);
return right_ind - left_ind + 1;
}
inline int get_kth(vector<pair<int, int> > &a, int md, int kvalue) {
int left_ind = get_leftmost(a, md);
return left_ind + kvalue - 1;
}
int main() {
cin >> n >> k;
upstream.resize(10);
downstream.resize(10);
for (int i = 1; i < 10; ++i) {
digits.push_back(i);
generate_upstream(1, i, upstream[i]);
digits.push_back(i);
generate_downstream(1, i, downstream[i]);
sort(upstream[i].begin(), upstream[i].end());
sort(downstream[i].begin(), downstream[i].end());
}
sort(upstream[0].begin(), upstream[0].end());
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < (int)upstream[i].size(); ++j)
totalup.push_back(pair<int, int>(upstream[i][j].first, (i << 26)));
for (int j = 0; j < (int)downstream[i].size(); ++j)
totaldown.push_back(pair<int, int>(downstream[i][j].first, (i << 26)));
}
sort(totalup.begin(), totalup.end());
sort(totaldown.begin(), totaldown.end());
for (int i = 0; i < (int)totalup.size(); ++i) {
++totalup[i].second;
if (i && totalup[i].first == totalup[i - 1].first)
totalup[i].second += (totalup[i - 1].second & 67108863);
}
for (int i = (int)totaldown.size() - 1; i >= 0; --i) {
++totaldown[i].second;
if (i + 1 != (int)totaldown.size() &&
totaldown[i + 1].first == totaldown[i].first)
totaldown[i].second += (totaldown[i + 1].second & 67108863);
}
sort(allnumbers.begin(), allnumbers.end());
for (int i = 0; i < (int)allnumbers.size(); ++i) {
if (i && allnumbers[i] == allnumbers[i - 1]) continue;
if (allnumbers[i] % n) continue;
--k;
if (!k) {
cout << allnumbers[i] << endl;
return 0;
}
}
for (int i = 0; i < (int)allnumbers.size(); ++i) {
if (i && allnumbers[i] == allnumbers[i - 1]) continue;
int value = allnumbers[i];
long long md64 = (n - ((long long)value * 10000000LL) % n) % n;
if (md64 >= 10000000LL) continue;
int md = (int)md64;
if (value < 10) {
for (int i = 0; i < value; ++i) {
int cnt = get_count(upstream[i], md);
if (cnt < k) {
k -= cnt;
continue;
}
int ind = get_kth(upstream[i], md, (int)k);
long long res =
(long long)value * 10000000LL + (long long)upstream[i][ind].second;
cout << res << endl;
return 0;
}
for (int i = value + 1; i < 10; ++i) {
int cnt = get_count(downstream[i], md);
if (cnt < k) {
k -= cnt;
continue;
}
int ind = get_kth(downstream[i], md, (int)k);
long long res = (long long)value * 10000000LL +
(long long)downstream[i][ind].second;
cout << res << endl;
return 0;
}
continue;
}
int last_digit = value % 10;
int prelast_digit = (value / 10) % 10;
if (prelast_digit < last_digit) {
int cnt = get_total_rightmost(totalup, md, last_digit - 1);
if (cnt < k) {
k -= cnt;
continue;
}
for (int i = 0; i < last_digit; ++i) {
int cnt = get_count(upstream[i], md);
if (cnt < k) {
k -= cnt;
continue;
}
int ind = get_kth(upstream[i], md, (int)k);
long long res =
(long long)value * 10000000LL + (long long)upstream[i][ind].second;
cout << res << endl;
return 0;
}
} else {
int cnt = get_total_leftmost(totaldown, md, last_digit + 1);
if (cnt < k) {
k -= cnt;
continue;
}
for (int i = last_digit + 1; i < 10; ++i) {
int cnt = get_count(downstream[i], md);
if (cnt < k) {
k -= cnt;
continue;
}
int ind = get_kth(downstream[i], md, (int)k);
long long res = (long long)value * 10000000LL +
(long long)downstream[i][ind].second;
cout << res << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int M = 8 * 9 * 5 * 7 * 11 * 13;
long long a, b;
int d[M + 1];
queue<int> Q;
void Q_add(int t, int ds) {
if (0 <= t && t <= M) {
if (d[t] == -1) {
d[t] = ds;
Q.push(t);
}
}
}
long long f(int s, int t, int k) {
for (int i = 0; i < M + 1; i++) {
d[i] = -1;
}
Q = queue<int>();
Q_add(s, 0);
while (!Q.empty()) {
int t1 = Q.front();
Q.pop();
for (int i = 1; i <= k; i++) {
int t2 = t1 - t1 % i;
Q_add(t2, d[t1] + 1);
}
Q_add(t1 - 1, d[t1] + 1);
}
return d[t];
}
int main() {
int k;
cin >> a >> b >> k;
long long aM = a - a % M;
long long bM = b - b % M + M;
long long s = 0;
if (aM >= bM) {
s = (aM - bM) / M * f(M, 0, k) + f(a % M, 0, k) + f(M, b % M, k);
} else {
s = f(a % M, b % M, k);
}
cout << s << endl;
}
| 3 |
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int N, M;
int sum_N = 0, sum_M = 0, c = 0;
while (scanf("%d,%d ", &N, &M) == 2) {
sum_N += N * M, sum_M += M;
c++;
}
printf("%d\n%d\n", sum_N, (int)((double)sum_M / c + 0.5));
return 0;
} | 0 |
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,c,p,ans;
int l[123456];
pair<int,int> wc[123456];
int main() {
cin>>n>>m>>c;
for(int i=1;i<=c;i++)cin>>l[i];
for(int i=0;i<n;i++){
cin>>wc[i].second>>wc[i].first;
wc[i].first=1000-wc[i].first;
}
sort(wc,wc+n);
for(int i=0;i<n&&p<m;i++){
if(l[wc[i].second]){
l[wc[i].second]--;p++;
ans+=1000-wc[i].first;
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char park[2007][2007];
int res[2007];
int n, m, k;
bool check(int x) { return (x >= 0 && x < m); }
int main() {
cin.sync_with_stdio(0);
cout.sync_with_stdio(0);
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> park[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
char c = park[i][j];
if (c == 'R')
if (check(i + j)) res[i + j]++;
if (c == 'L')
if (check(j - i)) res[j - i]++;
if (c == 'U')
if (!(i & 1)) res[j]++;
}
for (int i = 0; i < m; i++) cout << res[i] << " ";
cout << endl;
return 0;
}
| 2 |
#include <iostream>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int a;
string s[12];
void dfs(int y,int x){
s[y][x]='0';
if(y+1<12&&s[y+1][x]=='1')dfs(y+1,x);
if(y-1>=0&&s[y-1][x]=='1')dfs(y-1,x);
if(x+1<12&&s[y][x+1]=='1')dfs(y,x+1);
if(x-1>=0&&s[y][x-1]=='1')dfs(y,x-1);
}
int main(){
while(cin>>s[0]){
a=0;
r(i,11)cin>>s[i+1];
r(i,12)r(j,12)if(s[i][j]=='1'){dfs(i,j);a++;}
cout<<a<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void seif() { std::ios_base::sync_with_stdio(0); }
int main() {
seif();
int n;
cin >> n;
if (n <= 10)
cout << 0;
else if (n < 20)
cout << 4;
else if (n == 20)
cout << 15;
else if (n == 21)
cout << 4;
else
cout << 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, buf[100001], pat[100001];
vector<int> vec[100001], ex, t;
bool check(long double pos) {
for (int i = 1; i <= n; i++)
buf[i] = vec[i].size(), pat[i] = ceil(buf[i] * pos);
queue<int> s;
for (int i : ex) s.push(i), buf[i] = 1e7;
while (!s.empty()) {
for (int i : vec[s.front()]) {
if (--buf[i] < pat[i]) s.push(i), buf[i] = 1e7;
}
s.pop();
}
t.clear();
for (int i = 1; i < n + 1; i++)
if (buf[i] < 1e5 + 1) t.push_back(i);
return t.size();
}
int main() {
cin >> n >> m >> k;
for (int i = 0; i < k; i++) {
cin >> x;
ex.push_back(x);
}
for (int i = 0; i < m; i++) {
cin >> x >> y;
vec[x].push_back(y);
vec[y].push_back(x);
}
double st = 0, en = 1, mid = (st + en) / 2;
while (en - st > 1e-12) {
(check(mid) ? st : en) = mid;
mid = (st + en) / 2;
}
check((mid - 1e-12));
printf("%d\n", t.size());
for (int i = 0; i < t.size(); i++) {
if (i) printf(" ");
printf("%d", t[i]);
}
puts("");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n;
bool solve(long long a) {
long long curr = n;
long long cnt = 0;
while (curr != 0) {
cnt += min(curr, a);
curr -= min(curr, a);
curr -= curr / 10;
}
if (n % 2 == 0)
if (cnt >= n / 2) return 1;
if (n % 2 == 1)
if (cnt > n / 2) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
long long l = 1;
long long r = n;
long long result;
while (l <= r) {
long long m = (l + r) / 2;
if (solve(m)) {
result = m;
r = m - 1;
} else {
l = m + 1;
}
}
cout << result << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2 * 1e6 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
long long a[MAX_N];
bool seen[MAX_N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 1; i < n / 2 + 1; i++) {
cout << a[i] << " " << a[0] << '\n';
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
cout << x << endl;
exit(0);
}
const long long mod = 1e9 + 7;
const int maxn = 5152;
long long n, a, b;
int dp[maxn][maxn];
string s;
long long f[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
cin >> s;
s = "." + s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (s[i] == s[j]) {
dp[i][j] = 1 + dp[i - 1][j - 1];
}
}
}
for (int i = 1; i <= n; i++) {
f[i] = f[i - 1] + a;
for (int j = 1; j < i; j++) {
f[i] = min(f[i], f[max(j, i - dp[i][j])] + b);
}
}
out(f[n]);
return 0;
}
| 6 |
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<numeric>
using namespace std;
const double PI = 3.14159265358979;
class Point {
public:
double x, y;
Point(double x = 0.0, double y = 0.0) :x(x), y(y) {}
};
class Wind {
public:
double w, a;
};
bool include(double angle, double low, double high) {
if(low < 0.0) {
double l = low + 2.0 * PI;
if(l <= angle) return true;
}
if(high > 2.0 * PI) {
double h = high - 2.0 * PI;
if(angle <= h) return true;
}
return low <= angle && angle <= high;
}
bool savor(const Point &home, const Wind &wind, const vector<Point> &blossom, double d) {
d /= 2.0;
for(const auto &b: blossom) {
Point dif(home.x - b.x, home.y - b.y);
double angle = atan2(dif.y, dif.x);
double dist = hypot(dif.y, dif.x);
if(angle < 0.0) angle = angle + 2.0 * PI;
if(dist <= wind.a && include(angle, wind.w - d, wind.w + d)) return true;
}
return false;
}
double toRadian(double d) {
return d * PI / 180.0;
}
int main() {
int H, R;
while(cin >> H >> R, H | R) {
vector<Point> home(H);
for(auto &i: home) cin >> i.x >> i.y;
int U, M, S;
double du, dm, ds;
cin >> U >> M >> S >> du >> dm >> ds;
du = toRadian(du);
dm = toRadian(dm);
ds = toRadian(ds);
vector<Point> plum(U), peach(M), cherry(S);
for(auto &i: plum) cin >> i.x >> i.y;
for(auto &i: peach) cin >> i.x >> i.y;
for(auto &i: cherry) cin >> i.x >> i.y;
vector<Wind> wind(R);
for(auto &i: wind) cin >> i.w >> i.a;
for(auto &i: wind) i.w = toRadian(i.w);
// solve
vector<int> number(H, 0);
int maxNumber = 0;
for(int i = 0; i < H; ++i) {
for(int day = 0; day < R; ++day) {
bool ok = savor(home[i], wind[day], {Point(0.0, 0.0)}, du);
bool ng = false;
ng |= savor(home[i], wind[day], plum, du);
ng |= savor(home[i], wind[day], peach, dm);
ng |= savor(home[i], wind[day], cherry, ds);
if(!ok || ng) continue;
++number[i];
maxNumber = max(maxNumber, number[i]);
}
}
if(maxNumber) {
bool first = true;
for(int i = 0; i < H; ++i) if(number[i] == maxNumber) {
if(!first) cout << " ";
first = false;
cout << i + 1;
}
cout << endl;
} else {
cout << "NA" << endl;
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long modx(long long Base, long long exponent) {
long long ans = 1;
if (Base == 1) return Base;
while (exponent) {
if (exponent & 1) ans = (ans * Base) % 1000000007;
Base = (Base * Base) % 1000000007;
exponent = exponent >> 1;
}
return ans;
}
long long inmodx(long long num) { return (modx(num, 1000000007 - 2LL)); }
bool cmp() {
bool ans = 0;
return ans;
}
struct ST_Node {
ST_Node() { return; }
void assign_value_(int val) { return; }
void merge_nodes_(ST_Node& left, ST_Node& right) { return; }
};
const int N = (1e3) + 9;
const int M = (N << 1) + 9;
const int LOGN = ((int)log2(N)) + 3;
const int LOGM = ((int)log2(M)) + 3;
long long fact[N], ifact[N];
void pre_fact() {
int i;
fact[0] = ifact[0] = 1LL;
for (i = (1); i < (N); i++) {
fact[i] = (fact[i - 1] * ((long long)i)) % 1000000007;
ifact[i] = inmodx(fact[i]);
}
return;
}
long long choose(int n, int r) {
if (n < 0 || r < 0 || r > n) return 0LL;
long long ans = (fact[n] * ifact[r]) % 1000000007;
ans = (ans * ifact[n - r]) % 1000000007;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
pre_fact();
int n, m, k;
long long ans;
cin >> n >> m >> k;
ans = (choose(n - 1, k + k) * choose(m - 1, k + k)) % 1000000007;
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream& cerr, vector<long long> aux) {
cerr << "[";
for (auto e : aux) cerr << e << ' ';
cerr << "]";
return cerr;
}
const int maxN = 200011;
struct analyzer {
long long sum = 0;
int base = maxN;
long long up[maxN * 2], down[maxN * 2];
void init() {
sum = 0;
base = maxN;
memset(up, 0, sizeof(up));
memset(down, 0, sizeof(down));
}
void insert_up(long long value) {
sum -= 2LL * up[base];
sum += 2LL * down[base - 1];
base--;
up[base + 1] += value;
sum -= value;
}
void insert_down(long long value) {
sum -= 2LL * down[base];
sum += 2LL * up[base + 1];
base++;
down[base - 1] += value;
sum -= value;
}
void remove_up(long long value) {
up[base] -= value;
sum -= value;
}
void remove_down(long long value) {
down[base] -= value;
sum -= value;
}
};
long long m, x;
int n;
long long a[maxN], b[maxN];
vector<pair<long long, int> > order;
int answer[maxN];
analyzer graph;
void remove_duplicates(vector<pair<long long, int> >& order) {
auto aux = order;
vector<pair<long long, int> > plus, minus;
order.clear();
int j;
for (int i = 0; i < aux.size(); i = j) {
plus.clear();
minus.clear();
for (j = i; j < aux.size() && aux[j].first == aux[i].first; j++) {
if (aux[j].second < 0)
minus.push_back(aux[j]);
else
plus.push_back(aux[j]);
}
while (!minus.empty() && !plus.empty()) {
answer[plus.back().second] = -minus.back().second;
minus.pop_back();
plus.pop_back();
}
while (!minus.empty()) {
order.push_back(minus.back());
minus.pop_back();
}
while (!plus.empty()) {
order.push_back(plus.back());
plus.pop_back();
}
}
}
void find_best() {
int cnt = order.size();
pair<long long, int> best = make_pair(1LL << 60, -1);
for (int i = 0; i < cnt; i++)
order.push_back(make_pair(order[i].first + m, order[i].second));
for (int i = 2 * cnt - 1; i >= 0; i--) {
if (i < cnt) {
if (order[i + cnt].second < 0)
graph.remove_down(order[i + cnt].first);
else
graph.remove_up(order[i + cnt].first);
}
if (order[i].second < 0)
graph.insert_down(order[i].first);
else
graph.insert_up(order[i].first);
if (i < cnt) best = min(best, make_pair(graph.sum, i));
}
vector<int> minus, plus;
minus.clear();
plus.clear();
for (int i = 0; i < cnt; i++) {
if (order[best.second + i].second < 0)
minus.push_back(-order[best.second + i].second);
else
plus.push_back(order[best.second + i].second);
}
for (int i = 0; 2 * i < cnt; i++) {
answer[plus[i]] = minus[i];
}
if (cnt == 0) best.first = 0;
printf("%lld\n", best.first);
for (int i = 1; i <= n; i++) printf("%d ", answer[i]);
}
int main() {
scanf("%lld%d", &m, &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
order.push_back(make_pair(a[i], i));
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
order.push_back(make_pair(b[i], -i));
}
sort(order.begin(), order.end());
remove_duplicates(order);
find_best();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long p(long long a, long long b) {
long long rez = 1;
while (b != 0) {
if (b % 2) {
rez *= a;
rez %= 998244353;
}
a = a * a;
a %= 998244353;
b /= 2;
}
return rez;
}
long long inv_mod(long long a) { return p(a, 998244353 - 2); }
long long fact[1000006], n, rez;
void factorial() {
int i;
fact[0] = 1;
for (i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % 998244353;
}
long long comb(long long n, long long k) {
long long rez =
((fact[n] * inv_mod(fact[k])) % 998244353 * inv_mod(fact[n - k])) %
998244353;
return rez;
}
int main() {
int i;
cin >> n;
factorial();
rez = (p(3, n * n) - p(p(3, n) - 3, n) + 998244353) % 998244353;
for (i = 1; i <= n; i++)
if (i % 2) {
rez +=
(comb(n, i) * ((((long long)3 * p(p(3, n - i) - 1, n)) % 998244353 +
((p(3, i) - 3) * p(3, n * n - i * n)) % 998244353) %
998244353)) %
998244353;
rez %= 998244353;
} else {
rez -=
(comb(n, i) * ((((long long)3 * p(p(3, n - i) - 1, n)) % 998244353 +
((p(3, i) - 3) * p(3, n * n - i * n)) % 998244353) %
998244353)) %
998244353;
rez += 998244353;
rez %= 998244353;
}
cout << rez;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct node {
int l;
int r;
int id;
} a[N];
struct tree {
int l;
int r;
int id;
} b[N];
int num[N], ans[N];
bool cmp1(node a, node b) {
if (a.l == b.l) return a.r < b.r;
return a.l < b.l;
}
bool cmp2(tree a, tree b) {
if (a.l == b.l) return a.r < b.r;
return a.l < b.l;
}
set<pair<int, int>> s;
int tot = 1;
int main() {
int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r;
a[i].id = i;
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i].l >> b[i].r >> num[i];
b[i].id = i;
}
sort(a + 1, a + n + 1, cmp1);
sort(b + 1, b + m + 1, cmp2);
s.insert(make_pair(1e9 + 5, 0));
for (int i = 1; i <= n; i++) {
while (tot <= m && b[tot].l <= a[i].l) {
s.insert(make_pair(b[tot].r, b[tot].id));
tot++;
}
auto tmp = *(s.lower_bound(make_pair(a[i].r, 0)));
if (tmp.first == 1e9 + 5) {
cout << "NO" << endl;
return 0;
}
num[tmp.second]--;
ans[a[i].id] = tmp.second;
if (num[tmp.second] == 0) s.erase(tmp);
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
unordered_set<int> g[200005];
bool gone[200005];
void dfs1(int u, int p) {
printf("%d\n", u);
gone[u] = true;
for (auto v : g[u]) {
if (v == p) continue;
dfs1(v, u);
}
}
void dfs(int u, int p) {
if (gone[u]) return;
for (auto itr = g[u].begin(); itr != g[u].end();) {
int v = *itr;
auto nitr = next(itr);
if (v == p) {
itr = nitr;
continue;
}
dfs(v, u);
itr = nitr;
}
if (g[u].size() && (g[u].size() & 1) == 0) {
printf("%d\n", u);
gone[u] = true;
for (auto itr = g[u].begin(); itr != g[u].end();) {
int v = *itr;
auto nitr = next(itr);
g[u].erase(v);
g[v].erase(u);
if (v != p) {
dfs1(v, u);
}
itr = nitr;
}
}
}
int main() {
memset(gone, 0, sizeof(gone));
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int u = i;
int v;
scanf("%d", &v);
if (v == 0) continue;
g[u].insert(v);
g[v].insert(u);
}
if ((n & 1) == 0) {
printf("NO\n");
return (0);
}
printf("YES\n");
dfs(1, 0);
for (int i = 1; i <= n; i++) {
if (gone[i] == false) printf("%d\n", i);
}
return (0);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int main() {
scanf("%d%d", &n, &k);
if (k >= (n * (n - 1)) / 2)
puts("no solution");
else {
for (int i = 0, t = 1; t <= n; t++) printf("%d %d\n", i, t);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma G++ optimize(2)
using namespace std;
inline long long read() {
char ch = ' ', last;
long long ans = 0;
while (ch < '0' || ch > '9') last = ch, ch = getchar();
while (ch >= '0' && ch <= '9') ans = ans * 10 + int(ch - '0'), ch = getchar();
if (last == '-') return -ans;
return ans;
}
void write(long long x) {
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, b[300005], cnt;
double a[300005];
void init() {
n = read();
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] < 0)
if (int(a[i]) == a[i])
b[i] = a[i];
else
b[i] = a[i] - 1;
else
b[i] = a[i];
}
}
int main() {
init();
for (int i = 1; i <= n; i++) cnt += b[i];
int now = 1;
while (cnt) {
if (int(a[now]) != a[now]) b[now]++, cnt++;
now++;
}
for (int i = 1; i <= n; i++) cout << b[i] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
char str[1005][1005];
int n, m;
queue<pair<int, int> > q;
void bfs(int sx, int sy) {
vis[sx][sy] = 1;
q.push(pair<int, int>(sx, sy));
while (!q.empty()) {
pair<int, int> t = q.front();
q.pop();
int x = t.first, y = t.second;
if (x > 1 && str[x - 1][y] == '#' && !vis[x - 1][y]) {
vis[x - 1][y] = 1;
q.push(pair<int, int>(x - 1, y));
}
if (x < n && str[x + 1][y] == '#' && !vis[x + 1][y]) {
vis[x + 1][y] = 1;
q.push(pair<int, int>(x + 1, y));
}
if (y > 1 && str[x][y - 1] == '#' && !vis[x][y - 1]) {
vis[x][y - 1] = 1;
q.push(pair<int, int>(x, y - 1));
}
if (y < m && str[x][y + 1] == '#' && !vis[x][y + 1]) {
vis[x][y + 1] = 1;
q.push(pair<int, int>(x, y + 1));
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", str[i] + 1);
bool v = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (str[i][j] == '#') v = 1;
if (!v) {
puts("0");
return 0;
}
bool r0 = 0, c0 = 0;
for (int i = 1; i <= n; i++) {
int s = 0;
for (int j = 1; j <= m; j++)
if (str[i][j] == '#' && str[i][j - 1] != '#') s++;
if (s > 1) {
puts("-1");
return 0;
}
if (!s) r0 = 1;
}
for (int i = 1; i <= m; i++) {
int s = 0;
for (int j = 1; j <= n; j++)
if (str[j][i] == '#' && str[j - 1][i] != '#') s++;
if (s > 1) {
puts("-1");
return 0;
}
if (!s) c0 = 1;
}
if (r0 ^ c0) {
puts("-1");
return 0;
}
int s = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (str[i][j] == '#' && !vis[i][j]) {
s++;
bfs(i, j);
}
printf("%d\n", s);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
vector<T>& operator--(vector<T>& v) {
for (auto& i : v) --i;
return v;
}
template <typename T>
vector<T>& operator++(vector<T>& v) {
for (auto& i : v) ++i;
return v;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto& i : v) is >> i;
return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto& i : v) os << i << ' ';
return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
os << p.first << ' ' << p.second;
return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return make_pair(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
template <typename T, typename U>
void umin(T& a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
void umax(T& a, U b) {
if (a < b) a = b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, k;
cin >> n >> k;
if (k == 1) {
cout << 1 << '\n';
return 0;
}
if (k == 2) {
cout << n << '\n';
return 0;
}
k -= 2;
long long l = -1, r = n + 10;
while (r - l > 1) {
long long c = (l + r) / 2;
map<long long, long long> m;
function<long long(long long, long long)> count = [&](long long l,
long long r) {
if (l + 1 >= r) return 0ll;
if (m.count(r - l)) return m[r - l];
long long mid = (l + r) / 2;
if (abs(mid - l) < c) return 0ll;
return m[r - l] = count(l, mid) + count(mid, r) + 1;
};
auto cnt = count(1, n);
if (cnt < k)
r = c;
else
l = c;
}
long long d = l;
42;
;
map<pair<long long, long long>, long long> m;
function<long long(long long, long long, long long)> count =
[&](long long l, long long r, long long d) {
if (l + 1 >= r) return 0ll;
if (m.count({r - l, d})) return m[{r - l, d}];
long long mid = (l + r) / 2;
if (abs(mid - l) < d) return 0ll;
return m[{r - l, d}] = count(l, mid, d) + count(mid, r, d) + 1;
};
map<long long, long long> m2;
function<long long(long long, long long)> count2 = [&](long long l,
long long r) {
if (l + 1 >= r) return 0ll;
if (m2.count(r - l)) return m2[r - l];
long long mid = (l + r) / 2;
if (abs(mid - l) < d) return 0ll;
return m2[r - l] = count2(l, mid) + count2(mid, r) + (mid - l == d);
};
k -= count(1, n, d + 1);
function<long long(long long, long long, long long)> get_ans =
[&](long long l, long long r, long long k) {
42;
;
long long mid = (l + r) / 2;
k -= (mid - l == d);
if (k == 0) return mid;
long long cnt = count2(l, mid);
42;
;
if (cnt >= k)
return get_ans(l, mid, k);
else
return get_ans(mid, r, k - cnt);
};
cout << get_ans(1, n, k) << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long INF = 1e18;
const int M = 1e9 + 7;
const int maxn = 3e5;
char s[maxn], t[maxn];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(10);
int T;
cin >> T;
while (T--) {
int n;
cin >> n >> s >> t;
std::vector<int> cs(130), ct(130);
for (int i = 0; i < n; ++i) {
++cs[s[i]];
++ct[t[i]];
}
auto run = [&]() {
bool ok = 0;
for (int i = 'a'; i <= 'z'; ++i) {
if (cs[i] != ct[i]) {
cout << "NO" << endl;
return 0;
}
if (cs[i] > 1) {
ok = 1;
}
}
if (ok) {
cout << "YES" << endl;
return 0;
}
int tot = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (s[i] > s[j]) {
++tot;
}
if (t[i] > t[j]) {
++tot;
}
}
}
if (tot & 1) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
return 0;
};
run();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int sum(const vector<vector<int>>& s, int x0, int y0, int x1, int y1) {
if (y0 > y1 || x0 > x1) return 0;
int result = 0;
for (int i = x0; i <= x1; i++) {
result += s[i][y1];
if (y0) result -= s[i][y0 - 1];
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> f(n);
for (int i = 0; i < n; i++) {
cin >> f[i];
}
vector<vector<int>> a(n, vector<int>(m, 0));
vector<vector<int>> b(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i + 1 < n && f[i][j] == '.' && f[i + 1][j] == '.') {
++a[i][j];
}
if (j + 1 < m && f[i][j] == '.' && f[i][j + 1] == '.') {
++b[i][j];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
a[i][j] += a[i][j - 1];
b[i][j] += b[i][j - 1];
}
}
int qs;
cin >> qs;
for (int i = 0; i < qs; i++) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
--x0, --y0, --x1, --y1;
int result = sum(a, x0, y0, x1 - 1, y1) + sum(b, x0, y0, x1, y1 - 1);
cout << result << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
for (char c = 'a'; c <= 'z'; c++) {
if (count(S.begin(), S.end(), c) % 2 == 1) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > path[200005];
string s;
int a[200005], b[200005], f[200005], ff[200005], n, ans;
int r1() {
if (!b[n]) return -1;
if (b[n] != 1)
for (int i = n - 1;; i--) {
if (b[i] != b[i + 1] - 1) return -1;
if (b[i] == 1) break;
}
for (int i = (1); i <= (n); i++) ff[i] = f[i];
for (int i = b[n] + 1, j = 1; i <= n; i++, j++) {
if (!ff[i]) return -1;
ff[b[j]] = 1;
}
return n - b[n];
}
int r0(int x) {
for (int i = (1); i <= (n); i++) ff[i] = f[i];
for (int i = (1); i <= (x - n); i++) ff[b[i]] = 1;
for (int i = 1, j = x - n + 1; i <= n; i++, j++) {
if (!ff[i]) return 0;
if (j <= n) ff[b[j]] = 1;
}
return 1;
}
int main() {
cin >> n;
for (int i = (1); i <= (n); i++) scanf("%d", &a[i]), f[a[i]] = 1;
for (int i = (1); i <= (n); i++) scanf("%d", &b[i]);
int ans = r1();
if (ans != -1) {
cout << ans << endl;
return 0;
}
int l = n, r = n * 2;
while (l <= r) {
int mid = (l + r) / 2;
if (r0(mid)) {
r = mid - 1;
ans = mid;
} else
l = mid + 1;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << "\033[33;0m" << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
template <typename T>
void debug_out(T a[], int n) {
cerr << "{" << to_string(a[0]);
for (int i = 1; i < n; ++i) cerr << ", " << to_string(a[i]);
cerr << "}";
debug_out();
}
using LL = long long;
using PII = pair<int, int>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
LL powmod(LL a, LL b, LL MOD) {
LL ans = 1;
while (b) {
if (b & 1) ans = ans * a % MOD;
b >>= 1;
a = a * a % MOD;
}
return ans % MOD;
}
const int N = 100005, MOD = 1e9 + 7, INF = 1e9;
string s, ansS, ansT;
int cnt1[10], cnt2[10], cnt[10];
void init() {
cin >> s;
for (int i = 0; i < ((int)(s.size())); ++i) cnt[s[i] - '0']++;
}
void work() {
for (int i = 0; i < 10; ++i) cnt1[i] = cnt2[i] = cnt[i];
int Sum = 0, S;
for (int i = 0; i < 10; ++i) {
while (cnt1[i]) --cnt1[i], ansS.push_back(i + '0');
while (cnt2[i]) --cnt2[i], ansT.push_back(i + '0');
}
for (int ed = 1; ed <= 9; ++ed) {
int p1 = ed, p2 = (10 - p1) % 10;
S = 0;
string nowS, nowT;
for (int i = 0; i < 10; ++i) cnt1[i] = cnt2[i] = cnt[i];
if (cnt1[p1] and cnt2[p2]) {
nowS.push_back(p1 + '0');
nowT.push_back(p2 + '0');
cnt1[p1]--;
cnt2[p2]--;
++S;
} else {
continue;
}
for (int d = 0; d <= 9; ++d) {
p1 = d, p2 = (9 - d);
while (cnt1[p1] and cnt2[p2]) {
nowS.push_back(p1 + '0');
nowT.push_back(p2 + '0');
cnt1[p1]--;
cnt2[p2]--;
++S;
}
swap(p1, p2);
while (cnt1[p1] and cnt2[p2]) {
nowS.push_back(p1 + '0');
nowT.push_back(p2 + '0');
cnt1[p1]--;
cnt2[p2]--;
++S;
}
}
for (int i = 0; i < 10; ++i) {
while (cnt1[i]) --cnt1[i], nowS.push_back(i + '0');
while (cnt2[i]) --cnt2[i], nowT.push_back(i + '0');
}
if (S > Sum) ansS = nowS, ansT = nowT, Sum = S;
}
S = 0;
int qwq = -1, id = -1, Max = 0;
cerr << "\033[33;1m"
<< "["
<< "cnt, 10"
<< "] : ",
debug_out(cnt, 10);
for (int i = 1; i <= 9; ++i) {
S += min(cnt[i], cnt[9 - i]);
if (cnt[i] > cnt[9 - i] and cnt[10 - i] > cnt[9 - (10 - i)] and i != 1)
qwq = i;
else if (cnt[9 - i] > cnt[i] and
cnt[10 - (9 - i)] > cnt[9 - (10 - (9 - i))])
qwq = 9 - i;
}
if (cnt[5] > cnt[4]) qwq = 5;
if (qwq == -1) {
for (int i = 1; i <= 4; ++i) {
if (cnt[i] > cnt[9 - i] and cnt[10 - i])
qwq = i;
else if (cnt[9 - i] > cnt[i] and cnt[10 - (9 - i)])
qwq = 9 - i;
}
if (qwq != -1) S--;
}
for (int p = 1; p <= cnt[0]; ++p) {
int now = S;
if (qwq != -1) {
now = now + p + min(cnt[9], cnt[0] - p) + 1;
} else {
now = now + p + min(cnt[9], cnt[0] - p);
}
if (now > Max) Max = now, id = p;
}
cerr << "\033[33;1m"
<< "["
<< "S"
<< "] : ",
debug_out(S);
cerr << "\033[33;1m"
<< "["
<< "qwq"
<< "] : ",
debug_out(qwq);
if (Max > Sum and qwq != -1) {
string nowS, nowT;
for (int i = 0; i < 10; ++i) cnt1[i] = cnt2[i] = cnt[i];
for (int d = 1; d <= id; ++d) {
nowS.push_back('0');
nowT.push_back('0');
cnt1[0]--, cnt2[0]--;
}
if (qwq != -1) {
assert(qwq != 0);
assert(qwq != 9);
assert(qwq != 10);
nowS.push_back(qwq + '0');
nowT.push_back(10 - qwq + '0');
cnt1[qwq]--, cnt2[10 - qwq]--;
}
for (int d = 0; d <= 9; ++d) {
int p1 = d, p2 = (9 - d);
while (cnt1[p1] and cnt2[p2]) {
nowS.push_back(p1 + '0');
nowT.push_back(p2 + '0');
cnt1[p1]--;
cnt2[p2]--;
++S;
}
swap(p1, p2);
while (cnt1[p1] and cnt2[p2]) {
nowS.push_back(p1 + '0');
nowT.push_back(p2 + '0');
cnt1[p1]--;
cnt2[p2]--;
++S;
}
}
for (int i = 0; i < 10; ++i) {
while (cnt1[i]) --cnt1[i], nowS.push_back(i + '0');
while (cnt2[i]) --cnt2[i], nowT.push_back(i + '0');
}
ansS = nowS, ansT = nowT;
Sum = Max;
}
cerr << "\033[33;1m"
<< "["
<< "Max"
<< "] : ",
debug_out(Max);
reverse(ansS.begin(), ansS.end());
reverse(ansT.begin(), ansT.end());
cout << ansS << "\n" << ansT << endl;
}
int main() {
time_t tic = clock();
int T = 1;
while (T--) {
init();
work();
}
time_t toc = clock();
cerr << "execute time = " << (toc - tic) / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 2 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string str;
getline(cin, str);
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout << str << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int b) {
for (int i{2}; i < (b / 2) + 1; ++i) {
if (b % i == 0) {
return false;
}
}
return true;
}
void solve() {
int a, b;
cin >> a >> b;
if (!is_prime(b)) {
cout << "NO";
return;
}
for (int i{a + 1}; i < b; ++i) {
if (is_prime(i)) {
cout << "NO";
return;
}
}
cout << "YES";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
const int MOD = 1000000007;
vector<long long> al[N], sum[N];
vector<pair<int, int> > adj[N];
pair<int, int> p[N];
int cnt(int u, long long ls) {
return upper_bound(al[u].begin(), al[u].end(), ls) - al[u].begin();
}
long long hp(int u, long long h, long long ls) {
long long c1 = cnt(u, ls);
if (c1 == 0) return 0;
return h * c1 - sum[u][c1 - 1];
}
int n, k, l[N], q, a, h;
void dfs(int u, long long cost) {
al[u].push_back(0);
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i].first;
dfs(v, cost + adj[u][i].second);
for (int k = 0; k < al[v].size(); ++k) {
al[u].push_back(al[v][k] + adj[u][i].second);
}
}
sort(al[u].begin(), al[u].end());
for (int i = 0; i < al[u].size(); ++i) {
sum[u].push_back((i ? sum[u].back() : 0) + (al[u][i]));
}
}
int main() {
memset(p, -1, sizeof p);
scanf("%d%d", &n, &q);
for (int i = 1; i <= n - 1; ++i) {
scanf("%d", &l[i]);
int u = (i + 1) / 2;
int v = (i + 1);
p[v] = make_pair(u, l[i]);
adj[u].push_back(make_pair(v, l[i]));
}
dfs(1, 0);
while (q--) {
scanf("%d%d", &a, &h);
long long cur = a, prev = -1, rem = h;
long long add = 0;
while (1) {
add += hp(cur, rem, rem);
if (prev != -1) {
long long kk = rem - p[prev].second;
add -= hp(prev, kk, kk);
}
prev = cur;
rem = rem - p[cur].second;
cur = p[cur].first;
if (cur == -1 || rem < 0) break;
}
printf("%lld\n", add);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
int arr[n][m];
int mx = 0;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
int p, l;
int row[n];
for (int i = 0; i < n; i++) {
row[i] = 0;
int curr = 0;
for (int j = 0; j < m; j++) {
if (arr[i][j] == 1) curr++;
row[i] = max(row[i], curr);
if (arr[i][j] == 0) curr = 0;
}
}
for (int i = 0; i < q; i++) {
cin >> p >> l;
arr[p - 1][l - 1] = !(arr[p - 1][l - 1]);
int curr = 0;
row[p - 1] = 0;
for (int j = 0; j < m; j++) {
if (arr[p - 1][j] == 1) curr++;
row[p - 1] = max(row[p - 1], curr);
if (arr[p - 1][j] == 0) curr = 0;
}
mx = 0;
for (int j = 0; j < n; j++) {
mx = max(row[j], mx);
}
cout << mx << endl;
}
}
| 2 |
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <stdio.h>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
string S;
cin >> S;
int X = 0;
int Y = 0;
for (int i = 0; i < N; i++) {
if ('A' <= S[i] && S[i] <= 'M') {
X++;
}
else if ('N' <= S[i] && S[i] <= 'Z') {
X--;
}
else if ('a' <= S[i] && S[i] <= 'm') {
Y++;
}
else if ('n' <= S[i] && S[i] <= 'z') {
Y--;
}
}
string res;
if (X > 0) {
for (int i = 0; i < X; i++) {
res.push_back('A');
}
}
else {
for (int i = 0; i < -X; i++) {
res.push_back('N');
}
}
if (Y > 0) {
for (int i = 0; i < Y; i++) {
res.push_back('a');
}
}
else {
for (int i = 0; i < -Y; i++) {
res.push_back('n');
}
}
cout << res.size() << endl;
cout << res << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
string s;
cin >> s;
long long ans = INT_MAX;
long long a[3][n + 1];
memset(a, 0, sizeof(a));
for (long long i = 0; i < 3; i++) {
char ch[3] = {'R', 'G', 'B'};
long long num = i;
for (long long j = 0; j < n; j++) {
if (s[j] != ch[num % 3])
a[i][j + 1] = a[i][j] + 1;
else
a[i][j + 1] = a[i][j];
num++;
}
}
for (long long i = 0; i < 3; i++) {
for (long long j = n; j >= k; j--) ans = min(ans, a[i][j] - a[i][j - k]);
}
cout << ans << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> k >> n;
if ((long long)n * (n - 1) < k) {
cout << "NO\n";
return 0;
}
cout << "YES\n";
int a = 1, b = 2;
for (int i = 0; i < k; ++i) {
cout << a << ' ' << b << '\n';
swap(a, b);
if (i & 1) {
if (b == n) {
++a;
b = a + 1;
} else {
++b;
}
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long X, D, N, r, cnt;
long long A[200010], L[200010], R[200010], res[200010];
map<long long, bool> seen;
map<long long, vector<pair<long long, long long>>> M;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> X >> D;
if(D == 0 && X == 0){
cout << 1 << endl; return 0;
}
else if(D == 0){
cout << N + 1 << endl; return 0;
}
if(D < 0){
X *= -1; D *= -1;
}
r = (X + D * 100000000) % D;
for(int i = 1; i <= N; i++) A[i] = X + D * (i-1);
for(int i = 1; i <= N; i++) L[i] = L[i-1] + A[i];
for(int i = 1; i <= N; i++) R[i] = R[i-1] + A[N+1-i];
for(int i = 0; i <= N; i++){
res[i] = (r * i) % D;
M[res[i]].push_back(make_pair(L[i], R[i]));
}
for(int i = 0; i <= N; i++){
if(seen[res[i]]) continue;
sort(M[res[i]].begin(), M[res[i]].end());
seen[res[i]] = true;
long long right = -1e18;
for(auto p : M[res[i]]){
long long x = p.first, y = p.second;
if(right < x){
cnt += (y - x) / D + 1; right = y;
}
else if(right <= y){
cnt += (y - right) / D; right = y;
}
}
}
cout << cnt << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) {
return n | (1LL << i);
;
}
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long modPow(long long b, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1;
}
return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
return modMul(a, modInverse(b));
}
struct edge {
int u, v, w;
};
bool cmp(const edge &a, const edge &b) { return a.w < b.w; }
string to_s(int t) {
stringstream ss;
ss << t;
return ss.str();
}
struct {
int bit, status;
} t[150000 * 3];
long long a[150000], n, m;
void inti(int no, int i, int j) {
if (i == j) {
t[no].bit = a[i];
t[no].status = 0;
return;
}
int l = no << 1, r = l + 1, m = (i + j) >> 1;
inti(l, i, m);
inti(r, m + 1, j);
if (t[l].status == 0 && t[r].status == 0) {
t[no].bit = (t[l].bit | t[r].bit);
t[no].status = 1;
} else if (t[l].status == 1 && t[r].status == 1) {
t[no].bit = (t[l].bit ^ t[r].bit);
t[no].status = 0;
}
}
void update(int no, int i, int j, int x, int p) {
if (x > j || x < i) return;
if (i == j) {
if (i == x) {
t[no].bit = p;
t[no].status = 0;
return;
}
return;
}
int l = no << 1, r = l + 1, m = (i + j) >> 1;
update(l, i, m, x, p);
update(r, m + 1, j, x, p);
if (t[l].status == 0 && t[r].status == 0) {
t[no].bit = (t[l].bit | t[r].bit);
t[no].status = 1;
} else if (t[l].status == 1 && t[r].status == 1) {
t[no].bit = (t[l].bit ^ t[r].bit);
t[no].status = 0;
}
}
int main() {
cin >> n >> m;
int l = 1;
for (int i = 0; i < n; i++) l *= 2;
for (int i = 1; i <= l; i++) cin >> a[i];
inti(1, 1, l);
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
update(1, 1, l, x, y);
cout << t[1].bit << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
long m[100001];
int main() {
long n, p, a, i;
scanf("%ld", &n);
p = 0;
for (i = 1; i <= n; i++) scanf("%ld", &m[i]);
p = 0;
for (i = n; i > 1; i--) {
if (m[i - 1] > m[i]) {
p = i - 1;
break;
}
}
printf("%ld", p);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char s[n][m];
int a[n][m], t = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) a[i][j] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> s[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
if (k == j) continue;
if (s[i][j] == s[i][k]) {
a[i][k]++;
t++;
}
}
for (int k = 0; k < n; k++) {
if (k == i) continue;
if (s[i][j] == s[k][j]) {
a[k][j]++;
t++;
}
}
if (t > 0) a[i][j]++;
t = 0;
}
}
string ans = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 0) cout << s[i][j];
}
}
}
| 2 |
#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
std::string s;
std::cin >> s;
int q = ri();
for (int i = 0; i < q; i++) {
int k = ri();
std::deque<std::pair<int, int> > ds;
int64_t sum = 0;
int added = 0;
int64_t res = 0;
for (int j = 0; j < n; j++) {
if (ds.size() && ds.front().second <= j - k) sum -= added + ds.front().first, ds.pop_front();
if (s[j] == 'D') ds.push_back({-added, j});
else if (s[j] == 'M') added++, sum += ds.size();
else if (s[j] == 'C') res += sum;
}
printf("%" PRId64 "\n", res);
}
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int A,B,C;
int beki(int n){
int count = 0;
while(n%2==0){
count++;
n /= 2;
}
return count;
}
int main(){
cin >> A >> B >> C;
if(A%2==1 || B%2==1 || C%2==1) cout << 0 << endl;
else if(A==B && B==C) cout << -1 << endl;
else{
int ans = 1e9;
cout << min(beki(A+B),min(beki(B+C),beki(C+A))) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
long long dd(long long x, long long y, long long z) {
return (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x);
}
void gao(const vector<int>& vr, const vector<int>& vg, const vector<int>& vb,
int r, int g, int b) {
for (auto v : vr) {
auto it = lower_bound(vg.begin(), vg.end(), v);
if (it != vg.end()) {
int v2 = *it;
auto it3 = lower_bound(vb.begin(), vb.end(), (v + v2) / 2);
if (it3 != vb.end()) ans = min(ans, dd(v, v2, *it3));
if (it3 != vb.begin()) ans = min(ans, dd(v, v2, *(--it3)));
}
if (it != vg.begin()) {
int v2 = *(--it);
auto it3 = lower_bound(vb.begin(), vb.end(), (v + v2) / 2);
if (it3 != vb.end()) ans = min(ans, dd(v, v2, *it3));
if (it3 != vb.begin()) ans = min(ans, dd(v, v2, *(--it3)));
}
}
}
void solve() {
ans = 9e18;
int r, g, b;
scanf("%d%d%d", &r, &g, &b);
vector<int> vr(r), vg(g), vb(b);
for (int i = 0; i < r; ++i) scanf("%d", &vr[i]);
for (int i = 0; i < g; ++i) scanf("%d", &vg[i]);
for (int i = 0; i < b; ++i) scanf("%d", &vb[i]);
sort(vr.begin(), vr.end());
sort(vg.begin(), vg.end());
sort(vb.begin(), vb.end());
gao(vr, vg, vb, r, g, b);
gao(vr, vb, vg, r, b, g);
gao(vb, vr, vg, b, r, g);
gao(vb, vg, vr, b, g, r);
gao(vg, vr, vb, g, r, b);
gao(vg, vb, vr, g, b, r);
printf("%lld\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S, T, U;
cin >> S >> T >> U;
cout << S.at(0) << T.at(1) << U.at(2);
} | 0 |
#include<bits/stdc++.h>
using namespace std;
#define i64 long long int
#define ran 101111
i64 pos[ran], cnt[ran], s;
int n;
int o[ran], lo;
i64 AB(i64 x){return x>=0?x:-x;}
int main(){
ios::sync_with_stdio(false);
cin >> n >> s;
for(int i=0; i<n; i++)
cin >> pos[i] >> cnt[i];
int L = 0, R = n-1;
i64 totLef = cnt[L], totRig = cnt[R];
while(true){
if(pos[R] < s){
for(int i=L; i<=R; i++)
o[lo++] = i;
break;
}
if(pos[L] > s){
for(int i=R; i>=L; i--)
o[lo++] = i;
break;
}
if(totLef >= totRig){
totLef += totRig;
o[lo++] = R;
R --;
totRig = cnt[R];
}else{
totRig += totLef;
o[lo++] = L;
L++;
totLef = cnt[L];
}
}
reverse(o,o+lo);
i64 ret = 0;
for(int i=0; i<lo; i++){
ret += AB(pos[o[i]] - s);
s = pos[o[i]];
}
cout << ret << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tttt;
cin >> tttt;
for (int loop = 0; loop < (tttt); ++loop) {
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
int flag = 0;
int now = 0;
for (int i = 0; i < (n); ++i) {
if (now == m) break;
if (s[i] == t[now]) now++;
}
if (now == m) flag = 1;
for (int i = 1; i < m; ++i) {
if (flag) break;
int dp[n][i + 1];
for (int j = 0; j < (n); ++j)
for (int k = 0; k < (i + 1); ++k) dp[j][k] = -1;
for (int j = 0; j < (n); ++j) {
for (int k = 0; k < (i + 1); ++k) {
if (k > j + 1) break;
if (j == 0) {
if (k == 0) {
dp[j][k] = i;
if (s[j] == t[i]) dp[j][k] = i + 1;
} else {
if (s[j] == t[0]) dp[j][k] = i;
}
} else {
dp[j][k] = dp[j - 1][k];
if (dp[j][k] != -1 && dp[j][k] < m) {
if (s[j] == t[dp[j][k]]) dp[j][k]++;
}
if (k > 0) {
if (dp[j - 1][k - 1] != -1 && s[j] == t[k - 1]) {
dp[j][k] = max(dp[j][k], dp[j - 1][k - 1]);
}
}
}
}
}
if (dp[n - 1][i] == m) flag = 1;
}
if (flag) {
puts("YES");
} else {
puts("NO");
};
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, cnt = -1, h, hx;
int main() {
cin >> n;
while (n--) {
cin >> h;
if (hx > h) {
cnt += hx - h;
hx = h;
}
cnt += h - hx;
hx = h;
cnt += 2;
}
cout << cnt;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct zxc {
long long ces, doz, aql;
};
long long n, ans, used[100001];
zxc a[100001];
vector<long long> g;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].aql >> a[i].doz >> a[i].ces;
}
for (int i = 0; i < n; i++) {
if (used[i]) continue;
long long cry = a[i].aql, u = 0;
for (int j = i + 1; j < n; j++) {
if (used[j]) continue;
a[j].ces -= max(u, cry);
cry--;
}
long long sm = 0;
for (int j = i + 1; j < n; j++) {
if (used[j]) continue;
a[j].ces -= sm;
if (a[j].ces < 0) {
used[j] = 1;
sm += a[j].doz;
}
}
ans++;
g.push_back(i + 1);
}
cout << ans << endl;
for (int i = 0; i < g.size(); i++) cout << g[i] << " ";
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, t;
cin >> t;
while (t--) {
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) {
cin >> v[i];
}
if (n == 2) {
cout << 0 << endl;
} else {
sort(v.begin(), v.end());
long long int mn = min(v[n - 2], v[n - 1]);
if (n - 1 >= mn) {
cout << mn - 1 << endl;
} else {
cout << n - 2 << endl;
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
const int n = 8;
bitset<8> my;
int f(int a, int b) {
int rez = 1;
if (b == 0) return 1;
while (b--) {
rez = ((rez * a) % 1000003);
}
return rez;
}
void f2(int a) {
int k = 7;
unsigned i, j = 1 << 8 - 1;
for (i = 0; i < 8; ++i) {
my[i] = ((a & j) == 0 ? 0 : 1);
j >>= 1;
}
}
int main() {
string s;
getline(cin, s);
int otn = 0;
int h = 0;
for (int i = 0; i < s.length(); i++) {
f2(s[i]);
int f = my.to_ulong();
ans.push_back((otn - my.to_ulong()) % 256);
otn = my.to_ulong();
h++;
my.reset();
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long p, q;
cin >> p >> q;
vector<long long> factors;
long long temp = q;
for (int i = 2; i * i <= temp; i++) {
if (temp % i == 0) {
while (temp % i == 0) {
temp /= i;
}
factors.push_back(i);
}
}
if (temp > 1) {
factors.push_back(temp);
}
long long x = 1;
for (long long a : factors) {
temp = p;
while (temp % q == 0) {
temp /= a;
}
x = max(x, temp);
}
cout << x << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
long long power(long long b, long long e, long long m) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b % m, e / 2, m) % m;
return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << '(' << p.first << "," << p.second << ')';
}
const int MOD = 1000000007;
struct Mint {
int val;
Mint(long long v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = v;
}
static int mod_inv(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r;
swap(g, r);
x -= q * y;
swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
Mint &operator+=(const Mint &other) {
val += other.val;
if (val >= MOD) val -= MOD;
return *this;
}
Mint &operator-=(const Mint &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
return x % m;
unsigned x_high = x >> 32, x_low = (unsigned)x;
unsigned quot, rem;
asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
return rem;
}
Mint &operator*=(const Mint &other) {
val = fast_mod((uint64_t)val * other.val);
return *this;
}
Mint &operator/=(const Mint &other) { return *this *= other.inv(); }
friend Mint operator+(const Mint &a, const Mint &b) { return Mint(a) += b; }
friend Mint operator-(const Mint &a, const Mint &b) { return Mint(a) -= b; }
friend Mint operator*(const Mint &a, const Mint &b) { return Mint(a) *= b; }
friend Mint operator/(const Mint &a, const Mint &b) { return Mint(a) /= b; }
Mint &operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
Mint &operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
Mint operator++(int32_t) {
Mint before = *this;
++*this;
return before;
}
Mint operator--(int32_t) {
Mint before = *this;
--*this;
return before;
}
Mint operator-() const { return val == 0 ? 0 : MOD - val; }
bool operator==(const Mint &other) const { return val == other.val; }
bool operator!=(const Mint &other) const { return val != other.val; }
Mint inv() const { return mod_inv(val); }
Mint power(long long p) const {
assert(p >= 0);
Mint a = *this, result = 1;
while (p > 0) {
if (p & 1) result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend ostream &operator<<(ostream &stream, const Mint &m) {
return stream << m.val;
}
friend istream &operator>>(istream &stream, Mint &m) {
return stream >> m.val;
}
};
const int N = 202;
int a[N], n, k;
Mint dp[N][N][1002];
int _runtimeTerror_() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + n + 1);
dp[0][0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int l = 0; l <= k; ++l) {
Mint u = dp[i - 1][j][l];
int d = j * (a[i] - a[i - 1]);
if (l + d > k) continue;
dp[i][j][l + d] += j * u;
dp[i][j][l + d] += u;
dp[i][j - 1][l + d] += j * u;
dp[i][j + 1][l + d] += u;
}
}
}
Mint ans = 0;
for (int l = 0; l <= k; ++l) ans += dp[n][0][l];
cout << ans << "\n";
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) _runtimeTerror_();
return 0;
}
| 6 |
#include<iostream>
#include<string>
using namespace std;
int main (){
string n;
int i;
for(i=0;i<=2;i++){
cin>>n;
cout<<n.substr(i,1);}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef complex<double> Vec;
ll N, K;
vector<ll> A;
ll Sum;
set<ll> Divs;
ll R;
int main() {
cin >> N >> K;
A.resize(N);
for (ll i = 0; i < N; ++i) {
cin >> A[i];
Sum += A[i];
}
for (ll i = 1; i * i <= Sum; ++i) {
if (Sum % i == 0) {
Divs.insert(i);
Divs.insert(Sum / i);
}
}
for (ll div : Divs) {
vector<ll> rems;
ll diff = 0, k = 0;
for (ll a : A) {
ll rem = a % div;
rems.push_back(rem);
diff -= rem;
k += rem;
}
assert(diff % div == 0);
sort(rems.begin(), rems.end(), greater<ll>());
for (ll rem : rems) {
if (diff == 0) break;
diff += div;
k -= rem;
}
if (k <= K) R = max(R, div);
}
cout << R << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 1e5 + 5;
int n, K;
long long ans;
int cnta, a[Max_n];
vector<int> buk[Max_n], c[Max_n];
struct robot {
int x, r, q, pos;
} k[Max_n];
bool cmp(robot a, robot b) { return a.q == b.q ? a.x < b.x : a.q < b.q; }
bool cmp2(robot a, robot b) { return a.r > b.r; }
void add(int x, int k) {
for (int i = k, lim = c[x].size(); i < lim; i += i & -i) c[x][i]++;
}
int query(int x, int k) {
int ans = 0;
for (int i = k; i; i -= i & -i) ans += c[x][i];
return ans;
}
int main() {
n = read(), K = read();
for (int i = 1; i <= n; i++)
k[i].x = read(), k[i].r = read(), k[i].q = read();
sort(k + 1, k + n + 1, cmp);
for (int i = 1; i <= n; i++) c[i].push_back(0);
k[0].q = -1;
for (int i = 1; i <= n + 1; i++) {
if (k[i].q != k[i - 1].q) a[cnta++] = k[i - 1].q;
buk[cnta].push_back(k[i].x);
c[cnta].push_back(0);
k[i].pos = buk[cnta].size() - 1;
}
cnta--;
sort(k + 1, k + n + 1, cmp2);
for (int i = 1; i <= n; i++) {
int l = upper_bound(a + 1, a + cnta + 1, k[i].q - K - 1) - a;
int r = upper_bound(a + 1, a + cnta + 1, k[i].q + K) - a - 1;
if (a[l] > k[i].q + K || a[r] < k[i].q - K) continue;
int L = k[i].x - k[i].r, R = k[i].x + k[i].r;
for (int j = l; j <= r; j++) {
int rr =
upper_bound(buk[j].begin(), buk[j].end(), R) - buk[j].begin() - 1;
int ll =
upper_bound(buk[j].begin(), buk[j].end(), L - 1) - buk[j].begin();
if (buk[j][ll] > R || buk[j][rr] < L) continue;
ans += query(j, rr + 1) - query(j, ll);
}
add(lower_bound(a + 1, a + cnta + 1, k[i].q) - a, k[i].pos + 1);
}
printf("%lld\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[10000];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[i] = make_pair(x, y);
}
sort(a, a + n);
int cur = -1;
for (int i = 0; i < n; i++) {
if (a[i].second >= cur) {
cur = a[i].second;
} else
cur = a[i].first;
}
cout << cur;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[5010];
int dp[5010];
int ans = 0;
signed main() {
std::ios::sync_with_stdio(false);
cin >> s + 1;
int n = strlen(s + 1);
for (int i = 1; i <= n; i++) {
int ff = 0;
int dd = 0;
int pp = 0;
if (s[i] == ')')
dp[i] = -1, pp++;
else {
if (s[i] == '(')
dp[i] = 1, dd++;
else
ff++;
}
for (int j = i + 1; j <= n; j++) {
if (dp[j - 1] == -1) break;
if (s[j] == ')') {
dp[j] = max(dp[j - 1] - 1, 0);
pp++;
if (ff + dd < pp) dp[j] = -1;
if (dp[j] != 0) continue;
if (!((ff - (dd - pp)) & 1) && !((j - i + 1) & 1)) ans++;
}
if (s[j] == '(') dp[j] = dp[j - 1] + 1, dd++;
if (s[j] == '?') {
dp[j] = max(dp[j - 1] - 1, 0);
ff++;
if (dp[j] != 0) continue;
if (!((ff - (dd - pp)) & 1) && !((j - i + 1) & 1)) ans++;
}
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include<iostream>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
int main()
{
int a=0,b=0;
map<int,int> p,n;
string in;
while(getline(cin,in))
{
if(in=="") break;
int cnt=0;
a=0; b=0;
while(in[cnt]!=',')
{
a*=10;
a+=in[cnt]-'0';
cnt++;
}
cnt++;
for(int i = cnt; i < in.size(); ++i)
{
b*=10;
b+=in[i]-'0';
}
if(p.count(a)==0)
p.insert(map<int,int>::value_type(a,1));
else p[a]++;
}
while(scanf("%d,%d",&a,&b)!=EOF)
{
if(n.count(a)==0)
n.insert(map<int,int>::value_type(a,1));
else n[a]++;
}
map<int,int>::iterator it=p.begin();
while(it!=p.end())
{
if(n.count((*it).first)==1)
{
printf("%d %d\n",(*it).first,(*it).second+n[(*it).first]);
}
it++;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const bool debug = 0;
inline void read(int &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
x = flag ? -x : x;
}
inline void write(int x) {
static const int maxlen = 100;
static char s[maxlen];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
int len = 0;
for (; x; x /= 10) s[len++] = x % 10 + '0';
for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int MAXN = 310;
int n, m;
int g[MAXN][MAXN];
int bit[MAXN];
int du[100];
int get_siz(int x) {
int cnt = 0;
for (int i = 0; i <= 10; i++)
if (x & (1 << i)) cnt++;
return cnt;
}
int main() {
read(n);
read(m);
for (int i = 1; i <= m; i++) {
int a, b;
read(a);
read(b);
g[a][b] = 1;
g[b][a] = 1;
bit[a] |= (1 << (b - 1));
bit[b] |= (1 << (a - 1));
du[a]++;
du[b]++;
}
if (n < 7) {
printf("%d\n", m);
return 0;
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
ans = max(ans, m - du[i] - du[j] + get_siz(bit[i] | bit[j]));
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[10];
vector<string> res;
vector<int> fields;
int b[10];
void gao(const char* s, int len) {
int depth = (int)fields.size();
if ((4 - depth) * 3 < len) {
return;
}
if (depth == 4) {
if (len == 0) {
char buf[1024];
sprintf(buf, "%d.%d.%d.%d", fields[0], fields[1], fields[2], fields[3]);
res.push_back(buf);
}
} else {
if (s[0] == '0') {
fields.push_back(0);
gao(s + 1, len - 1);
fields.pop_back();
} else {
int value = 0;
for (int i = 1; i <= 3 && i <= len; ++i) {
value = value * 10 + s[i - 1] - '0';
if (value < 256) {
fields.push_back(value);
gao(s + i, len - i);
fields.pop_back();
}
}
}
}
}
void dfs(char* s, int len, int mask) {
if (mask == (1 << n) - 1) {
for (int i = 0; i < len; ++i) {
s[-i] = s[i];
}
gao(s - len + 1, len * 2 - 1);
for (int i = 0; i < len; ++i) {
s[-i - 1] = s[i];
}
gao(s - len, len * 2);
}
if (len < 6) {
for (int i = 0; i < n; ++i) {
s[len] = '0' + a[i];
dfs(s, len + 1, mask | 1 << i);
}
}
}
int main() {
char buf[1024];
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
b[a[i]] = i;
}
dfs(buf + 512, 0, 0);
printf("%d\n", (int)res.size());
for (int i = 0; i < (int)res.size(); ++i) {
printf("%s\n", res[i].c_str());
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int a, int b) { return (a + b) % MOD; }
int sub(int a, int b) { return (a - b + MOD) % MOD; }
int mul(int a, int b) { return ((long long)a * b) % MOD; }
int inv(int a) {
int b = 1;
for (int i = 0; (1 << i) <= (MOD - 2); i++) {
if ((1 << i) & (MOD - 2)) b = mul(b, a);
a = mul(a, a);
}
return b;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int X = 0;
for (char c : s)
if (c == '?') X++;
vector<int> fact(X + 1);
fact[0] = 1;
for (int i = 1; i <= X; i++) fact[i] = mul(fact[i - 1], i);
vector<int> sum1(X + 2);
sum1[X + 1] = 0;
for (int i = X; i >= 0; i--) {
sum1[i] = mul(fact[X], inv(mul(fact[i], fact[X - i])));
sum1[i] = add(sum1[i], sum1[i + 1]);
}
vector<int> sum2(X + 1);
sum2[X] = 0;
for (int i = X - 1; i >= 0; i--) {
sum2[i] = mul(fact[X - 1], inv(mul(fact[i], fact[X - 1 - i])));
sum2[i] = add(sum2[i], sum2[i + 1]);
}
int A = 0, B = 0, C = 0, sol = 0;
for (char c : s)
if (c == ')') C++;
for (char c : s) {
if (c == '(') {
A++;
int T = max(0, min(X + 1, A + B - C));
sol = add(sol, sum1[T]);
} else if (c == '?') {
B++;
int T = max(0, min(X, A + B - C));
sol = add(sol, sum2[T]);
} else if (c == ')') {
C--;
}
}
cout << sol;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
int a[5010];
int dfs(int l, int r, int now) {
int i, ans = 0;
int h = INF;
if (l == r) return 0;
for (i = l; i < r; i++) h = min(h, a[i]);
ans += h - now;
int L = l;
for (i = l; i < r; i++) {
if (a[i] == h) {
ans += dfs(L, i, h);
L = i + 1;
}
}
ans += dfs(L, r, h);
return min(r - l, ans);
}
int main() {
int i, n;
while (scanf("%d", &n) == 1) {
int m = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int ans = dfs(0, n, 0);
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
char str[maxn + 5];
int n;
bool ok(int a, int b) {
int x = 0, y = 0;
for (int i = a; i <= b; i++) {
if (str[i] == 'U') {
x--;
} else if (str[i] == 'D') {
x++;
} else if (str[i] == 'L') {
y--;
} else if (str[i] == 'R') {
y++;
}
}
return x == 0 && y == 0;
}
int main() {
scanf("%d\n%s", &n, str);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (ok(i, j)) ans++;
}
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
long long n, m, k, p[maxn], cur, lst, nww, ans;
int main() {
scanf("%lld%lld%lld", &n, &m, &k);
for (int i = 1; i <= m; i++) scanf("%lld", p + i);
cur = 1;
while (cur <= m) {
lst = cur - 1;
nww = (p[cur] - lst + k - 1) / k;
for (; ((p[cur] - lst + k - 1) / k) == nww; cur++)
;
ans++;
}
printf("%lld\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
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 c = 0, m = 0;
for (long long int i = 0; i < n; i++) {
if (a[i] == 0) {
c++;
m = max(m, c);
} else {
c = 0;
}
}
c = 0;
for (long long int i = 0; i < n - 1; i++) {
if (a[i] == 0 && a[i + 1] == 0) {
continue;
}
long long int x = a[i], y = a[i + 1], z = 2;
for (long long int j = i + 2; j < n; j++) {
if (a[j] == x + y) {
z++;
x = y;
y = a[j];
} else {
m = max(z, m);
break;
}
}
m = max(z, m);
}
if (n == 1) m = 1;
cout << m;
}
| 2 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
int dp[205][205][205];
char str0[205];
char str1[5];
void solve() {
int n, K;
scanf("%d%d%s%s", &n, &K, str0, str1);
int i, j, k;
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
for (k = 0; k <= n; k++) {
dp[i][j][k] = -2140000000;
}
}
}
dp[0][0][0] = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
if (dp[i][j][k] < 0) continue;
{
int j2 = j;
int k2 = (str0[i] == str1[0] ? k + 1 : k);
int val2 = dp[i][j][k] + (str0[i] == str1[1] ? k : 0);
dp[i + 1][j2][k2] =
((dp[i + 1][j2][k2]) < (val2) ? (val2) : (dp[i + 1][j2][k2]));
}
if (str1[0] == str1[1] && str0[i] != str1[0]) {
int j2 = j + 1;
int k2 = k + 1;
int val2 = dp[i][j][k] + k;
dp[i + 1][j2][k2] =
((dp[i + 1][j2][k2]) < (val2) ? (val2) : (dp[i + 1][j2][k2]));
}
if (str1[0] != str1[1] && str0[i] != str1[0]) {
int j2 = j + 1;
int k2 = k + 1;
int val2 = dp[i][j][k];
dp[i + 1][j2][k2] =
((dp[i + 1][j2][k2]) < (val2) ? (val2) : (dp[i + 1][j2][k2]));
}
if (str1[0] != str1[1] && str0[i] != str1[1]) {
int j2 = j + 1;
int k2 = k;
int val2 = dp[i][j][k] + k;
dp[i + 1][j2][k2] =
((dp[i + 1][j2][k2]) < (val2) ? (val2) : (dp[i + 1][j2][k2]));
}
}
}
}
int ans = -2140000000;
for (j = 0; j <= K; j++) {
for (k = 0; k <= n; k++) {
ans = ((ans) < (dp[n][j][k]) ? (dp[n][j][k]) : (ans));
}
}
printf("%d\n", ans);
return;
}
int main() {
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, uno, beg, imp[1000010];
void magic() {
cout << "(";
if (uno != -1 && uno != 0) {
for (int(i) = (int)(0); (i) < (int)(uno); (i)++) {
cout << "(";
}
cout << imp[0] << "->";
for (int(i) = (int)(1); (i) < (int)(uno); (i)++) {
cout << imp[i] << ")->";
}
cout << imp[uno] << ")";
beg = uno + 1;
} else if (uno == 0) {
cout << "1->";
beg = 1;
} else
beg = 0;
if (n - beg == 1)
cout << "->0)\n";
else {
if (uno != 0 && uno != -1) cout << "->";
for (int(i) = (int)(beg); (i) < (int)(n - 4); (i)++) {
cout << "(";
}
if (n - beg > 4) {
cout << "0->";
beg++;
} else if (n - beg == 4) {
cout << "0->";
beg = beg + 2;
}
for (int(i) = (int)(beg); (i) < (int)(n - 3); (i)++) {
cout << "0)->";
}
cout << "(0->0)->0)\n";
}
}
void magic2() {
int cer = -1;
if (n == 3)
cout << "NO\n";
else {
for (int(i) = (int)(0); (i) < (int)(uno); (i)++)
if (imp[i] == 0) cer = i;
if (cer == -1)
cout << "NO\n";
else {
cout << "YES\n";
if (cer > 1) {
cout << "(";
for (int(i) = (int)(0); (i) < (int)(cer - 1); (i)++)
cout << imp[i] << "->";
cout << imp[cer - 1] << ")->";
} else if (cer == 1) {
cout << imp[0] << "->";
}
cout << "(" << imp[cer] << "->(";
if (cer == n - 4)
cout << "1->0))->0\n";
else {
for (int(i) = (int)(cer + 1); (i) < (int)(n - 4); (i)++) {
cout << imp[i] << "->";
}
cout << imp[n - 4] << "->1->0))->0\n";
}
}
}
}
int main() {
while (cin >> n) {
uno = -1;
for (int(i) = (int)(0); (i) < (int)(n); (i)++) {
cin >> imp[i];
if (imp[i] == 1) {
uno = i;
}
}
if (n == 1) {
if (imp[0] == 1)
cout << "NO\n";
else {
cout << "YES\n";
cout << "0\n";
}
} else if (n == 2) {
if (imp[0] == 1 && imp[1] == 0) {
cout << "YES\n";
cout << "(1->0)\n";
} else {
cout << "NO\n";
}
} else if (imp[n - 1] == 1)
cout << "NO\n";
else if (imp[n - 2] == 1) {
cout << "YES\n";
magic();
} else if (imp[n - 2] == 0 && imp[n - 3] == 1) {
magic2();
} else {
cout << "YES\n";
magic();
}
}
return 0;
}
| 5 |
Subsets and Splits