solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int N, total = 0;
set<string> S;
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
string s1, s2;
cin >> s1 >> s2;
string A = string(1, s1[0]), B = string(1, s1[1]), C = string(1, s2[0]),
D = string(1, s2[1]);
if (S.insert(A + B + D + C).second) {
S.insert(B + D + C + A);
S.insert(D + C + A + B);
S.insert(C + A + B + D);
total++;
}
if (i < N - 1) cin >> s1;
}
cout << total << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a[9];
long long dfs(long long vis, long long w) {
if (vis > 8) return w;
long long res = min(a[vis], w / vis), mins = 1e18;
int len = log(840 / vis) / log(2) + 1;
for (long long i = res; i >= max(res - len, (long long)0); i--) {
mins = min(mins, dfs(vis + 1, w - i * vis));
}
return mins;
}
int main() {
long long w;
while (cin >> w) {
for (long long i = 1; i <= 8; i++) cin >> a[i];
cout << w - dfs(1, w) << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int arr[1000000], n;
int main() {
long long i, j, k;
while (scanf("%d", &n) != EOF) {
long long sum = 0;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
if (n == 1) {
cout << arr[0] << endl;
continue;
}
sort(arr, arr + n);
j = 0;
k = sum * 2;
for (i = 0; i < n - 2; i++) {
j = j + arr[i];
k = k + sum - j;
}
cout << k << endl;
}
}
| 1 |
#include <iostream>
using namespace std;
int main(){
long long n = 0, count = 0, last = 0;
cin >> n;
for(int i = 0;i < n; ++i){
int x;
cin >> x;
if(x >= last){
last = x;
}
else{
count += (last - x);
}
}
cout << count;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
std::ostream& operator<<(std::ostream& str, const std::vector<T>& v) {
str << "[";
for (auto n : v) str << n << ", ";
str << "]";
return str;
}
template <typename T>
std::ostream& operator<<(std::ostream& str, const std::unordered_set<T>& v) {
str << "{";
for (auto n : v) {
str << n << ", ";
}
str << "}";
return str;
}
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
int n;
cin >> n;
vector<int> primes;
primes.reserve(n);
vector<vector<int>> factors(n + 1);
vector<char> sieve(n + 1);
for (int i = 2; i <= n; ++i) {
if (!sieve.at(i)) {
for (size_t j = i; j < sieve.size(); j += i) {
sieve.at(j) = true;
factors.at(j).push_back(i);
}
primes.push_back(i);
}
}
unordered_set<int> sequence(primes.begin(), primes.end());
size_t log = n;
for (; log > 0; log >>= 1) {
for (auto p : primes) {
for (auto s : sequence) {
for (auto f : factors.at(s)) {
if (f * s <= n) sequence.insert(f * s);
}
}
}
}
cout << sequence.size() << '\n';
for (auto p : sequence) cout << p << " ";
cout << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
struct Hull {
long long a[MAXN], b[MAXN];
double x[MAXN];
int head, tail;
Hull() : head(1), tail(0) {}
long long get(long long xQuery) {
if (head > tail) return 0;
while (head < tail && x[head + 1] <= xQuery) head++;
x[head] = xQuery;
return a[head] * xQuery + b[head];
}
void add(long long aNew, long long bNew) {
double xNew = -1e18;
while (head <= tail) {
if (aNew == a[tail]) return;
xNew = 1.0 * (b[tail] - bNew) / (aNew - a[tail]);
if (head == tail || xNew >= x[tail]) break;
tail--;
}
a[++tail] = aNew;
b[tail] = bNew;
x[tail] = xNew;
}
};
const int MN = 100111;
int n, m, nFeed;
long long d[MN], p[MN], h[MN], a[MN], s[MN], t[MN];
long long f[111][MN];
int main() {
while (cin >> n >> m >> nFeed) {
for (int i = (2), _b = (n); i <= _b; i++) {
cin >> d[i];
p[i] = p[i - 1] + d[i];
}
for (int i = (1), _b = (m); i <= _b; i++) {
cin >> h[i] >> t[i];
a[i] = t[i] - p[h[i]];
}
sort(a + 1, a + m + 1);
for (int i = (1), _b = (m); i <= _b; i++) s[i] = s[i - 1] + a[i];
for (int i = (1), _b = (nFeed); i <= _b; i++) {
Hull hull;
for (int j = (1), _b = (m); j <= _b; j++) {
f[i][j] = hull.get(a[j]) + a[j] * j - s[j];
if (i > 1) hull.add(-j, f[i - 1][j] + s[j]);
}
}
cout << f[nFeed][m] << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool debug = 1;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long ln, lk, lm;
struct Point {
long long x, y;
Point() {}
Point(long long x, long long y) : x(x), y(y) {}
Point operator*(double o) const { return Point(x * o, y * o); }
Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); }
Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); }
bool operator<(const Point& o) const {
return x < o.x - 1e-9 || (x < o.x + 1e-9 && y < o.y - 1e-9);
}
long long cross(Point b) const { return x * b.y - b.x * y; }
void readin() { scanf("%lld%lld", &x, &y); }
} p[100125];
long long gcd1(long long x, long long y) {
if (x == 0) return y;
if (y == 0) return x;
long long z = y;
while (x % y != 0) {
z = x % y;
x = y;
y = z;
}
return z;
}
pair<long long, long long> cal(int l, int r) {
long long border = 0, inner = 0, s = 0;
for (int i = l;; i = (i + 1) % n) {
Point a = p[i], b = p[i == r ? l : (i + 1) % n];
Point c = a - b;
border += gcd1(abs(c.x), abs(c.y));
s += a.cross(b);
if (i == r) break;
}
assert((s + 2 - border) % 2 == 0);
inner = (s + 2 - border) / 2;
return {inner, border};
}
long long getInner(long long area2, long long border) {
return (area2 + 2 - border) / 2;
}
void cal() {
scanf("%d", &n);
for (int(i) = 0; (i) < (int)(n); (i)++) p[i].readin();
long long tt = cal(0, n - 1).first;
double ans = 0;
for (int(i) = 0; (i) < (int)(n); (i)++) {
long long border = 0, inner = 0, s = 0;
Point a = p[i], b = p[(i + 1) % n], c = a - b;
s += a.cross(b);
border += gcd1(abs(c.x), abs(c.y));
for (int j = (i + 2) % n, t = 0; t < 40; t++, j = (j + 1) % n) {
if ((j + 1) % n == i) break;
a = p[(j - 1 + n) % n];
b = p[j];
s += a.cross(b);
c = a - b;
border += gcd1(abs(c.x), abs(c.y));
c = p[i] - p[j];
long long X = gcd1(abs(c.x), abs(c.y)) - 1;
long long pps = getInner(s + p[j].cross(p[i]), border + X + 1);
pps += X;
int bt = 1 + t + 2;
double prob = 1;
if (n <= 60) {
prob = (double)((1LL << (n - bt)) - 1) /
((1LL << n) - 1 - n - n * (n - 1) / 2);
} else {
for (int(z) = 0; (z) < (int)(bt); (z)++) prob /= 2;
}
ans += prob * pps;
}
}
printf("%.10lf\n", tt - ans);
}
int main() {
cal();
return 0;
}
| 4 |
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
int n,i,j,k,s,t,u;
int carry = 0;
string a, b,d;
vector<int> c;
cin >> n;
for (i = 0; i < n; i++) {
c.clear();
cin >> a;
cin >> b;
while (a.size() > b.size()) {
b = "0" + b;
}
while (b.size() > a.size()) {
a = "0" + a;
}
s = a.size();
for (j = s-1; j >=0; j--) {
t = a[j]-'0' + b[j]-'0';
if (carry == 1) {
carry = 0;
t += 1;
}
while (t >= 10) {
t -= 10;
carry += 1;
}
c.push_back(t);
}
while (carry == 1) {
carry = 0;
c.push_back(1);
}
u = c.size();
if (u > 80||a.size()>80||b.size()>80) {
cout << "overflow" << endl;
}
else if(u<=80){
for (k = u - 1; k >= 0; k--) {
cout << c[k];
}
cout << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline int tavan(int a, int n, int mod) {
int p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
int n, m, d[(1111)][(1111)];
string s[(1111)];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == 'E') {
queue<pair<int, int> > q;
d[i][j] = 1;
q.push({i, j});
while (q.size()) {
int a = q.front().first, b = q.front().second;
q.pop();
if (a > 0 && !d[a - 1][b] && s[a - 1][b] != 'T')
d[a - 1][b] = d[a][b] + 1, q.push({a - 1, b});
if (a < n - 1 && !d[a + 1][b] && s[a + 1][b] != 'T')
d[a + 1][b] = d[a][b] + 1, q.push({a + 1, b});
if (b > 0 && !d[a][b - 1] && s[a][b - 1] != 'T')
d[a][b - 1] = d[a][b] + 1, q.push({a, b - 1});
if (b < m - 1 && !d[a][b + 1] && s[a][b + 1] != 'T')
d[a][b + 1] = d[a][b] + 1, q.push({a, b + 1});
}
int now, ans = 0;
for (int k1 = 0; k1 < n; k1++)
for (int k2 = 0; k2 < m; k2++)
if (s[k1][k2] == 'S') {
now = d[k1][k2];
break;
}
for (int k1 = 0; k1 < n; k1++)
for (int k2 = 0; k2 < m; k2++)
if ('0' < s[k1][k2] && s[k1][k2] <= '9' && d[k1][k2] <= now &&
d[k1][k2])
ans += s[k1][k2] - '0';
cout << ans;
break;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-9;
const int INF = 2e9;
const long long LINF = 1e18;
template <class T>
const T sqr(const T &x) {
return x * x;
}
template <class T>
const T &min(const T &a, const T &b, const T &c) {
return min(min(a, b), c);
}
template <class T>
const T &max(const T &a, const T &b, const T &c) {
return max(max(a, b), c);
}
struct DbgStream {
template <class T>
DbgStream &operator<<(const T &x) {
return *this;
}
} z_dbg;
struct SingleHash {
vector<int> suf, b;
int mod;
SingleHash(string s, int base = 153, int _mod = 1000000009) {
int n = s.length();
suf.assign(n + 1, 0);
b.assign(n + 1, 0);
b[0] = 1;
b[1] = base;
mod = _mod;
for (int i = n - 1; i >= 0; --i) {
suf[i] = (s[i] + (long long)suf[i + 1] * b[1]) % mod;
}
for (int i = 2; i <= n; ++i) {
b[i] = (long long)b[i - 1] * b[1] % mod;
}
}
int substr(int l, int r) const {
long long v = suf[l] - (long long)suf[r + 1] * b[r - l + 1];
v %= mod;
v += mod;
v %= mod;
return v;
}
};
bool check(int m, int n, const SingleHash &h) {
int pref = h.substr(0, m - 1);
for (int i = 1; i + m < n; ++i) {
if (pref == h.substr(i, i + m - 1)) {
return 1;
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
int n;
string s;
cin >> s;
SingleHash h(s);
n = s.length();
vector<int> lens;
lens.reserve(n);
for (int i = 1; i < n; ++i) {
if (h.substr(0, i - 1) == h.substr(n - i, n - 1)) {
lens.push_back(i);
}
}
int l = 0, r = lens.size();
while (r - l > 1) {
int m = (l + r) / 2;
if (check(lens[m], n, h)) {
l = m;
} else {
r = m;
}
}
if (lens.empty() || !check(lens[l], n, h)) {
cout << "Just a legend\n";
return 0;
}
for (int i = 0; i < lens[l]; ++i) {
cout << s[i];
}
cout << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
if (temp % 2 == 0 && temp > 2)
cout << "0" << endl;
else if (temp % 2 == 1 && temp > 2)
cout << "1" << endl;
else
cout << 4 - temp << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
template <typename T>
T abs(T x) {
return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
return x * x;
}
const int maxn = 1 << 13;
int a[maxn];
int b[maxn];
int main() {
for (int i = 0; i < maxn; i++) b[i] = -1;
int n;
scanf("%d", &n);
for (int i = 2; i <= (1 << (n + 1)) - 1; i++) {
scanf("%d", a + i);
}
long long ans = 0;
for (int i = (1 << (n + 1)); i >= 2; i--) {
if (b[i] == -1) b[i] = 0;
int c = b[i] + a[i];
if (b[i / 2] == -1)
b[i / 2] = c;
else {
ans += abs(b[i / 2] - c);
b[i / 2] = max(b[i / 2], c);
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
long long ans=1;
long long a[n];
for(i=0;i<n;i++){
cin>>a[i];
if(a[i]==0){
cout<<0;
return 0;
}
}
for(i=0;i<n;i++){
if(1000000000000000000/ans<a[i]){
cout<<-1;
return 0;
}
ans*=a[i];
}
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int minn(int a, int b) {
if (a < b) return a;
return b;
}
int a[100005], n;
bool ans[32];
int func(int bit = 29, int st = 0, int end = n) {
if (bit < 0) return 0;
if (st == end) return 0;
int i;
sort(a + st, a + end);
for (i = st; i < end; i++) {
if ((a[i] & (1 << bit)) > 0) break;
}
if (i == st || i == end)
return func(bit - 1, st, end);
else
return minn(func(bit - 1, st, i), func(bit - 1, i, end)) + (1 << bit);
}
int main() {
while (cin >> n) {
int i;
for (i = 0; i < n; i++) cin >> a[i];
cout << func() << endl;
}
}
| 4 |
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define MAXN 100010
int n, m, a[MAXN], b[MAXN], c[MAXN], p[MAXN];
struct edge {
int u, v, w;
inline friend bool operator<(const edge &a, const edge &b) {
return a.w < b.w;
}
} e[MAXN];
inline int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
std::vector<int> next[MAXN];
long long sum[MAXN];
long long dp(int x) {
long long ret = c[x];
sum[x] = b[x];
for (int y : next[x]) {
long long res = dp(y);
ret = std::min(ret, std::max(0LL + c[x], res) - sum[y]);
sum[x] += sum[y];
}
return ret + sum[x];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i] = std::max(a[i], b[i]);
c[i] = a[i] - b[i];
p[i] = i;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d", &e[i].u, &e[i].v);
e[i].w = std::max(c[e[i].u], c[e[i].v]);
}
std::sort(e + 1, e + m + 1);
for (int i = 1; i <= m; i++) {
int u = e[i].u, v = e[i].v;
if (find(u) == find(v)) continue;
if (c[u] < c[v]) std::swap(u, v);
next[find(u)].push_back(find(v));
p[find(v)] = find(u);
}
printf("%lld\n", dp(find(1)));
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
std::cin>>a>>b>>c;
std::cout<<max(a,max(b,c))+max(-a,max(-c,-b));
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<string> v;
v.reserve(n);
for (int i = 0; i < n; i++) {
char c[10];
cin >> c;
v.emplace_back(c);
}
unordered_set<string> us;
for (int i = n - 1; i >= 0; i--) {
if (us.count(v[i]) == 0) {
cout << v[i] << "\n";
us.insert(v[i]);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> sz[4];
int ord[4];
char toprint[4];
bool Success() {
int i1 = ord[1], i2 = ord[2], i3 = ord[3];
if (sz[i1].second == sz[i2].second && sz[i2].second == sz[i3].second &&
sz[i1].first + sz[i2].first + sz[i3].first == sz[i1].second) {
printf("%d\n", sz[i1].second);
for (int i = 1; i <= sz[i1].second; i++) {
for (int j = 1; j <= sz[i1].first; j++) {
printf("%c", toprint[i1]);
}
for (int j = 1; j <= sz[i2].first; j++) {
printf("%c", toprint[i2]);
}
for (int j = 1; j <= sz[i3].first; j++) {
printf("%c", toprint[i3]);
}
printf("\n");
}
return true;
} else if (sz[i1].second == sz[i2].second &&
sz[i3].first == sz[i1].first + sz[i2].first &&
sz[i1].second + sz[i3].second == sz[i1].first + sz[i2].first) {
printf("%d\n", sz[i1].second + sz[i3].second);
for (int i = 1; i <= sz[i1].second; i++) {
for (int j = 1; j <= sz[i1].first; j++) {
printf("%c", toprint[i1]);
}
for (int j = 1; j <= sz[i2].first; j++) {
printf("%c", toprint[i2]);
}
printf("\n");
}
for (int i = 1; i <= sz[i3].second; i++) {
for (int j = 1; j <= sz[i3].first; j++) {
printf("%c", toprint[i3]);
}
printf("\n");
}
return true;
} else if (sz[i1].first == sz[i2].first && sz[i2].first == sz[i3].first &&
sz[i1].first == sz[i1].second + sz[i2].second + sz[i3].second) {
printf("%d\n", sz[i1].first);
for (int i = 1; i <= sz[i1].second; i++) {
for (int j = 1; j <= sz[i1].first; j++) {
printf("%c", toprint[i1]);
}
printf("\n");
}
for (int i = 1; i <= sz[i2].second; i++) {
for (int j = 1; j <= sz[i2].first; j++) {
printf("%c", toprint[i2]);
}
printf("\n");
}
for (int i = 1; i <= sz[i3].second; i++) {
for (int j = 1; j <= sz[i3].first; j++) {
printf("%c", toprint[i3]);
}
printf("\n");
}
return true;
} else {
return false;
}
}
int main() {
for (int i = 1; i <= 3; i++) {
scanf("%d %d", &sz[i].first, &sz[i].second);
ord[i] = i;
}
toprint[1] = 'A';
toprint[2] = 'B';
toprint[3] = 'C';
bool res = false;
do {
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
if (a) swap(sz[ord[1]].first, sz[ord[1]].second);
if (b) swap(sz[ord[2]].first, sz[ord[2]].second);
if (c) swap(sz[ord[3]].first, sz[ord[3]].second);
res = (res || Success());
if (res) break;
if (a) swap(sz[ord[1]].first, sz[ord[1]].second);
if (b) swap(sz[ord[2]].first, sz[ord[2]].second);
if (c) swap(sz[ord[3]].first, sz[ord[3]].second);
}
if (res) break;
}
if (res) break;
}
} while (!res && next_permutation(ord + 1, ord + 4));
if (!res) {
printf("-1\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void FastIO() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cout.precision(15);
}
int main() {
FastIO();
long long int a, b, i, n, p, l, r, l1, l2, r1, r2, y, x, s, e;
double ans = 0.0;
vector<pair<long long int, long long int> > v;
cin >> n >> p;
for (i = 0; i < n; i++) {
cin >> l >> r;
v.push_back(make_pair(l, r));
}
for (i = 0; i < n - 1; i++) {
l1 = v[i].first;
l2 = v[i + 1].first;
r1 = v[i].second;
r2 = v[i + 1].second;
x = l1;
y = r1;
if (x % p != 0)
s = x / p + 1;
else
s = x / p;
e = y / p;
a = (e - s + 1);
x = l2;
y = r2;
if (x % p != 0)
s = x / p + 1;
else
s = x / p;
e = y / p;
b = (e - s + 1);
ans = ans + ((double)(1.0 * (a * (r2 - l2 + 1 - b) + b * (r1 - l1 + 1)))) /
((r1 - l1 + 1) * (r2 - l2 + 1));
}
l1 = v[n - 1].first;
l2 = v[0].first;
r1 = v[n - 1].second;
r2 = v[0].second;
x = l1;
y = r1;
if (x % p != 0)
s = x / p + 1;
else
s = x / p;
e = y / p;
a = (e - s + 1);
x = l2;
y = r2;
if (x % p != 0)
s = x / p + 1;
else
s = x / p;
e = y / p;
b = (e - s + 1);
ans = ans + ((double)(1.0 * (a * (r2 - l2 + 1 - b) + b * (r1 - l1 + 1)))) /
((r1 - l1 + 1) * (r2 - l2 + 1));
cout << ans * 2000 << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a[200001], x, y;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cin >> x >> y;
for (int i = 1; i <= n; ++i) {
int cnt1 = 0, cnt2 = 0;
for (int j = 1; j <= n; ++j)
if (j >= i)
cnt2 += a[j];
else
cnt1 += a[j];
if (cnt1 >= x && cnt1 <= y && cnt2 >= x && cnt2 <= y) {
cout << i;
return 0;
}
}
cout << 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int prime100[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
template <typename T>
inline bool isLeap(T y) {
return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false);
}
template <typename T>
inline T GCD(T a, T b) {
a = abs(a);
b = abs(b);
if (a < b) swap(a, b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T>
inline T LCM(T x, T y) {
T tp = GCD(x, y);
if ((x / tp) * 1. * y > 9e18) return 9e18;
return (x / tp) * y;
}
template <typename T>
inline T BIGMOD(T A, T B, T M = 1000000009) {
T ret = 1;
while (B) {
if (B & 1) ret = (ret * A) % M;
A = (A * A) % M;
B = B >> 1;
}
return ret;
}
template <typename T>
inline T BigMod(T A, T B, T M) {
T ret = 1;
while (B) {
if (B & 1) ret = (ret * A) % M;
A = (A * A) % M;
B = B >> 1;
}
return ret;
}
long long int MySqrt(long long int n) {
long long int p = sqrt(n);
if ((p + 1) * (p + 1) <= n)
return p + 1;
else if (p * p <= n)
return p;
else
return p - 1;
}
long long int MyPow(long long int x, long long int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return MyPow(x * x, n / 2);
else
return x * MyPow(x * x, ((n - 1) / 2));
}
std::vector<int> V[3003];
int visi[3003];
int Dis[3005];
int level[3005];
int flag;
void dfs(int u, int papa, int my) {
visi[u] = 1;
int sz = V[u].size();
for (int i = 0; i < sz; i++) {
int v = V[u][i];
if (visi[v] == 0) {
dfs(v, u, my);
} else if (v != papa && v == my)
flag = 1;
}
}
void bfs(int second) {
memset(level, 0, sizeof level);
queue<int> q;
q.push(second);
visi[second] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
int sz = V[u].size();
for (int i = 0; i < sz; i++) {
int v = V[u][i];
if (visi[v] == 0) {
visi[v] = 1;
q.push(v);
level[v] = level[u] + 1;
}
}
}
}
int main(int argc, char const *argv[]) {
int node;
scanf("%d", &(node));
for (int i = 1; i <= node; i++) {
int x, y;
scanf("%d%d", &(x), &(y));
V[x].push_back(y);
V[y].push_back(x);
Dis[i] = INT_MAX;
}
for (int i = 1; i <= node; i++) {
memset(visi, 0, sizeof visi);
flag = 0;
dfs(i, i, i);
if (flag == 1) Dis[i] = 0;
}
for (int i = 1; i <= node; i++) {
if (Dis[i] == 0) {
memset(visi, 0, sizeof visi);
bfs(i);
for (int j = 1; j <= node; j++) {
Dis[j] = min(Dis[j], level[j]);
}
}
}
for (int i = 1; i <= node; i++) {
printf("%d ", Dis[i]);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 1e5 + 228;
long long Sum(long long a, long long b) {
long long c = a + b;
if (c >= MOD) {
c -= MOD;
}
return c;
}
void Add(long long& a, long long b) {
a += b;
if (a >= MOD) {
a -= MOD;
}
}
void Sub(long long& x, long long y) {
if (y == 0) return;
Add(x, MOD - y);
}
void Mul(long long& x, long long y) { x = (x * y) % MOD; }
void solve() {
string s;
cin >> s;
long long ans = 1;
s += '@';
long long n = s.size();
for (int i = 0; i < n - 1; ++i) {
if (s[i] == 'm' || s[i] == 'w') {
cout << 0 << '\n';
return;
}
if (s[i] != 'n' && s[i] != 'u') {
s[i] = '@';
} else {
s[i] = (s[i] == 'n' ? '1' : '0');
}
}
long long cur = 0;
vector<long long> dp(n, 0);
dp[0] = 1;
for (int i = 1; i < n; ++i) {
Add(dp[i], Sum(dp[i - 1], (i - 2 >= 0 ? dp[i - 2] : 0)));
}
for (int i = 0; i < n; ++i) {
if (s[i] == '@') {
Mul(ans, dp[cur]);
cur = 0;
} else {
if (s[i] == '1') {
if (i == 0 || s[i - 1] == '0') {
Mul(ans, dp[cur]);
cur = 1;
} else {
cur++;
}
continue;
}
if (s[i] == '0') {
if (i == 0 || s[i - 1] == '1') {
Mul(ans, dp[cur]);
cur = 1;
} else {
cur++;
}
continue;
}
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long test = 1;
while (test--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int cnt[26];
char s[105], t[105], p[105];
void solve() {
memset(cnt, 0, sizeof cnt);
scanf("%s %s %s", s + 1, t + 1, p + 1);
int n = strlen(p + 1);
for (int i = 1; i <= n; i++) {
cnt[p[i] - 'a']++;
}
int l1 = strlen(s + 1);
int l2 = strlen(t + 1);
if (l1 > l2) {
puts("NO");
return;
}
int i = 1, j = 1;
for (; i <= l2; i++) {
for (; i <= l2 && t[i] != s[j]; i++) {
if (!cnt[t[i] - 'a']) {
puts("NO");
return;
}
cnt[t[i] - 'a']--;
}
j += i <= l2 && t[i] == s[j];
}
if (j <= l1) {
puts("NO");
return;
}
puts("YES");
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int q;
long long n, a, b;
int main() {
scanf("%d", &q);
while (q--) {
scanf("%lld%lld%lld", &n, &a, &b);
printf("%lld\n",
min(a * n + (b - 2 * a) * 0, a * n + (b - 2 * a) * (n / 2)));
}
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int a[300010];
int panduan(int x){
if(a[x]>a[x+1]&&a[x]>a[x-1]) return 1;
if(a[x]<a[x+1]&&a[x]<a[x-1]) return 1;
return 0;
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
int n,res=0,cnt=0;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
a[n+2]=a[n+1]=a[n];
a[-1]=a[0]=a[1];
for(int i=2;i<=n-1;i++){
res+=panduan(i);
}
cnt=res;
for(int i=1;i<=n;i++){
int p=panduan(i-1)+panduan(i+1)+panduan(i);
int f=a[i];
a[i]=a[i-1];
int k=panduan(i-1)+panduan(i+1)+panduan(i);
cnt=min(cnt,res-p+k);
a[i]=a[i+1];
k=panduan(i-1)+panduan(i+1)+panduan(i);
cnt=min(cnt,res-p+k);
a[i]=f;
}
cout<<cnt<<endl;
}
return 0;
} | 2 |
#include <bits/stdc++.h>
//typedef
//-------------------------#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s)
{
int v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x)
{
ostringstream sout;
sout << x;
return sout.str();
}
inline int readInt()
{
int x;
scanf("%d", &x);
return x;
}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
class UnionFind
{
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1)
{
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
void init(int sz_)
{
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x)
{
while (par[x] != x)
{
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y)
{
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y)
{
return root(x) == root(y);
}
int size(int x)
{
return siz[root(x)];
}
};
ll modPow(ll x, ll n, ll mod = MOD)
{
ll res = 1;
while (n)
{
if (n & 1)
res = (res * x) % mod;
res %= mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
#define SIEVE_SIZE 5000000 + 10
bool sieve[SIEVE_SIZE];
void makeSieve()
{
for (int i = 0; i < SIEVE_SIZE; ++i)
sieve[i] = true;
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < SIEVE_SIZE; ++i)
if (sieve[i])
for (int j = 2; i * j < SIEVE_SIZE; ++j)
sieve[i * j] = false;
}
bool isprime(ll n)
{
if (n == 0 || n == 1)
return false;
for (ll i = 2; i * i <= n; ++i)
if (n % i == 0)
return false;
return true;
}
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long extGCD(long long a, long long b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// 負の数にも対応した mod (a = -11 とかでも OK)
inline long long mod(long long a, long long m)
{
return (a % m + m) % m;
}
// 逆元計算 (ここでは a と m が互いに素であることが必要)
long long modinv(long long a, long long m)
{
long long x, y;
extGCD(a, m, x, y);
return mod(x, m); // 気持ち的には x % m だが、x が負かもしれないので
}
ll GCD(ll a, ll b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
typedef vector<ll> vec;
typedef vector<vec> mat;
mat mul(mat &A, mat &B)
{
mat C(A.size(), vec((int)B[0].size()));
for (int i = 0; i < A.size(); ++i)
{
for (int k = 0; k < B.size(); ++k)
{
for (int j = 0; j < B[0].size(); ++j)
{
C[i][j] = (C[i][j] + A[i][k] % MOD * B[k][j] % MOD) % MOD;
}
}
}
return C;
}
mat matPow(mat A, ll n)
{
mat B(A.size(), vec((int)A.size()));
for (int i = 0; i < A.size(); ++i)
{
B[i][i] = 1;
}
while (n > 0)
{
if (n & 1)
B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int N, W;
cin >> N >> W;
vector<pair<long double, int>> vp;
long double v[100010], w[100010];
REP(i, N)
{
cin >> v[i] >> w[i];
vp.push_back({v[i] / w[i], i});
}
sort(all(vp));
reverse(all(vp));
long double ans = 0.0;
for (int i = 0; i < N; i++)
{
if (W - (int)w[vp[i].second] >= 0)
{
ans += vp[i].first * w[vp[i].second];
W -= (int)w[vp[i].second];
}
else
{
ans += vp[i].first * (long double)W;
break;
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
int n;
cin >> n;
int t[100000];
int maxi = -1;
rep(i, n) {
cin >> t[i];
maxi = max(maxi, t[i]);
}
vector<int> div;
for (int i = 1; i*i <= maxi; i++) {
if (maxi%i == 0) {
div.push_back(i);
if (i != maxi/i) div.push_back(maxi/i);
}
}
sort(div.begin(), div.end());
//rep(i, div.size()) cout << div[i] << " ";
int ans = 0;
rep(i, n) rep(j, div.size()-1) {
if (div[j] < t[i] && t[i] <= div[j+1]) ans += div[j+1]-t[i];
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const int mo = 1000000007;
int n, m;
char a[505][505], a1[505][505];
int f0[505][505], f1[505][505];
inline void add(int &a, int b) { a = (a + b) % mo; }
int main() {
scanf("%d%d", &n, &m);
int i, j, k;
for (i = 1; i <= n; i++) {
scanf("%s", a[i] + 1);
}
if (n > m) {
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
a1[j][i] = a[i][j];
}
}
memcpy(a, a1, sizeof(a));
std::swap(n, m);
}
f0[1][m] = a[1][1] == a[n][m];
int t = (n + m - 2) / 2;
for (i = 0; i < t; i++) {
int L, R;
if (i <= n - 1) {
L = 1;
R = 1 + i;
} else {
L = 1 + i - (n - 1);
R = 1 + i;
}
int A, B;
if (i <= n - 1) {
A = m - i;
B = m;
} else {
A = m - i;
B = m - (i - (n - 1));
}
memset(f1, 0, sizeof(f1));
int t1, t2;
for (j = R, t1 = 1; j >= L; j--, t1++) {
for (k = A, t2 = n; k <= B; k++, t2--) {
int tmp = f0[j][k];
if (t1 < n) {
if (t2 > 1) {
if (a[t1 + 1][j] == a[t2 - 1][k]) {
add(f1[j][k], tmp);
}
}
if (k > 1) {
if (a[t1 + 1][j] == a[t2][k - 1]) {
add(f1[j][k - 1], tmp);
}
}
}
if (j < m) {
if (t2 > 1) {
if (a[t1][j + 1] == a[t2 - 1][k]) {
add(f1[j + 1][k], tmp);
}
}
if (k > 1) {
if (a[t1][j + 1] == a[t2][k - 1]) {
add(f1[j + 1][k - 1], tmp);
}
}
}
}
}
memcpy(f0, f1, sizeof(f0));
}
int ans = 0;
if ((n + m) % 2 == 0) {
for (i = 1; i <= m; i++) {
ans = (ans + f0[i][i]) % mo;
}
} else {
for (i = 1; i <= m; i++) {
ans = (ans + f0[i][i]) % mo;
ans = (ans + f0[i][i + 1]) % mo;
}
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
int main() {
cin >> n >> a >> b;
b++;
if (b > n - a)
cout << n - a;
else
cout << b;
}
| 1 |
#include <iostream>
using namespace std;
int main() {
int c[9];
for(int i = 0; i < sizeof(c); i++) cin >> c[i];
cout << (c[0]+c[4]+c[8] == c[1]+c[5]+c[6] && c[0]+c[4]+c[8] == c[2]+c[3]+c[7] ? "Yes" : "No") <<endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int m,n;
cin>>m>>n;
int a[1000005],maxn=-1;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n-1;i++){
maxn=max(a[i+1]-a[i],maxn);
}
maxn=max(m+a[0]-a[n-1],maxn);
cout<<m-maxn;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char a[1001][1001];
int b[1001][1001];
bool check[1001][1001];
map<int, int> ans;
int n, m, k;
int cnt;
void dfs(int x, int y, int ptr) {
check[x][y] = true;
b[x][y] = ptr;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (abs(i) + abs(j) != 1) {
continue;
}
if (x + i < 0 || x + i >= n) {
continue;
}
if (y + j < 0 || y + j >= m) {
continue;
}
if (check[x + i][y + j]) {
continue;
}
if (a[x + i][y + j] == '*') {
cnt++;
continue;
}
dfs(x + i, y + j, ptr);
}
}
}
int x, y;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
b[i][j] = -1;
}
}
for (int i = 0; i < k; i++) {
cin >> x >> y;
x--;
y--;
if (b[x][y] == -1) {
cnt = 0;
dfs(x, y, i);
ans[i] = cnt;
cout << cnt << endl;
} else {
cout << ans[b[x][y]] << endl;
}
}
return 0;
}
| 4 |
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
using namespace std;
int n;
struct S {
char f[5][5];
int x, y;
};
int conv(S s) {
int ret = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(s.f[i][j] == '#') ret |= 1 << (i * n + j);
}
}
return (((ret * n) + s.y)) * n + s.x;
}
bool isFree(S s) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(s.f[i][j] == '#') return false;
}
}
return true;
}
int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[8] = { 0, 1, 0, -1, 1, -1, 1, -1 };
bool in(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n;
}
S simulate(S s) {
S ns = s;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == s.y && j == s.x) continue;
int cnt = 0;
for(int k = 0; k < 8; k++) {
int x = j + dx[k];
int y = i + dy[k];
if(!in(x, y)) continue;
if(s.f[y][x] == '#' || (x == s.x && y == s.y)) cnt++;
}
if(s.f[i][j] == '#' && (cnt == 2 || cnt == 3)) ns.f[i][j] = '#';
else if(s.f[i][j] == '.' && cnt == 3) ns.f[i][j] = '#';
else ns.f[i][j] = '.';
}
}
return ns;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while(cin >> n, n) {
S s;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> s.f[i][j];
if(s.f[i][j] == '@') {
s.y = i, s.x = j;
s.f[i][j] = '.';
}
}
}
int ans = -1;
unordered_map<int, int> m;
queue<S> q;
q.push(s);
m[conv(s)] = 0;
while(q.size()) {
s = q.front();
q.pop();
int c = m[conv(s)];
if(isFree(s)) {
ans = c;
break;
}
for(int k = 0; k < 8; k++) {
S ns = s;
ns.x = s.x + dx[k];
ns.y = s.y + dy[k];
if(!in(ns.x, ns.y)) continue;
if(s.f[ns.y][ns.x] == '#') continue;
ns = simulate(ns);
int X = conv(ns);
if(m.count(X)) continue;
m[X] = c + 1;
q.push(ns);
}
}
cout << ans << endl;
}
} | 0 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define MAX_N 40
#define MAX_K 3
#define INF (1<<29)
int n;
int p[MAX_N+1], t[MAX_N+1];
int dp[MAX_N+1][MAX_K+1];
void disp(){
cout << "--- table ---" << endl;
for(int i=0;i<=n;i++){
for(int k=0;k<=3;k++){
if(dp[i][k] == INF)
cout << -1 << " ";
else
cout << dp[i][k] << " ";
}
cout << endl;
}
}
int I=0;
void solve(){
I++;
for(int i=0;i<=n;i++) for(int k=0;k<=3;k++) dp[i][k] = INF;
dp[0][0] = 0;
for(int i=0;i<n;i++){
// if(I==5) disp();
for(int k=0;k<=3;k++){
if(dp[i][k] == INF) continue;
int ta = (k+1)*(abs(p[i+1]-p[i])); // direct
int tb = (k+1)*p[i] + p[i+1]; // home and go
if(k != 3 && ta <= t[i+1] - t[i]){
dp[i+1][k+1] = min(dp[i+1][k+1], dp[i][k] + abs(p[i+1]-p[i]));
}
if(tb <= t[i+1] - t[i]){
dp[i+1][1] = min(dp[i+1][1], dp[i][k] + (p[i]+p[i+1]));
}
}
// check
bool flg = true;
for(int k=0;k<=3;k++){
if(dp[i+1][k] != INF) { flg = false; }
}
if(flg) {
cout << "NG " << i+1 << endl;
return;
}
}
int res = INF;
for(int k=0;k<=3;k++){
res = min(res, dp[n][k]);
}
if(res == INF)
cout << "NG " << n << endl;
else
cout << "OK " << res + p[n] << endl;
}
int main(void){
while(true){
cin >> n;
if(n == 0) break;
p[0] = t[0] = 0;
for(int i=1;i<=n;i++) cin >> p[i] >> t[i];
solve();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, x;
int a[20000];
bool changed[20000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int result = 0;
for (int i = 0; i < n; ++i) {
cin >> x;
if (x % 2 == 0) {
a[i] = x / 2;
result += a[i];
continue;
}
changed[i] = true;
if (x < 0) {
a[i] = x / 2 - 1;
} else {
a[i] = x / 2;
}
result += a[i];
}
int pos = 0;
while (result != 0 and pos < n) {
if (changed[pos]) {
a[pos] += 1;
result++;
}
pos++;
}
for (int i = 0; i < n; ++i) {
cout << a[i] << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
int power(int base, int exp) {
int sum = 1;
for (int i = 0; i < exp; i++) {
sum *= base;
}
return sum;
}
double maxV(double a, double b) {
if (a < b)
return b;
else
return a;
}
int main() {
int* times = (int*)malloc(sizeof(int) * 5);
int* mistakes = (int*)malloc(sizeof(int) * 5);
int probVal[5] = {500, 1000, 1500, 2000, 2500};
int hacks, fHacks;
double totalPoints = 0;
for (int i = 0; i < 5; i++) {
std::cin >> times[i];
}
for (int i = 0; i < 5; i++) {
std::cin >> mistakes[i];
}
std::cin >> hacks >> fHacks;
for (int i = 0; i < 5; i++) {
totalPoints += maxV(((1.0 - ((double)times[i] / 250.0)) * probVal[i]) -
(50 * (double)mistakes[i]),
0.3 * (double)probVal[i]);
}
totalPoints += 100 * hacks;
totalPoints -= 50 * fHacks;
std::cout << totalPoints;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, nm;
vector<string> a(10);
string s = "";
a[2] = "2";
a[3] = "3";
a[4] = "322";
a[5] = "5";
a[6] = "53";
a[7] = "7";
a[8] = "7222";
a[9] = "7332";
cin >> n >> nm;
stringstream ss;
ss << nm;
string str = ss.str();
int l = str.size();
for (int i = 0; i < l; i++) {
if (str[i] == '0' || str[i] == '1')
continue;
else {
stringstream ss;
ss << str[i];
long long indx;
ss >> indx;
s = s + a[indx];
}
}
sort(s.begin(), s.end(), greater<int>());
cout << s << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define mod 1000000007
#define inf 1e18
#define inp(arr,n) for(int i=0;i<n;i++) cin>>arr[i]
#define soa(arr,n) sort(arr,arr+n)
#define sov(arr) sort(arr.begin(),arr.end())
#define fr(i,a,b) for(int i=a;i<b;i++)
#define fre(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t;
cin>>t;
while(t--){
string s;
cin>>s;
fr(i,0,s.length()){
if(i%2==0){
if(s[i]=='a') cout<<'b';
else cout<<'a';
}
else{
if(s[i]=='z') cout<<'y';
else cout<<'z';
}
}
cout<<"\n";
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using f64 = double;
using f80 = long double;
using pii = pair<int, int>;
using ptx = pair<f64, f64>;
template <int MOD>
class Num {
private:
int expow(long base, int e) const {
long long ans = 1;
for (; e > 0; e /= 2) {
if (e % 2) ans = ans * base % MOD;
base = base * base % MOD;
}
return ans;
}
public:
int val;
Num(long long _val) {
val = _val % MOD;
val = val < 0 ? val + MOD : val;
}
Num() : val(0) {}
inline bool operator==(const Num &arg) const { return val == arg.val; }
inline Num operator+(const Num &arg) const {
Num res;
res.val = val + arg.val;
res.val = res.val >= MOD ? res.val - MOD : res.val;
return res;
}
inline Num operator-(const Num &arg) const {
Num res;
res.val = val - arg.val;
res.val = res.val < 0 ? res.val + MOD : res.val;
return res;
}
inline Num operator-() const {
Num res;
res.val = MOD - res.val;
res.val = res.val == MOD ? 0 : res.val;
return res;
}
inline Num operator^(const int &arg) const { return Num(expow(val, arg)); }
inline Num operator*(const Num &arg) const {
return Num(1LL * val * arg.val);
}
inline Num operator/(const Num &arg) const {
return Num(1LL * val * expow(arg.val, MOD - 2));
}
inline void operator+=(const Num &arg) { (*this) = (*this) + arg; }
inline void operator-=(const Num &arg) { (*this) = (*this) - arg; }
inline void operator*=(const Num &arg) { (*this) = (*this) * arg; }
inline void operator/=(const Num &arg) { (*this) = (*this) / arg; }
inline void operator^=(const long long &arg) { val = expow(val, arg); }
};
template <int MOD>
ostream &operator<<(ostream &fo, const Num<MOD> &c) {
fo << c.val;
return fo;
}
template <int MOD>
istream &operator>>(istream &fi, Num<MOD> &c) {
fi >> c.val;
c = Num<MOD>(c.val);
return fi;
}
const int MOD1 = 1e9 + 7, MOD2 = (1LL << 31) - 1;
const int N = 2e5 + 5, K = 11;
using hsh = pair<Num<MOD1>, Num<MOD2>>;
hsh hash_tab[K][K], pwn[N];
vector<pii> ig[N];
vector<int> g[N];
vector<int> prm;
int n, m, k, ant;
hsh good;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
ig[u].emplace_back(v, c);
}
for (int i = 1; i <= n; ++i) {
sort(begin(ig[i]), end(ig[i]), [&](const pii &a, const pii &b) -> bool {
return a.second < b.second;
});
for (auto &t : ig[i]) g[i].push_back(t.first);
}
pwn[0] = {1, 1};
for (int i = 1; i < N; ++i) {
pwn[i].first = pwn[i - 1].first * (n + 5);
pwn[i].second = pwn[i - 1].second * (n + 5);
}
for (int i = 1; i <= n; ++i) {
good.first += pwn[i].first;
good.second += pwn[i].second;
for (int j = 0; j < g[i].size(); ++j) {
hash_tab[g[i].size()][j + 1].first += pwn[g[i][j]].first;
hash_tab[g[i].size()][j + 1].second += pwn[g[i][j]].second;
}
}
function<void(int, hsh)> bkt = [&](int pos, hsh sofar) {
if (pos == k + 1) {
ant += int(sofar == good);
return;
}
for (int i = 1; i <= pos; ++i)
bkt(pos + 1, hsh(sofar.first + hash_tab[pos][i].first,
sofar.second + hash_tab[pos][i].second));
};
bkt(1, hsh(0, 0));
cout << ant << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18;
int n, m, k, x, p;
int vals[100005];
vector<int> g[100005];
pair<long long int, long long int> dfs(int src, int par) {
pair<long long int, long long int> ret = {0LL, 0LL};
for (int ch : g[src]) {
if (ch == par) continue;
pair<long long int, long long int> ans = dfs(ch, src);
ret.first = max(ret.first, ans.first);
ret.second = max(ret.second, ans.second);
}
long long int val = vals[src] + ret.first - ret.second;
if (val < 0)
ret.first += -val;
else
ret.second += val;
return ret;
}
void solve(int tc) {
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
for (int i = 0; i < n; ++i) {
cin >> vals[i];
}
pair<long long int, long long int> ret = dfs(0, -1);
cout << ret.first + ret.second << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
for (int i = 1; i <= tt; ++i) {
solve(i);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
void fastIO() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
}
int main() {
fastIO();
int n, k;
std::cin >> n >> k;
std::vector<int> digits(n);
for (int& digit : digits) {
char d;
std::cin >> d;
digit = d - '0';
}
std::vector<int> solution(n);
for (int i = 0; i < k; ++i) solution[i] = digits[i];
for (int i = k; i < n; ++i) solution[i] = solution[i - k];
bool smaller = false;
for (int i = 0; i < n && !smaller; ++i)
if (solution[i] < digits[i])
smaller = true;
else if (solution[i] > digits[i])
break;
if (smaller) {
for (int i = k - 1; i >= 0; --i) {
if (solution[i] != 9) {
for (int j = i; j < n; j += k) ++solution[j];
break;
} else {
for (int j = i; j < n; j += k) solution[j] = 0;
}
}
}
std::cout << n << '\n';
for (int i = 0; i < n; ++i) {
std::cout << (char)(solution[i] + '0');
}
std::cout << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long pi = 1000000000ll;
long long hrr[3100], idt[3100];
string srr[3100];
vector<pair<int, int> > arr;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
;
long long n, a, id, t;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> srr[i] >> a;
arr.push_back({a, i});
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n; i++) {
a = arr[i].first;
id = arr[i].second;
hrr[id] = pi;
pi--;
if (i < a) {
cout << "-1";
return 0;
}
idt[i] = id;
t = i;
while (t > a) {
swap(idt[t - 1], idt[t]);
t--;
}
}
for (int i = 0; i < n; i++) {
cout << srr[idt[i]] << " " << hrr[idt[i]] << '\n';
}
return 0;
}
| 3 |
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define MAX 100
using namespace std;
typedef pair<int,int> P;
int N,M;
int scene[MAX][MAX];
int s[MAX/2][MAX/2],rotate_s[MAX/2][MAX/2][4];
void rotate(){
rep(i,3){
rep(j,M){
rep(k,M){
rotate_s[j][k][i+1] = s[M-k-1][j];
}
}
rep(j,M){
rep(k,M){
s[j][k] = rotate_s[j][k][i+1];
}
}
}
}
void solve(){
vector<P> v;
rep(i,N-M+1){
rep(j,N-M+1){
rep(k,4){
bool flg = true;
rep(l,M){
rep(n,M){
if(rotate_s[l][n][k] == -1){
continue;
}
if(scene[i+l][j+n] != rotate_s[l][n][k]){
flg = false;
goto next;
}
}
}
next:;
if(flg){
rep(l,M){
bool found = false;
rep(n,M){
if(rotate_s[l][n][k] != -1){
v.push_back(P(i+l+1,j+n+1));
found = true;
break;
}
}
if(found){
break;
}
}
}
}
}
}
sort(v.begin(),v.end());
if(!v.size()){
cout << "NA" << endl;
}else{
cout << v[0].second << " " << v[0].first << endl;
}
}
int main(){
while(cin >> N >> M , (N | M)){
rep(i,N){
rep(j,N){
cin >> scene[i][j];
}
}
rep(i,M){
rep(j,M){
cin >> s[i][j];
rotate_s[i][j][0] = s[i][j];
}
}
rotate();
solve();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
class Point {
public:
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (Point p) {return Point(x+p.x,y+p.y);}
Point operator - (Point p) {return Point(x-p.x,y-p.y);}
Point operator * (double a) {return Point(a*x,a*y);}
Point operator / (double a) {return Point(x/a,y/a);}
double abs() {return sqrt(norm());}
double norm() {return x*x+y*y;}
bool operator<(const Point &p) const {
return x!=p.x ? x<p.x : y<p.y;
}
bool operator==(const Point &p) const {
return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
}
};
struct Segment {
Point p1,p2;
};
typedef Point Vector;
double dot(Vector a,Vector b) {
return a.x*b.x+a.y*b.y;
}
Point project(Segment s,Point p) {
Vector base=s.p2-s.p1;
double r=dot(p-s.p1,base)/base.norm();
return s.p1+base*r;
}
int main(void) {
struct Segment s;
cin>>s.p1.x>>s.p1.y>>s.p2.x>>s.p2.y;
int q;
cin>>q;
while(q) {
Point p;
cin>>p.x>>p.y;
Point ans=project(s,p);
printf("%.10f %.10f\n",ans.x,ans.y);
q--;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n];
int vis[5001] = {0};
set<int> se;
for (int i = 0; i < n; ++i) cin >> arr[i], se.insert(arr[i]);
vector<pair<int, int> > v;
for (int i = 0; i < n; ++i) {
if (vis[i] == 0) {
for (int j = 0; j < n; ++j) {
{
if (arr[i] != arr[j]) {
v.push_back(make_pair(i + 1, j + 1));
vis[j] = 1;
break;
}
}
}
}
}
if (se.size() == 1)
cout << "NO\n" << endl;
else {
cout << "YES\n" << endl;
for (auto it : v) cout << it.first << " " << it.second << "\n";
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int best = -2e9;
const int P1 = 53;
const int P2 = 43;
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e8 + 7;
int best_vertex_cnt;
const int maxn = 3e5 + 5;
set<pair<int, int> >* S[maxn];
int c[maxn];
vector<int> edges[maxn];
char letter[maxn];
inline void recalc(pair<int, int>& p, char c) {
p = make_pair(((long long)p.first * P1 + c) % MOD1,
((long long)p.second * P2 + c) % MOD2);
}
void dfs(int v, int anc, pair<int, int> cur) {
recalc(cur, letter[v]);
int biggest = -1;
for (int u : edges[v])
if (u != anc) {
dfs(u, v, cur);
if (biggest == -1 || S[u]->size() > S[biggest]->size()) biggest = u;
}
if (biggest == -1) {
S[v] = new set<pair<int, int> >;
S[v]->insert(cur);
} else {
S[v] = S[biggest];
S[v]->insert(cur);
for (int u : edges[v])
if (u != anc && u != biggest) {
for (pair<int, int> p : *S[u]) S[v]->insert(p);
}
}
int v_val = S[v]->size() + c[v];
if (v_val > best) {
best = S[v]->size() + c[v];
best_vertex_cnt = 1;
} else if (v_val == best)
best_vertex_cnt++;
}
int main() {
int n;
scanf("%d", &n);
for (int j = (int)1; j <= (int)n; j++) scanf("%d", &c[j]);
scanf("\n");
for (int j = (int)1; j <= (int)n; j++) scanf("%c", &letter[j]);
for (int j = 0; j < (int)n - 1; j++) {
int x, y;
scanf("%d%d", &x, &y);
edges[x].push_back(y);
edges[y].push_back(x);
}
dfs(1, -1, make_pair(0, 0));
printf("%d\n%d", best, best_vertex_cnt);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxNum = 200010;
const int maxMult = maxNum * 2;
struct Edge {
int u, v, w;
int nextNum;
int type, id;
} edge[maxMult];
int head[maxNum], totalNums;
void setStruct() {
totalNums = 0;
memset(head, -1, sizeof(head));
}
void addEdges(int u, int v, int w, int type, int id) {
edge[totalNums].u = u;
edge[totalNums].v = v;
edge[totalNums].w = w;
edge[totalNums].type = type;
edge[totalNums].id = id;
edge[totalNums].nextNum = head[u];
head[u] = totalNums++;
}
queue<int> q;
int N, M;
bool visible[maxNum];
int ans[maxNum];
int val[maxNum];
int main() {
while (scanf("%d%d", &N, &M) != EOF) {
setStruct();
memset(val, 0, sizeof(val));
for (int i = 0; i < M; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdges(u, v, w, 0, i + 1);
addEdges(v, u, w, 1, i + 1);
val[u] += w;
val[v] += w;
}
while (!q.empty()) q.pop();
visible[1] = true;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = edge[i].nextNum) {
int v = edge[i].v;
if (visible[v]) continue;
val[v] -= 2 * edge[i].w;
ans[edge[i].id] = edge[i].type;
if (val[v] == 0 && v != N) {
visible[v] = true;
q.push(v);
}
}
}
for (int i = 1; i <= M; i++) printf("%d\n", ans[i]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long b, long long p, long long md) {
if (p == 0) return 1;
if (p % 2 == 1) {
return ((b % md) * bigmod(b, p - 1, md)) % md;
} else {
long long y = bigmod(b, p / 2, md);
return (y * y) % md;
}
}
int main() {
string str;
cin >> str;
vector<long long> v;
vector<char> cv;
for (long long i = 0; i < str.size(); i++) {
long long c = 0;
long long j = i;
while (j < str.size() && str[j] == str[i]) {
c++;
j++;
}
v.push_back(c);
cv.push_back(str[i]);
--j;
i = j;
}
long long op = 0;
while (1) {
if (v.size() <= 1) break;
vector<long long> tem_v;
vector<char> tem_cv;
for (long long i = 0; i < v.size(); i++) {
long long bad = 2;
if (i == 0 || i + 1 == v.size()) {
bad = 1;
}
if (v[i] > bad) {
if (tem_cv.size() && tem_cv.back() == cv[i]) {
long long t = tem_v.back() + v[i] - bad;
tem_v.pop_back();
tem_v.push_back(t);
} else {
tem_v.push_back(v[i] - bad);
tem_cv.push_back(cv[i]);
}
}
}
op++;
v = tem_v;
cv = tem_cv;
}
cout << op << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
const int MOD=1000000007;
ll n,K;
ll calc(ll x){
ll sum=0;
int cnt=0;
while(x){
sum+=x;
x>>=1;
cnt++;
if(cnt==n)break;
}
return sum;
}
int main(){
cin>>n>>K;
ll ok=0,ng=K+1;
while(ng-ok>1){
ll t=(ok+ng)/2;
if(calc(t)<=K)ok=t;
else ng=t;
}
cout<<ok<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int base = 131;
const int p = 7000007;
int n, ans, h[N];
bool last[N], f[N], can[7000010];
char s[N];
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
int main() {
scanf("%d%s", &n, s + 1);
ans = 1;
for (int i = 1; i <= n; i++) h[i] = s[i] - 'a', f[i] = 1;
for (int i = 2; i <= 1000; i++) {
memset(can, 0, sizeof(can));
memcpy(last, f, sizeof(f));
memset(f, 0, sizeof(f));
for (int j = n - i + 1; j >= 1; j--) {
if (can[h[j]] || can[h[j + 1]]) f[j] = 1, ans = i;
if (last[j + i - 1]) can[h[j + i - 1]] = 1;
}
if (ans < i) break;
for (int j = 1; j <= n - i + 1; j++)
h[j] = (h[j] * base + s[j + i - 1] - 'a') % p;
}
printf("%d\n", ans);
fclose(stdin);
fclose(stdout);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, j, c, c2 = 0;
cin >> n;
if (n > 1000) return -1;
int a[1000][3];
for (int i = 0; i < n; i++) {
cin >> a[i][0];
cin >> a[i][1];
cin >> a[i][2];
}
for (int i = 0; i < n; i++) {
c = 0;
for (j = 0; j < 3; j++)
if (a[i][j] == 1) c++;
if (c >= 2) {
c2++;
}
}
cout << c2;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int A[1000100];
int main() {
int n;
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < 1000100; ++i) A[i] = 0;
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
A[x]++;
}
for (int i = 0; i < 1000100 - 1; i++) {
if (A[i] % 2 == 1) ans++;
A[i + 1] += A[i] / 2;
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, f, t, a[1001][1001], b[1001], c[1001];
int main() {
cin >> n >> m;
for (; n; n--) {
cin >> i >> j;
a[i][j]++;
b[j]++;
}
for (; m; m--) {
cin >> i >> j;
if (a[i][j]) {
b[j]--;
a[i][j]--;
f++;
} else
c[j]++;
}
for (i = 1, t = f; i <= 1000; i++) t += c[i] < b[i] ? c[i] : b[i];
cout << t << ' ' << f;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n;
cin>>n;
double a[n],b[n],A=0,B=0,C=0,D=0;
r(i,n)cin>>a[i];
r(i,n)cin>>b[i];
r(i,n){
A+=abs(a[i]-b[i]);
B+=abs(a[i]-b[i])*abs(a[i]-b[i]);
D+=abs(a[i]-b[i])*abs(a[i]-b[i])*abs(a[i]-b[i]);
C=max(C,abs(a[i]-b[i]));
}
B=sqrt(B);
D=pow(D,1.0/3);
printf("%.11f\n",A);
printf("%.11f\n",B);
printf("%.11f\n",D);
printf("%.11f\n",C);
}
| 0 |
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double R;
typedef complex<R> P;
const R EPS = 1e-9;
const R PI = acos((R)(-1));
int sgn(R a) {
if (a < -EPS) return -1;
if (a > EPS) return 1;
return 0;
}
R cross(P a, P b) { return a.real()*b.imag() - a.imag()*b.real(); }
typedef vector<P> Pol;
P cu(const Pol &p, int i) {
int s = p.size();
return p[(i%s+s)%s];
}
R area_naive(const Pol &p) {
R u = 0;
for (int i = 0; i < (int)p.size(); i++) {
u += cross(cu(p, i), cu(p, i+1));
}
return u/2;
}
R area(const Pol &p) {
return abs(area_naive(p));
}
/*
-1 -> clock
0 -> non polygon
1 -> counter clock
*/
int iscclock(const Pol &p) {
return sgn(area_naive(p));
}
int dualGraph(P p[], vector<int> g[], int n,
Pol pol[], vector<int> rg[]) {
map<pair<int, int>, int> mp;
for (int i = 0; i < n; i++) {
sort(g[i].begin(), g[i].end(), [&](int l, int r){
return arg(p[l]-p[i]) > arg(p[r]-p[i]);
});
for (int j = 0; j < (int)g[i].size(); j++) {
mp[make_pair(i, g[i][j])] = j;
}
}
vector<vector<int>> idx(n), rev(n);
for (int i = 0; i < n; i++) {
idx[i] = vector<int>(g[i].size(), -1);
rev[i].resize(g[i].size());
for (int j = 0; j < (int)g[i].size(); j++) {
rev[i][j] = mp[make_pair(g[i][j], i)];
}
}
int idc = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int)g[i].size(); j++) {
if (idx[i][j] != -1) continue;
Pol po;
int ii = i, jj = j;
while (idx[ii][jj] != -2) {
po.push_back(p[ii]);
idx[ii][jj] = -2;
int ni = g[ii][jj];
int nj = (rev[ii][jj]+1) % g[ni].size();
ii = ni; jj = nj;
}
int id;
if (iscclock(po) != 1) {
id = 0;
} else {
id = idc;
pol[idc] = po;
idc++;
}
while (idx[ii][jj] == -2) {
idx[ii][jj] = id;
int ni = g[ii][jj];
int nj = (rev[ii][jj]+1) % g[ni].size();
ii = ni; jj = nj;
}
}
}
for (int i = 0; i < idc; i++) {
rg[i].clear();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (int)g[i].size(); j++) {
if (idx[i][j] < 0) continue;
rg[idx[i][j]].push_back(idx[g[i][j]][rev[i][j]]);
}
}
for (int i = 0; i < n; i++) {
sort(rg[i].begin(), rg[i].end());
rg[i].erase(unique(rg[i].begin(), rg[i].end()), rg[i].end());
}
return idc;
}
const int MN = 110;
P p[MN];
vector<int> g[MN];
Pol pol[2*MN];
vector<int> rg[2*MN];
int dist[2*MN][2*MN];
bool solve() {
int c, w;
cin >> c >> w;
if (!c) return false;
for (int i = 0; i < c; i++) {
R x, y;
cin >> x >> y;
p[i] = P(x, y);
g[i].clear();
}
for (int i = 0; i < w; i++) {
int s, t;
cin >> s >> t; s--; t--;
g[s].push_back(t);
g[t].push_back(s);
}
dualGraph(p, g, c, pol, rg);
int ma = 0;
bool used[2*MN] = {};
queue<pair<int, int>> q;
q.push(make_pair(0, 0));
while (q.size()) {
int t, di;
tie(t, di) = q.front(); q.pop();
if (used[t]) continue;
used[t] = true;
ma = max(ma, di);
for (int d: rg[t]) {
if (used[d]) continue;
q.push(make_pair(d, di+1));
}
}
cout << ma << endl;
return true;
}
int main() {
while (solve()) {}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long nod(long long a, long long b) {
if (b > 0) return nod(b, a % b);
return a;
}
int main() {
long long n, m, x, y, a, b;
cin >> n >> m >> x >> y >> a >> b;
long long d = nod(a, b);
a /= d;
b /= d;
long long dx, dy;
if (n * b < a * m) {
dx = n / a * a;
dy = dx / a * b;
} else if (n * b > a * m) {
dy = m / b * b;
dx = dy / b * a;
} else {
dx = n;
dy = m;
}
long long x1, x2, y1, y2;
if (2 * x < dx) {
x1 = 0;
x2 = dx;
} else if (2 * (n - x) < dx) {
x1 = n - dx;
x2 = n;
} else {
x2 = x + dx / 2;
x1 = x2 - dx;
}
if (2 * y < dy) {
y1 = 0;
y2 = dy;
} else if (2 * (m - y) < dy) {
y1 = m - dy;
y2 = m;
} else {
y2 = y + dy / 2;
y1 = y2 - dy;
}
cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string Sum(string s) {
long long int S = 0;
for (int i = 0; i < s.size(); i++) {
S += int(s[i] - '0');
}
return to_string(S);
}
int main() {
string s;
cin >> s;
int count = 0;
while (int(s.size()) != 1) {
count += 1;
s = Sum(s);
}
cout << count;
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
typedef unsigned long long ull;
int main()
{
int t;cin>>t;
string s[t];
int a[t];
int ans=0;
for(int i=0;i<t;i++)
{
cin>>s[i]>>a[i];
}
string str;
cin>>str;
int itr=0;
while(itr<t && s[itr]!=str)
{
itr++;
}
itr++;
for(;itr<t;itr++)
{
ans+=a[itr];
}
cout<<ans;
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int F[102][102];
int color,gx,gy;
bool maze;
void DFS1(int Y,int X){
if(F[Y][X]!=color)return;
if(Y==gy&&X==gx)maze=true;
F[Y][X]=0;
DFS1(Y-1,X );
DFS1(Y ,X-1);
DFS1(Y ,X+1);
DFS1(Y+1,X );
}
int main(){
int N,M,sx,sy,n;
cin >> N >> M;
while(N!=0&&M!=0){
cin >> sx >> sy >> gx >> gy >> n;
int c,d,nx,ny;
for(int i=0;i<n;i++){
cin >> c >> d >> nx >> ny;
for(int y=ny;y<ny+2*(1+d);y++){
for(int x=nx;x<nx+2*(2-d);x++){
F[y][x] = c;
}
}
}
color = F[sy][sx];
maze = false;
if(color!=0){
DFS1(sy,sx);
}
cout << (maze?"OK":"NG") << endl;
for(int y=0;y<102;y++){
for(int x=0;x<102;x++){
F[y][x] = 0;
}
}
cin >> N >> M;
}
return 0;
} | 0 |
#include <cstdio>
const int SIZE = 200005;
typedef long long int lli;
lli comb1[SIZE]; //H-A+B-1 C i
lli comb2[SIZE]; //W-B+A-1 C i
lli inv[SIZE];
int N;
//p = p / n * n + p % n
//inv(n) = -inv(p%n) * (p/n)
int main(){
int H, W, A, B;
lli ans=0, p=1e9+7;
scanf("%d %d %d %d", &H, &W, &A, &B);
inv[1] = 1;
N = H + W;
for(int i=2; i<=N; ++i)
inv[i] = (p*p - inv[p%i] * (p/i)) % p;
comb1[0] = comb2[0] = 1;
for(int i=0; i<N; ++i){
comb1[i+1] = (comb1[i] * inv[i+1]) % p;
comb1[i+1] = (comb1[i+1] * (H-A+B-1-i)) % p;
comb2[i+1] = (comb2[i] * inv[i+1]) % p;
comb2[i+1] = (comb2[i+1] * (W-B+A-1-i)) % p;
}
for(int i=0; i<H-A; ++i){
ans = (ans + comb1[i] * comb2[H-1-i]) % p;
}
printf("%lld\n", ans);
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> res(n, 0);
for (int i=0; i<n; ++i) {
res[i] = i+1;
}
for (int i=1; i<n; i += 2) {
int temp = res[i];
res[i] = res[i-1];
res[i-1] = temp;
}
if (n%2) {
int tmp = res[n-1];
res[n-1] = res[n-2];
res[n-2] = tmp;
}
for (int i=0; i<n; ++i) {
cout << res[i] << " ";
}
cout << "\n";
//cout << ((n%2 == 0) ? n : n+1) << "\n";
}
return 0;
} | 1 |
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
int max_matching(const deque<int> &aite, const deque<int> &jibun) {
int i = 0, j = 0;
for (; i < aite.size() && j < jibun.size(); ) {
if (aite[i] < jibun[j]) ++i, ++j;
else ++j;
}
return i;
}
int main() {
int N; cin >> N;
deque<int> aite(N), jibun(N);
for (int i = 0; i < N; ++i) cin >> aite[i];
for (int i = 0; i < N; ++i) cin >> jibun[i];
deque<int> sortaite = aite;
sort(sortaite.begin(), sortaite.end());
sort(jibun.begin(), jibun.end());
int mm = max_matching(sortaite, jibun);
deque<int> res;
for (int i = 0; i < N; ++i) {
deque<int> sortaite_next;
bool finish = false;
for (int j = 0; j < sortaite.size(); ++j) {
if (sortaite[j] == aite[0] && !finish) {
finish = true;
continue;
}
sortaite_next.push_back(sortaite[j]);
}
// check can win
int first = (int)jibun.size();
for (int j = 0; j < jibun.size(); ++j) {
if (jibun[j] > aite[0]) {
first = j;
break;
}
}
deque<int> temp_jibun;
for (int j = 0; j < jibun.size(); ++j) {
if (j == first) continue;
temp_jibun.push_back(jibun[j]);
}
int tmm = max_matching(sortaite_next, temp_jibun) + 1;
// binary search (low: ok id, high: ng id)
int low, high;
if (tmm == mm) low = first, high = (int)jibun.size();
else low = 0, high = first;
while (high - low > 1) {
int mid = (low + high) / 2;
int val = jibun[mid];
temp_jibun.clear();
for (int j = 0; j < jibun.size(); ++j) {
if (j == mid) continue;
temp_jibun.push_back(jibun[j]);
}
tmm = max_matching(sortaite_next, temp_jibun);
if (val > aite[0]) ++tmm;
if (tmm < mm) high = mid;
else low = mid;
}
// match i-th player
if (jibun[low] > aite[0]) --mm;
res.push_back(jibun[low]);
jibun.erase(jibun.begin() + low);
aite.pop_front();
sortaite = sortaite_next;
if (mm == 0) break;
}
sort(jibun.begin(), jibun.end(), greater<int>());
for (auto val : jibun) res.push_back(val);
for (int i = 0; i < res.size(); ++i) {
cout << res[i];
if (i != (int)res.size() - 1) cout << " ";
}
cout << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, p;
cin >> n >> m >> p;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
string foo;
cin >> foo;
for (int j = 0; j < m; j++) {
if (foo[j] == '1') {
a[i] |= (1LL << j);
}
}
}
vector<int> order(n);
iota(order.begin(), order.end(), 0);
shuffle(order.begin(), order.end(), rng);
int best = 0;
string ans(m, '0');
for (int it = 0; it < min(n, 50); it++) {
int me = order[it];
vector<int> bits;
for (int j = 0; j < m; j++) {
if ((a[me] >> j) & 1) {
bits.push_back(j);
}
}
int sz = (int) bits.size();
vector<int> cnt(1 << sz);
for (int i = 0; i < n; i++) {
int u = 0;
for (int j = 0; j < sz; j++) {
if ((a[i] >> bits[j]) & 1) {
u |= (1 << j);
}
}
cnt[u] += 1;
}
for (int bit = 0; bit < sz; bit++) {
for (int t = 0; t < (1 << sz); t++) {
if (t & (1 << bit)) {
cnt[t ^ (1 << bit)] += cnt[t];
}
}
}
for (int t = 0; t < (1 << sz); t++) {
if (2 * cnt[t] >= n && __builtin_popcount(t) > best) {
best = __builtin_popcount(t);
ans = string(m, '0');
for (int j = 0; j < sz; j++) {
if (t & (1 << j)) {
ans[bits[j]] = '1';
}
}
}
}
}
cout << ans << '\n';
return 0;
} | 4 |
#include <cstdio>
#include <cctype>
inline void read(int&x){
char c11=getchar();x=0;while(!isdigit(c11))c11=getchar();
while(isdigit(c11))x=x*10+c11-'0',c11=getchar();
}
inline void cmax(int&A,int B){A=A>B?A:B;}
const int N=2050;
struct Edge{int v,nxt;}a[N+N];
int head[N],n,k,ans,_;
int dfs(int x,int las,int dep){
if(dep==-1)return 0;
int res(1);
for(int i=head[x];i;i=a[i].nxt)
if(a[i].v!=las)
res+=dfs(a[i].v,x,dep-1);
return res;
}
int main(){
read(n),read(k);int x,y;
for(int i=1;i<n;++i){
read(x),read(y);
a[++_].v=y,a[_].nxt=head[x],head[x]=_;
a[++_].v=x,a[_].nxt=head[y],head[y]=_;
}
if(!(k&1))
for(int i=1;i<=n;++i)
cmax(ans,dfs(i,i,k>>1));
else
for(int x=1;x<=n;++x)
for(int i=head[x];i;i=a[i].nxt)
if(a[i].v>x)cmax(ans,dfs(x,a[i].v,k>>1)+dfs(a[i].v,x,k>>1));
printf("%d\n",n-ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int dp[11], c[11][11];
int main() {
int n, k;
cin >> n >> k;
long long ans = 1;
for (int i = 0; i < n - k; i++) ans *= n - k, ans %= mod;
dp[1] = 1;
dp[0] = 1;
c[0][0] = 1;
for (int i = 1; i < 11; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
for (int i = 2; i <= k - 1; i++) {
for (int j = 0; j <= i - 1; j++)
dp[i] += dp[j] * (i - j) * c[i - 1][j] * dp[i - j - 1];
}
ans *= dp[k - 1] * k;
ans %= mod;
cout << ans;
return 0;
}
| 2 |
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cassert>
#include<map>
#define _A_ true
#define EPS 1e-10
#define COUNTER_CLOCKWISE 1
#define CLOCKWISE -1
#define ONLINE_BACK 2
#define ONLINE_FRONT -2
#define ON_SEGMENT 0
using namespace std;
class Point{
public:
double x,y;
Point(double x =0,double y=0):x(x),y(y){}
Point operator + (Point p){return Point(x+p.x,y+p.y);}
Point operator - (Point p){return Point(x-p.x,y-p.y);}
Point operator * (double a){return Point(x*a,y*a);}
Point operator / (double a){return Point(x/a,y/a);} //もし悪を使うなら要確認
double norm() {return x*x+y*y;}
double abs() {return sqrt(norm());}
bool operator < (const Point &p) const{
return x != p.x ? x < p.x : y < p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
}
};
typedef Point Point;
struct Segment{
public:
Point p1,p2;
Segment(Point p = Point(),Point pp = Point()):p1(p),p2(pp){}
};
typedef vector<Point> Polygon;
typedef Segment Line;
Point ACM,IC,PC;
Segment IC_PC,PC_ACM,ACM_IC;
double norm(Point p){
return p.x*p.x+p.y*p.y;
}
double abs(Point p){
return sqrt(norm(p));
}
Point polar(double a,double rad){
return Point(a*cos(rad),a*sin(rad));
}
double args(Point p){
return atan2(p.y,p.x);
}
double dot(Point a,Point b){
return a.x*b.x+a.y*b.y;
}
double cross(Point a,Point b){
return a.x*b.y-b.x*a.y;
}
Point project(Segment s,Point p){
Point base = s.p2 - s.p1;
double t = dot(p-s.p1,base)/base.norm();
return s.p1+base*t;
}
Point reflect (Segment s, Point p){
return p + (project(s,p)-p)*2.0;
}
int ccw(Point p0,Point p1,Point p2){
Point a = p1-p0;
Point b = p2-p0;
if(cross(a,b) > EPS) return COUNTER_CLOCKWISE;
if(cross(a,b) < -EPS) return CLOCKWISE;
if(dot(a,b) < -EPS) return ONLINE_BACK;
if(a.norm() < b.norm() ) return ONLINE_FRONT;
return ON_SEGMENT;
}
bool isIntersect(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 isIntersect(Segment s1,Segment s2){
return isIntersect(s1.p1,s1.p2,s2.p1,s2.p2);
}
Point getCrossPointLines( Line s1, Line s2){
Point a = s1.p2 - s1.p1;
//cout << "In getCrossPointLines--------" << endl;
//cout << "a = " <<a.x << "," << a.y << endl;
Point base = Point(s2.p2.x - s2.p1.x,s2.p2.y - s2.p1.y);
//cout <<"base = "<< base.x << "," << base.y << endl;
return s1.p1 + a * cross(base, s2.p1 - s1.p1)/cross(base, a);
}
Polygon cutPolygon( Polygon P, Line l ){
Polygon u;
for ( int i = 0; i < P.size(); i++ ){
Point a = P[i], b = P[(i+1)%P.size()];
if ( ccw(l.p1, l.p2, a) != CLOCKWISE ) u.push_back(a);
if ( ccw(l.p1, l.p2, a) * ccw(l.p1, l.p2, b) == -1 ){
u.push_back(getCrossPointLines(Segment(a, b), l));
}
}
return u;
}
Line CreateLine(Point p,Point pp){
Point mid = (p+pp)/2;
Point sl = pp-p;//原点に5
double rad = args(sl);
Point ap = polar(abs(sl),rad+M_PI/2)+mid;
//ap.x = abs(sl)*cos(rad+M_PI/2)+mid.x;
//ap.y = abs(sl)*sin(rad+M_PI/2)+mid.y;
return Line(mid,ap);
}
double AreaCalc(Polygon p){
double area = 0;
p.push_back(p[0]);
for(int i=0;i<p.size()-1;i++){
area+=cross(p[i],p[i+1]);
}
return abs(area/2);
}
int main(){
Segment ss;
Point pp;
while(scanf("%lf,%lf,%lf,%lf,%lf,%lf",&ss.p1.x,&ss.p1.y,&ss.p2.x,&ss.p2.y,&pp.x,&pp.y) != EOF){
Point VVV = reflect(ss,pp);
printf("%.6f %.6f\n",VVV.x,VVV.y);
}
return 0;
} | 0 |
#include<bits/stdc++.h>
#define fi(i,a,b) for(int i = (a); i <= (b); i++)
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
int a[N];
int main(){
// freopen("in.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--){
int n, k;
scanf("%d %d", &n, &k);
int tmp = 0;int re, ans = 1654;
for(int i = 0; i < n; i++){
printf("%d\n", i^tmp);
fflush(stdout);
// printf("-----%d\n", tmp);
scanf("%d", &re);
if(re == 1){
break;
}
else{
tmp = tmp^(i^tmp);
}
}
// for(int i = 0; i < n; i++){
//// printf("%d\n", i^tmp);
// if((i^tmp) == ans){
// printf("ok%d\n", i);
// break;
// }else{
// ans ^= (i^tmp);
// tmp = tmp^(i^tmp);
// printf("%d----\n", ans);
// }
//// printf("-----%d\n", tmp);
//// scanf("%d", &re);
//
// }
}
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define RI register int
int read() {
int q=0;char ch=' ';
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar();
return q;
}
typedef long long LL;
const int N=2005;
int n,tot,a[N],vis[N],h[N],ne[N<<1],to[N<<1],du[N];
vector<int> orz[N];
int gcd(int x,int y) {return y?gcd(y,x%y):x;}
void add(int x,int y) {to[++tot]=y,ne[tot]=h[x],h[x]=tot,++du[y];}
void dfs(int x) {
vis[x]=1;
for(RI i=0;i<orz[x].size();++i)
if(!vis[orz[x][i]]) add(x,orz[x][i]),dfs(orz[x][i]);
}
priority_queue<int> q;
int main()
{
n=read();
for(RI i=1;i<=n;++i) a[i]=read();
sort(a+1,a+1+n);
for(RI i=1;i<=n;++i)
for(RI j=i+1;j<=n;++j)
if(gcd(a[i],a[j])>1) orz[i].push_back(j),orz[j].push_back(i);
for(RI i=1;i<=n;++i) if(!vis[i]) dfs(i);
for(RI i=1;i<=n;++i) if(!du[i]) q.push(i);
while(!q.empty()) {
int x=q.top();q.pop();printf("%d ",a[x]);
for(RI i=h[x];i;i=ne[i]) {
--du[to[i]];
if(!du[to[i]]) q.push(to[i]);
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
using cd = complex<ld>;
using vcd = vector<cd>;
using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T> >;
template <class T>
inline int ckmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int ckmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
template <class T>
void remDup(vector<T>& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), end(v));
}
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }
constexpr int p2(int x) { return 1 << x; }
constexpr int msk2(int x) { return p2(x) - 1; }
ll ceilDiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll floorDiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
void setPrec(int x) { cout << fixed << setprecision(x); }
string to_string(char c) { return string(1, c); }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
string to_string(bool b) { return to_string((int)b); }
template <class T>
string to_string(complex<T> c) {
stringstream ss;
ss << c;
return ss.str();
}
template <class T>
using V = vector<T>;
string to_string(V<bool> v) {
string res = "{";
for (int i = (0); i <= (int((v).size()) - 1); i++) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t sz>
string to_string(bitset<sz> b) {
string res = "";
for (int i = (0); i <= (int((b).size()) - 1); i++) res += char('0' + b[i]);
return res;
}
template <class T, class U>
string to_string(pair<T, U> p);
template <class T>
string to_string(T v) {
bool fst = 1;
string res = "";
for (const auto& x : v) {
if (!fst) res += " ";
fst = 0;
res += to_string(x);
}
return res;
}
template <class T, class U>
string to_string(pair<T, U> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class T>
void pr(T x) {
cout << to_string(x);
}
template <class T, class... U>
void pr(const T& t, const U&... u) {
pr(t);
pr(u...);
}
void ps() { pr("\n"); }
template <class T, class... U>
void ps(const T& t, const U&... u) {
pr(t);
if (sizeof...(u)) pr(" ");
ps(u...);
}
void DBG() { cerr << "]" << endl; }
template <class T, class... U>
void DBG(const T& t, const U&... u) {
cerr << to_string(t);
if (sizeof...(u)) cerr << ", ";
DBG(u...);
}
const ld PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld EPS = 1e-9;
const ll MODBASE = 1000000007LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int n, res = 0, a[35], b[35];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j) {
if (a[i] == b[j]) res++;
}
cout << res;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100005;
int n;
int p[Maxn];
bool tk[Maxn];
vector<vector<int> > S[Maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i <= n; i++)
if (!tk[i]) {
vector<int> seq;
seq.push_back(i);
tk[i] = true;
int num = p[i];
while (i != num) {
seq.push_back(num);
tk[num] = true;
num = p[num];
}
S[seq.size()].push_back(seq);
}
if (S[1].empty() && S[2].empty()) {
printf("NO\n");
return 0;
}
if (!S[1].empty()) {
printf("YES\n");
vector<int> cur = S[1].back();
S[1].pop_back();
for (int i = 1; i < Maxn; i++)
for (int j = 0; j < S[i].size(); j++)
for (int l = 0; l < S[i][j].size(); l++)
printf("%d %d\n", cur[0], S[i][j][l]);
} else {
bool ok = true;
for (int i = 3; i < Maxn; i += 2)
if (!S[i].empty()) ok = false;
if (!ok) {
printf("NO\n");
return 0;
}
printf("YES\n");
vector<int> cur = S[2].back();
S[2].pop_back();
printf("%d %d\n", cur[0], cur[1]);
for (int i = 2; i < Maxn; i++)
for (int j = 0; j < S[i].size(); j++)
for (int l = 0; l < S[i][j].size(); l++)
printf("%d %d\n", cur[l % 2], S[i][j][l]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b,k;
cin >> a >> b >> k;
vector<int> vec;
for(int i=1;i<200;i++) {
if(a%i==0&&b%i==0) vec.push_back(i);
}
cout << vec[vec.size()-k] << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 15;
const int INF = 1111111111;
int N, M, ans;
int tab[MX][MX], deg[MX];
vector<int> con[1 << MX], w[1 << MX];
struct data {
int u, val;
data(int u = 0, int val = 0) : u(u), val(val) {}
bool operator<(const data &D) const { return val > D.val; }
};
priority_queue<data> que;
int val[1 << MX];
void solve() {
int now;
for (int stat = 0; stat < (1 << N); stat++) {
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
if (tab[i][j] < INF) {
now = stat;
now ^= (1 << i), now ^= (1 << j);
con[stat].push_back(now);
w[stat].push_back(tab[i][j]);
}
}
now = 0;
for (int i = 0; i < N; i++) {
now |= ((deg[i] % 2) << i);
}
for (int i = 0; i < (1 << N); i++) val[i] = INF;
que.push(data(now, 0));
val[now] = 0;
while (!que.empty()) {
data tmp = que.top();
que.pop();
if (tmp.u == 0) {
ans += tmp.val;
printf("%d\n", ans);
return;
}
for (int i = con[tmp.u].size() - 1; i >= 0; i--) {
int v = con[tmp.u][i];
if (val[v] > val[tmp.u] + w[tmp.u][i]) {
val[v] = val[tmp.u] + w[tmp.u][i];
que.push(data(v, val[v]));
}
}
}
puts("-1");
}
int vis[MX];
void dfs(int u) {
vis[u] = true;
for (int i = 0; i < N; i++)
if (!vis[i] && tab[u][i] < INF) dfs(i);
}
bool connected() {
dfs(0);
for (int i = 0; i < N; i++)
if (!vis[i] && deg[i]) return false;
return true;
}
void init() {
for (int i = 0; i < MX; i++)
for (int j = 0; j < MX; j++) tab[i][j] = INF;
}
int main() {
init();
scanf("%d%d", &N, &M);
while (M--) {
int u, v, ww;
scanf("%d%d%d", &u, &v, &ww);
u--, v--;
ans += ww;
if (tab[u][v] > ww) tab[u][v] = tab[v][u] = ww;
deg[u]++, deg[v]++;
}
if (!connected()) {
puts("-1");
return 0;
}
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct point {
double x, y;
point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
point operator+(point v) { return point(x + v.x, y + v.y); }
point operator*(double k) { return point(x * k, y * k); }
};
double dist(point a, point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
point s, f;
cin >> s.x >> s.y >> f.x >> f.y;
double vmax, t;
cin >> vmax >> t;
point v1, v2;
cin >> v1.x >> v1.y >> v2.x >> v2.y;
v1 = v1 * -1.0, v2 = v2 * -1.0;
double L = 0.0, R = 1e9;
for (int i = 0; i < 1000; i++) {
double mid = (L + R) / 2;
(dist(s, f + v1 * min(mid, t) + v2 * max(0.0, mid - t)) > mid * vmax ? L
: R) =
mid;
}
cout << fixed << setprecision(15) << R;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v1;
vector<int> v2;
int n;
int dp[(int)1e5 + 5][5];
bool solve(int i, int cur) {
if (i == n - 1) return 1;
int &ret = dp[i][cur];
if (~ret) return ret;
ret = 0;
for (int j = 0; j <= 3; j++) {
if (((cur | j) == v1[i]) && ((cur & j) == v2[i])) {
ret |= solve(i + 1, j);
}
}
return ret;
}
void build(int i, int cur) {
if (i == n - 1) return;
for (int j = 0; j <= 3; j++) {
if (((cur | j) == v1[i]) && ((cur & j) == v2[i])) {
if (solve(i + 1, j)) {
cout << j << " ";
build(i + 1, j);
break;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
cin >> n;
v1.resize(n - 1);
v2.resize(n - 1);
for (auto &i : v1) cin >> i;
for (auto &i : v2) cin >> i;
bool ans = 0;
memset(dp, -1, sizeof(dp));
for (int i = 0; i <= 3; i++) {
ans |= solve(0, i);
}
if (ans) {
cout << "YES" << '\n';
for (int i = 0; i <= 3; i++) {
if (solve(0, i)) {
cout << i << " ";
build(0, i);
break;
}
}
} else
cout << "NO" << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 30;
const int S = 320;
int ff[maxn];
int freq[maxn];
vector<int> big;
int perform() {
priority_queue<int, vector<int>, greater<int> > Q;
int ans = 0;
for (int c : big) {
if (freq[c] >= S) {
Q.push(freq[c]);
}
}
vector<int> temp(S);
for (int i = 0; i < S; i++) {
temp[i] = ff[i];
}
int unmatched = 0;
for (int i = 1; i < S; i++) {
if (temp[i] == 0) continue;
if (unmatched != 0) {
int nxt = unmatched + i;
ans += nxt;
unmatched = 0;
temp[i]--;
if (nxt < S)
temp[nxt]++;
else
Q.push(nxt);
}
if (temp[i] % 2 != 0) {
unmatched = i;
temp[i]--;
}
int nxt = 2 * i;
ans += i * temp[i];
if (nxt < S)
temp[nxt] += temp[i] / 2;
else
for (int r = 0; r < temp[i] / 2; r++) Q.push(nxt);
}
if (unmatched != 0) Q.push(unmatched);
while (Q.size() > 1) {
int g = Q.top();
Q.pop();
int h = Q.top();
Q.pop();
ans += (g + h);
Q.push(g + h);
}
return ans;
}
void update(int p, int delta) {
ff[freq[p]]--;
freq[p] += delta;
ff[freq[p]]++;
}
pair<int, int> query[maxn];
int a[maxn];
int ANS[maxn];
bool cmp(int i, int j) {
if (query[i].first / S == query[j].first / S) {
return query[i].second < query[j].second;
}
return query[i].first / S < query[j].first / S;
}
int main() {
int n;
cin >> n;
vector<int> temp(maxn);
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
a[i] = t;
temp[t]++;
if (temp[t] == S) big.push_back(t);
}
int q;
cin >> q;
vector<int> order;
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r);
query[i] = pair<int, int>(l, r);
order.push_back(i);
}
sort(order.begin(), order.end(), cmp);
int lo = 1;
int hi = 1;
for (int e : order) {
int l = query[e].first;
int r = query[e].second + 1;
while (hi < r) {
update(a[hi], 1);
hi++;
}
while (hi > r) {
hi--;
update(a[hi], -1);
}
while (lo < l) {
update(a[lo], -1);
lo++;
}
while (lo > l) {
lo--;
update(a[lo], 1);
}
ANS[e] = perform();
}
for (int i = 0; i < q; i++) cout << ANS[i] << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8, pi = acos(-1.0);
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int CEIL(double z) { return ceil(z - eps); }
int FLOOR(double z) { return floor(z + eps); }
int dblcmp(double d) { return (d < -eps) ? -1 : (d > eps); }
const int inf = 999999999;
template <class T>
inline void checkmin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void myswap(T &a, T &b) {
T c = a;
a = b;
b = c;
}
template <class T>
inline T mysqr(T z) {
return z * z;
}
int main() {
int n, m, x;
char a[9], b[9], c[9], d[9];
scanf("%d%d", &n, &m);
int low = 1, high = n;
while (m--) {
scanf("%s%s%s%s%d", a, b, c, d, &x);
if (c[0] == 'l')
checkmin(high, x - 1);
else
checkmax(low, x + 1);
}
if (low > high)
puts("-1");
else
printf("%d\n", high - low + 1);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1009;
int s, n;
pair<int, int> p[N];
int cnt;
int main() {
cin >> s >> n;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p, p + n);
for (int i = 0; i < n; i++) {
if (s > p[i].first) {
s += p[i].second;
cnt++;
}
}
if (cnt == n)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4005;
struct Hopcroft_Karp {
static const int inf = 1e9;
int n;
vector<int> matchL, matchR, dist;
vector<vector<int> > g;
Hopcroft_Karp(int n)
: n(n), matchL(n + 1), matchR(n + 1), dist(n + 1), g(n + 1) {}
void addEdge(int u, int v) { g[u].push_back(v); }
bool bfs() {
queue<int> q;
for (int u = 1; u <= n; u++) {
if (!matchL[u]) {
dist[u] = 0;
q.push(u);
} else
dist[u] = inf;
}
dist[0] = inf;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : g[u]) {
if (dist[matchR[v]] == inf) {
dist[matchR[v]] = dist[u] + 1;
q.push(matchR[v]);
}
}
}
return (dist[0] != inf);
}
bool dfs(int u) {
if (!u) return true;
for (auto v : g[u]) {
if (dist[matchR[v]] == dist[u] + 1 && dfs(matchR[v])) {
matchL[u] = v;
matchR[v] = u;
return true;
}
}
dist[u] = inf;
return false;
}
int max_matching() {
int matching = 0;
while (bfs()) {
for (int u = 1; u <= n; u++) {
if (!matchL[u])
if (dfs(u)) matching++;
}
}
return matching;
}
};
int n, m, node = 0;
int a[N], par[N];
Hopcroft_Karp match(N);
vector<pair<int, int> > g[N];
void factorise(int idx) {
int cur = a[idx];
for (int i = 2; i * i <= cur; i++) {
while (cur % i == 0) {
g[idx].push_back({++node, i});
cur /= i;
}
}
if (cur > 1) g[idx].push_back({++node, cur});
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) factorise(i);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (v % 2) swap(u, v);
for (auto p : g[u]) {
for (auto q : g[v]) {
if (p.second == q.second) match.addEdge(p.first, q.first);
}
}
}
cout << match.max_matching();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long nm = 0;
bool fh = true;
char cw = getchar();
for (; !isdigit(cw); cw = getchar()) fh ^= (cw == '-');
for (; isdigit(cw); cw = getchar()) nm = nm * 10 + (cw - '0');
return fh ? nm : -nm;
}
char ch[1202000];
int n, m, f[1202000], hv[1202000], t[1202000][2], sz[1202000], ans = 0;
inline int fd(int x) { return x == f[x] ? x : f[x] = fd(f[x]); }
inline int ot(int x) { return (x > m) ? (x - m) : (x + m); }
inline void merge(int a, int b) {
a = fd(a), b = fd(b);
if (a == b) return;
int ota = fd(ot(a)), otb = fd(ot(b));
ans -= min(hv[a], hv[ota]);
ans -= min(hv[b], hv[otb]);
f[a] = b, hv[b] += hv[a], hv[b] = min(hv[b], 1202000 + 1202000);
f[ota] = otb, hv[otb] += hv[ota], hv[otb] = min(hv[otb], 1202000 + 1202000);
ans += min(hv[b], hv[otb]);
}
inline void calc(int x) {
if (!sz[x]) return;
int a0 = fd(t[x][0]), a1 = fd(t[x][0] + m);
int b0 = fd(t[x][1]), b1 = fd(t[x][1] + m);
if (ch[x] == '1') swap(b0, b1);
merge(a0, b1);
}
inline void solve() {
n = read(), m = read() + n, scanf("%s", ch + 1);
for (int i = 1; i <= m + m; i++) f[i] = i, hv[i] = (i > m);
for (int i = 1; i <= n; i++) hv[m + m - n + i] = 1202000 + 1202000;
for (int i = 1; i <= n; i++) t[i][0] = t[i][1] = m - n + i;
for (int i = 1; i <= m - n; i++)
for (int tt = read(), x; tt; tt--) x = read(), t[x][sz[x]++] = i;
for (int i = 1; i <= n; i++) calc(i), printf("%d\n", ans);
}
int main() {
for (int Cas = 1; Cas; --Cas) solve();
return 0;
}
| 3 |
#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>
using namespace std;
typedef long long ll;
int w, h;
int field[10][60];
vector <string> temp;
int num;
vector <pair <double, double> > memo(150); // 重心(x)と質量
bool ans;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
bool is_in(int x, int y) {
return x >= 0 && y >= 0 && x < w && y < h;
}
// int calcSum(int x, int y, int n) {
// int ret = x;
// for (int i = 0; i < 4; i++) {
// int nx = x+dx[i];
// int ny = y+dy[i];
// if (is_in(nx, ny) && field[nx][ny] == n) {
// ret += calcSum(nx, ny, n);
// }
// }
// return ret;
// }
pair <double, double> rec(int x, int y) {
int col = field[x][y];
if (memo[col].first != -1) return memo[col];
pair<double, double> ret = make_pair(0, 0);
vector <pair<double, double> > list;
double xl, xr;
xl = 100;
xr = 0;
double sum = 0;
bool vis[10][60];
memset(vis, false, sizeof(vis));
bool vis2[150]; // 色
memset(vis2, false, sizeof(vis2));
vis2[col] = true;
stack <int> st;
st.push(x);
st.push(y);
vis[x][y] = true;
sum += double(x)+0.5;
while (!st.empty()) {
int py = st.top(); st.pop();
int px = st.top(); st.pop();
for (int i = 0; i < 4; i++) {
int nx = px+dx[i];
int ny = py+dy[i];
if (!is_in(nx, ny) || vis[nx][ny] || field[nx][ny] == -1) continue;
if (dx[i] == 0 && dy[i] == 1 &&
field[nx][ny] != col) {
xl = min(xl, double(nx));
xr = max(xr, double(nx));
}
if (dx[i] == 0 && dy[i] == -1 &&
field[nx][ny] != col &&
!vis2[field[nx][ny]]) {
list.push_back(rec(nx, ny));
vis2[field[nx][ny]] = true;
}
if (field[nx][ny] == col) {
sum += double(nx)+0.5;
vis[nx][ny] = true;
st.push(nx);
st.push(ny);
}
}
}
double down = 0;
double up = 0;
for (int i = 0; i < list.size(); i++) {
up += list[i].second*list[i].first;
down += list[i].second;
}
ret.first = (up+sum)/(down+4);
ret.second = 4+down;
if (xl == 100) {
for (int i = 0; i < w; i++) {
if (field[i][y] == col) {
xl = i;
break;
}
}
}
if (xr == 0) {
for (int i = w-1; i >= 0; i--) {
if (field[i][y] == col) {
xr = i;
break;
}
}
}
if (ret.first <= xl || ret.first >= xr+1) {
// std::cout << col << ":" << xl << " " << xr << "::" << ret.first << std::endl;
ans = false;
}
return memo[col] = ret;
}
void calcNum() {
int cnt = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (temp[y][x] != '.') {
int col = temp[y][x]-'0';
field[x][y] = cnt;
stack <int> st;
st.push(x);
st.push(y);
while(!st.empty()) {
int py = st.top(); st.pop();
int px = st.top(); st.pop();
temp[py][px] = '.';
for (int i = 0; i < 4; i++) {
int nx = px+dx[i];
int ny = py+dy[i];
if (is_in(nx, ny) && temp[ny][nx]-'0' == col) {
st.push(nx);
st.push(ny);
field[nx][ny] = cnt;
}
}
}
cnt++;
}
}
}
}
void viewField() {
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (field[x][y] == -1) {
std::cout << ". ";
}else {
std::cout << field[x][y] << " ";
}
}
std::cout << "" << std::endl;
}
std::cout << "" << std::endl;
}
int main() {
while(true) {
cin >> w >> h;
if (w == 0 && h == 0) break;
vector <string> mainField;
string str;
for (int y = 0; y < h; y++) {
cin >> str;
mainField.push_back(str);
}
temp = mainField;
memset(field, -1, sizeof(field));
calcNum();
for (int i = 0; i < 150; i++) {
memo[i].first = -1;
memo[i].second = 0;
}
ans = true;
for (int i = 0; i < w; i++) {
if (field[i][h-1] != -1) {
rec(i, h-1);
break;
}
}
if (ans) {
std::cout << "STABLE" << std::endl;
}else {
std::cout << "UNSTABLE" << std::endl;
}
}
} | 0 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
string a;
while(cin>>a){
int b=0,c=0;
rep(i,a.size()-2){
if(a[i]=='J'&&a[i+1]=='O'&&a[i+2]=='I')b++;
if(a[i]=='I'&&a[i+1]=='O'&&a[i+2]=='I')c++;
}
cout<<b<<endl<<c<<endl;
}
return (0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long pos = 0;
long long neg = 0;
long long ans1 = 1e18;
long long ans2 = 0;
int a[1000005];
int cnt[2000005];
int tot1 = 0;
int tot2 = 0;
int n;
void update(int i, int p) {
if (a[i] - i >= 0) {
if (a[i] - n >= 0) {
cnt[a[i] - i]--;
pos -= a[i] - n;
tot1--;
} else {
neg -= n - a[i];
tot2--;
}
} else {
neg -= n - a[i];
tot2--;
}
tot1 -= cnt[p];
pos -= tot1;
neg += cnt[p];
neg += tot2;
tot2 += cnt[p];
cnt[a[i] - 1 + p + 1]++;
pos += a[i] - 1;
tot1++;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] - i >= 0) {
cnt[a[i] - i]++;
pos += a[i] - i;
tot1++;
} else {
neg += i - a[i];
tot2++;
}
}
int p = 0;
if (ans1 > neg + pos) {
ans1 = neg + pos;
ans2 = p;
}
update(n, p);
p++;
for (int i = n - 1; i >= 1; i--, p++) {
if (ans1 > neg + pos) {
ans1 = neg + pos;
ans2 = p;
}
update(i, p);
}
printf("%lld %lld\n", ans1, ans2);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int query(int a, int b) {
cout << "? " << a << " " << b << endl;
fflush(stdout);
int tmp;
cin >> tmp;
return tmp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int flag = query(0, 0);
int a = 0, b = 0;
int tmp;
for (int i = 29; i >= 0; i--) {
if (flag == 0) {
tmp = query(a | (1 << i), b);
if (tmp == -1) {
a |= (1 << i);
b |= (1 << i);
}
} else if (flag == 1) {
tmp = query(a | (1 << i), b | (1 << i));
if (tmp == -1) {
a |= (1 << i);
flag = query(a, b);
} else {
tmp = query(a | (1 << i), b);
if (tmp == -1) {
a |= (1 << i);
b |= (1 << i);
}
}
} else {
tmp = query(a | (1 << i), b | (1 << i));
if (tmp == 1) {
b |= (1 << i);
flag = query(a, b);
} else {
tmp = query(a | (1 << i), b);
if (tmp == -1) {
a |= (1 << i);
b |= (1 << i);
}
}
}
}
cout << "! " << a << " " << b << endl;
fflush(stdout);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n;
cin >> n;
vector<long int> v;
for (int i = 0; i < n; i++) {
long int temp;
cin >> temp;
v.push_back(temp);
}
sort(v.rbegin(), v.rend());
long int last = v[0] + 2;
int counter = 0;
for (int i = 0; i < n; i++) {
if (v[i] + 1 <= 150001 && v[i] + 1 < last) {
last = v[i] + 1;
counter++;
} else if (v[i] < last) {
last = v[i];
counter++;
} else if (v[i] - 1 < last && v[i] - 1 > 0) {
last = v[i] - 1;
counter++;
} else
;
}
cout << counter << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<pair<int, int> > g[N];
int fa[N], dis[N], mx[N], upmx[N], id[N];
vector<int> d;
void dfs(int x) {
mx[x] = dis[x];
for (auto u : g[x])
if (u.first != fa[x]) {
dis[u.first] = dis[x] + u.second;
fa[u.first] = x;
dfs(u.first);
if (mx[x] < mx[u.first]) mx[x] = mx[u.first], id[x] = u.first;
}
}
void dfs1(int x) {
mx[x] = dis[x];
for (auto u : g[x])
if (u.first != fa[x]) {
dis[u.first] = dis[x] + u.second;
fa[u.first] = x;
int v = upmx[x];
if (id[x] == u.first) {
for (auto u : g[x])
if (u.first != id[x] && u.first != fa[x])
v = max(v, mx[u.first] - dis[x]);
} else {
v = max(v, mx[x] - dis[x]);
}
upmx[u.first] = v + u.second;
dfs1(u.first);
mx[x] = max(mx[x], mx[u.first]);
}
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x, z));
}
dfs(1);
int s = 1;
for (int i = 1; i <= n; i++)
if (dis[i] > dis[s]) s = i;
memset(fa, 0, sizeof(fa));
memset(dis, 0, sizeof(dis));
dfs(s);
dfs1(s);
int t = s;
for (int i = 1; i <= n; i++)
if (dis[i] > dis[t]) {
t = i;
}
while (t != s) {
d.push_back(t);
t = fa[t];
}
d.push_back(t);
reverse(d.begin(), d.end());
int ans = 2e9;
int v = 0;
for (int i = (int)d.size() - 1; i >= 0; i--) {
for (auto u : g[d[i]])
if (u.first != d[i + 1] && u.first != fa[d[i]])
v = max(v, mx[u.first] - dis[d[i]]);
if (i + k - 1 < d.size())
for (auto u : g[d[i + k - 1]])
if (u.first != fa[d[i + k - 1]])
v = max(v, mx[u.first] - dis[d[i + k - 1]]);
ans = min(ans, max(v, upmx[d[i]]));
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> e[100000];
int sz[100000];
double ans[100000];
void dfs(int now) {
sz[now] = 1;
for (int i = 0; i < e[now].size(); ++i) {
int next = e[now][i];
dfs(next);
sz[now] += sz[next];
}
}
void calc(int now) {
for (int i = 0; i < e[now].size(); ++i) {
int next = e[now][i];
ans[next] = ans[now] + 1.0;
ans[next] += 0.5 * (sz[now] - sz[next] - 1);
calc(next);
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
int p;
scanf("%d", &p);
p--;
e[p].push_back(i + 1);
}
dfs(0);
ans[0] = 1.0;
calc(0);
for (int i = 0; i < n; ++i) printf("%.10f ", ans[i]);
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, l, r, ans(1000000007);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(0);
cin >> n >> m >> l >> r;
if (l == 1 && r == n) ans = 0;
if (l == 1) ans = min(ans, abs(m - r) + 1);
if (r == n) ans = min(ans, abs(m - l) + 1);
ans = min(ans, abs(m - l) + 1 + abs(l - r) + 1);
ans = min(ans, abs(m - r) + 1 + abs(l - r) + 1);
cout << ans << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std ;
#define ll long long
void FastInputOutput(){
ios_base :: sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
}
inline int D(){
int t ;
scanf( "%d" , &t ) ;
return t ;
}
inline ll LLD(){
ll t ;
scanf( "%lld" , &t ) ;
return t ;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b){
// gcd * lcm = a * b --> lcm = a / gcd * b;
return a / gcd(a, b) * b;
}
ll power(ll x, ll p, ll m){
ll res = 1;
while(p){
if(p & 1)
res = (res * x) % m;
x = ((x % m) * (x % m)) % m;
p >>= 1ll;
}
return res;
}
long double ModLog(long double base, long double x) {
return (logl(x) / logl(base));
}
int mod(int a, int m){
return (((a % m) + m) % m);
}
const int N = 300 + 5, MOD = 998244353;
char a[N][N];
void init(){
}
void solve(){
int n = D();
for(int i = 0; i < n; ++i)
scanf("%s", a[i]);
vector<int> x(3), o(3);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
x[(i + j) % 3] += a[i][j] == 'X';
o[(i + j) % 3] += a[i][j] == 'O';
}
}
int mnX = 0, mnO = 1;
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
if(i != j && x[i] + o[j] < x[mnX] + o[mnO])
mnX = i, mnO = j;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(a[i][j] == 'X' && ((i + j) % 3) == mnX)
printf("O");
else if(a[i][j] == 'O' && ((i + j) % 3) == mnO)
printf("X");
else
printf("%c", a[i][j]);
}
puts("");
}
}
int main(){
int t;
t = D();
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int dis[200009], hdis[2000009], n, m, s, w[200009], c[200009], e[2][200009];
bool tak[200009];
pair<int, pair<int, int> > sparse[20][200009];
int h[200009];
long long tot = 0;
vector<pair<int, pair<int, int> > > graph[200009];
bool check(int a, int b) {
while (dis[a]) a = dis[a];
while (dis[b]) b = dis[b];
if (a == b) return 1;
return 0;
}
void join(int a, int b) {
if (check(a, b)) return;
while (dis[a]) a = dis[a];
while (dis[b]) b = dis[b];
if (hdis[a] > hdis[b])
dis[b] = a;
else if (hdis[a] == hdis[b]) {
dis[b] = a;
hdis[a]++;
} else
dis[a] = b;
}
void dfs(int i, int prev, int weight, int he, int ed) {
if (prev) {
h[i] = he;
sparse[0][i] = {prev, {weight, ed}};
for (int f = 1; f < 20; f++) {
sparse[f][i].first = sparse[f - 1][sparse[f - 1][i].first].first;
sparse[f][i].second = max(sparse[f - 1][sparse[f - 1][i].first].second,
sparse[f - 1][i].second);
}
}
for (int f = 0; f < graph[i].size(); f++)
if (graph[i][f].first != prev)
dfs(graph[i][f].first, i, graph[i][f].second.first, he + 1,
graph[i][f].second.second);
}
pair<int, int> getmax(int a, int b) {
pair<int, int> ret = {0, 0};
if (h[a] > h[b]) swap(a, b);
int hh = h[b] - h[a];
for (int f = 19; f >= 0; f--) {
if ((1 << f) <= hh) {
ret = max(ret, sparse[f][b].second);
b = sparse[f][b].first;
hh -= (1 << f);
}
}
for (int f = 19; f >= 0; f--) {
if (sparse[f][a] != sparse[f][b]) {
ret = max(ret, sparse[f][b].second);
b = sparse[f][b].first;
ret = max(ret, sparse[f][a].second);
a = sparse[f][a].first;
}
}
if (a != b) {
ret = max(ret, sparse[0][b].second);
b = sparse[0][b].first;
ret = max(ret, sparse[0][a].second);
a = sparse[0][a].first;
}
return ret;
}
int main() {
scanf("%d %d", &n, &m);
for (int f = 0; f < m; f++) scanf("%d", &w[f]);
for (int f = 0; f < m; f++) scanf("%d", &c[f]);
vector<pair<int, int> > v;
for (int f = 0; f < m; f++) {
scanf("%d %d", &e[0][f], &e[1][f]);
v.push_back({w[f], f});
}
scanf("%d", &s);
sort(v.begin(), v.end());
for (int f = 0; f < v.size(); f++) {
if (check(e[0][v[f].second], e[1][v[f].second])) continue;
join(e[0][v[f].second], e[1][v[f].second]);
graph[e[0][v[f].second]].push_back(
{e[1][v[f].second], {v[f].first, v[f].second}});
graph[e[1][v[f].second]].push_back(
{e[0][v[f].second], {v[f].first, v[f].second}});
tot += v[f].first;
tak[v[f].second] = 1;
}
dfs(1, 0, 0, 0, 0);
long long ans = tot, xx;
int nt = m, tt = m;
pair<int, int> temp;
for (int f = 0; f < m; f++) {
temp = getmax(e[0][f], e[1][f]);
xx = tot - temp.first + w[f] - s / c[f];
if (xx < ans) {
ans = xx;
nt = temp.second;
tt = f;
}
}
printf("%I64d\n", ans);
for (int f = 0; f < m; f++) {
if (tt == f)
printf("%d %d\n", f + 1, w[f] - s / c[f]);
else if (nt != f && tak[f])
printf("%d %d\n", f + 1, w[f]);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
const long long Mod = 1e9 + 7;
long long n, m, mod, ans, A[N], B[N], C[N], D[N];
inline long long read() {
long long ret = 0, f = 0;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = 1;
c = getchar();
}
while (isdigit(c)) {
ret = ret * 10 + c - 48;
c = getchar();
}
if (f) return -ret;
return ret;
}
struct node {
long long m[105][105];
} a, b, res;
node operator*(node a, node b) {
node c;
for (long long i = 0; i < mod; i++)
for (long long j = 0; j < mod; j++) c.m[i][j] = 0;
for (long long i = 0; i < mod; i++)
for (long long j = 0; j < mod; j++)
for (long long k = 0; k < mod; k++)
c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % Mod;
return c;
}
node kuai(node a, long long b) {
for (long long i = 0; i < mod; i++) res.m[i][i] = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b = b / 2;
}
return res;
}
signed main() {
n = read();
m = read();
mod = read();
for (long long i = 1; i <= n; i++) {
long long x = read() % mod;
A[x]++;
}
for (long long i = 1; i <= n; i++) {
long long x = read() % mod;
B[x]++;
D[i] = x;
}
for (long long i = 1; i <= n; i++) C[i] = read();
for (long long i = 0; i < mod; i++) {
for (long long j = 0; j < mod; j++) {
if (i >= j)
a.m[i][j] = B[i - j];
else
a.m[i][j] = B[i + mod - j];
}
}
a = kuai(a, m - 2);
for (long long i = 0; i < mod; i++) b.m[i][0] = A[i];
a = a * b;
for (long long i = 0; i < mod; i++)
for (long long j = 1; j <= n; j++)
if ((i + D[j] + C[j]) % mod == 0) ans = (ans + a.m[i][0]) % Mod;
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
const ll MAX_X=1e3,MAX_Y=1e3,MAX_Z=1e3;
ll X,Y,Z,K,A[MAX_X],B[MAX_Y],C[MAX_Z],cmb[MAX_X*MAX_Y],inx[MAX_Z];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>X>>Y>>Z>>K;
REP(i,X)cin>>A[i];
REP(i,Y)cin>>B[i];
REP(i,Z)cin>>C[i];
REP(i,X)REP(j,Y){
cmb[i*Y+j]=A[i]+B[j];
}
sort(cmb,cmb+X*Y);
fill(inx,inx+Z,X*Y-1);
while(K--){
ll ans=-1;
REP(i,Z){
if(inx[i]>=0 && (ans==-1 || cmb[inx[ans]]+C[ans]<cmb[inx[i]]+C[i])){
ans=i;
}
}
cout<<cmb[inx[ans]]+C[ans]<<endl;
inx[ans]--;
}
} | 0 |
#include<cstdio>
#include<vector>
#include<cstdlib>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int INF=(1<<31)-1;
template<class T> struct Interval{
T a,b;
Interval(){}
Interval(T A,T B):a(A),b(B){}
};
template<class T>
class RMQ{
int n;
T *a;
T query(const Interval<int> &I,const Interval<int> &J,int u){
if(J.b<=I.a || I.b<=J.a) return INF;
if(I.a<=J.a && J.b<=I.b) return a[u];
int m=(J.a+J.b)/2;
T tl=query(I,Interval<int>(J.a,m),2*u+1);
T tr=query(I,Interval<int>(m,J.b),2*u+2);
return tl<tr?tl:tr;
}
public:
RMQ(){}
RMQ(const vector< vector<T> > &v,int k,int b):n(1){
int N;
if(!b) N=v[0].size();
else N=v.size();
while(n<N) n<<=1;
a=(T *)malloc((2*n-1)*sizeof(T));
rep(i,2*n-1) a[i]=0;
if(!b) rep(i,N) a[n+i-1]=v[k][i];
else rep(i,N) a[n+i-1]=v[i][k];
for(int i=n-2;i>=0;i--) a[i]=min(a[2*i+1],a[2*i+2]);
}
T query(int a,int b){
return query(Interval<int>(a,b),Interval<int>(0,n),0);
}
};
int main(){
for(int h,w,q;scanf("%d%d%d",&h,&w,&q),h||w||q;){
vvi a(h,vi(w));
rep(i,h) rep(j,w) scanf("%d",&a[i][j]);
RMQ<int> *rmq1=(RMQ<int> *)malloc(h*sizeof(RMQ<int>));
RMQ<int> *rmq2=(RMQ<int> *)malloc(w*sizeof(RMQ<int>));
rep(i,h) rmq1[i]=RMQ<int>(a,i,0);
rep(j,w) rmq2[j]=RMQ<int>(a,j,1);
rep(_,q){
int t,l,b,r; scanf("%d%d%d%d",&t,&l,&b,&r); b++; r++;
int ans=INF;
if(b-t<r-l) for(int i=t;i<b;i++) ans=min(ans,rmq1[i].query(l,r));
else for(int j=l;j<r;j++) ans=min(ans,rmq2[j].query(t,b));
printf("%d\n",ans);
}
free(rmq1);
free(rmq2);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
int _f = N / 2, _g = M / 2;
for (int x = 1; x <= _f; x++) {
int tmp = N - x + 1;
for (int i = 0; i < M; i++) {
cout << x << ' ' << i + 1 << '\n';
cout << tmp << ' ' << M - i << '\n';
}
}
if (N & 1) {
for (int i = 0; i < _g; i++) {
cout << _f + 1 << ' ' << i + 1 << '\n';
cout << _f + 1 << ' ' << M - i << '\n';
}
if (M & 1) cout << _f + 1 << ' ' << _g + 1 << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, k, p[N];
int cnt[N];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", p + i);
bitset<N> vis;
vector<int> v;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
int cur = i, cnt = 0;
while (!vis[cur]) {
cnt++;
vis[cur] = 1;
cur = p[cur];
}
v.push_back(cnt);
}
sort(v.begin(), v.end());
int tmp = k;
int ans = 0;
for (int i = 0; i < v.size(); i++) {
cnt[v[i]]++;
int x = min(tmp, v[i] / 2);
ans += x * 2;
v[i] -= x * 2;
tmp -= x;
}
for (int i = 0; i < v.size(); i++) {
int x = min(tmp, v[i]);
ans += x;
tmp -= x;
}
vis.reset();
vis[0] = 1;
int ans2 = k + 1;
for (int i = 1; i <= k; i++) {
if (i > 30) {
for (int j = 0; j < cnt[i]; j++) {
vis = (vis | (vis << i));
}
} else {
if (!cnt[i]) continue;
vector<deque<int>> dq(i);
vector<int> sum(i);
int cur = 0;
for (int j = 0; j <= k; j++) {
dq[cur].push_back(vis[j]);
sum[cur] += vis[j];
if (sum[cur]) vis[j] = 1;
if (dq[cur].size() > cnt[i]) {
sum[cur] -= dq[j % i].front();
dq[cur].pop_front();
}
++cur;
if (cur == i) cur = 0;
}
}
}
if (vis[k]) ans2 = k;
cout << ans2 << ' ' << ans << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 20, M = 4096;
int w[N];
int cnt[M];
int ans[M][110];
int main() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < n; i++) scanf("%d", w + i);
for (int i = 0; i < m; i++) {
char s[15];
scanf("%s", s);
bitset<20> t(s);
cnt[t.to_ulong()]++;
}
m = 1 << n;
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++) {
int t = 0;
for (int k = 0; k < n; k++)
if (((i >> k) & 1) == ((j >> k) & 1)) t += w[n - k - 1];
if (t <= 100) ans[i][t] += cnt[j];
}
for (int i = 0; i < m; i++)
for (int j = 1; j <= 100; j++) ans[i][j] += ans[i][j - 1];
while (q--) {
char s[15];
int k;
scanf("%s%d", s, &k);
bitset<20> t(s);
printf("%d\n", ans[t.to_ulong()][k]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int askQueryA(int x) {
cout << "A " << x << '\n';
cout.flush();
cin >> x;
return x;
}
int askQueryB(int x) {
cout << "B " << x << '\n';
cout.flush();
cin >> x;
return x;
}
void answerQuery(int x) {
cout << "C " << x << '\n';
cout.flush();
}
int n;
set<int> s1, s2;
bool used[MAXN];
vector<int> graph[MAXN];
queue<int> q;
int BFS(int x) {
while (q.empty() == false) q.pop();
for (int i = 1; i <= n; i++) {
used[i] = false;
}
q.push(x);
used[x] = true;
while (q.empty() == false) {
x = q.front();
q.pop();
if (s1.count(x) == true) {
return x;
}
for (int y : graph[x]) {
if (used[y] == false) {
q.push(y);
used[y] = true;
}
}
}
return 0;
}
void solve() {
cin >> n;
s1.clear();
s2.clear();
for (int x = 1; x <= n; x++) {
graph[x].clear();
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
int k;
cin >> k;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
s1.insert(x);
}
cin >> k;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
s2.insert(x);
}
int x = BFS(askQueryB(*s2.begin()));
int y = askQueryA(x);
if (s2.count(y) == true)
answerQuery(x);
else
answerQuery(-1);
}
int main() {
int T;
cin >> T;
while (T--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double r1,r2; cin >> r1 >> r2;
printf("%.10f\n",1/(1/r1+1/r2));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
struct node {
int v, next, en;
} e[2 * N];
int cf[N];
int ans[N];
bool vis[N];
int tot, a, b, n, u, v, k;
int head[N], deep[N];
int f[N][32];
void add_edge(int u, int v, int en) {
e[tot].en = en;
e[tot].v = v;
e[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
f[u][0] = fa;
for (int i = 1; (1 << i) <= deep[u]; i++) f[u][i] = f[f[u][i - 1]][i - 1];
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (v != fa) dfs(v, u);
}
}
int LCA(int a, int b) {
if (deep[a] > deep[b]) swap(a, b);
for (int i = 31; i >= 0; i--)
if (deep[a] <= deep[b] - (1 << i)) b = f[b][i];
if (a == b) return a;
for (int i = 31; i >= 0; i--) {
if (f[a][i] == f[b][i])
continue;
else
a = f[a][i], b = f[b][i];
}
return f[a][0];
}
void make_cf(int u) {
vis[u] = true;
for (int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if (vis[v]) continue;
make_cf(v);
cf[u] += cf[v];
ans[e[i].en] += cf[v];
}
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
add_edge(u, v, i);
add_edge(v, u, i);
}
cout << endl;
dfs(1, 0);
scanf("%d", &k);
for (int i = 1; i <= k; i++) {
scanf("%d%d", &u, &v);
cf[u]++;
cf[v]++;
cf[LCA(u, v)] -= 2;
}
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
make_cf(1);
for (int i = 1; i < n; i++) printf("%d ", ans[i]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int r[100000];
vector<int> cont[100000];
struct UnionFind {
vector<int> data;
UnionFind(int sz) { data.assign(sz, -1); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return (false);
if (data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0) return (k);
return (data[k] = find(data[k]));
}
int size(int k) { return (-data[find(k)]); }
};
struct Bipartite_Graph : UnionFind {
vector<int> color;
Bipartite_Graph(int v) : color(v + v, -1), UnionFind(v + v) {}
bool bipartite_graph_coloring() {
for (int i = 0; i < color.size() / 2; i++) {
int a = find(i);
int b = find(i + color.size() / 2);
if (a == b) return (false);
if (color[a] < 0) color[a] = 0, color[b] = 1;
}
return (true);
}
bool operator[](int k) { return (bool(color[find(k)])); }
};
int main() {
int N, A[100000], B[100000];
cin >> N;
int sz = N * 2;
Bipartite_Graph uf(sz);
for (int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
--A[i], --B[i];
uf.unite(A[i], B[i] + sz);
uf.unite(A[i] + sz, B[i]);
uf.unite(i * 2, i * 2 + 1 + sz);
uf.unite(i * 2 + sz, i * 2 + 1);
}
uf.bipartite_graph_coloring();
for (int i = 0; i < N; i++) {
cout << uf[A[i]] + 1 << " " << uf[B[i]] + 1 << endl;
}
}
| 5 |
#include <bits/stdc++.h>
const long long maxn = 1e4 + 7;
using namespace std;
struct _IO {
_IO() {
ios::sync_with_stdio(0);
cin.tie(0);
}
} _io;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
const int N = 1e4 + 10, M = 1e5 + 10;
struct Edge {
int u, v, t;
} edge[M];
int head[N * 3], cent;
int n, m;
int maxflow;
int dep[N * 3], ans[N * 3], vir[N * 3];
struct Node {
int ui, vi;
int ti;
int next;
} node[M * 4];
void add(int u, int v, int t) {
node[cent].vi = v;
node[cent].ti = t;
node[cent].next = head[u];
head[u] = cent++;
}
bool bfs() {
for (int i = 0; i <= 2 * n + 1; i++) {
dep[i] = 0x3f3f3f3f;
ans[i] = 0;
vir[i] = head[i];
}
queue<int> q;
q.push(0);
dep[0] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
ans[now] = 0;
for (int i = head[now]; ~i; i = node[i].next) {
int to = node[i].vi;
if (dep[to] > dep[now] + 1 && node[i].ti) {
dep[to] = dep[now] + 1;
if (ans[to] == 0) {
ans[to] = 1;
q.push(to);
}
}
}
}
if (dep[2 * n + 1] != 0x3f3f3f3f) return 1;
return 0;
}
int dfs(int u, int flow) {
int rlow = 0;
if (u == 2 * n + 1) {
maxflow += flow;
return flow;
}
int used = 0;
for (int i = vir[u]; ~i; i = node[i].next) {
vir[u] = i;
int d = node[i].vi;
if (node[i].ti && dep[d] == dep[u] + 1) {
if (rlow = dfs(d, min(flow - used, node[i].ti))) {
used += rlow;
node[i].ti -= rlow;
node[i ^ 1].ti += rlow;
if (used == flow) break;
}
}
}
return used;
}
int Dinic() {
while (bfs()) {
dfs(0, 0x3f3f3f3f);
}
return maxflow;
}
int check(int f) {
memset(head, -1, sizeof(head));
cent = 0;
for (int i = 1; i <= n; i++) {
add(0, i, 1);
add(i, 0, 0);
add(n + i, 2 * n + 1, 1);
add(2 * n + 1, n + i, 0);
}
for (int i = 1; i <= m; i++) {
if (edge[i].t <= f) {
add(edge[i].u, edge[i].v + n, 1);
add(edge[i].v + n, edge[i].u, 0);
}
}
maxflow = 0;
Dinic();
if (maxflow < n)
return 0;
else
return 1;
}
int main() {
cin >> n >> m;
int minn = 0x3f3f3f3f, maxn = 0;
for (int i = 1; i <= m; i++) {
cin >> edge[i].u >> edge[i].v >> edge[i].t;
minn = min(minn, edge[i].t);
maxn = max(maxn, edge[i].t);
}
int l = minn, r = maxn;
int res = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
res = mid;
r = mid - 1;
} else
l = mid + 1;
}
cout << res << '\n';
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.