solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int INFTY = 0x3f3f3f3f;
const int MAXN = 10010;
int n;
int a[MAXN];
int b[MAXN];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0, p = 0;
for (int i = 0; i < n - 1; i++)
if (a[i] >= a[i + 1]) b[p++] = a[i], ans++;
b[p++] = a[n - 1], ans++;
cout << ans << '\n';
for (int i = 0; i < p; i++) cout << b[i] << ' ';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
seed_seq seq{
(uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count(),
};
mt19937 rng(seq);
long long n, first, second;
long long dp[500005], s[500005], mn = 1000000000000000005;
vector<int> g[500005];
bool comp2(int a, int b) { return s[a] < s[b]; }
void DFS(int nod, int par) {
for (int i : g[nod]) {
if (i != par) {
DFS(i, nod);
}
}
for (int i : g[nod]) {
if (i != par) {
s[nod] += s[i];
}
}
s[nod]++;
sort(g[nod].begin(), g[nod].end(), comp2);
vector<pair<long long, long long> > q;
long long l = -1, mnt = 0, mnt2 = 0;
dp[nod] = 1000000000000000005;
for (int i : g[nod]) {
if (i != par) {
dp[nod] = min((s[nod] - s[i]) * (s[nod] - s[i]) + dp[i], dp[nod]);
mn = min(mn, (n - s[i]) * (n - s[i]) + dp[i]);
if (s[i] != l) {
if (l != -1) q.push_back({l, mnt}), q.push_back({l, mnt2});
l = s[i];
mnt = dp[i];
mnt2 = 1000000000000000005;
} else {
if (dp[i] < mnt)
mnt2 = mnt, mnt = dp[i];
else if (dp[i] < mnt2)
mnt2 = dp[i];
}
}
}
if (g[nod].size() == 1 && par)
dp[nod] = 1, mn = min(mn, (n - 1) * (n - 1) + 1);
else
q.push_back({l, mnt}), q.push_back({l, mnt2});
for (int i = 0; i < q.size(); i++) {
for (int j = i + 1; j < q.size(); j++) {
mn = min(mn,
(n - q[i].first - q[j].first) * (n - q[i].first - q[j].first) +
q[i].second + q[j].second);
}
}
}
int32_t main() {
ios_base ::sync_with_stdio(0);
cin.tie();
cout.tie();
cin >> n;
for (int i = 1; i < n; i++) {
cin >> first >> second;
g[first].push_back(second);
g[second].push_back(first);
}
DFS(1, 0);
cout << (2ll * n * n - n - mn) / 2;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base ::sync_with_stdio(false);
long long n, m, k;
cin >> n >> m >> k;
vector<long long> arr(n - 1);
long long prev = 0;
long long curr = 0;
cin >> prev;
for (int i = 0; i < n - 1; i++) {
cin >> curr;
arr[i] = curr - (prev + 1);
prev = curr;
}
sort(arr.begin(), arr.end());
long long sum = n;
for (int i = 0; i < n - k; i++) sum += arr[i];
cout << sum;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int ESP = 1e18;
long long int FCB = 1e9 + 7;
long long int dir[][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const long long int N = 5005;
long long int l[N], r[N];
vector<pair<long long int, long long int>> vv;
bool fly(long long int x, long long int y) {
if (l[x] >= r[y]) return false;
if (r[x] <= l[y]) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n;
cin >> n;
for (long long int i = 1; i < n + 1; i++) cin >> l[i] >> r[i];
for (long long int i = 1; i < n + 1; i++) {
for (long long int j = 1; j < n + 1; j++) {
if (i != j && fly(i, j)) vv.push_back(make_pair(i, j));
}
}
map<long long int, long long int> mp;
long long int zz = vv.size();
for (long long int i = 0; i < zz; i++) {
mp[vv[i].first]++;
mp[vv[i].second]++;
}
vector<long long int> ans;
for (long long int i = 1; i <= n; i++) {
if (mp[i] == zz) ans.push_back(i);
}
long long int dd = ans.size();
cout << dd << "\n";
for (long long int i = 0; i < dd; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
vector<long long> pos;
for (long long i = 0; i < n; i++) {
if (a[i]) {
pos.push_back(i);
}
}
if (pos.size() == 0)
cout << 0;
else {
long long ans = 1;
for (long long i = 0; i < pos.size() - 1; i++)
ans = ans * (pos[i + 1] - pos[i]);
cout << ans;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<unsigned long long, unsigned long long> a,
pair<unsigned long long, unsigned long long> b) {
unsigned long long s1, s2, h1, h2;
tie(s1, h1) = a;
tie(s2, h2) = b;
if (s1 * h2 > s2 * h1) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
unsigned long long ans = 0;
string str;
vector<pair<unsigned long long, unsigned long long> > arr(n);
for (int i = 0; i < n; i++) {
cin >> str;
unsigned long long s1 = 0, h = 0;
for (auto x : str) {
if (x == 's')
s1++;
else if (x == 'h')
h++, ans += s1;
}
arr[i] = make_pair(s1, h);
}
sort((arr).begin(), (arr).end(), comp);
unsigned long long s1 = 0;
for (auto i : arr) {
ans += s1 * 1LL * (i.second);
s1 += i.first;
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T &a, T &b) {
return a < b ? a : b;
}
template <class T>
inline T max(T &a, T &b) {
return a > b ? a : b;
}
template <class T>
void read(T &x) {
char ch;
while ((ch = getchar()) && !isdigit(ch))
;
x = ch - '0';
while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0';
}
struct point {
int x, y;
point() {}
point(int _x, int _y) : x(_x), y(_y) {}
};
const int N = 110000;
int t[N], n, m, l, r, T;
int ask(int x) {
int ans = 0;
for (int i = x; i; i -= i & (-i)) ans += t[i];
return ans;
}
void add(int x, int c) {
for (int i = x; i <= n; i += i & (-i)) t[i] += c;
}
void change(int x) {
if (T == 0) {
if (x <= (r - l) / 2) {
for (int i = 1; i <= x; i++)
add(l + x + i, ask(l + x - i + 1) - ask(l + x - i));
l += x;
} else {
for (int i = 1; i <= r - l - x; i++)
add(l + x - i + 1, ask(l + x + i) - ask(l + x + i - 1));
r = l + x;
T ^= 1;
}
} else {
x = r - l - x;
if (x <= (r - l) / 2) {
for (int i = 1; i <= x; i++)
add(l + x + i, ask(l + x - i + 1) - ask(l + x - i));
l += x;
T ^= 1;
} else {
for (int i = 1; i <= r - l - x; i++)
add(l + x - i + 1, ask(l + x + i) - ask(l + x + i - 1));
r = l + x;
}
}
}
void ask(int L, int R) {
if (T == 0)
printf("%d\n", ask(l + R) - ask(l + L));
else
printf("%d\n", ask(r - L) - ask(r - R));
}
int main() {
scanf("%d%d", &n, &m);
r = n;
int ty, x, L, R;
for (int i = 1; i <= n; i++) add(i, 1);
for (int i = 1; i <= m; i++) {
scanf("%d", &ty);
if (ty == 1)
scanf("%d", &x), change(x);
else
scanf("%d%d", &L, &R), ask(L, R);
}
}
| 3 |
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<eps)
#define SQ(X) ((X)*(X))
int AN,BN,R,ans;
int AX[100100],AY[100100],BX[100100],BY[100100];
vector<T> event;
set<P> fighter,energy;
bool ok(int a,int b)
{
return (SQ(AX[a]-BX[b])+SQ(AY[a]-BY[b]) <= 16*R*R);
}
int main()
{
while(1)
{
scanf("%d %d %d",&AN,&BN,&R);
if(AN==0&&BN==0)break;
ans=0;
event.clear();
fighter.clear();
energy.clear();
for(int i=0;i<AN;i++)
{
scanf("%d %d",&AX[i],&AY[i]);
event.pb(T(AX[i]-2*R,P(-1,i)));
event.pb(T(AX[i]+2*R,P(1,i)));
}
for(int i=0;i<BN;i++)
{
scanf("%d %d",&BX[i],&BY[i]);
event.pb(T(BX[i]-2*R,P(-2,i)));
event.pb(T(BX[i]+2*R,P(2,i)));
}
sort(event.begin(),event.end());
for(int i=0;i<event.size();i++)
{
T cur = event[i];
int k = cur.sec.sec;
int type = cur.sec.fi;
set<P>::iterator it,it1,it2;
if(abs(type)==1)
{
if(type<0)
{
it1 = energy.lower_bound(P(AY[k]-4*R,-1));
it2 = energy.upper_bound(P(AY[k]+4*R,INF));
for(it=it1;it!=it2;it++)if(ok(k,it->sec))ans++;
fighter.insert(P(AY[k],k));
}
else fighter.erase(P(AY[k],k));
}
else
{
if(type<0)
{
it1 = fighter.lower_bound(P(BY[k]-4*R,-1));
it2 = fighter.upper_bound(P(BY[k]+4*R,INF));
for(it=it1;it!=it2;it++)if(ok(it->sec,k))ans++;
energy.insert(P(BY[k],k));
}
else energy.erase(P(BY[k],k));
}
}
cout << ans << endl;
}
return 0;
} | 0 |
#include "bits/stdc++.h"
using namespace std;
int main() {
long long N, A, B;
cin >> N >> A >> B;
if ((A + B) % 2 == 0) {
cout << (B - A) / 2 << endl;
}
else {
cout << min(A - 1, N - B) + 1 + (B - A - 1) / 2 << endl;
}
}
| 0 |
# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq) (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw) sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe) transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr) transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy) for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu) FOR(i,0,upupu)
# define INIT std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)
//定義系
double EPS = 1e-10;
//誤差を考慮して足し算を行う
double add(double a, double b) {
if (abs(a + b) < EPS*(abs(a) + abs(b)))return 0;
return a + b;
}
//Point
struct Point {
double x, y;
Point() {}
Point(double x, double y) :x(x), y(y) {
}
Point operator + (Point p) {
return Point(add(x, p.x), add(y, p.y));
}
Point operator - (Point p) {
return Point(add(x, -p.x), add(y, -p.y));
}
Point operator * (double d) {
return Point(x*d, y*d);
}
Point operator / (double d) {
return Point(x / d, y / d);
}
//内積
double dot(Point p) {
return add(x*p.x, y*p.y);
}
//外積
double det(Point p) {
return add(x*p.y, -y*p.x);
}
//点の大小比較
bool operator <(const Point &p)const {
if (fabs(add(x, -p.x)) < EPS)return y < p.y;
return x < p.x;
}
bool operator ==(const Point &p)const {
return fabs(x - p.x) < EPS&&fabs(y - p.y) < EPS;
}
};
//ベクトル。使い分けるといいかも
typedef Point Vector;
//ベクトルの大きさの2乗
double norm(Vector p) {
return p.x*p.x + p.y*p.y;
}
//ベクトルの大きさ
double abs(Vector p) {
return sqrt(norm(p));
}
//線分
struct Segment {
Point p1, p2;
};
//直線
typedef Segment Line;
//中心c,半径rの円
struct Circle {
Point c;
double r;
Circle(Point c = Point(), double r = 0.0) :c(c), r(r) {}
};
//多角形
typedef vector<Point> Polygon;
//頂点集合
typedef vector<Point> Points;
//計算・アルゴリズム系
//反時計回りCCW
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
int ccw(Point p0, Point p1, Point p2) {
Vector a = p1 - p0;
Vector b = p2 - p0;
if (a.det(b) > EPS)return COUNTER_CLOCKWISE;
if (a.det(b) < -EPS)return CLOCKWISE;
if (a.dot(b) < -EPS)return ONLINE_BACK;
if (norm(a) < norm(b))return ONLINE_FRONT;
return ON_SEGMENT;
}
//線分p1p2と線分p3p4の交差判定
bool intersect(Point p1, Point p2, Point p3, Point p4) {
return (ccw(p1, p2, p3)*ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1)*ccw(p3, p4, p2) <= 0);
}
bool intersect(Segment s1, Segment s2) {
return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
static const int ICC_SEPERATE = 4;
static const int ICC_CIRCUMSCRIBE = 3;
static const int ICC_INTERSECT = 2;
static const int ICC_INSCRIBE = 1;
static const int ICC_CONTAIN = 0;
//円と円の交差判定
int intersect(Circle c1, Circle c2) {
if (c1.r<c2.r) swap(c1, c2);
double d = abs(c1.c - c2.c);
double r = c1.r + c2.r;
if (d == r) return ICC_CIRCUMSCRIBE;
if (d>r) return ICC_SEPERATE;
if (d + c2.r== c1.r) return ICC_INSCRIBE;
if (d + c2.r<c1.r) return ICC_CONTAIN;
return ICC_INTERSECT;
}
//ベクトルa,bの直交判定
bool isOrthogonal(Vector a, Vector b) {
return a.dot(b) == 0.0;
}
bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) {
return isOrthogonal(a1 - a2, b1 - b2);
}
bool isOrthogonal(Segment s1, Segment s2) {
return (s1.p2 - s1.p1).dot(s2.p2 - s2.p1) == 0.0;
}
//ベクトルa,bの並行判定
bool isParallel(Vector a, Vector b) {
return a.det(b) == 0.0;
}
bool isParallel(Point a1, Point a2, Point b1, Point b2) {
return isParallel(a1 - a2, b1 - b2);
}
bool isParallel(Segment s1, Segment s2) {
return (s1.p2 - s1.p1).det(s2.p2 - s2.p1) == 0.0;
}
//射影(点p1と点p2を通る直線に点pから垂線を引いた交点xを求める)
Point project(Segment s, Point p) {
Vector base = s.p2 - s.p1;
double r = (p - s.p1).dot(base) / norm(base);
return s.p1 + base*r;
}
//反射(点p1と点p2を通る直線を対象軸として点pと線対称の位置にある点xを求める)
Point reflect(Segment s, Point p) {
return p + (project(s, p) - p)*2.0;
}
//点aと点bの距離
double getDistance(Point a, Point b) {
return abs(a - b);
}
//直線lと点pの距離
double getDistanceLP(Line l, Point p) {
return abs((l.p2 - l.p1).det(p - l.p1) / abs(l.p2 - l.p1));
}
//線分sと点pの距離
double getDistanceSP(Segment s, Point p) {
if ((s.p2 - s.p1).dot(p - s.p1) < 0.0)return abs(p - s.p1);
if ((s.p1 - s.p2).dot(p - s.p2) < 0.0)return abs(p - s.p2);
return getDistanceLP(s, p);
}
//線分s1と線分s2の距離
double getDistance(Segment s1, Segment s2) {
if (intersect(s1, s2))return 0.0;
return min({ getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2), getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2) });
}
//線分s1と線分s2の交点
Point getCrossPoint(Segment l, Segment m) {
double d1 = (l.p2 - l.p1).det( m.p2 - m.p1);
double d2 = (l.p2 - l.p1).det( l.p2 - m.p1);
if (abs(d1) < EPS && abs(d2) < EPS) return m.p1;
return m.p1 + (m.p2 - m.p1) * d2 / d1;
}
//円cと線分lの交点
pair<Point, Point>getCrossPoints(Circle c, Line l) {
Vector pr = project(l, c.c);
Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
double base = sqrt(c.r*c.r - norm(pr - c.c));
return make_pair(pr + e*base, pr - e*base);
}
//円c1と円c2の交点
double arg(Vector p) { return atan2(p.y, p.x); }
Vector polar(double a, double r) { return Point(cos(r)*a, sin(r)*a); }
pair<Point, Point>getCrossPoints(Circle c1, Circle c2) {
double d = abs(c1.c - c2.c);
double a = acos((c1.r*c1.r + d*d - c2.r*c2.r) / (2 * c1.r*d));
double t = arg(c2.c - c1.c);
return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a));
}
//点pを通る円cの接線
pair< Point, Point > tangent( Circle c1, Point p2) {
pair<Point, Point> d = getCrossPoints(c1, Circle(p2, sqrt(norm(c1.c - p2) - c1.r * c1.r)));
return minmax(d.first, d.second);
}
//点の内包 0:in,1:on,2:out
int contains(Polygon g, Point p) {
int n = g.size();
bool x = false;
for (int i = 0; i < n; i++) {
Point a = g[i] - p, b = g[(i + 1) % n] - p;
if (abs(a.det(b)) < EPS&&a.dot(b) < EPS) return 1;
if (a.y > b.y)swap(a, b);
if (a.y < EPS&&EPS < b.y&&EPS < a.det(b))x = !x;
}
return (x ? 2 : 0);
}
//凸包を求める
Polygon convex_hull(Polygon s) {
Polygon u, l;
if (s.size() <= 2)return s;
sort(s.begin(), s.end(), [](const Point &p1, const Point &p2) {return p1.y == p2.y ? p1.x<p2.x : p1.y<p2.y; });
u.push_back(s[0]);
u.push_back(s[1]);
l.push_back(s[s.size() - 1]);
l.push_back(s[s.size() - 2]);
for (int i = 2; i<s.size(); i++){
for (int n = u.size(); n >= 2 && ccw(u[n - 2], u[n - 1], s[i]) != CLOCKWISE&&ccw(u[n - 2], u[n - 1], s[i]) != ONLINE_FRONT; n--){
u.pop_back();
}
u.push_back(s[i]);
}
for (int i = s.size() - 3; i >= 0; i--){
for (int n = l.size(); n >= 2 && ccw(l[n - 2], l[n - 1], s[i]) != CLOCKWISE&&ccw(l[n - 2], l[n - 1], s[i]) != ONLINE_FRONT; n--){
l.pop_back();
}
l.push_back(s[i]);
}
reverse(l.begin(), l.end());
for (int i = u.size() - 2; i >= 1; i--)l.push_back(u[i]);
return l;
}
//y座標の昇順でマージするための比較関数
bool compare_y(Point a, Point b) {
return a.y < b.y;
}
//最近点対
double closest_pair(Point *a, int n) {
if (n <= 1)return INF<double>();
sort(a, a + n);
int m = n / 2;
double x = a[m].x;
double d = min({ closest_pair(a,m),closest_pair(a + m,n - m) });//p,qが違う区間にある
inplace_merge(a, a + m, a + n, compare_y);//2つのソートされた列をマージ
//p,qが同じ区間にある
Points b;//直線から距離d未満の頂点を入れていく
for (int i = 0; i < n; i++) {
if (add(fabs(add(a[i].x, -x)), -d) >= 0.0)continue;
//bに入っている頂点を、末尾からy座標の差がd以上になるまで見ていく
for (int j = 0; j < b.size(); j++) {
Point dd;
dd.x = add(a[i].x, -b[b.size() - j - 1].x);
dd.y = add(a[i].y, -b[b.size() - j - 1].y);
if (add(dd.y, -d) >= 0.0)break;
d = min(d, abs(dd));
}
b.emplace_back(a[i]);
}
return d;
}
//多角形の面積
double area(Polygon p) {
int n = p.size();
double sum = 0.0;
for (int i = 0; i < n; i++) {
sum = add(sum,0.5*p[i].det(p[(i + 1) % n]));
}
return sum < 0.0 ? -sum : sum;
}
//凸性判定
bool is_convex(Polygon p) {
for (int i = 0; i < p.size(); i++) {
if (ccw(p[(i - 1 + p.size()) % p.size()], p[i], p[(i + 1) % p.size()]) == -1)return false;
}
return true;
}
//切断
Polygon convex_cut(Polygon p, Line l) {
Polygon ret;
for (int i = 0; i < p.size(); i++) {
Point cur = p[i], nxt = p[(i + 1) % p.size()];
if (ccw(l.p1, l.p2, cur) != -1)ret.emplace_back(cur);
if (ccw(l.p1, l.p2, cur)*ccw(l.p1, l.p2, nxt) < 0) {
Segment seg;
seg.p1 = cur;
seg.p2 = nxt;
ret.emplace_back(getCrossPoint(seg, l));
}
}
return ret;
}
//端点の種類
# define BOTTOM 0
# define LEFT 1
# define RIGHT 2
# define TOP 3
class EndPoint {
public:
Point p;
int seg, st;//入力線分のID,端点の種類
EndPoint() {}
EndPoint(Point p, int seg, int st) :p(p), seg(seg), st(st) {}
bool operator <(const EndPoint &ep)const {
//y座標が小さい順に整列
if (p.y == ep.p.y) {
return st < ep.st;//yが同一の場合は、下端点、左端点、右端点、上端点の順に調べる
}
else {
return p.y < ep.p.y;
}
}
};
EndPoint EP[202020];//端点のリスト
//線分交差問題(マンハッタン幾何)
int ManhattanIntersection(vector<Segment> s) {
int n = s.size();
for (int i = 0, k = 0; i < n; i++) {
//端点p1,p2が左下を基準に並ぶように調整
if (s[i].p1.y == s[i].p2.y) {
if(s[i].p1.x>s[i].p2.x)swap(s[i].p1, s[i].p2);
}
else if (s[i].p1.y > s[i].p2.y)swap(s[i].p1, s[i].p2);
if (s[i].p1.y == s[i].p2.y) {//水平線分を端点リストに追加
EP[k++] = EndPoint(s[i].p1, i, LEFT);
EP[k++] = EndPoint(s[i].p2, i, RIGHT);
}
else {//垂直線分を端点リストに追加
EP[k++] = EndPoint(s[i].p1, i, BOTTOM);
EP[k++] = EndPoint(s[i].p2, i, TOP);
}
}
sort(EP, EP + 2 * n);//端点のy座標に関して昇順に整列
set<LL> bt;//二分探索木
bt.insert(1010101010);
int cnt = 0;
for (int i = 0; i < 2 * n; i++) {
if (EP[i].st == TOP) {
bt.erase(EP[i].p.x);//上端点を削除
}
else if (EP[i].st == BOTTOM) {
bt.insert(EP[i].p.x);
}
else if (EP[i].st == LEFT) {
set<LL>::iterator b = bt.lower_bound(s[EP[i].seg].p1.x);
set<LL>::iterator e = bt.upper_bound(s[EP[i].seg].p2.x);
cnt += distance(b, e);//bとeの距離(点の数)を加算
}
}
return cnt;
}
int main() {
Point p;
cin >> p.x >> p.y;
Circle c;
cin >> c.c.x >> c.c.y >> c.r;
pair<Point, Point> q = tangent(c, p);
cout << fixed << setprecision(13) << q.first.x << " " << q.first.y << endl;
cout << fixed << setprecision(13) << q.second.x << " " << q.second.y << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double eps = 1e-10;
const int MAXN = 200009;
int n;
double T;
struct Lu {
double x, y;
bool operator<(const Lu &p) const { return y < p.y; }
} L[MAXN];
int cmp(double xx) {
if (fabs(xx) < eps)
return 0;
else
return xx < 0 ? -1 : 1;
}
int main() {
scanf("%d %lf", &n, &T);
for (int i = 1; i <= n; ++i) scanf("%lf", &L[i].x);
for (int i = 1; i <= n; ++i) scanf("%lf", &L[i].y);
sort(L + 1, L + 1 + n);
double ans = 0;
if (L[1].y > T || L[n].y < T) {
printf("%.6f\n", ans);
return 0;
}
int kk = 0, k = n + 1, flag = 0;
for (int i = 1; i <= n; ++i) {
if (L[i].y == T)
ans += L[i].x;
else if (L[i].y < T) {
kk = i;
} else if (L[i].y > T) {
k = i;
break;
}
}
while (kk >= 1 && k <= n) {
if (cmp(L[kk].x) == 0) --kk;
if (kk < 1) break;
if (cmp(L[k].x) == 0) ++k;
if (k > n) break;
double tmp1 = L[k].y - T;
double tmp2 = T - L[kk].y;
if (cmp(L[k].x - L[kk].x * tmp2 / tmp1) == -1) {
ans += L[k].x;
double tmp = L[k].x * tmp1 / tmp2;
ans += tmp;
L[kk].x -= tmp;
L[k].x = 0;
} else {
ans += L[kk].x;
double tmp = L[kk].x * tmp2 / tmp1;
ans += tmp;
L[k].x -= tmp;
L[kk].x = 0;
}
}
printf("%.6f\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
map<int, int> m;
for (int i = 0; i < n; ++i) cin >> a[i], m[a[i]] = i + 1;
int c = 0;
for (auto it : m) {
a[it.second - 1] = ++c;
}
vector<int> dp(n + 1, 0);
for (int i = 0; i < n; ++i) dp[a[i]] = dp[a[i] - 1] + 1;
cout << n - *max_element(dp.begin(), dp.end()) << "\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int inf = 1e18;
long long int expo(long long int x, long long int y) {
long long int ans = 1;
while (y != 0) {
if (y % 2) {
ans = ans * x;
}
y = y >> 1;
x = x * x;
}
return ans;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int max(long long int x, long long int y) {
if (x > y) return x;
return y;
}
long long int min(long long int x, long long int y) {
if (x < y) return x;
return y;
}
vector<long long int> SieveOfEratosthenes(long long int n) {
vector<long long int> primes;
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long int p = 2; p <= n; p++)
if (prime[p]) primes.push_back(p);
return primes;
}
vector<long long int> getDivisors(long long int num) {
vector<long long int> divisors;
for (long long int i = 1; i * i <= num; i++) {
if (!(num % i)) {
if (i * i == num)
divisors.push_back(i);
else {
divisors.push_back(i);
divisors.push_back(num / i);
}
}
}
sort(divisors.begin(), divisors.end());
return divisors;
}
void displayVector(vector<long long int> vect) {
for (long long int i = 0; i < vect.size(); i++) cout << vect[i] << " ";
cout << endl;
}
int visited[300005] = {0};
int recStack[300005] = {0};
vector<long long int> gr[300005];
vector<long long int> topSort;
bool hasCycle(long long int node) {
if (!visited[node]) {
visited[node] = 1;
recStack[node] = 1;
long long int len = gr[node].size();
long long int nbr;
for (long long int i = 0; i < len; i++) {
nbr = gr[node][i];
if (!visited[nbr] && hasCycle(nbr))
return true;
else {
if (recStack[nbr]) return true;
}
}
}
recStack[node] = 0;
return false;
}
void topoSort(long long int node) {
visited[node] = 1;
long long int len = gr[node].size();
long long int nbr;
for (long long int i = 0; i < len; i++) {
nbr = gr[node][i];
if (!visited[nbr]) {
topoSort(nbr);
}
}
topSort.push_back(node);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
if (!(n % 2)) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
vector<long long int> arr(2 * n);
int x;
for (int i = 0; i < n; i++) {
x = i + 1;
if (!(i % 2)) {
arr[i] = 2 * x - 1;
arr[i + n] = 2 * x;
} else {
arr[i] = 2 * x;
arr[i + n] = 2 * x - 1;
}
}
displayVector(arr);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n, ans; string A, B, S;
int main()
{
cin>>S; for(int i = 0; i < S.size(); ++i) A += S[i], A != B?(B = A, A = "", ++ans):0;
printf("%d\n", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
#define fastio() ios_base::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 55;
const ll mod = 1e9+7;
int n;
ll K;
vector<vector<ll>> multiply(vector<vector<ll>>& a, vector<vector<ll>>&b){
vector<vector<ll>> c(n, vector<ll>(n, 0));
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++)
for(int k = 0; k<n; k++) (c[i][j] += a[i][k]*b[k][j])%=mod;
return c;
}
int main(){
cin >> n >> K;
vector<vector<ll>> a(n, vector<ll>(n, 0)), ans(n, vector<ll>(n, 0));
for(int i = 0; i<n; i++) ans[i][i] = 1;
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++) scanf("%lld", &a[i][j]);
while(K>0){
if (K&1) ans = multiply(ans, a);
a = multiply(a, a);
K>>=1;
}
ll sum = 0;
for(int i = 0; i<n; i++)
for(int j = 0; j<n; j++) (sum += ans[i][j])%=mod;
cout << sum << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int N = 1000000;
int id[N + 10];
int main() {
int n = 0;
long long k = 0;
scanf("%d", &n);
std::cin >> k;
for (int i = 1; i <= n; i++) scanf("%d", id + i);
for (int i = 1; i <= n; i++) {
if (k <= 1LL * i) {
printf("%d\n", id[k]);
break;
}
k -= i;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
int maxz, tempbuff, tempbuff2;
cin >> maxz;
vector<pair<int, int>> z;
for (int i = 0; i < maxz; i++) {
cin >> tempbuff >> tempbuff2;
z.push_back(make_pair(tempbuff, tempbuff2));
}
sort(z.begin(), z.end(), compare);
for (int i = 0; i < maxz - 1; i++) {
if (z[i].first > z[i + 1].first) {
cout << "Happy Alex";
return 0;
}
}
cout << "Poor Alex";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int b, s = 0, v[100], j = 0;
string a;
int main() {
cin >> b >> a;
for (int i = 0; i < b; i++) {
if (a[i] == 'B') {
s++;
v[j] = s;
if (i == b - 1) {
j++;
}
} else if (s != 0) {
s = 0;
j++;
}
}
cout << j << endl;
for (int i = 0; i < j; i++) {
cout << v[i] << " ";
}
}
| 1 |
#include <iostream>
#include <cstring>
using namespace std;
long long n,k;
char s[105];
long long p[105],cnt=1;
long long ans=0;
int main()
{
cin>>s;
cin>>k;
n=strlen(s);
p[1]=1;
for(int i=1;i<n;i++)
{
if(s[i]==s[i-1])
p[cnt]++;
else
p[++cnt]++;
}
if(s[0]!=s[n-1])
{
for(int i=1;i<=cnt;i++)
ans+=k*(p[i]/2);
cout<<ans<<endl;
}
else
{
if(cnt==1)
cout<<k*n/2<<endl;
else
{
for(int i=2;i<cnt;i++)
ans+=k*(p[i]/2);
ans+=p[1]/2+p[cnt]/2;
ans+=(p[1]+p[cnt])/2*(k-1);
cout<<ans<<endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, c;
cin >> n >> c;
long long p[n], s[n];
for (int i = 0; i < n; i++) cin >> p[i];
for (int i = 0; i < n; i++) cin >> s[i];
long long dp[2][n + 2];
memset(dp, 0, sizeof(dp));
int me = 0, other = 1;
for (int i = 0; i < n; i++) {
swap(me, other);
for (int j = 0; j < i + 2; j++) dp[me][j] = 1e18;
for (int j = 0; j < i + 1; j++) dp[me][j] = p[i] + dp[other][j] + c * j;
for (int j = 0; j < i + 1; j++)
dp[me][j + 1] = min(dp[me][j + 1], s[i] + dp[other][j]);
}
long long mx = 1e18;
for (int i = 0; i < n + 1; i++) mx = min(mx, dp[me][i]);
cout << mx << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
int n, a[100001], b[100001], Q, u, v, k, i, tmp, j;
double dp[100001][102], now;
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
dp[i][a[i]] = 1;
now += dp[i][0];
}
scanf("%d", &Q);
while (Q--) {
scanf("%d%d%d", &u, &v, &k);
now -= dp[u][0];
for (i = 1, tmp = a[u]; i <= k; i++, --tmp)
for (j = 0; j <= b[u]; j++)
dp[u][j] = dp[u][j] * (tmp - j) / tmp + dp[u][j + 1] * (j + 1) / tmp;
now += dp[u][0];
a[u] -= k;
a[v] += k;
printf("%.12lf\n", now);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 101010;
string s, srev;
int m;
string word;
int Z1[Maxn], Z2[Maxn];
int res;
void Z(const string &s, int Z[], int delta) {
int l = 0, r = 0;
Z[0] = 0;
for (int i = 1; i < s.length(); i++) {
Z[i] = 0;
if (i <= r) Z[i] = min(Z[i - l], r - i);
while (i + Z[i] < s.length() && s[i + Z[i]] == s[Z[i]]) Z[i]++;
if (Z[i]) {
l = i;
r = i + Z[i] - 1;
}
}
for (int i = 0; i + delta < s.length(); i++) Z[i] = Z[i + delta];
}
int main() {
std::ios_base::sync_with_stdio(0);
cin >> srev;
s = srev;
reverse(srev.begin(), srev.end());
cin >> m;
while (m--) {
cin >> word;
if (word.length() == 1) continue;
Z(word + "#" + s, Z1, word.length() + 1);
reverse(word.begin(), word.end());
Z(word + "#" + srev, Z2, word.length() + 1);
for (int i = 1; i < s.length(); i++) Z2[i] = max(Z2[i], Z2[i - 1]);
int i;
for (i = 0; i + word.length() <= s.length(); i++)
if (word.length() - Z1[i] <= Z2[s.length() - i - word.length()]) break;
res += i + word.length() <= s.length();
}
printf("%d\n", res);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int p, q, l, r, i, j, count = 0, k, flag = 0;
scanf("%d%d%d%d", &p, &q, &l, &r);
std::pair<int, int> x[p];
std::pair<int, int> y[q];
for (i = 0; i < p; i++) scanf("%d%d", &x[i].first, &x[i].second);
for (i = 0; i < q; i++) scanf("%d%d", &y[i].first, &y[i].second);
for (i = l; i <= r; i++) {
flag = 0;
for (j = 0; j < q; j++) {
k = 0;
while (y[j].second + i >= x[k].first && k < p) {
if (y[j].first + i <= x[k].second || (y[j].second + i <= x[k].second)) {
count++;
flag = 1;
break;
}
k++;
}
if (flag) break;
}
}
printf("%d", count);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N_;
long long A[210][210];
long long B[210][210];
long long work[210][210];
const long long LIM = LLONG_MAX - (1000000007LL - 1) * (1000000007LL - 1);
void matmul(long long a[][210], long long b[][210]) {
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++) work[i][j] = 1070000000LL * 1070000000LL;
for (int i = 0; i < (N_); i++)
for (int k = 0; k < (N_); k++)
for (int j = 0; j < (N_); j++) {
work[i][j] = min(work[i][j], a[i][k] + b[k][j]);
}
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++) a[i][j] = work[i][j];
}
void matsq(long long a[][210]) {
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++) work[i][j] = 1070000000LL * 1070000000LL;
for (int i = 0; i < (N_); i++)
for (int k = 0; k < (N_); k++)
for (int j = 0; j < (N_); j++) {
work[i][j] = min(work[i][j], a[i][k] + a[k][j]);
}
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++) a[i][j] = work[i][j];
}
void matpow(long long a[][210], long long n) {
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++)
B[i][j] = i == j ? 0 : 1070000000LL * 1070000000LL;
while (n) {
if (n & 1) matmul(B, A);
matsq(A);
n >>= 1;
}
for (int i = 0; i < (N_); i++)
for (int j = 0; j < (N_); j++) A[i][j] = B[i][j];
}
const int L = 80;
int n, m, a[22], b[22];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < (n); i++) cin >> a[i];
for (int i = 0; i < (n); i++) cin >> b[i];
for (int i = 0; i < (L + 1); i++)
for (int j = 0; j < (L + 1); j++) A[i][j] = 1070000000LL * 1070000000LL;
for (int mask = 0; mask < (1 << n); mask++) {
long long cost = 0;
int open = 0;
int need = 0;
for (int i = 0; i < (n); i++) {
if (mask >> i & 1) {
cost += a[i];
open++;
} else {
cost += b[i];
open--;
need = max(need, -open);
}
}
for (int i = need; i < (min(L, L - open) + 1); i++) {
A[i][i + open] = min(A[i][i + open], cost);
}
}
N_ = L + 1;
matpow(A, m);
cout << A[0][0];
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void re(long long &x) {
x = 0;
char ch = getchar();
int b = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') b = 1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
if (b == 1) x *= -1;
}
long long n, m;
long long a[110000], b[110000], sum[110000];
long long ans;
int main() {
re(n), re(m);
for (register int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + i;
for (register int i = 1; i <= m; i++) {
scanf("%I64d %I64d", &a[i], &b[i]);
if (b[i] >= 0) {
ans = ans + n * a[i] + sum[n - 1] * b[i];
} else {
if (n % 2 == 0)
ans = ans + n * a[i] + sum[n / 2] * b[i] + sum[n / 2 - 1] * b[i];
else
ans = ans + n * a[i] + sum[n / 2] * b[i] * 2;
}
}
printf("%.8lf", (double)(ans * 1.0 / n));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
const int dr[] = {1, 1, 0, -1, -1, -1, 0, 1};
const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int asked[50005];
void endIt(int ans) {
printf("! %d\n", ans);
fflush(stdout);
}
pair<int, int> doQuery(int node) {
printf("? %d\n", node);
fflush(stdout);
pair<int, int> ans;
scanf("%d%d", &ans.first, &ans.second);
fflush(stdin);
return ans;
}
int main() {
int n, start, x;
scanf("%d%d%d", &n, &start, &x);
if (n < 2000) {
int nowNode = start;
pair<int, int> cur = {0, 0};
while (nowNode != -1) {
cur = doQuery(nowNode);
if (cur.first >= x) {
endIt(cur.first);
return 0;
}
nowNode = cur.second;
}
endIt(-1);
return 0;
}
random_device rd;
mt19937_64 generator{rd()};
uniform_int_distribution<> dist{1, n};
pair<int, int> cur = doQuery(start);
asked[start] = 1;
if (cur.first >= x) {
endIt(cur.first);
return 0;
}
pair<int, int> closest = cur;
int node;
for (int i = 0; i < 1000; i++) {
do {
node = dist(generator);
} while (asked[node]);
cur = doQuery(node);
if (cur.first == x) {
endIt(cur.first);
return 0;
}
if (cur.first < x && cur.first > closest.first) {
closest = cur;
}
}
int nowNode = closest.second;
cur = {0, 0};
while (cur.second != -1) {
cur = doQuery(nowNode);
if (cur.first >= x) {
endIt(cur.first);
return 0;
}
nowNode = cur.second;
}
endIt(-1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double COMPARISION_THRESHOLD = 1e-8;
const std::streamsize PRECISION = 10;
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
for (size_t i = 0; i < vector.size(); ++i) {
os << vector[i] << " ";
}
return os;
}
std::ostream& operator<<(std::ostream& os,
const vector<pair<int, int> >& vector) {
for (size_t i = 0; i < vector.size(); ++i) {
os << vector[i].first << " " << vector[i].second << "\n";
}
os << std::endl;
return os;
}
vector<double> Input() {
int size;
std::cin >> size;
vector<double> result(size);
for (int i = 0; i < size; ++i) {
int x, y;
cin >> x >> y;
if (x >= 0) {
result[i] = atan(double(y) / double(x)) * 180.0 / M_PI;
} else {
result[i] = 180.0 + atan(double(y) / double(x)) * 180.0 / M_PI;
}
}
return result;
}
bool equal(double first, double second) {
return fabs(first - second) < COMPARISION_THRESHOLD;
}
int main() {
std::ios_base::sync_with_stdio(false);
vector<double> v = Input();
sort(v.begin(), v.end());
if (v.size() == 1) {
cout << "0\n";
} else if (v.size() == 2) {
cout << min(v[1] - v[0], 360.0 - v[1] - v[0]) << endl;
} else {
double best = 1E5;
int i;
for (i = 0; i + 1 < v.size(); ++i) {
best = min(best, 360.0 - (v[i + 1] - v[i]));
}
best = min(best, v[i] - v[0]);
if (equal(best, 360)) {
cout << "0\n";
} else {
cout.precision(PRECISION);
cout << std::fixed << best << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<long long> v;
map<string, int> n;
long long d = 0, e = 0, i, f = 0, g = 0, j, k = 1, l = 0, r = 1, z, m, h,
p[101];
string y, t;
double a, b, c, q, u, s, x = 10000000;
cin >> a >> b >> c;
for (i = 1; i <= a; i++) {
cin >> p[i];
}
cin >> q >> u;
for (i = 1; i <= a; i++) {
s = p[i] / b + sqrt((q - p[i]) * (q - p[i]) + u * u) / c;
if (s <= x && i > 1) {
x = s;
d = i;
}
}
cout << d;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1);
long long n, l, i, x, y;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
cout << (n - 1) * 3 + 7 << '\n';
cout << "0 0\n";
cout << "0 1\n";
cout << "1 0\n";
cout << "1 1\n";
x = 2;
y = 2;
for (i = 1; i <= n; i++) {
cout << x << ' ' << y << '\n';
cout << x - 1 << ' ' << y << '\n';
cout << x << ' ' << y - 1 << '\n';
x++;
y++;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int n,m;
vector<int> y;
vector<int> x;
void solve() {
map<int,int> line;
int add = 0;
y.push_back(0);
x.push_back(0);
rep(i,n) {
int a; cin>>a;
add += a;
y.push_back(add);
}
add = 0;
rep(i,m) {
int a; cin>>a;
add += a;
x.push_back(add);
}
rep(i,y.size()) rep(j,x.size()) line[y[i]-x[j]]++;
int ans = 0;
map<int,int>::iterator it = line.begin();
while(it!=line.end()) {
ans += it->second*(it->second-1)/2;
it++;
}
cout<<ans<<endl;
y.clear();
x.clear();
}
int main() {
while(cin>>n>>m) {
if(n+m==0) return 0;
solve();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
const int RIBBON_SIZE = 100001;
int a[RIBBON_SIZE];
bool is_visited[RIBBON_SIZE] = {false};
char c;
for (int i = 1; i <= n; i++) {
cin >> c;
if (c == '<')
a[i] = -1;
else
a[i] = 1;
}
int value;
for (int i = 1; i <= n; i++) {
cin >> value;
a[i] *= value;
}
int pos = 1;
while (1 <= pos && pos <= 100000 && !is_visited[pos]) {
is_visited[pos] = true;
pos += a[pos];
}
if (pos < 1 || pos > n)
cout << "FINITE" << endl;
else
cout << "INFINITE" << endl;
return 0;
}
| 1 |
#include<iostream>
using namespace std;
int main()
{
int a,b,d,e = 0;
int f[100]{};
cin >> a >> b;
while(cin >> d){
f[d] = 1;
}
while(e < 51){
if(f[a - e] != 1){
cout << a - e;
return 0;
}
if(f[a + e] != 1){
cout << a + e;
return 0;
}
e++;
}
cout << a;
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
enum { MOD0 = 1000000000, MOD7 = 1000000007, MOD9 = 1000000009 };
template <typename T>
ostream &operator<<(ostream &cout, vector<T> &a) {
for (size_t i = 0; i < a.size(); ++i) cout << a[i] << " ";
return cout;
}
template <typename T>
ostream &operator<<(ostream &cout, vector<vector<T> > &a) {
for (size_t i = 0; i < a.size(); ++i) cout << a[i] << endl;
return cout;
}
int p[100000];
vector<int> kids[100000];
int h[100000];
int ans[100000];
void dfs(int x) {
for (int i = 0; i < kids[x].size(); ++i) {
int to = kids[x][i];
h[to] = h[x] + 1;
dfs(to);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
cin >> p[i];
--p[i];
kids[p[i]].push_back(i);
}
dfs(0);
for (int i = 0; i < n; ++i) {
ans[h[i]]++;
}
int ret = 0;
for (int i = 0; i < n; ++i) {
ret += ans[i] & 1;
}
cout << ret << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
const double eps = 1e-8;
struct P {
double x, y;
P() {}
P(double _x, double _y) : x(_x), y(_y) {}
P operator-(const P& t) { return P(x - t.x, y - t.y); }
};
inline int sign(double x) { return (x > eps) - (x < -eps); }
inline double cross(P a, P b) { return a.x * b.y - a.y * b.x; }
inline double cross(P a, P b, P c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
inline double dis(P a, P b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
P p[maxn], hull[maxn];
bool cmp(const P& b, const P& c) {
double tmp = cross(b, c, p[0]);
if (tmp > eps) return 1;
if (fabs(tmp) < eps && dis(b, p[0]) < dis(c, p[0])) return 1;
return 0;
}
int graham(P* hull, P* p, int n) {
int k = 0;
for (int i = 1; i < n; ++i)
if (sign(p[k].y - p[i].y) > 0 ||
(!sign(p[i].y - p[k].y) && sign(p[k].x - p[i].x) > 0))
k = i;
swap(p[0], p[k]);
sort(p + 1, p + n, cmp);
for (int i = 0; i < 3; ++i) hull[i] = p[i];
if (n < 3) return n;
int top = 3;
for (int i = 3; i < n; ++i) {
while (top >= 2 && cross(hull[top - 2], hull[top - 1], p[i]) < eps) --top;
hull[top++] = p[i];
}
return top;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) scanf("%lf%lf", &p[i].x, &p[i].y);
n = graham(hull, p, n);
hull[n] = hull[0];
long long ans = 0;
for (int i = 0; i < n; ++i)
ans += max(abs(int(hull[i].x) - int(hull[i + 1].x)),
abs(int(hull[i].y) - int(hull[i + 1].y)));
cout << ans + 4;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n;
vector<int> v(n);
bool allone = true;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] != 1) allone = false;
}
if (allone) {
if (n % 2 == 0)
cout << "Second"
<< "\n";
else {
cout << "First"
<< "\n";
}
} else {
int p = 0;
while (p < n && v[p] == 1) {
p++;
}
if (p % 2 == 0)
cout << "First"
<< "\n";
else {
cout << "Second"
<< "\n";
}
}
}
return 0;
}
| 2 |
#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 N = 1000005;
bool prime[N];
long long pf[N];
void sieve() {
int k = sqrt(N - 2);
for (int i = 1; i <= N - 2; ++i) prime[i] = true;
for (int i = 2; i <= k; ++i) {
if (prime[i]) {
for (int k = i * i; k <= N - 2; k += i) {
prime[k] = false;
pf[k] = i;
}
}
}
}
template <typename flow_t = long long>
struct Dinic {
struct FlowEdge {
int v, u;
flow_t cap, flow = 0;
FlowEdge(int v, int u, flow_t cap) : v(v), u(u), cap(cap) {}
};
const flow_t flow_inf = numeric_limits<flow_t>::max() / 2;
vector<FlowEdge> edges;
vector<vector<int>> adj;
int n, m = 0;
int s, t;
vector<int> level, ptr;
queue<int> q;
Dinic(int n, int s, int t) : n(n), s(s), t(t) {
adj.resize(n + 1);
level.resize(n + 1);
ptr.resize(n + 1);
}
void add_edge(int v, int u, flow_t cap) {
edges.emplace_back(v, u, cap);
edges.emplace_back(u, v, 0);
adj[v].push_back(m);
adj[u].push_back(m + 1);
m += 2;
}
bool bfs() {
while (!q.empty()) {
int v = q.front();
q.pop();
for (int id : adj[v]) {
if (edges[id].cap - edges[id].flow < 1) continue;
if (level[edges[id].u] != -1) continue;
level[edges[id].u] = level[v] + 1;
q.push(edges[id].u);
}
}
return level[t] != -1;
}
flow_t dfs(int v, flow_t pushed) {
if (pushed == 0) return 0;
if (v == t) return pushed;
for (int &cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
int id = adj[v][cid];
int u = edges[id].u;
if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
continue;
flow_t tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
if (tr == 0) continue;
edges[id].flow += tr;
edges[id ^ 1].flow -= tr;
return tr;
}
return 0;
}
flow_t flow() {
flow_t f = 0;
while (true) {
fill(level.begin(), level.end(), -1);
level[s] = 0;
q.push(s);
if (!bfs()) break;
fill(ptr.begin(), ptr.end(), 0);
while (flow_t pushed = dfs(s, flow_inf)) {
f += pushed;
}
}
return f;
}
};
bitset<201> vis;
vector<int> g[201];
void dfs(int s, vector<int> &u) {
u.push_back(s);
vis[s] = true;
for (auto &j : g[s]) {
if (!vis[j]) dfs(j, u);
}
}
int _runtimeTerror_() {
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; ++i) cin >> v[i];
Dinic<int> flow(n + 2, 0, n + 1);
vector<int> cnt(2, 0);
for (int i = 1; i <= n; ++i) {
++cnt[v[i] & 1];
if (v[i] & 1)
flow.add_edge(0, i, 2);
else
flow.add_edge(i, n + 1, 2);
for (int j = 1; j <= n; ++j) {
if (v[i] % 2 == 1 && v[j] % 2 == 0 && prime[v[i] + v[j]])
flow.add_edge(i, j, 1);
}
}
if (cnt[0] != cnt[1]) {
cout << "Impossible\n";
return 0;
}
int ans = flow.flow();
if (ans == 2 * cnt[0]) {
for (auto &k : flow.edges) {
if (k.flow == 1) g[k.v].push_back(k.u), g[k.u].push_back(k.v);
}
vector<vector<int>> ans;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
vector<int> u;
dfs(i, u);
ans.push_back(u);
}
}
cout << (long long)ans.size() << "\n";
for (auto &j : ans) {
cout << (long long)j.size() << " ";
for (auto &k : j) cout << k << " ";
cout << "\n";
}
} else {
cout << "Impossible\n";
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
sieve();
int TESTS = 1;
while (TESTS--) _runtimeTerror_();
return 0;
}
| 3 |
#include <iostream>
#include <string.h>
using namespace std;
int n;
char room[10] = "ABCDEFGHI";
char s, t, b;
int off[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int x, y;
double table[3][3];
double solve()
{
memset(&table[0][0], 0, sizeof(double) * 9);
table[y][x] = 1.0;
for(int k=0; k<n; k++)
{
double temp[3][3];
memset(&temp[0][0], 0, sizeof(double) * 9);
for(int j=0; j<3; j++)
{
for(int i=0; i<3; i++)
{
if(table[j][i] != 0.0)
{
for(int m=0; m<4; m++)
{
int xx = i + off[m][0];
int yy = j + off[m][1];
int index = xx + yy * 3;
xx = (xx>=0 && xx<3 && room[index] != b) ? xx : i;
yy = (yy>=0 && yy<3 && room[index] != b) ? yy : j;
temp[yy][xx] += table[j][i] / 4.0;
}
}
}
}
memcpy(&table[0][0], &temp[0][0], sizeof(double) * 9);
}
for(int i=0; i<9; i++)
{
if(room[i]==t) return table[i/3][i%3];
}
return 0.0;
}
int main()
{
while( 1 )
{
cin >> n;
if(n==0) break;
cin >> s >> t >> b;
for(int i=0; i<9; i++)
{
if(room[i] == s)
{
x = i%3;
y = i/3;
}
}
printf("%.8f\n", solve());
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, state, zero = 0;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> state;
if (n == 1) {
if (state == 0)
zero = 2;
else
zero = 1;
} else {
if (state == 0) ++zero;
}
}
if (zero == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(0);
string s;
int n; cin >> n;
map<string, int> a;
while (n--) {
cin >> s;
a[s]++;
}
cin >> n;
while (n--) {
cin >> s;
a[s]--;
}
int ans = 0;
for (auto it = a.begin(); it != a.end(); it++) {
ans = max(ans, it->second);
}
cout << ans;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
priority_queue<long long int, vector<long long int>, greater<long long int>>
huff;
int n;
long long int t;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> t;
huff.push(t);
}
t = 0;
if (!(n % 2)) {
t += huff.top();
huff.pop();
t += huff.top();
huff.pop();
huff.push(t);
}
long long int s;
while (huff.size() > 2) {
s = 0;
s += huff.top();
huff.pop();
s += huff.top();
huff.pop();
s += huff.top();
huff.pop();
huff.push(s);
t += s;
}
cout << t;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 503;
int m, n, vis[N][N];
vector<int> adj[N * N];
void init() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int id(int x, int y) { return (x - 1) * n + y; }
struct ds {
char c;
int x, y;
};
vector<ds> V;
void print(int u, int cc, int type = -1) {
int x = (u - 1) / n + 1, y = u - (x - 1) * n;
if (adj[u].size() == 0 && !type) {
V.push_back((ds){'R', x, y});
return;
}
V.push_back((ds){'B', x, y});
for (int v : adj[u]) print(v, cc, 0);
if (!type) V.push_back((ds){'D', x, y}), V.push_back((ds){'R', x, y});
}
int cc = 0;
queue<pair<int, int> > Q;
int hang[4] = {0, 0, 1, -1};
int cot[4] = {1, -1, 0, 0};
void build(int orgx, int orgy, int cc) {
vis[orgx][orgy] = cc;
Q.push({orgx, orgy});
while (Q.size()) {
int x = Q.front().first, y = Q.front().second;
Q.pop();
for (int t = 0; t <= (int)3; ++t) {
int nx = x + hang[t], ny = y + cot[t];
if (nx < 1 || nx > m || ny < 1 || ny > n || vis[nx][ny]) continue;
adj[id(x, y)].push_back(id(nx, ny));
vis[nx][ny] = cc;
Q.push({nx, ny});
}
}
print(id(orgx, orgy), cc);
}
void solve() {
cin >> m >> n;
for (int i = 1; i <= (int)m; ++i)
for (int j = 1; j <= (int)n; ++j) {
char c;
cin >> c;
if (c == '#') vis[i][j] = -1;
}
for (int i = 1; i <= (int)m; ++i)
for (int j = 1; j <= (int)n; ++j)
if (!vis[i][j]) build(i, j, ++cc);
cout << V.size() << '\n';
for (ds it : V) cout << it.c << ' ' << it.x << ' ' << it.y << '\n';
}
int main() {
init();
solve();
}
| 4 |
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
#define EPS 1e-8
struct MinimumCostFlow {
using Flow = int;
using Cost = double;
const Cost kInfCost = INF;
struct Edge {
int to, rev;
Flow cap;
Cost cost;
Edge() {}
Edge(int to, int rev, Flow cap, Cost cost) :to(to), rev(rev), cap(cap), cost(cost) {}
};
int n;
vector<vector<Edge>> g;
vector<Cost> dist;
vector<int> prevv, preve;
MinimumCostFlow(int n) :n(n), g(n), dist(n), prevv(n), preve(n) {}
void addArc(int from, int to, Flow cap, Cost cost) {
g[from].emplace_back(to, (int)g[to].size(), cap, cost);
g[to].emplace_back(from, (int)g[from].size() - 1, Flow(), -cost);
}
// s??????t????????????f???????°??????¨???
// ??????????????´?????? kInfCost
Cost minimumCostFlow(int s, int t, Flow f) {
Cost total = Cost();
while (f > 0) {
// Bellman-Ford
fill(dist.begin(), dist.end(), kInfCost);
dist[s] = 0;
bool update = true;
while (update) {
update = false;
for (int v = 0; v < n; v++) {
if (dist[v] == kInfCost)continue;
for (int i = 0; i < g[v].size(); i++) {
Edge &e = g[v][i];
if (e.cap > Flow() && dist[e.to] > EPS + dist[v] + e.cost) {
dist[e.to] = dist[v] + e.cost;
prevv[e.to] = v;
preve[e.to] = i;
update = true;
}
}
}
}
// ????????\???????????????
if (dist[t] == kInfCost)
return kInfCost;
// ?????????????????£??????????????????
Flow d = f;
for (int v = t; v != s; v = prevv[v])
d = min(d, g[prevv[v]][preve[v]].cap);
f -= d;
total += dist[t] * d;
for (int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return total;
}
};
// ??¬??????????¶???????, Gauss elimination
// O(n^3)
// ???????¶????, forward elimination
// ????????£??\, back substitution
using Num = double;
using Vec = vector<Num>;
using Mat = vector<Vec>;
Vec gaussianElimination(Mat A, Vec b) {
const int n = A.size(), m = A[0].size();
int pi = 0, pj = 0;
while (pi < n && pj < m) {
for (int i = pi + 1; i < n; i++)
if (abs(A[i][pj]) > abs(A[pi][pj]))
A[i].swap(A[pi]), swap(b[i], b[pi]);
if (abs(A[pi][pj]) > 0) {
Num d = A[pi][pj];
for (int j = 0; j < m; j++)
A[pi][j] /= d;
b[pi] /= d;
for (int i = pi + 1; i < n; i++) {
Num k = A[i][pj];
for (int j = 0; j < m; j++)
A[i][j] -= k * A[pi][j];
b[i] -= k * b[pi];
}
pi++;
}
pj++;
}
for (int i = pi; i < n; i++)
if (abs(b[i]) > 0)
return Vec();
if (pi < m || pj < m)
return Vec();
for (int j = m - 1; j >= 0; j--)
for (int i = 0; i < j; i++)
b[i] -= b[j] * A[i][j];
return b;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T; cin >> T;
cout << fixed << setprecision(10);
while (T--) {
int N, s, t, F; cin >> N >> s >> t >> F;
MinimumCostFlow mcf(N);
Mat a(N, Vec(N));
Vec c(N);
rep(i, 0, N) rep(j, 0, N + 1) {
if (j == N)cin >> c[i];
else cin >> a[i][j];
}
Vec x = gaussianElimination(a, c);
dump(x);
rep(i, 0, N) {
int M; cin >> M;
vector<int> d(M); rep(j, 0, M) {
cin >> d[j];
}
vector<int> f(M); rep(j, 0, M) {
cin >> f[j];
}
rep(j, 0, M) {
mcf.addArc(i, d[j], f[j], abs(x[i] - x[d[j]]));
}
}
auto res = mcf.minimumCostFlow(s, t, F);
if (res == INF)
cout << "impossible" << endl;
else
cout << res << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
long long check = 10000000000;
long long ans[n];
for (long long i = 0; i < n; i++) {
if (arr[i] == 0) {
ans[i] = 0;
check = 1;
} else {
ans[i] = check;
check++;
}
}
long long check1 = 10000000000;
long long ans1[n];
for (long long j = n - 1; j >= 0; j--) {
if (arr[j] == 0) {
ans1[j] = 0;
check1 = 1;
} else {
ans1[j] = check1;
check1++;
}
}
for (long long i = 0; i < n; i++) cout << min(ans[i], ans1[i]) << ' ';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
bool cmp(long long a, long long b) { return a > b; }
long long prem[maxn], preu[maxn];
long long totm = 0, totu = 0;
long long mrr[maxn], urr[maxn];
int main() {
long long n, d, m;
scanf("%lld %lld %lld", &n, &d, &m);
long long val;
for (int i = 0; i < n; i++) {
scanf("%lld", &val);
if (val > m)
mrr[totm++] = val;
else
urr[totu++] = val;
}
sort(mrr, mrr + totm, cmp);
sort(urr, urr + totu, cmp);
prem[0] = mrr[0], preu[0] = urr[0];
for (int i = 1; i < totm; i++) prem[i] = prem[i - 1] + mrr[i];
for (int i = 1; i < n; i++) preu[i] = preu[i - 1] + urr[i];
long long ans = 0;
if (totu == n) ans = preu[totu - 1];
for (int i = 1; i <= totm; i++) {
long long delay = (i - 1) * (d + 1) + 1;
if (delay > n) break;
long long res = prem[i - 1];
ans = max(ans, preu[n - delay - 1] + res);
}
printf("%lld\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100011, base = 1000000007;
int cnt[nmax], n, f[nmax];
long long pp(long long a, int n) {
long long r = 1;
while (n) {
if (n & 1) r = r * a % base;
a = a * a % base;
n >>= 1;
}
return r;
}
long long wui(int n) {
int r = 0;
while (n > 1) {
int u = f[n], k = 0;
while (n % u == 0) {
n /= u;
++k;
}
if (k > 1) return 0;
r++;
}
if (r % 2) return -1;
return 1;
}
int main() {
for (int i = 2; i < nmax; ++i)
if (f[i] == 0)
for (int j = i; j < nmax; j += i) f[j] = i;
cin >> n;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
cnt[a]++;
}
long long ans = 0;
for (int i = 1; i < nmax; ++i) {
int n = 0;
for (int j = i; j < nmax; j += i) n += cnt[j];
if (n) ans = (ans + (wui(i) + base) * (pp(2, n) - 1 + base) % base) % base;
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s = "01001010111001010";
inline int read() {
int f = 1, x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
return x * f;
}
int main() {
n = read();
cout << s[n] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
int a[Nmax];
int t[Nmax];
int cnt[Nmax];
int n, k, ans, m;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> t[i];
}
int cur = 0;
for (int last = 1; last <= min(n, k - 1); last++) {
if (t[last] <= k - 1) {
cur++;
int bound = last + (k - t[last]);
if (bound <= n) cnt[bound]++;
}
cur -= cnt[last];
ans = max(ans, cur);
}
cout << ans;
return 0;
}
| 4 |
#include <stdio.h>
const long long mod = 998244353;
const long long inv2 = (mod + 1) / 2;
int N, S; long long D[3030];
int main()
{
scanf ("%d %d", &N, &S);
D[0] = 1;
for (int i = 0; i < N; i++) D[0] = D[0] * 2 % mod;
while (N--){
int x; scanf ("%d", &x);
for (int i = S - x; i >= 0; i--){
D[i + x] = (D[i + x] + D[i] * inv2) % mod;
}
}
printf ("%lld\n", D[S]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
double xp, yp, vp;
double R, angel;
double x, y, v, r;
double min(double x, double y) { return x < y ? x : y; }
double max(double x, double y) { return x > y ? x : y; }
double init(double x1, double y1) {
double tmp;
if (fabs(x1 - 0) <= 0.0000000001 || fabs(y1 - 0) <= 0.0000000001) {
if (fabs(x1 - 0) <= 0.0000000001 && y1 < 0) {
return 3.0 * 3.1415926535897939 / 2.0;
} else if (fabs(x1 - 0) <= 0.0000000001 && y1 > 0) {
return 3.1415926535897939 / 2.0;
} else if (fabs(y1 - 0) <= 0.0000000001 && x1 > 0) {
return 0;
} else if (fabs(y1 - 0) <= 0.0000000001 && x1 < 0) {
return 3.1415926535897939;
}
}
if (x1 > 0) {
tmp = acos(x1 / sqrt(x1 * x1 + y1 * y1));
if (y1 > 0)
return tmp;
else if (y1 < 0) {
tmp = 2 * 3.1415926535897939 - tmp;
return tmp;
}
} else {
tmp = acos((-x1) / sqrt(x1 * x1 + y1 * y1));
if (y1 > 0) {
tmp = 3.1415926535897939 - tmp;
return tmp;
} else {
tmp = 3.1415926535897939 + tmp;
return tmp;
}
}
}
double judge(double t) {
double x_now, y_now;
double angel_sum;
double angel_now, T;
double len;
double L;
int k;
angel_now = angel + (vp * t) / (R);
k = 1;
while (angel_now > 2.0 * 3.1415926535897939 * k) k *= 10;
k /= 10;
while (k) {
while (angel_now > 2.0 * 3.1415926535897939 * k)
angel_now -= 2.0 * 3.1415926535897939 * k;
k /= 10;
}
x_now = R * cos(angel_now);
y_now = R * sin(angel_now);
L = sqrt((x - x_now) * (x - x_now) + (y - y_now) * (y - y_now));
T = L / v;
len = fabs(-(y - y_now) / (x - x_now) * x_now + y_now) /
sqrt((y - y_now) / (x - x_now) * (y - y_now) / (x - x_now) + 1);
if (len >= r) return T;
double x_mid = (((y - y_now) / (x - x_now)) * x_now - y_now) /
((y - y_now) / (x - x_now) - (x_now - x) / (y - y_now));
if (x_mid <= min(x_now, x) || x_mid >= max(x_now, x))
return T;
else {
T = 0;
T += sqrt(x_now * x_now + y_now * y_now - r * r) / v;
T += sqrt(x * x + y * y - r * r) / v;
angel_sum = fabs(init(x, y) - init(x_now, y_now));
if (angel_sum > 3.1415926535897939)
angel_sum = 2 * 3.1415926535897939 - angel_sum;
angel_sum -= acos(r / sqrt(x * x + y * y));
angel_sum -= acos(r / sqrt(x_now * x_now + y_now * y_now));
T += angel_sum * r / v;
return T;
}
}
int main() {
double l1, r1, m;
double tmp;
scanf("%lf%lf%lf", &xp, &yp, &vp);
scanf("%lf%lf%lf%lf", &x, &y, &v, &r);
R = sqrt(xp * xp + yp * yp);
angel = init(xp, yp);
r1 = 1000000;
l1 = 0;
while (r1 - l1 > 0.000000001) {
m = (r1 + l1) / 2;
tmp = judge(m);
if (tmp < m)
r1 = m;
else
l1 = m;
}
long long st = (long long)(m * 10000000);
st = st % 10;
printf("%.9f\n", m);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const int N = 1e5 + 5, inf = 1e9 + 5;
long long add(long long x, long long y) {
x += y;
if (x >= MOD) return x - MOD;
return x;
}
long long sub(long long x, long long y) {
x -= y;
if (x < 0) return x + MOD;
return x;
}
long long mult(long long x, long long y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
set<pair<int, int>> count;
vector<int> a(k);
vector<bool> seen(n + 5);
for (int i = 0; i < k; i++) cin >> a[i];
for (int i = 0; i < k; ++i) {
if (seen[a[i] - 1]) count.insert({a[i] - 1, a[i]});
if (seen[a[i] + 1]) count.insert({a[i] + 1, a[i]});
count.insert({a[i], a[i]});
seen[a[i]] = true;
}
cout << (n * 3 - 2) - count.size() << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct road {
int next, num;
} temp;
struct point {
int id, s;
};
vector<road> G[300005];
int vis[300005];
int visroad[300005];
int ans, res[300005];
queue<point> Q;
point hh, t, tt;
int main() {
hh.s = 0;
int m, n, k, cont = 0, c, d;
scanf("%d%d%d", &m, &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &hh.id);
if (!vis[hh.id]) Q.push(hh);
vis[hh.id] = 1;
}
for (int i = 1; i <= m - 1; i++) {
scanf("%d%d", &c, &d);
temp.num = i;
temp.next = d;
G[c].push_back(temp);
temp.next = c;
G[d].push_back(temp);
}
while (!Q.empty()) {
t = Q.front();
Q.pop();
tt.s = t.s + 1;
int len = G[t.id].size();
for (int i = 0; i < len; i++) {
int next = G[t.id][i].next;
if (visroad[G[t.id][i].num]) continue;
if (vis[next]) {
visroad[G[t.id][i].num] = 1;
res[cont++] = G[t.id][i].num;
} else if (t.s < k) {
vis[next] = 1;
tt.id = next;
Q.push(tt);
visroad[G[t.id][i].num] = 1;
}
}
}
printf("%d\n", cont);
for (int i = 0; i < cont; i++) {
printf("%d ", res[i]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
const double PI = acos(-1);
const double EPS = 1e-9;
int main() {
ios_base::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int mt[n][m];
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> mt[i][j];
if (j == 0) {
ans ^= mt[i][j];
}
}
}
if (ans > 0) {
cout << "TAK\n";
for (int i = 0; i < n; i++) {
cout << 1;
if (i == n - 1)
cout << endl;
else
cout << " ";
}
} else {
ii res(-1, -1);
for (int i = 0; i < n; i++) {
for (int j = 1; j < m; j++) {
int kk = ans ^ mt[i][0] ^ mt[i][j];
if (kk > 0) {
res.first = i;
res.second = j;
}
}
}
int aa = ans ^ 1 ^ 0;
if (res.first >= 0)
ans = ans ^ mt[res.first][0] ^ mt[res.first][res.second];
if (ans > 0) {
cout << "TAK\n";
for (int i = 0; i < n; i++) {
if (i == res.first)
cout << res.second + 1;
else
cout << 1;
if (i == n - 1)
cout << endl;
else
cout << " ";
}
} else {
cout << "NIE\n";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, a[303030], dp[303030][22], bf[303030], nw[303030], nt[303030], ans;
int gt(int l, int r) {
int k = 0, rr = 1;
while (rr * 2 <= r - l + 1) {
k++;
rr *= 2;
}
return max(dp[l][k], dp[r - rr + 1][k]);
}
int main() {
cin >> n;
memset(bf, 0x3f, sizeof(bf));
for (int i = 1; i <= n; i++) {
cin >> a[i];
dp[i][0] = a[i];
bf[nw[a[i]]] = i;
nw[a[i]] = i;
}
nt[n] = n;
for (int i = n - 1; i >= 1; i--) {
nt[i] = min(nt[i + 1], bf[i] - 1);
}
for (int i = 1; i <= 20; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
dp[j][i] = max(dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= nt[i];) {
int aaa = gt(i, j);
if (aaa <= j - i + 1) {
ans++;
j++;
} else {
j = i + aaa - 1;
}
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 10;
int N, K;
char s[MAX];
int col[MAX][3], pre[MAX << 1], siz[MAX << 1], ans;
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void merge(int x, int y) {
x = find(x), y = find(y);
if (x != y) pre[x] = y, siz[y] += siz[x];
}
int calc(int x) { return min(siz[find(x)], siz[find(x + K)]); }
void solve(int i) {
int cnt = col[i][0];
if (cnt == 2) {
int x = col[i][1], y = col[i][2];
if (s[i] == '1') {
if (find(x) == find(y)) return;
ans -= calc(x) + calc(y);
merge(x, y), merge(x + K, y + K);
ans += calc(x);
} else {
if (find(x) == find(y + K)) return;
ans -= calc(x) + calc(y);
merge(x, y + K), merge(x + K, y);
ans += calc(x);
}
} else if (cnt == 1) {
int x = col[i][1];
if (s[i] == '1') {
if (find(x) == find(0)) return;
ans -= calc(x);
merge(x, 0);
ans += calc(x);
} else {
if (find(x + K) == find(0)) return;
ans -= calc(x);
merge(x + K, 0);
ans += calc(x);
}
}
}
int main() {
scanf("%d%d%s", &N, &K, s + 1);
for (int i = 1; i <= K; i++) {
int c;
scanf("%d", &c);
for (int j = 1; j <= c; j++) {
int x;
scanf("%d", &x);
col[x][++col[x][0]] = i;
}
}
for (int i = 1; i <= (K << 1); i++) pre[i] = i, siz[i] = i <= K;
siz[0] = 1e9;
for (int i = 1; i <= N; i++) {
solve(i);
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
int upmin(T1 &x, T2 v) {
if (x <= v) {
return 0;
}
x = v;
return 1;
}
template <class T1, class T2>
int upmax(T1 &x, T2 v) {
if (x >= v) {
return 0;
}
x = v;
return 1;
}
void init() {
int N;
cin >> N;
vector<string> op(N);
vector<int> id(N);
for (int i = 0; i < N; ++i) {
cin >> op[i] >> id[i];
}
set<int> bag;
set<int> z;
for (int i = 0; i < N; ++i) {
if (op[i] == "-") {
if (not bag.count(id[i])) {
z.emplace(id[i]);
} else {
bag.erase(id[i]);
}
} else {
bag.emplace(id[i]);
}
}
bag.clear();
int ans = z.size();
for (int i = 0; i < N; ++i) {
if (op[i] == "-") {
if (bag.count(id[i])) {
bag.erase(id[i]);
}
if (z.count(id[i])) {
z.erase(id[i]);
}
} else {
bag.emplace(id[i]);
}
upmax(ans, bag.size() + z.size());
}
cout << ans << endl;
}
void preprocess() {}
void solve() {}
signed main() {
ios::sync_with_stdio(0);
init();
preprocess();
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1);
}
int sum = 0;
for (int i = 0; i < n; ++i) {
vector<int> neighbors = g[i];
vector<int> lifelines;
for (int n : neighbors) {
for (int n2 : g[n])
if (n2 > i) sum++;
}
}
cout << sum << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<db,db> pd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; }
int pct(int x) { return __builtin_popcount(x); }
int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
int cdiv(int a, int b) { return a/b+!(a<0||a%b == 0); } // division of a by b rounded up, assumes b > 0
int fstTrue(function<bool(int)> f, int lo, int hi) {
hi ++; assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
int mid = (lo+hi)/2;
f(mid) ? hi = mid : lo = mid+1;
}
return lo;
}
// INPUT
template<class A> void re(complex<A>& c);
template<class A, class B> void re(pair<A,B>& p);
template<class A> void re(vector<A>& v);
template<class A, size_t SZ> void re(array<A,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(db& d) { str t; re(t); d = stod(t); }
void re(ld& d) { str t; re(t); d = stold(t); }
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; }
template<class A, class B> void re(pair<A,B>& p) { re(p.f,p.s); }
template<class A> void re(vector<A>& x) { trav(a,x) re(a); }
template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); }
// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) {
str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; F0R(i,SZ) res += char('0'+b[i]);
return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) { // containers with begin(), end()
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
return "("+ts(p.f)+", "+ts(p.s)+")"; }
// OUTPUT
template<class A> void pr(A x) { cout << ts(x); }
template<class H, class... T> void pr(const H& h, const T&... t) {
pr(h); pr(t...); }
void ps() { pr("\n"); } // print w/ spaces
template<class H, class... T> void ps(const H& h, const T&... t) {
pr(h); if (sizeof...(t)) pr(" "); ps(t...); }
// DEBUG
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
// FILE I/O
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
void setIO(string s = "") {
unsyncIO();
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
/**
* Description: modular arithmetic operations
* Source:
* KACTL
* https://codeforces.com/blog/entry/63903
* https://codeforces.com/contest/1261/submission/65632855 (tourist)
* https://codeforces.com/contest/1264/submission/66344993 (ksun)
* Verification:
* https://open.kattis.com/problems/modulararithmetic
*/
struct mi {
typedef decay<decltype(MOD)>::type T;
/// don't silently convert to T
T v; explicit operator T() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0) v += MOD;
}
friend bool operator==(const mi& a, const mi& b) {
return a.v == b.v; }
friend bool operator!=(const mi& a, const mi& b) {
return !(a == b); }
friend bool operator<(const mi& a, const mi& b) {
return a.v < b.v; }
friend void re(mi& a) { ll x; re(x); a = mi(x); }
friend str ts(mi a) { return ts(a.v); }
mi& operator+=(const mi& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this; }
mi& operator-=(const mi& m) {
if ((v -= m.v) < 0) v += MOD;
return *this; }
mi& operator*=(const mi& m) {
v = (ll)v*m.v%MOD; return *this; }
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans;
}
friend mi inv(const mi& a) { assert(a.v != 0);
return pow(a,MOD-2); }
mi operator-() const { return mi(-v); }
mi& operator++() { return *this += 1; }
mi& operator--() { return *this -= 1; }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
typedef vector<mi> vmi;
typedef pair<mi,mi> pmi;
typedef vector<pmi> vpmi;
/**
* Description: pre-compute factorial mod inverses,
* assumes $MOD$ is prime and $SZ < MOD$.
* Time: O(SZ)
* Source: KACTL
* Verification: https://dmoj.ca/problem/tle17c4p5
*/
vi invs, fac, ifac;
void genFac(int SZ) {
invs.rsz(SZ), fac.rsz(SZ), ifac.rsz(SZ);
invs[1] = fac[0] = ifac[0] = 1;
FOR(i,2,SZ) invs[i] = MOD-(ll)MOD/i*invs[MOD%i]%MOD;
FOR(i,1,SZ) {
fac[i] = (ll)fac[i-1]*i%MOD;
ifac[i] = (ll)ifac[i-1]*invs[i]%MOD;
}
}
mi comb(int a, int b) {
if (a < b || b < 0) return 0;
return (ll)fac[a]*ifac[b]%MOD*ifac[a-b]%MOD;
}
int N,A;
mi dp[5001][5001],need[5001];
mi range(int l, int r) {
mi res = 1; FOR(i,l,r+1) res *= i;
return res;
}
int extra;
mi getCycs(int num, int sing) {
//if (!num) return 0;
int lef = N-A-sing; if (lef < 0) return 0;
mi tmp = mi(fac[N-A])*ifac[lef];
tmp *= mi(fac[num+lef-1])*ifac[num-1];
return tmp;
//return tmp;
// num .. num+lef-1
assert(sing <= num);
mi res = 0;
F0R(i,sing+1) { // (A+1)*...*N
// - comb(i,1)*A*...*N-1
// + comb(i,2)*(A-1)*...*(N-2)
mi tmp = range(num-i,num+extra-1-i)*comb(sing,i);
if (i&1) tmp *= -1;
res += tmp;
}
dbg("HUH",num,sing,res);
dbg("OOPS",tmp,res);
return res;
}
bool ok(vi perm) {
vector<bool> vis(N);
bool single = 0;
F0R(i,N) if (!vis[i]) {
if (i >= A) return 0; // not in a good cycle ...
if (perm[i] == i) single = 1;
else if (single) return 0;
int t = i;
while (!vis[t]) {
vis[t] = 1;
t = perm[t];
}
}
return 1;
}
mi cum[5005];
int main() {
genFac((int)1e7+5);
setIO(); re(N,A); extra = N-A;
// vi perm; F0R(i,N) perm.pb(i);
// int res = 0;
// do {
// if (!ok(perm)) dbg("BAD",perm);
// res += ok(perm);
// } while (next_permutation(all(perm)));
// ps(res);
//exit(0);
//dbg("HUH");
F0R(i,A+1) {
F0R(j,A+1-i) { // i = # left
dp[i][j] = getCycs(A-i,j)*fac[max(i-1,0)]; // A-i = # so far, j = # singles so far
if (i) dp[i][j] += dp[i-1][j+1];
if (i >= 2) cum[j] += ifac[i-2]*dp[i-2][j];
dp[i][j] += fac[i-1]*cum[j];
// FOR(cyc,2,i+1) dp[i][j] += ifac[i-cyc]*dp[i-cyc][j]*fac[i-1];
}
}
ps(dp[A][0]);
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
| 0 |
#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
const int mod = 1000000007;
const int N = 3e5;
vector<int> g[1002];
bool vis[1002];
void dfs(int u) {
vis[u] = true;
for (auto i : g[u]) {
if (!vis[i]) dfs(i);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, a, b;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int falg = 0;
if (m != n - 1)
cout << "no";
else {
dfs(1);
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
falg = 1;
cout << "no";
break;
}
}
if (!falg) cout << "yes";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[11];
int du[11];
int dv[11];
string str = "";
void reset() {
for (int i = 0; i < 10; i++) {
du[i] = dv[i] = a[i];
}
}
int main() {
while (cin >> str) {
for (int i = 0; i < 10; i++) {
a[i] = 0;
}
for (int i = 0; i < str.size(); i++) {
a[str[i] - '0']++;
}
int best = 0;
string su = str;
string sv = str;
for (int u = 9; u >= 1; u--) {
int v = 10 - u;
reset();
int temp = 0;
string tu = "";
string tv = "";
string tt = "";
string uu = "";
string vv = "";
bool flag = (du[u] > 0 && dv[v] > 0);
bool flag2 = false;
if (flag) {
temp += 1;
tu += '0' + u;
tv += '0' + v;
du[u]--;
dv[v]--;
uu = "";
vv = "";
for (int k = 0; k <= 9; k++) {
int j = 9 - k;
while (du[k] > 0 && dv[j] > 0) {
uu += '0' + k;
vv += '0' + j;
du[k]--;
dv[j]--;
temp += 2;
flag2 = true;
}
}
tu = uu + tu;
tv = vv + tv;
}
tt = "";
while (du[0] > 0 && dv[0] > 0) {
tt += "0";
du[0]--;
dv[0]--;
temp++;
}
{
tu = tu + tt;
tv = tv + tt;
}
tt = "";
for (int k = 0; k < 10; k++) {
while (du[k] > 0) {
tt += '0' + k;
du[k]--;
}
}
tu = tt + tu;
tt = "";
for (int k = 0; k < 10; k++) {
while (dv[k] > 0) {
tt += '0' + k;
dv[k]--;
}
}
tv = tt + tv;
if (temp > best) {
best = temp;
su = tu;
sv = tv;
}
}
cout << su << endl;
cout << sv << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using LL = int64_t;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n);
for (int &i : a) std::cin >> i;
std::sort(a.begin(), a.end());
int maxa = a.back();
std::vector<std::vector<int>> mx(maxa + 1);
for (auto &i : mx) i.assign(2, -1);
std::vector<int> cnt(maxa + 1);
for (int i : a) ++cnt[i];
for (int i = 1; i <= maxa; ++i) {
if (cnt[i] >= 2)
mx[i][0] = mx[i][1] = i;
else if (cnt[i] == 1) {
mx[i][0] = i;
mx[i][1] = mx[i - 1][0];
} else
mx[i] = mx[i - 1];
cnt[i] += cnt[i - 1];
}
LL ans = 0;
for (int y = 2; y <= maxa; ++y) {
int more[] = {-1, -1};
LL cnty = 0;
for (int k = 1; k <= maxa / y; ++k)
cnty += (LL)k * (cnt[std::min((k + 1) * y - 1, maxa)] - cnt[k * y - 1]);
for (int k = maxa / y; k >= 0; --k) {
more[1] =
std::max(more[1], mx[std::min((k + 1) * y - 1, maxa)][0] - k * y);
if (more[0] < more[1]) std::swap(more[0], more[1]);
more[1] =
std::max(more[1], mx[std::min((k + 1) * y - 1, maxa)][1] - k * y);
int x = std::min((LL)(k * y + more[0]) / 2, cnty - k);
if (x >= 2) ans = std::max(ans, LL(x) * y);
if (more[1] != -1) {
x = std::min((LL)k * y + more[1], cnty - 2 * k);
if (x >= 2) ans = std::max(ans, LL(x) * y);
}
if (n - cnt[std::max(0, k * y + more[0] - 1)] >= 2) {
x = std::min((LL)k * y + more[0], cnty - 2 * k - 1);
if (x >= 2) ans = std::max(ans, LL(x) * y);
}
}
}
std::cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
char s[maxn];
int sum[maxn];
set<int> t[4 * maxn];
int read() {
int x = 0, y = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') y = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return x * y;
}
int main() {
int T;
T = read();
while (T--) {
int n, q;
n = read();
q = read();
scanf("%s", s + 1);
for (int i = 0; i <= 4 * n; i++) t[i].clear();
for (int i = 1; i <= n; i++) {
int x = (s[i] == '+' ? 0 : 1);
x ^= ((i & 1) ^ 1);
if (!x)
sum[i] = sum[i - 1] + 1;
else
sum[i] = sum[i - 1] - 1;
t[sum[i] + sum[i - 1] + 2 * n].insert(i);
}
for (int i = 1; i <= q; i++) {
int l, r;
l = read();
r = read();
if (sum[r] == sum[l - 1])
puts("0");
else if ((r - l + 1) & 1) {
puts("1");
printf("%d\n", *t[sum[r] + sum[l - 1] + 2 * n].lower_bound(l));
} else {
puts("2");
printf("%d %d\n", *t[sum[r - 1] + sum[l - 1] + 2 * n].lower_bound(l),
r);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
map<pair<char, int>, int> f;
int main() {
ios ::sync_with_stdio(false);
cin.tie(NULL);
cout << (fixed) << setprecision(6);
string s;
while (cin >> s) {
f.clear();
int res = 0;
for (int i = 0, _a = (s.length()); i < _a; i++) {
int cur = f[make_pair(s[i], i % 2)] + 1;
res = max(res, cur);
f[make_pair(s[i], 1 - i % 2)] = cur;
}
cout << res << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long double eps = 1e-7;
int cmp(long double a, long double b = 0) {
return a + eps < b ? -1 : a - eps > b ? 1 : 0;
}
struct point {
long double x, y;
point(long double a = 0, long double b = 0) : x(a), y(b) {}
bool operator<(const point& q) const {
if (int t = cmp(x, q.x))
return t < 0;
else
return cmp(y, q.y) < 0;
}
bool operator==(const point& q) const {
return cmp(x, q.x) == 0 && cmp(y, q.y) == 0;
}
void read() { cin >> x >> y; }
};
long double dist(point& a, point& b) { return hypot(a.x - b.x, a.y - b.y); }
point mid(point& a, point& b) {
return point((a.x + b.x) / 2, (a.y + b.y) / 2);
}
int cross(point& a, point& p, point& q) {
long double x = (p.x - a.x) * (q.y - a.y) - (p.y - a.y) * (q.x - a.x);
return cmp(x);
}
bool on(point& a, point& p, point& q) {
if (cmp(p.x, q.x) == 0)
return cmp(a.y, min(p.y, q.y)) >= 0 && cmp(a.y, max(p.y, q.y)) <= 0;
return cmp(a.x, min(p.x, q.x)) >= 0 && cmp(a.x, max(p.x, q.x)) <= 0;
}
point tri[100][4];
int n;
bool libre(point p) {
for (int i = 0; i < n; i++) {
int a1 = cross(p, tri[i][0], tri[i][1]);
int a2 = cross(p, tri[i][1], tri[i][2]);
int a3 = cross(p, tri[i][2], tri[i][3]);
if (a1 != 0 && a2 != 0 && a3 != 0)
if (a1 == a2 && a2 == a3) return false;
}
return true;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < 3; j++) tri[i][j].read();
for (int i = 0; i < n; i++) tri[i][3] = tri[i][0];
long double res = 0;
point pivot;
for (int i = 0; i < n; i++)
for (int j = 0; j < 3; j++) {
vector<point> inter;
long double A1 = tri[i][j + 1].y - tri[i][j].y;
long double B1 = tri[i][j].x - tri[i][j + 1].x;
long double C1 = A1 * tri[i][j].x + B1 * tri[i][j].y;
inter.push_back(tri[i][j]);
inter.push_back(tri[i][j + 1]);
for (int a = 0; a < n; a++)
for (int b = 0; b < 3; b++) {
long double A2 = tri[a][b + 1].y - tri[a][b].y;
long double B2 = tri[a][b].x - tri[a][b + 1].x;
long double C2 = A2 * tri[a][b].x + B2 * tri[a][b].y;
long double det = A1 * B2 - A2 * B1;
if (cmp(det) != 0) {
point p((C1 * B2 - C2 * B1) / det, (A1 * C2 - A2 * C1) / det);
if (on(p, tri[i][j], tri[i][j + 1]) &&
on(p, tri[a][b], tri[a][b + 1]))
inter.push_back(p);
}
}
sort(inter.begin(), inter.end());
inter.erase(unique(inter.begin(), inter.end()), inter.end());
for (int k = 1; k < inter.size(); k++) {
point aux = mid(inter[k], inter[k - 1]);
if (i == 1 && j == 1 && k == 1) pivot = aux;
if (libre(aux)) res += dist(inter[k], inter[k - 1]);
}
}
cout << setprecision(10) << res << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
const int mod = 1000000007;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
void read(int &first) {
char ch = getchar();
first = 0;
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) first = first * 10 + ch - '0';
}
int n, m;
long long ans, tot;
long long a[7010], b[7010], t[7010];
int main() {
scanf("%d", &n);
map<long long, long long> mp1, mp2;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
mp1[a[i]]++;
}
for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
for (int i = 1; i <= n; i++) mp2[a[i]] += b[i];
for (int i = 1; i <= n; i++) {
if (mp1[a[i]] >= 2) {
ans += mp2[a[i]];
mp1[a[i]] = 0;
t[++tot] = a[i];
}
}
for (int i = 1; i <= n; i++) {
if (mp1[a[i]]) {
for (int j = 1; j <= tot; j++) {
if ((t[j] | a[i]) == t[j]) {
ans += mp2[a[i]];
break;
}
}
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 50;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
int a[2500];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
n *= 2;
for (int i = 1; i <= n; ++i) cin >> a[i];
int ans = 0;
for (int i = 1; i <= n;) {
if (a[i] == a[i + 1]) {
i += 2;
continue;
}
int j;
for (j = i + 1; j <= n; ++j)
if (a[j] == a[i]) break;
ans += j - (i + 1);
for (int k = j; k > i; --k) a[k] = a[k - 1];
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, P = 998244353;
int n;
int fw[N], fw2[N];
bool vis[N];
int a[N][N], dp[N][N];
int lowBit(int k) { return k & -k; }
void ch(int* fw, int k, int x) {
for (; k <= n; k += lowBit(k)) fw[k] += x;
}
int qry(int* fw, int k) {
int ret = 0;
for (; k; k -= lowBit(k)) ret += fw[k];
return ret;
}
int main() {
scanf("%d", &n);
if (n == 1) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
dp[i][0] = dp[i - 1][0] * (long long)i % P;
for (int j = 1; j <= i; ++j) {
dp[i][j] = (dp[i - 1][j - 1] * (long long)(i - j) +
(j > 1 ? dp[i - 1][j - 2] * (long long)(j - 1) : 0)) %
P;
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
ch(fw, a[1][i], 1);
ans = (ans + dp[n - i][0] * (long long)(a[1][i] - qry(fw, a[1][i]))) % P;
}
for (int i = 2; i <= n; ++i) {
memset(fw, 0, sizeof(fw));
memset(fw2, 0, sizeof(fw2));
memset(vis, 0, sizeof(vis));
int con = n;
ans = ans * (long long)dp[n][n] % P;
for (int j = 1; j < n; ++j) {
bool flag = a[i - 1][j] < a[i][j] && !vis[a[i - 1][j]];
ch(fw, a[i][j], 1);
if (vis[a[i - 1][j]] ^= true) {
ch(fw2, a[i - 1][j], 1);
--con;
}
int chose = a[i][j] - 1 - qry(fw, a[i][j] - 1),
par = qry(fw2, a[i][j] - 1);
int tmp = ans;
if (flag) {
ans = (ans + (par - 1) * (long long)dp[n - j][con] +
(chose - par) * (long long)dp[n - j][con - 1]) %
P;
} else {
ans = (ans + par * (long long)dp[n - j][con] +
(chose - par) * (long long)dp[n - j][con - 1]) %
P;
}
if (vis[a[i][j]] ^= true)
--con;
else {
ch(fw2, a[i][j], -1);
}
}
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T read1() {
T t = 0;
char k;
bool v = 0;
do (k = getchar()) == '-' && (v = 1);
while ('0' > k || k > '9');
while ('0' <= k && k <= '9')
t = (t << 3) + (t << 1) + (k ^ '0'), k = getchar();
return v ? -t : t;
}
int f[200005], s, m;
int Find(int n) { return f[n] ^ n ? f[n] = Find(f[n]) : n; }
bool judge(int u, int v) { return Find(u) == Find(v); }
void Merge(int u, int v) { f[Find(u)] = Find(v); }
int qkpow(int n) {
if (n < 63) return (1ll << n) % 1000000007;
long long t = qkpow(n >> 1);
t = t * t % 1000000007;
if (n & 1) t = (t << 1) % 1000000007;
return t;
}
int main() {
s = read1<long long>(), m = read1<long long>();
int t = s;
for (int i = 1; i <= (s << 1); ++i) f[i] = i;
for (int i = 1; i <= m; ++i) {
int u = read1<long long>(), v = read1<long long>();
if (!read1<long long>()) {
if (judge(u, v) || judge(u + s, v + s)) return puts("0"), 0;
if (!judge(u, v + s)) --t, Merge(u, v + s);
Merge(u + s, v);
} else {
if (judge(u + s, v) || judge(u, v + s)) return puts("0"), 0;
if (!judge(u, v)) --t, Merge(u, v);
Merge(u + s, v + s);
}
}
printf("%d\n", qkpow(t - 1));
return 0;
}
| 3 |
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
int i;
vector<char> prime;
for(i=0;i<10000000;i++)
{
prime.push_back('1');
}
prime[0]=prime[1]='0';
for(i=0;i<10000000;i++)
{
if(prime[i]=='1')
{
for(int j=2;j*i<10000000;j++)
{
prime[j*i]='0';
}
}
}
for(i=0;i<10000000;i++)
{
if(i+8<10000000)
{
if((prime[i]=='1' || prime[i]=='2') && (prime[i+2]=='1' || prime[i+2]=='2') && (prime[i+6]=='1' || prime[i+6]=='2') && (prime[i+8]=='1' || prime[i+8]=='2'))
{
prime[i+8]='2';
}
}
}
for(;;)
{
cin >> n ;
if(n==0)
break;
for(;prime[n]!='2';--n)
{
}
cout << n << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, gen;
long long pos;
string f0 =
"What are you doing at the end of the world? Are you busy? Will you save "
"us?";
string formal = "What are you doing while sending \"";
string latter = "\"? Are you busy? Will you send \"";
string last = "\"?";
long long getLength(int n) {
if (n > 55) {
return LLONG_MAX - 1;
}
return pow(2, n) * (75 + 68) - 68;
}
void recursive(int n, long long pos) {
if (n == 0) {
printf("%c", f0[pos - 1]);
return;
}
if (pos <= 34) {
printf("%c", formal[pos - 1]);
return;
}
pos -= 34;
long long temp = getLength(n - 1);
if (pos <= temp) {
recursive(n - 1, pos);
return;
}
pos -= temp;
if (pos <= 32) {
printf("%c", latter[pos - 1]);
return;
}
pos -= 32;
if (pos <= temp) {
recursive(n - 1, pos);
return;
}
pos -= temp;
if (pos < 3) {
printf("%c", last[pos - 1]);
return;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %lld", &gen, &pos);
long long length = 0;
int j;
if (getLength(gen) < pos) {
printf(".");
continue;
}
recursive(gen, pos);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int t = 1;
while (t--) {
long long int n;
cin >> n;
long long int arr[n];
long long int temp[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
temp[i] = arr[i];
}
long long int bob = n - 1;
long long int al = 0;
long long int ca = 0, cb = 0;
bool a_still_eating = false;
bool b_still_eating = false;
while (al <= bob) {
long long int maxi = max(arr[bob], arr[al]);
long long int sb = 0;
long long int sa = 0;
if (al == bob && a_still_eating == false && b_still_eating == false) {
ca++;
al++;
break;
}
if (al == bob) {
if (a_still_eating)
ca++;
else
cb++;
break;
}
while (sa < maxi && al <= bob) {
if (arr[al] <= maxi - sa) {
ca++;
sa += arr[al];
arr[al] = 0;
al++;
a_still_eating = false;
} else {
arr[al] -= (maxi - sa);
sa = maxi;
a_still_eating = true;
}
}
while (sb < maxi && bob >= al) {
if (arr[bob] <= maxi - sb) {
cb++;
sb += arr[bob];
arr[bob] = 0;
bob--;
b_still_eating = false;
} else {
arr[bob] -= (maxi - sb);
sb = maxi;
b_still_eating = true;
}
}
}
cout << ca << " " << cb;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> A(N, 0), r(N, 0);
for (int i = 0; i < N; i++) { (cin >> A[i]), A[i] = i - A[i]; }
for (int i = 0, maxi = 0; i < N; i++) {
if (A[i] < maxi or A[i] > i) { return (cout << -1 << endl), 0; }
maxi = max(maxi, A[i]), r[A[i]] = i - A[i];
}
cout << accumulate(r.begin(), r.end(), 0LL) << endl;
return 0;
}
| 0 |
#include <iostream>
#include <regex>
using namespace std;
int main() {
string S;
cin >> S;
cout << (regex_search(S.begin(), S.end(), regex("AC")) ? "Yes" : "No") << endl;
} | 0 |
#include<iostream>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(void){
int n;
while(cin>>n,n){
int a[n],sum=0;
r(i,n)cin>>a[i];
r(i,n)for(int j=1;j<n;j++){
if(a[j-1]>a[j]){
swap(a[j-1],a[j]);
sum++;
}
}
cout<<sum<<endl;
}
} | 0 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
const int oo=1073741819;
using namespace std;
struct point{
int x,y,A,B,D;
}G[2005];
int f[205][105][105],g[105][105][3],b[105][105],d[205][105][105];
int n,m,s,t,F;
inline void Up(int &x,int y)
{
if (x>y) x=y;
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&s,&t,&F);
s++,t++;
int id=0;
for (int i=1;i<=m;i++) {
scanf("%d%d%d%d%d",&G[i].x,&G[i].y,&G[i].A,&G[i].B,&G[i].D);
G[i].x++,G[i].y++;
if (G[i].A<G[i].B) id=i;
}
for (int P=1;P<=F;P++) {
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (i!=j) f[P][i][j]=d[P][i][j]=oo;else f[P][i][j]=d[P][i][j]=0;
for (int i=1;i<=m;i++) {
if (G[i].A<G[i].B) continue;
int x=G[i].x,y=G[i].y;
if (P>G[i].D) Up(f[P][x][y],G[i].A*G[i].D+G[i].B*(P-G[i].D));
else Up(f[P][x][y],P*G[i].A);
}
for (int k=1;k<=n;k++)
for (int i=1;i<=n;i++)
if (f[P][i][k]<oo)
for (int j=1;j<=n;j++)
if (f[P][k][j]<oo)
Up(f[P][i][j],f[P][i][k]+f[P][k][j]);
}
if (id)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int p=0;p<=F;p++) {
int cos=0;
if (p>G[id].D) cos=G[id].A*G[id].D+G[id].B*(p-G[id].D);
else cos=G[id].A*p;
if (f[F-p][i][j]<oo && f[p][i][G[id].x]<oo && f[p][G[id].y][j]<oo)
Up(d[F][i][j],f[F-p][i][j]+f[p][i][G[id].x]+cos+f[p][G[id].y][j]);
}
int ans=min(f[F][s][t],d[F][s][t]);
//cout<<f[F][s][s]<<endl;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (f[F][s][i]<oo && d[F][i][j]<oo && f[F][j][t]<oo)
Up(ans,f[F][s][i]+d[F][i][j]+f[F][j][t]);
if (ans<oo) printf("%d\n",ans);
else printf("Impossible\n");
return 0;
} | 0 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int, int> seg;
int R, C, N;
vector<seg> edges;
stack<int> st;
int Convert(int x, int y)
{
if (y == 0) return x;
if (x == R) return R + y;
if (y == C) return R + C + R - x;
if (x == 0) return R + C + R + C - y;
return -1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin >> R >> C >> N;
for (int i = 1; i <= N; i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int p = Convert(a, b), q = Convert(c, d);
if (p >= 0 && q >= 0)
edges.emplace_back(make_pair(min(p, q), max(p, q)));
}
sort(edges.begin(), edges.end());
bool flag = true;
for (auto k : edges)
{
while (!st.empty() && k.first >= st.top())
st.pop();
if (!st.empty() && k.second > st.top())
{
flag = false;
break;
}
st.push(k.second);
}
cout << (flag ? "YES" : "NO") << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
cin >> n;
while (n--) {
cin >> x;
cout << (x + 1) / 10 << endl;
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
struct node{
string name;
int length;
} song[1010];
int n,fl=0,ans=0;
string s;
int main()
{ cin>>n;
for(int i=1;i<=n;++i)
cin>>song[i].name>>song[i].length;
cin>>s;
for(int i=1;i<=n;++i)
if(fl)
ans+=song[i].length;
else if(song[i].name==s)
fl=1;
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x, y, max = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (x + y > max) max = x + y;
}
cout << max;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
char a[9][9];
int k1[2], k2[2];
cin >> t;
while (t--) {
int isK1 = 0;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
cin >> a[i][j];
if (a[i][j] == 'K' && isK1) {
isK1 = 0;
k1[0] = i;
k1[1] = j;
} else if (a[i][j] == 'K' && !isK1) {
isK1 = 1;
k2[0] = i;
k2[1] = j;
}
}
}
if ((k1[0] - k2[0]) % 4 == 0 && (k1[1] - k2[1]) % 4 == 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
template<typename T>inline bool chkmin(T &x,T y){return (y<x)?(x=y,1):0;}
template<typename T>inline bool chkmax(T &x,T y){return (y>x)?(x=y,1):0;}
inline int read(){
int x;
char c;
int f=1;
while((c=getchar())!='-' && (c>'9' || c<'0'));
if(c=='-') f=-1,c=getchar();
x=c^'0';
while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
return x*f;
}
inline ll readll(){
ll x;
char c;
int f=1;
while((c=getchar())!='-' && (c>'9' || c<'0'));
if(c=='-') f=-1,c=getchar();
x=c^'0';
while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
return x*f;
}
const int maxn=2e5+10,inf=0x3f3f3f3f;
int idx[maxn],idx_cnt,a[maxn];
struct Segment_tree{
int Min[maxn<<2],tag[maxn<<2];
inline void push_down(int x){
if(tag[x]){
Min[x<<1]+=tag[x];
Min[x<<1|1]+=tag[x];
tag[x<<1]+=tag[x];
tag[x<<1|1]+=tag[x];
tag[x]=0;
}
}
inline void push_up(int x){
Min[x]=min(Min[x<<1],Min[x<<1|1]);
}
void build_tree(int x,int L,int R){
if(L==R){
Min[x]=idx[L];
return;
}
int Mid=(L+R)>>1;
build_tree(x<<1,L,Mid),build_tree(x<<1|1,Mid+1,R);
push_up(x);
}
void update(int x,int L,int R,int ql,int qr,int v){
if(ql>qr) return;
if(ql<=L && R<=qr){
tag[x]+=v,Min[x]+=v;
return;
}
int Mid=(L+R)>>1;
push_down(x);
if(ql<=Mid) update(x<<1,L,Mid,ql,qr,v);
if(qr>Mid) update(x<<1|1,Mid+1,R,ql,qr,v);
push_up(x);
}
int query(int x,int L,int R,int ql,int qr){
if(ql<=L && R<=qr) return Min[x];
int Mid=(L+R)>>1,res=inf;
push_down(x);
if(ql<=Mid) chkmin(res,query(x<<1,L,Mid,ql,qr));
if(qr>Mid) chkmin(res,query(x<<1|1,Mid+1,R,ql,qr));
push_up(x);
return res;
}
}Seg;
ll ans;
int main(){
int n=read();
REP(i,1,n) a[i]=read();
REP(i,1,n) idx[++idx_cnt]=a[i];
sort(idx+1,idx+idx_cnt+1);
idx_cnt=unique(idx+1,idx+idx_cnt+1)-idx-1;
Seg.build_tree(1,1,idx_cnt);
REP(i,1,n){
int u=lower_bound(idx+1,idx+idx_cnt+1,a[i])-idx;
ans+=Seg.query(1,1,idx_cnt,u,idx_cnt)-a[i];
Seg.update(1,1,idx_cnt,1,u-1,1);
}
printf("%lld\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 5010;
vector<int> G[maxn];
int d[maxn][maxn][2];
void check(int &a, int b) {
if (a > b) a = b;
}
int dfs(int u, int fa) {
int num = G[u].size(), tot = 0, now = 0;
d[u][0][0] = 0;
d[u][0][1] = 0;
if (num == 1) {
d[u][0][1] = INF;
d[u][1][1] = 0;
return 1;
}
for (int i = 0; i < num; ++i) {
int v = G[u][i];
if (v == fa) continue;
now = dfs(v, u);
tot += now;
for (int j = tot; j >= 0; --j) {
int mn0, mn1;
mn0 = mn1 = INF;
for (int k = 0; k <= now; ++k) {
check(mn0, min(d[u][j - k][0] + d[v][k][0],
d[u][j - k][0] + d[v][k][1] + 1));
check(mn1, min(d[u][j - k][1] + d[v][k][0] + 1,
d[u][j - k][1] + d[v][k][1]));
}
d[u][j][0] = mn0;
d[u][j][1] = mn1;
}
}
return tot;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if (n == 2) {
puts("1");
return 0;
}
int cnt = 0, p = 1;
memset(d, INF, sizeof d);
for (int i = 1; i <= n; ++i) {
if (G[i].size() == 1)
++cnt;
else
p = i;
}
dfs(p, 0);
int ans = min(d[p][cnt / 2][0], d[p][cnt / 2][1]);
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll,ll> P;
vector<int> makePrimeList(){
int range = 1000000;
vector<int> ret;
vector<bool> notp(range);
notp[0] = notp[1] = true;
for(int i = 2;i < range;i++){
if(!notp[i]){
ret.PB(i);
for(int j = 2 * i;j < range;j += i){
notp[j] = true;
}
}
}
return ret;
}
void check(vector<ll> v, vector<ll> &ans, ll now){
bool flag = true;
int n = v.size();
REP(i, n)if(v[i] % now)flag = false;
if(flag)ans.PB(now);
}
int main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
ll n;cin >> n;
vector<ll> v(n+1);
REV(i, n+1)cin >> v[i];
auto p = makePrimeList();
vector<ll> ans;
for(ll i = 0;p[i] <= n;i++){
if(!(v[0] == 0 || v[0] % p[i] == 0))continue;
vector<ll> now(p[i]-1, 0);
for(int j = 1;j <= n;j++){
now[(j-1)%(p[i]-1)] += v[j];
}
bool flag = true;
REP(j, p[i]-1){
if(abs(now[j]) % p[i] != 0)flag = false;
}
if(flag)ans.PB(p[i]);
}
REP(i,n+1)v[i] = abs(v[i]);
REP(i, p.size()){
if(p[i] > n && v[n] % p[i] == 0){
check(v, ans, p[i]);
}
while(v[n] != 0 && v[n] % p[i] == 0)v[n] /= p[i];
}
if(v[n] > 1){
check(v, ans, v[n]);
}
REP(i, ans.size()){
cout << ans[i] << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = (1 << 29) - 1;
const double eqs = 1e-8;
const int N = 100003;
int have[N];
int a[N];
int b[N];
long long pow_mod(long long a, int b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main() {
int n, i, j, k, qmax = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", a + i);
have[a[i]]++;
qmax = max(qmax, a[i]);
}
for (i = qmax - 1; i >= 0; i--) have[i] += have[i + 1];
int front = 0, rear = N - 1;
long long ans = 0;
for (i = 2; i <= qmax; i++) {
front = 0, rear = N - 1;
for (j = 1; j * j <= i; j++)
if (i % j == 0) {
b[front++] = j;
b[rear--] = i / j;
if (b[front - 1] == b[rear + 1]) rear++;
}
while (rear < N - 1) b[front++] = b[++rear];
long long tmp = 1;
for (k = front - 1; k >= 0; k--) {
if (k == front - 1)
tmp = tmp *
((pow_mod(k + 1, have[b[k]]) - pow_mod(k, have[b[k]]) + mod) %
mod) %
mod;
else
tmp = tmp * pow_mod(k + 1, have[b[k]] - have[b[k + 1]]) % mod;
}
ans += tmp;
}
ans++;
cout << ans % mod << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000007;
const long long int inf = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-9;
const double PI = acos(-1);
long long int i, j, k;
void solve(void);
inline bool equal(double a, double b) { return fabs(a - b) < eps; }
int main() {
cout << fixed << setprecision(10);
int t = 1;
while (t--) solve();
return 0;
}
map<pair<int, string>, int> ct;
void solve() {
int k, n, m, q;
cin >> k >> n >> m >> q;
unordered_map<string, vector<pair<string, int> > > compp;
string s;
for (i = (1); i <= (n); ++i) cin >> s;
for (i = (1); i <= (m); ++i) {
string x, S;
cin >> x;
x = x.substr(0, (long long)x.size() - 1);
string t;
int num;
bool next = 1;
getline(cin, S);
{};
for (j = (1); j <= ((long long)S.size() - 1); ++j) {
{};
if (S[j] == ' ') {
next ^= 1;
continue;
}
if (S[j] == ',') continue;
if (next)
t += S[j];
else
num = S[j] - '0';
{};
if (next == 0) {
compp[x].emplace_back(make_pair(t, num));
t.clear();
}
}
}
unordered_map<int, vector<string> > ans;
while (q--) {
int x;
string s;
cin >> x >> s;
{};
ct[make_pair(x, s)]++;
pair<int, string> p = make_pair(x, s);
for (auto it : compp) {
bool pos = 1;
for (auto jt : it.second) {
string t = jt.first;
int val = jt.second;
if (ct[make_pair(x, t)] < val) {
pos = 0;
break;
}
}
if (pos) {
ans[x].emplace_back(it.first);
for (auto jt : it.second) {
string t = jt.first;
int val = jt.second;
ct[make_pair(x, t)] -= val;
}
break;
}
}
}
for (auto it : ct) {
int val = it.second;
while (val--) ans[it.first.first].emplace_back(it.first.second);
}
for (i = (1); i <= (k); ++i) {
map<string, int> temp;
for (auto it : ans[i]) temp[it]++;
cout << (long long)temp.size() << '\n';
for (auto it : temp) cout << it.first << " " << it.second << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long A[(800000 + 10)], B[(800000 + 10)], C[(800000 + 10)],
d[(800000 + 10)], h[(800000 + 10)];
void up(int id) {
int ls = id << 1, rs = ls | 1;
A[id] = max(A[ls], A[rs]);
B[id] = max(B[ls], B[rs]);
C[id] = max(B[ls] + A[rs], max(C[ls], C[rs]));
C[id] = max(B[rs] + A[ls], C[id]);
}
void build(int id, int l, int r) {
if (l == r) {
A[id] = 2 * h[l] - d[l - 1];
B[id] = 2 * h[l] + d[l - 1];
C[id] = 0;
return;
}
int m = l + r >> 1;
build(id << 1, l, m), build(id << 1 | 1, m + 1, r);
up(id);
}
long long query(int id, int l, int r, int ll, int rr, long long &aa,
long long &bb) {
if (l == ll && r == rr) {
aa = A[id];
bb = B[id];
return C[id];
}
int m = l + r >> 1;
if (rr <= m) {
return query(id << 1, l, m, ll, rr, aa, bb);
} else if (ll > m)
return query(id << 1 | 1, m + 1, r, ll, rr, aa, bb);
else {
long long la, lb, ra, rb;
long long t1 = query(id << 1, l, m, ll, m, la, lb),
t2 = query(id << 1 | 1, m + 1, r, m + 1, rr, ra, rb);
long long res = max(t1, t2);
res = max(lb + ra, res);
res = max(la + rb, res);
aa = max(la, ra);
bb = max(lb, rb);
return res;
}
}
int main() {
int n, m;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; ++i) scanf("%I64d", &d[i]);
for (int i = 1; i <= n; ++i) scanf("%I64d", &h[i]);
for (int i = n + 1; i <= 2 * n; ++i) h[i] = h[i - n], d[i] = d[i - n];
for (int i = 2; i <= n * 2; ++i) d[i] = d[i] + d[i - 1];
build(1, 1, 2 * n);
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
if (l > r) {
swap(l, r);
++l;
--r;
} else {
int tl = l, tr = r;
l = tr + 1;
r = tl + n - 1;
}
long long aa, bb;
printf("%I64d\n", query(1, 1, 2 * n, l, r, aa, bb));
}
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
#define ll long long
#define re register
#define ull unsigned ll
using namespace std;
inline int read(){
int s=0,t=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*t;
}
const int N=5e5+5;
const int P=998244353;
int n,m,Ans,fac[N],inv[N],fn[N],fm[N];
int ksm(int x,int k){
int res=1;
while(k){
if(k&1)res=1ll*res*x%P;
x=1ll*x*x%P,k>>=1;
}return res;
}
void Pre(int Lim){
fac[0]=1;for(int i=1;i<=Lim;i++)fac[i]=1ll*fac[i-1]*i%P;
inv[Lim]=ksm(fac[Lim],P-2);for(int i=Lim;i>=1;i--)inv[i-1]=1ll*inv[i]*i%P;
fn[0]=1;for(int i=1;i<=Lim;i++)fn[i]=1ll*fn[i-1]*(n+1)%P;
fm[0]=1;for(int i=1;i<=Lim;i++)fm[i]=1ll*fm[i-1]*(m+1)%P;
}
int C(int n,int m){return (n<m||m<0)?0:1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read(),Pre(5e5);
for(int i=0;i<=n;i++){
int t0=1ll*(i&1?P-1:1)*C(n,i)%P*C(m,i)%P;
int t1=1ll*fac[i]%P*fn[m-i]%P*fm[n-i]%P;
Ans=(Ans+1ll*t0*t1%P)%P;
}printf("%d",Ans);
return 0;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class RangeAddQuery {
public:
explicit RangeAddQuery(int n) : N(calcN_(n)) {
mVal.assign(2*N+1, 0);
}
void update(int l, int r, T value){
l = max(0, l);
r = min(N, r);
int offset = N;
while(offset > 0){
if(l >= r) break;
if(l&1){ update_(mVal[offset+l-1], value); l++; }
if(r&1){ update_(mVal[offset+r-2], value); }
l /= 2;
r /= 2;
offset /= 2;
}
}
T get(int idx){
int i = N + idx - 1;
auto res = mVal[i];
while(i > 0){
i = (i-1)/2;
update_(res, mVal[i]);
}
return res;
}
private:
int calcN_(int n){
int res = 1;
while(res < n) res *= 2;
return res;
}
inline void update_(T& data, T val) { data += val; }
const int N;
vector<T> mVal;
};
int main(){
int n, q;
while(cin >> n >> q){
RangeAddQuery<int> raq(n+1);
for(int i=0;i<q;i++){
int c; cin >> c;
if(c == 0){
int s, t, x; cin >> s >> t >> x;
raq.update(s, t+1, x);
} else {
int p; cin >> p;
cout << raq.get(p) << endl;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[200010];
string S;
int main() {
int a;
scanf("%d", &a);
int p = 0;
int l = 0;
int r = a - 1;
for (int i = 0; i < a; i++) {
scanf("%d", &arr[i]);
}
int lc = 0;
int rc = 0;
while (l <= r) {
if (arr[l] >= p) {
lc = 1;
}
if (arr[r] >= p) {
rc = 1;
}
if (lc && rc) {
if (arr[l] < arr[r]) {
S.append("L");
p = arr[l];
l++;
} else {
S.append("R");
p = arr[r];
r--;
}
} else if (lc) {
S.append("L");
p = arr[l];
l++;
} else if (rc) {
S.append("R");
p = arr[r];
r--;
} else {
break;
}
lc = 0;
rc = 0;
}
cout << S.length() << endl << S;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
double f[N][N];
double dp(int n, int m) {
if (!n) return 1.0 / (m + 1);
if (!m) return 1;
if (f[n][m]) return f[n][m];
double A = (1 - dp(m - 1, n)) * m / (m + 1);
double B = (1 - dp(m - 1, n)) * m / (m + 1) + 1.0 / (m + 1);
double C = 1;
double D = 1 - dp(m, n - 1);
double p = (D - C) / ((A - C) - (B - D));
return f[n][m] = A * p + C * (1 - p);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
printf("%.10lf %.10lf\n", dp(n, m), 1 - dp(n, m));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
multiset<int> s;
int n, can, a, res(0);
cin >> n;
cin >> can;
for (int i = 0; i < n - 1; i++) cin >> a, s.insert(a);
multiset<int>::reverse_iterator it;
multiset<int>::iterator lo;
it = s.rbegin();
while (can <= *it) {
a = *it - 1;
lo = s.lower_bound(*it);
s.erase(lo);
s.insert(a);
can++;
res++;
it = s.rbegin();
}
cout << res;
return 0;
}
| 1 |
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
int dp[1010][1010]={};
int n,m;cin>>n>>m;
int d[1010];
int c[1010];
for(int i=0;i<n;i++)cin>>d[i];
for(int i=0;i<m;i++)cin>>c[i];
for(int i=0;i<n;i++)for(int j=0;j<m;j++)dp[i][j]=1<<28;
for(int i=0;i<=m;i++)dp[n][i]=0;
for(int i=n-1;i>=0;i--)
{
for(int j=m;j>=0;j--)
{
if(j+1<=m)dp[i][j]=min(dp[i][j+1],dp[i+1][j+1]+d[i]*c[j]);
else if(i!=n)dp[i][j]=1<<28;
}
}
cout<<dp[0][0]<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
template <typename T>
inline void SWAP(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
template <typename T>
inline T ABS(const T &val) {
return val < 0 ? -val : val;
}
template <typename T>
inline T MAX(const T &a, const T &b) {
return a > b ? a : b;
}
template <typename T>
inline T MIN(const T &a, const T &b) {
return a < b ? a : b;
}
const int INTinf = 2147483647;
const int nINTinf = 0 - 2147483648;
using namespace std;
int n;
list<pair<int, int> > *lst[100005];
int xr[100005];
int mass[1000];
int kol;
inline void func(const pair<int, int> &q) {
mass[kol++] = xr[q.second] ^ xr[q.first];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
lst[i] = new list<pair<int, int> >();
}
for (int i = 1; i != n; i++) {
int sum = (i << 1) | 1;
int kon = i + 1;
while (sum <= n) {
lst[sum]->push_back(pair<int, int>(i - 1, kon));
sum += ++kon;
}
}
lst[n]->sort(greater<pair<int, int> >());
xr[0] = 0;
xr[1] = 0;
for (int i = 1; i != n; i++) {
kol = 0;
for_each((*lst[i]).begin(), (*lst[i]).end(), func);
sort(mass, mass + kol);
kol = unique(mass, mass + kol) - mass;
int l, r;
l = 0;
r = kol;
while (l < r) {
int m = (l + r) >> 1;
if (mass[m] == m)
l = m + 1;
else
r = m;
}
xr[i] = xr[i - 1] ^ l;
}
for (list<pair<int, int> >::iterator it = lst[n]->begin();
it != lst[n]->end(); it++) {
if (!(xr[it->second] ^ xr[it->first])) {
cout << it->second - it->first << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int mx = 0, a = 0, n;
string s;
cin >> n;
while (cin >> s) {
for (char c : s)
if (isupper(c)) a++;
mx = max(mx, a), a = 0;
}
cout << mx << endl;
}
| 1 |
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(void)
{
while(1){
int n;
cin >> n;
if(!n) break;
int ret = 0;
while(n){
n /= 5;
ret += n;
}
cout << ret << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long q;
cin >> q;
while (q--) {
long long s = 0, n, h, p[200001], c = 0, f = 0;
cin >> h >> n;
for (long long i = 0; i < n; ++i) cin >> p[i];
p[n] = 0;
for (long long i = 1; i <= n; ++i) {
if (p[i - 1] - p[i] == 1)
c++;
else if (f == 0) {
f = 1;
if (c % 2) s++;
c = 0;
} else {
if (c % 2 == 0) s++;
c = 0;
}
}
cout << s << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-')) c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 1e6 + 10;
int n, m, a, b, c, d;
vector<int> e[N];
int fa[N], siz[N], dep[N];
inline void dfs(int u) {
siz[u] = (((int)(e[u]).size()) == 1), dep[u] = dep[fa[u]] + 1;
for (auto v : e[u])
if (v != fa[u]) fa[v] = u, dfs(v), siz[u] += siz[v];
}
inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
if (dep[x] > dep[y])
while (dep[x] > dep[y]) x = fa[x];
if (x == y) return x;
while (x != y) x = fa[x], y = fa[y];
return x;
}
inline int jump(int u, int k) {
for (register int i = (1); i <= (k); i++) u = fa[u];
return u;
}
vector<int> v1, v2, v3;
bool vis[N];
inline void add(vector<int> &v, int x, int y) {
if (x == y) return;
int las = x;
x = fa[x];
while (x != y) {
for (auto son : e[x])
if (son != fa[x] && son != las && !vis[son])
v.push_back(siz[son]), vis[son] = 1;
las = x, x = fa[x];
}
}
inline bitset<N / 2> work(vector<int> v) {
bitset<N / 2> f;
f[0] = 1;
for (auto i : v) f = f | (f << i);
return f;
}
inline bool check(int a, int b, int c) {
memset((vis), (0), sizeof(vis));
vis[jump(a, dep[a] - 2)] = vis[jump(b, dep[b] - 2)] =
vis[jump(c, dep[c] - 2)] = 1;
vector<int> v;
add(v, a, 1), add(v, c, 1);
for (auto son : e[1])
if (!vis[son]) v.push_back(siz[son]);
auto f = work(v);
if (m / 2 - 1 - siz[b] >= 0 && f[m / 2 - 1 - siz[b]]) return 1;
return 0;
}
int main() {
n = read(), a = read(), b = read(), c = read(), d = read();
for (register int i = (2); i <= (n); i++) {
int fa = read();
e[fa].push_back(i), e[i].push_back(fa);
}
dfs(1), m = siz[1];
if ((m & 1) || lca(a, b) != 1 || lca(c, d) != 1) return puts("NO"), 0;
if (check(a, c, b) && check(c, b, d)) return puts("YES"), 0;
if (check(a, d, b) && check(c, b, d)) return puts("YES"), 0;
puts("NO");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[100005], v, n, b[100005], tot = 0, ff[40];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
v = 0;
for (int i = 1; i <= 30; i++) {
memset(b, 0, sizeof(b));
memset(ff, 0, sizeof(ff));
for (int j = 1; j <= n; j++)
if ((a[j] & (1 << i)) == (1 << i)) {
b[0]++;
b[b[0]] = a[j];
for (int k = 0; k < i; k++)
if ((b[b[0]] & (1 << k)) == 0) ff[k] = 1;
}
for (int k = 0; k < i; k++)
if (!ff[k]) goto NE;
v = max(v, i);
NE:;
}
for (int i = 1; i <= n; i++)
if ((a[i] & (1 << v)) == (1 << v)) tot++;
cout << tot << endl;
bool flag = 0;
for (int i = 1; i <= n; i++)
if ((a[i] & (1 << v)) == (1 << v)) {
if (!flag)
flag = 1;
else
printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+1;
int n,fa[N],tot[N],fr[N],blk[N],wht[N],c[N],cnt,head[N];
struct nd{int ne,to;}e[N];
void in(int x,int y){e[++cnt].to=y;e[cnt].ne=head[x];head[x]=cnt;}
int can[5001],tmp[N];
bool dfs(int x)
{
for(int i=head[x];i;i=e[i].ne) if(!dfs(e[i].to)) return false;
if(!head[x]){
blk[x]=tot[x];
return true;
}
tmp[0]=0;
int ret=tot[x],tot1=0;
for(int i=head[x];i;i=e[i].ne)
{
ret-=min(blk[e[i].to],wht[e[i].to]);
if(ret<0) return false;
tmp[++tmp[0]]=abs(blk[e[i].to]-wht[e[i].to]);
tot1+=blk[e[i].to]+wht[e[i].to];
}
memset(can,0,sizeof(can));
can[0]=1;
for(int i=1;i<=tmp[0];++i)
for(int t=5000;t>=tmp[i];--t)
can[t]|=can[t-tmp[i]];
for(int i=ret;i>=0;--i)
if(can[i]){ret-=i;break;}
blk[x]=tot[x];wht[x]=tot1+ret-blk[x];
return true;
}
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;++i)
scanf("%d",&fa[i]),in(fa[i],i);
for(int i=1;i<=n;++i)
{
scanf("%d",&tot[i]);
}
if(dfs(1)) cout<<"POSSIBLE";
else cout<<"IMPOSSIBLE";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int TT;
cin >> TT;
while (TT--) {
int N;
long long K;
cin >> N >> K;
vector<int> A(N);
long long sum = 0;
for (int i = 0; i < N; ++i) {
cin >> A[i];
sum += A[i];
}
sort(A.rbegin(), A.rend());
long long ans = sum - K;
sum -= K;
for (int i = 0; i < N - 1; ++i) {
sum -= A[i] - A[N - 1];
void(37);
ans = min(ans, max(0LL, (sum + (i + 1)) / (i + 2)) + (i + 1));
}
cout << max(0LL, ans) << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
void solve() {
int n;
cin >> n;
int a[n + 2];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long int tot = a[n];
long long int cnt = 0;
for (int i = 2; i <= n; i++) {
if (a[i] > a[i - 1]) {
cnt += (a[i] - a[i - 1]);
}
}
if (cnt > tot) {
cout << "NO" << '\n';
return;
} else {
cout << "YES" << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
cin >> T;
while (T--) {
solve();
}
}
| 4 |
Subsets and Splits