solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
int __gcd(int a, int b) { return b == 0 ? a : __gcd(b, a % b); }
int power(int x, int n) {
if (n == 0)
return 1;
else if (n % 2 == 0) {
int y = power(x, n / 2);
return y * y;
} else
return x * power(x, n - 1);
}
int z[1000010];
int cnt[1000010];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
string s;
cin >> s;
int n = s.size(), i, t;
memset(z, 0, sizeof(z));
z[0] = 0;
int l = 0, r = 0;
for (i = 1; i < n; i++) {
if (i > r) {
l = r = i;
while (r < n && s[r - l] == s[r]) r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (z[k] < r - i + 1) {
z[i] = z[k];
} else {
l = i;
while (r < n && s[r - l] == s[r]) r++;
z[i] = r - l;
r--;
}
}
}
int ma = -1, ans = -1;
for (i = 1; i < n; i++) {
if (i + z[i] == n && ma >= z[i]) {
ans = max(z[i], ans);
}
ma = max(ma, z[i]);
}
if (ans == -1)
cout << "Just a legend" << '\n';
else {
for (i = 0; i < ans; i++) cout << s[i];
cout << '\n';
}
}
| 4 |
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int m, nmin, nmax;
while (cin >> m >> nmin >> nmax&&m&&nmin&&nmax) {
int p[200];
for (int i = 0; i < m; i++)cin >> p[i];
int soeji, sa1=p[nmin-1]-p[nmin],sa2;
for (int i = nmin - 1; i <= nmax - 1; i++) {
if (sa1 <= p[i] - p[i + 1]) {
sa1 = p[i] - p[i + 1];
soeji = i+1;
}
}
cout << soeji << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d, e, f, i, j, k, n, m, t, s, q, l, r, x, y, z;
cin >> n >> a;
if ((n - a) > (a - 1))
cout << a + 1 << "\n";
else {
if ((a - 1) == 0)
cout << a << "\n";
else
cout << a - 1 << "\n";
}
return 0;
}
| 2 |
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include <iomanip>
#include<math.h>
#define rep0(i,N) for(int i=0;i<N;i++)
#define rep1(i,a,N) for(int i=0+a;i<N;i++)
#define lint long long int
#define SIZE 100005
using namespace std;
int main(){
int a[10];
cin>>a[1]>>a[2]>>a[5];
int sum;
sum=a[5]*3;
a[9]=sum-a[1]-a[5];
a[8]=sum-a[2]-a[5];
a[7]=sum-a[8]-a[9];
a[4]=sum-a[1]-a[7];
a[6]=sum-a[4]-a[5];
a[3]=sum-a[1]-a[2];
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
cout<<a[4]<<" "<<a[5]<<" "<<a[6]<<endl;
cout<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int totans;
int ans[205];
int getsum(int g1) {
int ret = 0;
while (g1) {
ret += g1 % 10;
g1 = g1 / 10;
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i1 = max(0, n - 90); i1 <= n; i1++)
if (getsum(i1) + i1 == n) totans++, ans[totans] = i1;
printf("%d\n", totans);
for (int i1 = 1; i1 <= totans; i1++) printf("%d ", ans[i1]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'W') {
if (i >= 1 and grid[i - 1][j] == 'P') {
count++;
} else if (i < n - 1 and grid[i + 1][j] == 'P') {
count++;
} else if (j >= 1 and grid[i][j - 1] == 'P') {
count++;
} else if (j < m - 1 and grid[i][j + 1] == 'P') {
count++;
}
}
}
}
cout << count << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, m, i, j, a, b;
cin >> n >> m >> i >> j >> a >> b;
if ((i + a > n && i - a < 1) || (j + b > m && j - b < 1)) {
if ((i == 1 || i == n) && (j == 1 || j == m))
cout << 0 << endl;
else
cout << "Poor Inna and pony!" << endl;
return 0;
}
int ans = 1 << 29;
if ((i - 1) % a == 0) {
if ((j - 1) % b == 0 && (i - 1) / a % 2 == (j - 1) / b % 2)
ans = min(ans, max((i - 1) / a, (j - 1) / b));
if ((m - j) % b == 0 && (i - 1) / a % 2 == (m - j) / b % 2)
ans = min(ans, max((i - 1) / a, (m - j) / b));
}
if ((n - i) % a == 0) {
if ((j - 1) % b == 0 && (n - i) / a % 2 == (j - 1) / b % 2)
ans = min(ans, max((n - i) / a, (j - 1) / b));
if ((m - j) % b == 0 && (n - i) / a % 2 == (m - j) / b % 2)
ans = min(ans, max((n - i) / a, (m - j) / b));
}
if (ans < (1 << 29))
cout << ans << endl;
else
cout << "Poor Inna and pony!" << endl;
{
int _;
cin >> _;
return 0;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n,x,ans;
int a[110];
int main()
{
scanf("%d%d",&n,&x);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
for (int i=1;i<n;i++) if (x>=a[i]) {
ans++; x-=a[i];
} else break;
if (x==a[n]) ans++;
printf("%d\n",ans);
} | 0 |
#include <iostream>
#include <vector>
using namespace std;
int n;
vector<int> a;
int main(){
int bu, len = 1;
cin >> n;
cin >> bu;
for( int i = 1; i < n; i++ ){
int u;
cin >> u;
if( bu == u ){
a.push_back(len);
len = 1;
} else {
len++;
}
bu = u;
}
a.push_back(len);
int ans = 0;
for( int i = 0; i < a.size(); i++ ){
int k = 0;
for( int j = i; j < a.size() && j < i + 3; j++ ){
k += a[j];
}
ans = max(ans, k);
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
int x,y,z;
set<int> a,b,c;
cin>>n;
cin>>x;
for(int i=0;i<x;i++){
int g;cin>>g;
a.insert(g);
}
cin>>y;
for(int i=0;i<y;i++){
int g;cin>>g;
b.insert(g);
}
cin>>z;
for(int i=0;i<z;i++){
int g;cin>>g;
c.insert(g);
}
int ans=0;
for(set<int>::iterator i=c.begin();i!=c.end();i++){
int g=*i;
if(a.find(g)==a.end()||b.find(g)!=b.end()){
ans++;
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
const long long int INFLL = 10000000001;
const long double EPS = 1e-6;
const int MAXN = 100005;
const int FACTORS_THRESHOLD = 100005;
int A[MAXN];
int T[MAXN];
long long int Power(int x, int y) {
long long int r = 1;
for (int i = 0; i < y; i++) {
r = min(INFLL, r * x);
if (r >= INFLL) break;
}
return r;
}
void Sieve(int n, vector<int>* primes, vector<int>* factors) {
factors->resize(n + 1);
for (int i = 2; i * i <= n; i++) {
if ((*factors)[i] != 0) continue;
for (int j = 2 * i; j <= n; j += i)
if ((*factors)[j] == 0) (*factors)[j] = i;
}
(*factors)[1] = 1;
for (int i = 2; i <= n; i++)
if ((*factors)[i] == 0) {
(*factors)[i] = i;
primes->push_back(i);
}
}
int SmallestFactor(long long int n, const vector<int>& primes,
const vector<int>& factors) {
if (n <= FACTORS_THRESHOLD) {
if (factors[n] == n) return 1;
return factors[n];
} else {
for (int i = 0; i < primes.size(); i++)
if (n % primes[i] == 0) return primes[i];
return 1;
}
}
void Factorize(long long int n, const vector<int>& primes,
const vector<int>& factors,
unordered_map<long long int, int>* factorization) {
if (n == 1) return;
long long int d = SmallestFactor(n, primes, factors);
if (d == 1) {
(*factorization)[n]++;
return;
}
Factorize(d, primes, factors, factorization);
Factorize(n / d, primes, factors, factorization);
}
unordered_map<long long int, int> Factorize(long long int n,
const vector<int>& primes,
const vector<int>& factors) {
unordered_map<long long int, int> factorization;
Factorize(n, primes, factors, &factorization);
return factorization;
}
void Divisors(const unordered_map<long long int, int>& factorization,
unordered_map<long long int, int>::iterator it,
long long int divisor, vector<long long int>* result) {
if (it == factorization.end()) {
result->push_back(divisor);
return;
}
for (int i = 0; i <= it->second; i++) {
Divisors(factorization, next(it), divisor, result);
divisor *= it->first;
}
}
vector<long long int> Divisors(long long int n, const vector<int>& primes,
const vector<int>& factors) {
unordered_map<long long int, int> factorization =
Factorize(n, primes, factors);
vector<long long int> divisors;
Divisors(factorization, factorization.begin(), 1LL, &divisors);
return divisors;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<int> primes, factors;
Sieve(FACTORS_THRESHOLD, &primes, &factors);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> T[i];
A[T[i]]++;
}
long long int result = 0;
for (int i = 1;; i++) {
long long int pwr = Power(i, k);
if (pwr >= INFLL) break;
vector<long long int> dvs = Divisors(pwr, primes, factors);
for (long long int x : dvs) {
long long int y = pwr / x;
if (x >= MAXN) continue;
if (y >= MAXN) continue;
if (x > y) continue;
if (x != y) result += (long long int)A[x] * A[y];
if (x == y) result += ((long long int)A[x] * (A[x] - 1)) / 2;
}
}
cout << result << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y, z, l, r;
cin >> n;
x = 2 * n + 1;
y = n + 1;
l = y;
r = y;
z = 0;
for (int i = 1; i <= x; i++) {
z = 0;
for (int j = 1; j <= x; j++) {
if (j >= l && j <= r) {
cout << z;
if (j < y)
z = z + 1;
else
z = z - 1;
} else {
cout << " ";
}
if (j == r) break;
cout << " ";
}
cout << "\n";
if (i < y)
l = l - 1, r = r + 1;
else
l = l + 1, r = r - 1;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct __ {
int x, y, l;
bool operator<(const __ &ob) const {
if (x != ob.x) return x < ob.x;
if (y != ob.y) return y < ob.y;
return l < ob.l;
}
};
set<__> xxx;
struct _ {
int x, y;
};
vector<_> s[400010];
int top;
struct node {
int fa, w, dep;
} a[100010];
struct oper {
int over, pos;
node val;
} sta[400010];
int fail;
void update(int L, int R, _ c, int l, int r, int rt) {
if (L <= l && r <= R) {
s[rt].push_back(c);
return;
}
int m = l + r >> 1;
if (m >= L) update(L, R, c, l, m, rt << 1);
if (m < R) update(L, R, c, m + 1, r, rt << 1 | 1);
}
node find(int x) {
if (a[x].fa == x) return a[x];
node tmp = find(a[x].fa);
tmp.w ^= a[x].w;
return tmp;
}
int ans[100010];
void dfs(int l, int r, int rt) {
int i, optop = 0;
node tmpx, tmpy;
for (i = 0; i < s[rt].size(); i++) {
tmpx = find(s[rt][i].x);
tmpy = find(s[rt][i].y);
if (tmpx.fa == tmpy.fa) {
if (tmpx.w == tmpy.w) {
sta[top++].over = 1;
fail++;
optop++;
break;
}
} else {
int x = tmpx.fa, y = tmpy.fa;
sta[top++] = (oper){0, x, a[x]}, optop++;
sta[top++] = (oper){0, y, a[y]}, optop++;
if (tmpx.dep < tmpy.dep) {
tmpx.fa = y;
tmpx.w ^= 1 ^ tmpy.w;
tmpy.dep = max(tmpy.dep, tmpx.dep + 1);
tmpy.w = 0;
} else {
tmpy.fa = x;
tmpy.w ^= 1 ^ tmpx.w;
tmpx.dep = max(tmpx.dep, tmpy.dep + 1);
tmpx.w = 0;
}
a[x] = tmpx, a[y] = tmpy;
}
}
int m = l + r >> 1;
if (l == r) {
ans[l] = !fail;
} else {
if (!fail) {
dfs(l, m, rt << 1);
dfs(m + 1, r, rt << 1 | 1);
}
}
while (optop--) {
if (sta[--top].over) {
fail--;
} else {
a[sta[top].pos] = sta[top].val;
}
}
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
int i, x, y;
for (i = 1; i <= n; i++) {
a[i] = (node){i, 0, 1};
}
set<__>::iterator tmp;
for (i = 1; i <= q; i++) {
scanf("%d%d", &x, &y);
tmp = xxx.lower_bound((__){x, y, 0});
if (tmp == xxx.end() || (tmp->x != x || tmp->y != y)) {
xxx.insert((__){x, y, i});
} else {
update(tmp->l, i - 1, (_){x, y}, 1, q, 1);
xxx.erase(tmp);
}
}
while (!xxx.empty()) {
tmp = xxx.begin();
update(tmp->l, q, (_){tmp->x, tmp->y}, 1, q, 1);
xxx.erase(tmp);
}
dfs(1, q, 1);
for (i = 1; i <= q; i++) {
if (ans[i])
puts("YES");
else
puts("NO");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp = 0, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> temp;
sum += temp;
}
if (sum % n == 0)
cout << n << endl;
else
cout << n - 1 << endl;
}
| 2 |
#include<iostream>
using namespace std;
int main(void)
{
int i,n,x,y;
cin>>n;
for(i=1;i<=n;i++){
y=0;
if(i%3==0){
y=1;
}
x=i;
while(0<x){
if(x%10==3){
y=1;
}
x=x/10;
}
if(y==1){
cout<<" "<<i;
}
}
cout<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int cansel_sync = (ios::sync_with_stdio(0), cin.tie(0), 0);
const int N = 110;
int T, n, m;
string s[N];
string ans;
bool ok() {
for (int i = (0), _ = (n); i < _; i++) {
int cnt = 0;
for (int j = (0), _ = (m); j < _; j++) cnt += (ans[j] != s[i][j]);
if (cnt > 1) return 0;
}
return 1;
}
void solve() {
cin >> n >> m;
for (int i = (0), _ = (n); i < _; i++) cin >> s[i];
ans = s[0];
for (int i = (0), _ = (n); i < _; i++)
for (int j = (0), _ = (m); j < _; j++) {
int t = ans[j];
ans[j] = s[i][j];
if (ok()) {
cout << ans << endl;
return;
}
ans[j] = t;
}
cout << -1 << endl;
}
signed main() {
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int weight;
vector<int> edges;
};
struct edge {
int x;
int y;
int w;
int p;
};
istream& operator>>(istream& is, edge& e) {
is >> e.x >> e.y >> e.w >> e.p;
e.x--;
e.y--;
return is;
}
ostream& operator<<(ostream& os, const edge& e) {
return os << e.x + 1 << " " << e.y + 1 << " " << e.w << " " << e.p;
}
bool lower(vector<node>& nodes, vector<edge>& edges, int i) {
node& n = nodes[i];
n.weight = 0;
if (n.edges.empty()) {
return true;
}
for (int j : n.edges) {
edge& e = edges[j];
node& child = nodes[e.y];
if (!lower(nodes, edges, e.y) || e.p < child.weight) {
return false;
}
int cut = min(e.w - 1, e.p - child.weight);
e.w -= cut;
e.p -= cut;
n.weight += e.w + child.weight;
}
return true;
}
void raise(vector<node>& nodes, vector<edge>& edges,
const vector<edge>& original, int i, int w_upper) {
assert(w_upper >= 0);
node& n = nodes[i];
if (n.edges.empty() || n.weight == w_upper) {
return;
}
for (int j : n.edges) {
edge& e = edges[j];
node& child = nodes[e.y];
int e_w_increase = original[j].w - e.w;
if (i != 0) {
e_w_increase = min(e_w_increase, w_upper - n.weight);
}
e.w += e_w_increase;
e.p += e_w_increase;
n.weight += e_w_increase;
int c_w_upper = e.p;
if (i != 0) {
c_w_upper = min(c_w_upper, w_upper - n.weight + child.weight);
}
int c_w_original = child.weight;
raise(nodes, edges, original, e.y, c_w_upper);
n.weight += (child.weight - c_w_original);
}
}
int main() {
int n;
cin >> n;
vector<node> nodes(n);
vector<edge> edges(n - 1);
for (int i = 0; i < n - 1; i++) {
cin >> edges[i];
nodes[edges[i].x].edges.push_back(i);
}
vector<edge> original = edges;
if (!lower(nodes, edges, 0)) {
cout << -1 << endl;
return 0;
}
raise(nodes, edges, original, 0, numeric_limits<int>::max());
cout << n << endl;
for (const edge& e : edges) {
cout << e << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool d[10];
bool f(int n);
int main() {
int n, r = 0, i;
cin >> n;
int m = n;
while (m > 0) {
d[m % 10] = 1;
m /= 10;
}
for (i = 1; i * i < n; i++) {
if (n % i != 0) continue;
if (f(i)) r++;
if (f(n / i)) r++;
}
if (i * i == n && f(i)) r++;
cout << r;
}
bool f(int n) {
while (n > 0) {
if (d[n % 10]) return true;
n /= 10;
}
return false;
}
| 2 |
#include <iostream>
using namespace std;
int main()
{
int a,b,x;
cin>>a>>b>>x;
if(a+b>=x&&x>=a)cout<<"YES";
else cout<<"NO";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[2005];
int dp[2005];
bool chk(long long x) {
dp[1] = 0;
for (int i = 2; i <= n; i++) {
dp[i] = i - 1;
for (int j = 1; j < i; j++)
if (abs(a[i] - a[j]) <= (i - j) * x)
dp[i] = min(dp[i], dp[j] + (i - j - 1));
}
for (int i = n; i >= 1; i--)
if (dp[i] + (n - i) <= k) return 1;
return 0;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
long long l = 0, r = (1ll << 55);
long long mid;
while (r - l > 2) {
mid = (l + r) / 2;
if (chk(mid))
r = mid;
else
l = mid + 1;
}
for (;; l++)
if (chk(l)) {
cout << l;
return 0;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = int64_t;
using pii = pair<int, int>;
using ull = uint64_t;
ll a[100100];
ll X[100100], Y[100100];
ll gcd(ll a, ll b, ll& x, ll& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll d = gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
if (n == 1) {
cout << "1 1" << endl;
cout << -a[0] << endl;
cout << "1 1" << endl;
cout << 0 << endl;
cout << "1 1" << endl;
cout << 0 << endl;
return 0;
}
for (int i = 1; i < n; ++i) {
gcd(n, n - 1, X[i], Y[i]);
X[i] *= -a[i];
Y[i] *= -a[i];
}
cout << 1 << " " << n << endl;
cout << 0;
for (int i = 1; i < n; ++i) cout << ' ' << X[i] * n;
cout << endl;
cout << "1 1" << endl;
cout << -a[0] << endl;
cout << 2 << " " << n << endl;
for (int i = 1; i < n; ++i) {
if (i > 1) cout << ' ';
cout << Y[i] * (n - 1);
}
cout << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, numOn, res[200000];
set<int> f[200000];
bool on[200000];
vector<pair<int, int> > v;
queue<int> toRemove;
void update() {
while (!toRemove.empty()) {
int r = toRemove.front();
toRemove.pop();
numOn--;
for (auto next : f[r]) {
f[next].erase(r);
if (on[next] && ((int)(f[next]).size()) < k) {
on[next] = 0;
toRemove.push(next);
}
}
f[r].clear();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 0; i < (m); i++) {
int x, y;
cin >> x >> y;
x--, y--;
v.push_back(make_pair(x, y));
f[x].insert(y), f[y].insert(x);
}
for (int i = 0; i < (n); i++) on[i] = 1;
numOn = n;
for (int i = 0; i < (n); i++)
if (((int)(f[i]).size()) < k) {
on[i] = 0;
toRemove.push(i);
}
update();
res[m - 1] = numOn;
for (int i = (m)-1; i >= (1); i--) {
int x = v[i].first, y = v[i].second;
f[x].erase(y), f[y].erase(x);
if (on[x] && ((int)(f[x]).size()) < k) {
on[x] = 0;
toRemove.push(x);
}
if (on[y] && ((int)(f[y]).size()) < k) {
on[y] = 0;
toRemove.push(y);
}
update();
res[i - 1] = numOn;
}
for (int i = 0; i < (m); i++) cout << res[i] << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, a, b, ans;
cin >> t;
while (t--) {
cin >> a >> b;
if (a == b)
cout << 0 << endl;
else {
if (a > b) swap(a, b);
b = b - a;
ans = b / 5;
if (b % 5 == 0)
cout << ans << endl;
else if (b % 5 == 1 || b % 5 == 2)
cout << ans + 1 << endl;
else
cout << ans + 2 << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<char> hola;
int main() {
int tam, total = 0;
char a;
cin >> tam;
for (int i = 0; i < (tam); i++) {
cin >> a;
hola.push_back(a);
total++;
a -= '0';
if ((int)a == 0) {
cout << total << endl;
return 0;
}
}
cout << tam << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int val[1000], arr[100022], len[1000];
int main() {
int n, m, i, j, k;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
memset(val, -1, sizeof(val));
for (i = 0; i < n; i++) {
int ele = arr[i];
if (val[ele] != -1) continue;
for (j = ele; j >= (ele - m + 1) && (val[j] == -1) && j >= 0; j--)
;
if (j >= 0 && (val[j] != -1) && (len[j] + ele - j <= m)) {
for (k = j + 1; k <= ele; k++) {
val[k] = val[j];
len[k] = len[j] + k - j;
}
} else {
j++;
for (k = j; k <= ele; k++) {
val[k] = j;
len[k] = k - j + 1;
}
}
}
for (i = 0; i < n; i++) printf("%d ", val[arr[i]]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n % 2 == 0)
cout << n / 2 - 1 << endl;
else
cout << n / 2 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j;
cin >> n;
long long int dist[n + 1][n + 1], total = 0;
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++) {
cin >> dist[i][j];
total += dist[i][j];
}
total /= 2;
int q;
cin >> q;
while (q--) {
int a, b, c;
cin >> a >> b >> c;
if (dist[a][b] <= c) {
cout << total << " ";
continue;
} else {
dist[a][b] = c;
dist[b][a] = c;
total = 0;
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++) {
if (dist[i][j] > dist[i][a] + dist[a][j])
dist[i][j] = dist[i][a] + dist[a][j];
}
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < n + 1; j++) {
if (dist[i][j] > dist[i][b] + dist[b][j])
dist[i][j] = dist[i][b] + dist[b][j];
total += dist[i][j];
}
total /= 2;
cout << total << " ";
}
}
}
| 3 |
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
int a,b,c,d,e,f;
while(cin >> a >> b >> c >> d >> e >> f){
if((a|b|c|d|e|f) == 0) break;
int MD = a + d, GG = b + e, GO = c + f;
int ret = 0;
for(int i=0;i<=2;i++){
if(MD >= i && GG >= i && GO >= i)
ret = max(ret, (MD-i)/3 + (GG-i)/3 + (GO-i)/ 3 + i);
}
cout << ret << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<long long> a[n];
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int c;
cin >> c;
a[i].push_back(c - 1);
}
}
int fl[n];
vector<long long> x;
for (int i = 0; i < n; i++) fl[i] = 0;
for (int i = 0; i < n; i++) {
int gl = 0;
for (int j = 0; j < a[i].size(); j++) {
if (fl[a[i][j]] == 0) {
fl[a[i][j]] = gl = 1;
break;
}
}
if (gl == 0) x.push_back(i);
}
int gl = 0;
for (int i = n - 1; i >= 0; i--) {
if (fl[i] == 0) {
cout << "IMPROVE\n" << x[0] + 1 << " " << i + 1 << "\n";
gl = 2;
break;
}
}
if (gl != 2) cout << "OPTIMAL\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxK = 100 + 5;
const int maxN = 50 + 5;
int k, n, m, q;
int cnt[maxK][maxN];
string basic[maxN];
vector<string> res[maxK];
map<string, int> str_int;
struct node {
string name;
int cnt[maxN];
node() {
memset(cnt, 0, sizeof cnt);
name = "";
}
} a[maxN];
inline void print(int id, int poi, int dist) {
if (poi == -1) {
cout << dist << endl;
return;
}
int End = poi;
while (poi != -1 && res[id][poi] == res[id][End]) poi--;
print(id, poi, dist + 1);
cout << res[id][End] << ' ' << End - poi << endl;
}
inline bool cmp(int idC, int idA) {
for (int i = 1; i <= n; i++)
if (cnt[idA][i] < a[idC].cnt[i]) return false;
return true;
}
inline void mines(int idC, int idA) {
for (int i = 1; i <= n; i++) cnt[idA][i] -= a[idC].cnt[i];
}
int main() {
cin >> k >> n >> m >> q;
for (int i = 1; i <= n; i++) {
cin >> basic[i];
str_int[basic[i]] = i;
}
scanf("\n");
for (int i = 1; i <= m; i++) {
string tmp;
getline(cin, tmp);
int poi = 0;
while (tmp[poi] != ':') poi++;
a[i].name = tmp.substr(0, poi);
poi += 2;
while (poi < (int)tmp.size()) {
int start = poi;
while (tmp[poi] != ' ') poi++;
int id = str_int[tmp.substr(start, poi - start)];
start = (++poi);
while (poi < (int)tmp.size() && tmp[poi] != ',') poi++;
int rep = 0;
while (start < poi) rep = rep * 10 + tmp[start++] - '0';
a[i].cnt[id] += rep;
poi += 2;
}
}
for (int i = 1; i <= q; i++) {
int id;
string s;
cin >> id >> s;
cnt[id][str_int[s]]++;
for (int j = 1; j <= m; j++)
if (cmp(j, id)) {
res[id].push_back(a[j].name);
mines(j, id);
}
}
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++)
while (cnt[i][j]) {
res[i].push_back(basic[j]);
cnt[i][j]--;
}
sort(res[i].begin(), res[i].end());
}
for (int i = 1; i <= k; i++) print(i, res[i].size() - 1, 0);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long v[110000];
int n;
int calc(int y) {
int ret = 1;
for (int i = 1; i < 1000; i++) {
if (ret * 2 > y) return ret;
ret *= 2;
}
return -1;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
long long acc = 0;
for (int i = 0; i < n - 1; i++) {
acc += v[i];
cout << acc << endl;
int x = i + calc(n - i - 1);
v[x] += v[i];
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, m;
long long check(long long sum, long long val, long long mov) {
long long lo = val, hi = val, los = sum + val, his = sum + val;
mov--;
while (mov--) {
lo = los + 1;
los += lo;
hi = his + m;
his += hi;
}
if (hi < b) {
return 1;
} else if (lo > b) {
return -1;
} else {
return 0;
}
}
long long Bsrch(long long sum, long long pval, long long mov) {
long long lo = sum + 1, hi = sum + m;
while (hi >= lo) {
long long mid = (hi + lo) / 2;
long long va = check(sum, mid, mov);
if (va == 1) {
lo = mid + 1;
} else if (va == -1) {
hi = mid - 1;
} else {
return mid;
}
}
return -1;
}
long long checker() {
long long mo = 1;
long long lo = a, hi = a, los = a, his = a;
while (hi < b) {
lo = los + 1;
los += lo;
hi = his + m;
his += hi;
mo++;
}
if (lo > b)
return -1;
else
return mo;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q;
cin >> q;
while (q--) {
cin >> a >> b >> m;
if (a == b) {
cout << "1 " << a << endl;
continue;
}
if (a + m >= b) {
cout << "2 " << a << " " << b << endl;
continue;
}
long long mov = checker();
if (mov == -1) {
cout << "-1" << endl;
continue;
}
vector<long long> ans;
mov--;
long long sum = a;
ans.push_back(a);
while (1) {
long long val = Bsrch(sum, ans[ans.size() - 1], mov);
mov--;
sum += val;
ans.push_back(val);
if (mov == 0) break;
}
cout << ans.size() << " ";
for (long long i : ans) cout << i << " ";
cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, rez = 0, c;
string a, b;
cin >> n >> a >> b;
while (n--) {
c = abs(a[n] - b[n]);
rez += min(c, 10 - c);
}
cout << rez;
return 0;
}
| 1 |
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
typedef long long LL;
static const double EPS = 1e-9;
#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define P(x) cout << (x) << endl
int main(void){
int n,m;
while(cin>>n>>m){
if(!n) break;
int ch[102] = {0};
char a;
REP(h,m){
cin >> a;
if(a == 'A'){
FOR(i,1,n+1){
if(ch[i] == 0){
ch[i] = 'A';
break;
}
}
}else if(a == 'B'){
bool f=false;
for(int i=n; i>0; i--){
if(ch[i]==0 && ch[i+1] != 'A' && ch[i-1] != 'A'){
ch[i] = 'B';
f = true;
break;
}
}
if(!f){
FOR(i,1,n+1){
if(ch[i] == 0){
ch[i] == 'B';
break;
}
}
}
}else if(a == 'C'){
int c=-1;
FOR(i,1,n+1){
if(ch[i] != 0){
c = i;
break;
}
}
if(c==-1){
ch[(n%2)?(n+1)/2:n/2+1] = 'C';
}else{
while(c<n+1){
if(ch[c+1]==0&&c<n){
ch[c+1] = 'C';
break;
}else if(ch[c-1]==0&&c>1){
ch[c-1] = 'C';
break;
}
c++;
}
}
}else if(a == 'D'){
int maxi,max=0;
FOR(i,1,n+1){
if(ch[i] == 0){
int l=100,r=100,mi;
for(int j=i-1; j>0; j--)
if(ch[j] != 0){
l = i-j;
break;
}
for(int j=i+1; j<n+1; j++)
if(ch[j] != 0){
r = j-i;
break;
}
mi = min(l,r);
if(mi>max){
maxi = i;
max = mi;
}
}
}
ch[maxi] = 'D';
}
}
for(int i=1; i<n+1; i++){
if(ch[i] == 0) cout <<"#";
else printf("%c",ch[i]);
}
cout << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m;
cin >> n >> m;
char a[n][m];
for (long long int i = 0; i < n; i++) {
for (long long int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
long long int poss = true;
for (long long int i = 0; i < n; i++) {
vector<long long int> vec;
for (long long int j = 0; j < m; j++) {
if (a[i][j] == '#') {
vec.push_back(j);
}
}
if (!vec.empty()) {
char temp[n];
for (long long int k = 0; k < n; k++) {
temp[k] = a[k][vec[0]];
}
for (long long int j : vec) {
for (long long int k = 0; k < n; k++) {
if (temp[k] != a[k][j]) {
poss = false;
cout << "No" << endl;
return 0;
}
}
}
}
}
cout << "Yes" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
if (0) cout << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',')
if (0) cout << *sdbg++;
if (0) cout << "=" << h << ",";
_dbg(sdbg + 1, a...);
}
const int MX = 1e5 + 7;
int a[MX];
vector<pair<int, int> > wek;
set<pair<int, int> > seg;
multiset<int> roz;
void usun(int x) {
auto it = prev(seg.lower_bound({x, MX}));
pair<int, int> p = *it;
seg.erase(it);
int l = p.first, r = p.second;
roz.erase(roz.find(r - l + 1));
if (x > l && x < r) {
seg.insert({l, x - 1});
seg.insert({x + 1, r});
roz.insert(x - l);
roz.insert(r - x);
} else {
if (l == r) return;
if (x == l)
seg.insert({l + 1, r});
else
seg.insert({l, r - 1});
roz.insert(r - l);
}
}
int32_t main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), wek.push_back({a[i], i});
sort(wek.begin(), wek.end(), greater<pair<int, int> >());
seg.insert({0, 0});
seg.insert({1, n});
seg.insert({n + 1, n + 1});
roz.insert(n);
int maxG = 1, minAns = wek[0].first + 1;
for (int i = 0; i < ((int)(wek).size()); ++i) {
usun(wek[i].second);
if (!roz.empty() && (*roz.begin()) == (*roz.rbegin()) &&
((int)(roz).size()) >= maxG) {
maxG = ((int)(roz).size());
minAns = (i == n - 1 ? wek[i].first : wek[i + 1].first + 1);
}
}
printf("%d", minAns);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
string s;
long long int i, m, p, j;
char c;
map<char, long long int> mp;
cin >> s;
long long int n = s.length();
for (i = 0; i < n; i++) {
mp[s[i]]++;
}
p = 0;
cin >> m;
char as[m + 5];
long long int b[m + 5];
for (i = 0; i < m; i++) {
cin >> b[i];
}
c = 'z';
map<long long int, long long int> first;
while (1) {
p = 0;
vector<long long int> v;
long long int cc[m + 5];
for (i = 0; i < m; i++) {
cc[i] = 0;
}
for (i = 0; i < m; i++) {
if (b[i] == 0 && first[i] == 0) {
p++;
first[i] = 1;
v.push_back(i);
for (j = 0; j < m; j++) {
if (b[j] != 0) {
cc[j] += (abs(i - j));
}
}
}
}
for (i = 0; i < m; i++) {
b[i] -= cc[i];
}
if ((p) == 0) {
break;
}
for (; c >= 'a'; c--) {
if (mp[c] >= (p)) {
for (j = 0; j < v.size(); j++) {
as[v[j]] = c;
}
break;
}
}
c--;
}
for (j = 0; j < m; j++) {
cout << as[j];
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, c = 0, z, k;
cin >> n;
if (n % 2 != 0) {
cout << "0\n";
return;
}
for (int i = 1;; ++i) {
z = i * 2;
k = (n - z) / 2;
if (i < k)
c++;
else
break;
}
cout << c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T &a, T &b) {
return a < b ? a : b;
}
template <class T>
inline T max(T &a, T &b) {
return a > b ? a : b;
}
template <class T>
void read(T &x) {
char ch;
while ((ch = getchar()) && !isdigit(ch))
;
x = ch - '0';
while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0';
}
struct point {
int x, y;
point() {}
point(int _x, int _y) : x(_x), y(_y) {}
};
const int N = 400000;
set<pair<int, int> > F_s[N], S_s[N];
set<pair<int, int> >::iterator It1;
set<pair<int, int>, greater<pair<int, int> > > F_b[N], S_b[N];
set<pair<int, int>, greater<pair<int, int> > >::iterator It2;
map<pair<int, int>, bool> Hash;
long long ans = 1;
int n, m, Sx, Sy;
int Get_dir(char s[]) {
if (s[0] == 'N' && s[1] == 'E')
return 0;
else if (s[0] == 'N' && s[1] == 'W')
return 1;
else if (s[0] == 'S' && s[1] == 'W')
return 2;
else
return 3;
}
int Flag;
int vis(int x, int y) { return Hash.count(make_pair(x, y)); }
void add(int x, int y) {
Hash[make_pair(x, y)] = 1;
F_s[x + y].insert(make_pair(x, y)), F_b[x + y].insert(make_pair(x, y));
S_s[x - y + m + 1].insert(make_pair(x, y)),
S_b[x - y + m + 1].insert(make_pair(x, y));
}
int dis(pair<int, int> a, pair<int, int> b) {
assert(abs(a.first - b.first) == abs(a.second - b.second));
return abs(a.first - b.first);
}
int In_line(pair<int, int> x, pair<int, int> a, pair<int, int> b) {
int X = x.first, x1 = a.first, x2 = b.first, y1 = a.second, y2 = b.second;
if (!(x.first >= min(a.first, b.first) && x.first <= max(a.first, b.first)))
return 0;
return (x.second - y1) * (x2 - x1) == (X - x1) * (y2 - y1);
}
pair<pair<int, int>, int> Get_nxt(pair<int, int> Now, int dir) {
int ax, ay, V1, V2;
if (!dir) {
It2 = F_b[Now.first + Now.second].lower_bound(
make_pair(Now.first, Now.second));
ax = It2->first, ay = It2->second;
V1 = vis(ax, ay - 1), V2 = vis(ax + 1, ay);
if (Now != make_pair(Sx, Sy) &&
In_line(make_pair(Sx, Sy), Now, make_pair(ax, ay))) {
Flag = 2;
ans += (long long)dis(Now, make_pair(Sx, Sy)) - 1;
return make_pair(make_pair(0, 0), dir);
} else
ans += dis(Now, make_pair(ax + 1, ay - 1));
if (!V1 && !V2) return Flag = 1, make_pair(make_pair(ax + 1, ay - 1), 2);
if (V1 && !V2) return make_pair(make_pair(ax + 1, ay), 3);
if (!V1 && V2) return make_pair(make_pair(ax, ay - 1), 1);
return Flag = 1, make_pair(make_pair(ax + 1, ay - 1), 2);
} else if (dir == 1) {
It2 = S_b[Now.first - Now.second + m + 1].lower_bound(
make_pair(Now.first, Now.second));
ax = It2->first, ay = It2->second;
V1 = vis(ax + 1, ay), V2 = vis(ax, ay + 1);
if (Now != make_pair(Sx, Sy) &&
In_line(make_pair(Sx, Sy), Now, make_pair(ax, ay))) {
Flag = 2;
ans += (long long)dis(Now, make_pair(Sx, Sy)) - 1;
return make_pair(make_pair(0, 0), dir);
} else
ans += dis(Now, make_pair(ax + 1, ay + 1));
if (!V1 && !V2) return Flag = 1, make_pair(make_pair(ax + 1, ay + 1), 3);
if (V1 && !V2) return make_pair(make_pair(ax, ay + 1), 0);
if (!V1 && V2) return make_pair(make_pair(ax + 1, ay), 2);
return Flag = 1, make_pair(make_pair(ax + 1, ay + 1), 3);
} else if (dir == 2) {
It1 = F_s[Now.first + Now.second].lower_bound(
make_pair(Now.first, Now.second));
ax = It1->first, ay = It1->second;
V1 = vis(ax - 1, ay), V2 = vis(ax, ay + 1);
if (Now != make_pair(Sx, Sy) &&
In_line(make_pair(Sx, Sy), Now, make_pair(ax, ay))) {
Flag = 2;
ans += (long long)dis(Now, make_pair(Sx, Sy)) - 1;
return make_pair(make_pair(0, 0), dir);
} else
ans += dis(Now, make_pair(ax - 1, ay + 1));
if (!V1 && !V2) return Flag = 1, make_pair(make_pair(ax - 1, ay + 1), 0);
if (V1 && !V2) return make_pair(make_pair(ax, ay + 1), 3);
if (!V1 && V2) return make_pair(make_pair(ax - 1, ay), 1);
return Flag = 1, make_pair(make_pair(ax - 1, ay + 1), 0);
} else {
It1 = S_s[Now.first - Now.second + m + 1].lower_bound(
make_pair(Now.first, Now.second));
ax = It1->first, ay = It1->second;
V1 = vis(ax - 1, ay), V2 = vis(ax, ay - 1);
if (Now != make_pair(Sx, Sy) &&
In_line(make_pair(Sx, Sy), Now, make_pair(ax, ay))) {
Flag = 2;
ans += (long long)dis(Now, make_pair(Sx, Sy)) - 1;
return make_pair(make_pair(0, 0), dir);
} else
ans += dis(Now, make_pair(ax - 1, ay - 1));
if (!V1 && !V2) return Flag = 1, make_pair(make_pair(ax - 1, ay - 1), 1);
if (V1 && !V2) return make_pair(make_pair(ax, ay - 1), 2);
if (!V1 && V2) return make_pair(make_pair(ax - 1, ay), 0);
return Flag = 1, make_pair(make_pair(ax - 1, ay - 1), 1);
}
}
int k, x, y, VIS[10];
char s[6];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++) scanf("%d%d", &x, &y), add(x, y);
for (int i = 1; i <= m; i++) add(n + 1, i), add(0, i);
for (int i = 1; i <= n; i++) add(i, m + 1), add(i, 0);
add(n + 1, m + 1);
add(0, 0);
add(0, m + 1);
add(n + 1, 0);
scanf("%d%d%s", &Sx, &Sy, s);
pair<int, int> Now = make_pair(Sx, Sy);
int dir = Get_dir(s), Fdir;
while (!VIS[dir]) {
VIS[dir] = 1;
Flag = 0;
Fdir = dir;
ans--;
while (1) {
pair<pair<int, int>, int> T = Get_nxt(Now, dir);
pair<int, int> Nxt = T.first;
Now = Nxt;
dir = T.second;
ans++;
if (Nxt == make_pair(Sx, Sy)) break;
if (Flag == 1) {
Now = make_pair(Sx, Sy);
dir = (Fdir + 2) % 4;
break;
}
if (Flag == 2) break;
}
}
printf("%lld\n", ans);
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 1e6 + 36;
const long long INF = 2e18 + 7;
const int MOD = 1e9 + 7;
mt19937 gen(19937);
vector<int> a, b;
vector<pair<long long, int> > A, B, LL, LR, RR, RL;
void die() {
cout << "0 1 ";
for (auto to : RL) {
cout << to.second << ' ';
}
for (auto to : LR) {
cout << to.second << ' ';
}
cout << "2 ";
for (auto to : RR) {
cout << to.second << ' ';
}
for (auto to : LL) {
cout << to.second << ' ';
}
cout << endl;
exit(0);
}
signed main() {
ios_base::sync_with_stdio(0);
int n;
long long res;
{
cin >> n;
for (int i(3); i <= n; ++i) {
cout << "2 1 2 " << i << endl;
cin >> res;
if (res > 0) {
a.push_back(i);
}
if (res < 0) {
b.push_back(i);
}
}
for (int i(0); i < int(a.size()); ++i) {
cout << "1 1 2 " << a[i] << endl;
cin >> res;
A.push_back({res, a[i]});
}
for (int i(0); i < int(b.size()); ++i) {
cout << "1 1 2 " << b[i] << endl;
cin >> res;
B.push_back({res, b[i]});
}
if (int(A.size())) {
sort(A.rbegin(), A.rend());
}
if (int(B.size())) {
sort(B.rbegin(), B.rend());
}
}
{
if (int(A.size()) >= 2 && A[0].first == A[1].first) {
cout << "2 " << A[0].second << " 1 " << A[1].second << endl;
cin >> res;
if (res != 1) {
swap(A[0], A[1]);
}
}
if (int(A.size())) {
LL.push_back(A[0]);
}
for (int i(1); i < int(A.size()); ++i) {
cout << "2 " << A[0].second << " 1 " << A[i].second << endl;
cin >> res;
if (res != 1) {
LL.push_back(A[i]);
} else {
RR.push_back(A[i]);
}
}
if (int(LL.size())) {
sort(LL.rbegin(), LL.rend());
}
if (int(RR.size())) {
sort(RR.begin(), RR.end());
}
if (int(B.size()) >= 2 && B[0].first == B[1].first) {
cout << "2 " << B[0].second << " 1 " << B[1].second << endl;
cin >> res;
if (res != 1) {
swap(B[0], B[1]);
}
}
if (int(B.size())) {
LR.push_back(B[0]);
}
for (int i(1); i < int(B.size()); ++i) {
cout << "2 " << B[0].second << " 1 " << B[i].second << endl;
cin >> res;
if (res != 1) {
LR.push_back(B[i]);
} else {
RL.push_back(B[i]);
}
}
if (int(RL.size())) {
sort(RL.begin(), RL.end());
}
if (int(LR.size())) {
sort(LR.rbegin(), LR.rend());
}
die();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int n, m, k, x, y, w, res = 0;
vector<pair<int, int> > v[2 * maxn];
vector<int> aux[10];
bitset<2 * maxn> bset[10][10];
bitset<2 * maxn> nset;
int vis[2 * maxn];
int pick[10];
bool poss[10][10];
bool good[10][10][10][10];
void dfs(int node) {
if (node == k + 1) {
++res;
return;
}
if ((int)aux[node].size() == 0) {
dfs(node + 1);
return;
}
auto check = [&](int x, int y) {
pick[x] = y;
for (int i = 1; i < x; ++i)
if (!aux[i].empty()) {
if (!good[i][pick[i]][x][y]) return false;
}
return true;
};
for (int i = 1; i <= node; ++i) {
if (poss[node][i]) {
if (check(node, i)) dfs(node + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
for (int i = 0; i < m; ++i) {
cin >> x >> y >> w;
v[x].emplace_back(w, y);
}
for (int i = 1; i <= n; ++i) {
sort(v[i].begin(), v[i].end());
aux[(int)v[i].size()].push_back(i);
}
for (int i = 1; i <= k; ++i)
for (int j = 1; j <= i; ++j) {
bool flag = true;
for (auto &l : aux[i]) {
int t = v[l][j - 1].second;
if (vis[t]) {
flag = false;
break;
} else {
vis[t] = l;
bset[i][j][t] = 1;
}
}
if (flag) poss[i][j] = true;
for (auto &l : aux[i]) {
int t = v[l][j - 1].second;
if (vis[t] == l)
vis[t] = 0;
else
break;
}
}
for (int i = 1; i <= k; ++i)
for (int j = i + 1; j <= k; ++j)
for (int g = 1; g <= i; ++g) {
if (poss[i][g]) {
for (int h = 1; h <= j; ++h) {
if (poss[j][h]) {
if ((bset[i][g] & bset[j][h]) == nset) {
good[i][g][j][h] = true;
good[j][h][i][g] = true;
}
}
}
}
}
int p = 1;
for (int i = 1; i <= k; ++i)
if ((int)aux[i].size() == 0) p *= i;
dfs(1);
cout << (p * res);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << (a) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int N = 5050;
int n, odd, da[N], db[N], za, zb;
string a,b;
int get(const vi &p, int i) { return i < 0 ? -1 : i < si(p) ? p[i] : n-1; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> a >> b;
odd = a[0] != b[0];
vi pa,pb;
forn(i,n-1) if (a[i] == a[i+1]) pa.pb(i);
forn(i,n-1) if (b[i] == b[i+1]) pb.pb(i);
if (si(pa) < si(pb)) swap(pa,pb);
int ans = 1e9;
for (int left = -n; left <= n; left++) if ((odd + left)%2 == 0) {
int cand = 0;
for (int i = -n; i < 2*n; i++) {
int pi = get(pa,i), pj = get(pb,i+left);
cand += abs(pi-pj);
}
ans = min(ans, cand);
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool visited[2001];
long long maxcnt;
std::vector<long long> adjlis[2001];
stack<long long> stac;
void dfs(long long u) {
visited[u] = true;
if (!adjlis[u].size()) {
if (maxcnt < stac.size()) maxcnt = stac.size();
}
for (auto i : adjlis[u]) {
if (!visited[i]) {
stac.push(i);
dfs(i);
stac.pop();
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
std::vector<long long> graph(n + 1, 0);
for (long long i = 1; i < n + 1; ++i) {
cin >> graph[i];
}
std::vector<long long> managers;
for (long long i = 1; i <= n; ++i) {
if (graph[i] != -1)
adjlis[graph[i]].push_back(i);
else
managers.push_back(i);
}
maxcnt = 1;
for (auto i : managers) {
stac.push(i);
if (!adjlis[i].size()) {
} else {
if (!visited[i]) {
dfs(i);
}
}
stac.pop();
}
cout << maxcnt << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1010, inf = 0x3f3f3f3f;
const int dl[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};
int N, M;
int d[3][NMAX][NMAX];
char S[NMAX][NMAX];
queue<pair<int, int>> Q;
bool inq[NMAX][NMAX];
void BFS(int d[NMAX][NMAX]) {
while (!Q.empty()) {
auto now = Q.front();
Q.pop();
int x = now.first, y = now.second;
inq[x][y] = 0;
for (int i = 0; i < 4; ++i) {
int nx = x + dl[i];
int ny = y + dc[i];
if (S[nx][ny] == 0 || S[nx][ny] == '#') continue;
if (d[nx][ny] == 0 || d[nx][ny] > d[x][y] + (S[nx][ny] == '.')) {
d[nx][ny] = d[x][y] + (S[nx][ny] == '.');
if (inq[nx][ny] == 0) Q.push(make_pair(nx, ny));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
int i, j;
cin >> N >> M;
cin.get();
for (i = 1; i <= N; ++i) cin.getline(S[i] + 1, NMAX);
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j) {
if (S[i][j] == '1') {
d[0][i][j] = 1;
inq[i][j] = 1;
Q.push(make_pair(i, j));
}
}
BFS(d[0]);
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j) {
if (S[i][j] == '2') {
d[1][i][j] = 1;
inq[i][j] = 1;
Q.push(make_pair(i, j));
}
}
BFS(d[1]);
for (i = 1; i <= N; ++i)
for (j = 1; j <= M; ++j) {
if (S[i][j] == '3') {
d[2][i][j] = 1;
inq[i][j] = 1;
Q.push(make_pair(i, j));
}
}
BFS(d[2]);
int answer = inf;
for (i = 1; i <= N; ++i) {
for (j = 1; j <= M; ++j) {
if (d[0][i][j] && d[1][i][j] && d[2][i][j] &&
d[0][i][j] + d[1][i][j] + d[2][i][j] - 2 * (S[i][j] == '.') < answer)
answer = d[0][i][j] + d[1][i][j] + d[2][i][j] - 2 * (S[i][j] == '.');
}
}
if (answer == inf)
cout << "-1\n";
else
cout << answer - 3 << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int A[31][31];
int n, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) scanf("%d", &A[i][j]);
int result = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
int column = 0;
int c = 0;
for (; c < n; c++) column += A[c][j];
int row = 0;
int r = 0;
for (; r < n; r++) row += A[i][r];
if (column > row) result++;
}
printf("%d", result);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
string a = "BWBWBWBW", b = "WBWBWBWB", c;
int main() {
init_ios();
bool res = true;
for (int i = 1; i <= 8; ++i) {
cin >> c;
if (c != a && c != b) res = false;
}
if (res)
cout << "YES\n";
else
cout << "NO\n";
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64777216")
using namespace std;
const long long maxn = 501;
const long long maxk = 200001;
const long long maxx = 10001;
const long long secret_number = 87468267;
const long long linf = 99999999999999999;
const long long inf = 100000;
const double pi = 3.141592653589793238462643383279502884;
const double eps = 1e-7;
const string something_very_long =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaa";
const string govno = "g";
int main() {
cin.sync_with_stdio(0);
int n;
int c[maxn];
cin >> n;
for (int i = 0; i < n; ++i) cin >> c[i];
int dp[maxn][maxn];
for (int i = 0; i < maxn; ++i)
for (int j = 0; j < maxn; ++j) dp[i][j] = inf;
for (int i = 0; i < n; ++i) dp[i][i] = 1;
for (int i = 0; i < n - 1; ++i)
if (c[i] == c[i + 1])
dp[i][i + 1] = 1;
else
dp[i][i + 1] = 2;
for (int len = 3; len <= n; ++len) {
for (int i = 0; i <= n - len; ++i) {
int best = inf;
if (c[i] == c[i + len - 1]) best = min(best, dp[i + 1][i + len - 2]);
for (int j = 1; j < len; ++j)
best = min(best, dp[i][i + j - 1] + dp[i + j][i + len - 1]);
dp[i][i + len - 1] = min(dp[i][i + len - 1], best);
}
}
cout << dp[0][n - 1];
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize(3)
struct {
inline operator int() {
int x;
return scanf("%d", &x), x;
}
} read;
const int maxn = 50005;
long double lk[maxn], lb[maxn];
struct Vector {
long double x, y, at;
int id;
} vec[maxn << 1];
struct Range {
int l, r;
int id;
} ran[maxn];
int getCross(long double p, long double q, long double r, int n) {
int vp = 0;
for (int i = 1; i <= n; i++) {
long double a = lk[i] * lk[i] + 1;
long double b = 2 * (lk[i] * (lb[i] - q) - p);
long double c = p * p + (lb[i] - q) * (lb[i] - q) - r * r;
long double dt = b * b - 4 * a * c;
if (dt < 0) continue;
long double x1 = (-b - sqrt(dt)) / a / 2;
long double x2 = (-b + sqrt(dt)) / a / 2;
long double y1 = lk[i] * x1 + lb[i];
long double y2 = lk[i] * x2 + lb[i];
vec[++vp] = {x1 - p, y1 - q, atan2(y1 - q, x1 - p), i};
vec[++vp] = {x2 - p, y2 - q, atan2(y2 - q, x2 - p), i};
}
std::sort(vec + 1, vec + vp + 1,
[](Vector a, Vector b) { return a.at < b.at; });
return vp;
}
int bit[maxn << 1];
int query(int p) {
int res = 0;
for (int k = p; k; k -= k & -k) res += bit[k];
return res;
}
void modify(int p, int lim) {
for (int k = p; k <= lim; k += k & -k) ++bit[k];
}
int vis[maxn];
long long check(long double p, long double q, long double r, int n) {
int vp = getCross(p, q, r, n);
long long res = 0;
std::fill(vis + 1, vis + n + 1, 0);
std::fill(bit + 1, bit + vp + 1, 0);
for (int i = 1; i <= vp; i++)
if (!vis[vec[i].id]) {
vis[vec[i].id] = i;
res -= query(i);
} else {
res += query(vis[vec[i].id]);
modify(vis[vec[i].id], vp);
}
return res;
}
long double crossdis(long double p, long double q, int i, int j) {
long double x = (lb[i] - lb[j]) / (lk[j] - lk[i]);
long double y = lk[i] * x + lb[i];
return sqrt((p - x) * (p - x) + (q - y) * (q - y));
}
Range tmp[maxn];
long double ans = 0;
int tot = 0;
void cdq(long double p, long double q, int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
cdq(p, q, l, mid);
cdq(p, q, mid + 1, r);
int p1 = l, tp = l;
for (int p2 = mid + 1; p2 <= r; p2++) {
while (p1 <= mid and ran[p1].r < ran[p2].r) tmp[tp++] = ran[p1++];
tmp[tp++] = ran[p2];
for (int i = p1 - 1; i >= l and ran[i].r > ran[p2].l; i--) {
++tot;
ans += crossdis(p, q, ran[i].id, ran[p2].id);
}
}
while (p1 <= mid) tmp[tp++] = ran[p1++];
for (int i = l; i <= r; i++) ran[i] = tmp[i];
}
int main() {
int n = read;
long double p = read * 1e-3, q = read * 1e-3;
int m = read;
for (int i = 1; i <= n; i++) {
lk[i] = read * 1e-3;
lb[i] = read * 1e-3;
}
long double l = 0, r = 1e10;
for (int i = 0; i < 120; i++) {
long double mid = (l + r) / 2;
if (check(p, q, mid, n) >= m)
r = mid;
else
l = mid;
}
int vp = getCross(p, q, l, n);
std::fill(vis + 1, vis + n + 1, 0);
int rp = 0;
for (int i = 1; i <= vp; i++) {
int id = vec[i].id;
if (vis[id])
ran[vis[id]].r = i;
else
ran[vis[id] = ++rp] = {i, 0, id};
}
cdq(p, q, 1, rp);
ans += (m - tot) * r;
std::cout << std::fixed << std::setprecision(9) << ans << std::endl;
}
| 5 |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,a[20];
ll dp(int l,int r,ll xl,ll xr)
{
if(l+1==r) return 0;
ll ans=1e18;
for(int i=l+1;i<r;i++)
ans=min(ans,dp(l,i,xl,xl+xr)+dp(i,r,xl+xr,xr)+(xl+xr)*a[i]);
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
printf("%lld",dp(1,n,1,1)+a[1]+a[n]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
out << "(" << obj.first << ", " << obj.second << ")";
return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
typename map<T, T1>::const_iterator itr = cont.begin();
typename map<T, T1>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> cont) {
typename set<T>::const_iterator itr = cont.begin();
typename set<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> cont) {
typename multiset<T>::const_iterator itr = cont.begin();
typename multiset<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T,
template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
typename CONT<T>::const_iterator itr = cont.begin();
typename CONT<T>::const_iterator ends = cont.end();
for (; itr != ends; ++itr) out << *itr << " ";
out << endl;
return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
for (auto i = 0; i < N; ++i) out << arr[i] << " ";
out << endl;
return out;
}
template <typename T>
T gcd(T a, T b) {
T min_v = min(a, b);
T max_v = max(a, b);
while (min_v) {
T temp = max_v % min_v;
max_v = min_v;
min_v = temp;
}
return max_v;
}
template <typename T>
T lcm(T a, T b) {
return (a * b) / gcd(a, b);
}
template <typename T>
T fast_exp_pow(T base, T exp, T mod) {
T res = 1;
while (exp) {
if (exp & 1) {
res *= base;
res %= mod;
}
exp >>= 1;
base *= base;
base %= mod;
}
return res % mod;
}
string strS, strT;
long long dpSolve[100010], jump[100010], prefSum[100010];
vector<int> positions;
int lps[100010];
void computeLPS() {
int len = 0, i = 1;
lps[0] = 0;
while (i < strT.size()) {
if (strT[i] == strT[len]) {
lps[i++] = ++len;
} else if (len) {
len = lps[len - 1];
} else {
lps[i++] = 0;
}
}
}
void KMP() {
computeLPS();
int i = 0, j = 0;
while (i < strS.size()) {
if (strS[i] == strT[j]) {
++i, ++j;
} else if (j) {
j = lps[j - 1];
} else {
++i;
}
if (j == strT.size()) {
positions.push_back(i - strT.size());
j = lps[j - 1];
}
}
}
int main() {
cin >> strS >> strT;
KMP();
reverse(positions.begin(), positions.end());
dpSolve[strS.size()] = prefSum[strS.size()] = 1;
int pos = 0;
for (int i = strS.size() - 1; i >= 0; --i) {
if (pos + 1 < positions.size() && i == positions[pos + 1]) {
++pos;
}
dpSolve[i] = dpSolve[i + 1];
if (!positions.empty() && i <= positions[0]) {
dpSolve[i] =
(dpSolve[i] + prefSum[positions[pos] + strT.size()]) % 1000000007LL;
}
prefSum[i] = (prefSum[i + 1] + dpSolve[i]) % 1000000007LL;
}
printf("%lld\n", (dpSolve[0] - 1 + 1000000007LL) % 1000000007LL);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main() {
int N;
cin >> N;
while (N--) {
int a[200005];
int n;
cin >> n;
int ans = inf, l = 0;
for (int i = 0; i < 200005; i++) a[i] = 0;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (a[x]) {
ans = min(ans, i - a[x] + 1);
}
a[x] = i;
}
cout << (ans == inf ? -1 : ans) << endl;
}
return 0;
}
| 3 |
#include <iostream>
#include <vector>
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int main() {
vector<int> prime;
vector<bool> is_prime(1e5, true);
is_prime[0] = is_prime[1] = false;
for(int i=2; i<1e5; ++i) {
if(is_prime[i]) {
prime.push_back(i);
for(int j=i+i; j<100000; j+=i) {
is_prime[j] = false;
}
}
}
int p, q;
cin >> p >> q;
int g = gcd(p, q);
p /= g, q /= g;
int ans = 1;
for(int i=0; i<prime.size(); ++i) {
if(q % prime[i] == 0) {
ans *= prime[i];
while(q % prime[i] == 0) {
q /= prime[i];
}
}
}
if(q / ans > 1) {
cout << q * ans << endl;
} else {
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct RTC {};
long long int A, B;
map<long long int, long long int> sol;
bool gao(long long int cur) {
if (cur > B) return false;
if (cur == B) return true;
if (gao(cur << 1LL)) {
sol[cur] = cur << 1LL;
return true;
}
if (gao(cur * 10LL + 1LL)) {
sol[cur] = cur * 10LL + 1LL;
return true;
}
return false;
}
int main() {
scanf("%lld %lld", &A, &B);
if (gao(A)) {
vector<int> ans;
int cur = A;
while (cur != B) {
ans.push_back(cur);
cur = sol[cur];
}
ans.push_back(B);
printf("YES\n");
printf("%d\n", ((int)(ans).size()));
for (int i = 0; i < (((int)(ans).size())); i++) {
if (i) putchar(' ');
printf("%d", ans[i]);
}
} else {
printf("NO");
}
putchar('\n');
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
long x,p;cin>>x>>p;
cout<<fixed<<setprecision(10)<<50.0*(x+x%2)/p;
} | 0 |
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &sz) {
sz[v] = 1;
for (auto u : graph[v])
if (u != p) {
dfs(u, v, graph, sz);
sz[v] += sz[u];
}
}
void dfs(int v, int p, vector<vector<int> > &graph, vector<int> &c, int C) {
c[v] = C;
for (auto u : graph[v])
if (u != p) {
dfs(u, v, graph, c, C);
}
}
signed main() {
int n;
cin >> n;
vector<vector<int> > graph(n);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<int> sz(n);
dfs(0, 0, graph, sz);
vector<int> cen;
long long ans = 0;
for (int i = 0; i < n; i++) {
if ((n - sz[i]) * 2 <= n) {
int t = 1;
for (auto u : graph[i]) {
if (sz[u] < sz[i]) {
if (2 * sz[u] <= n) continue;
t = 0;
}
}
if (t) cen.push_back(i);
}
}
for (int i = 0; i < n; i++) {
ans += 2 * min(sz[i], n - sz[i]);
}
cout << ans << "\n";
if (cen.size() == 2) {
int v = cen[0], u = cen[1];
vector<int> c(n);
dfs(v, u, graph, c, 0);
dfs(u, v, graph, c, 1);
vector<int> p0, p1;
for (int i = 0; i < n; i++) {
if (c[i] == 0) p0.push_back(i);
if (c[i] == 1) p1.push_back(i);
}
vector<int> ans(n);
for (int i = 0; i < n / 2; i++) {
ans[p0[i]] = p1[i];
ans[p1[i]] = p0[i];
}
for (int i = 0; i < n; i++) {
cout << ans[i] + 1 << " ";
}
return 0;
} else {
dfs(cen[0], cen[0], graph, sz);
int C = 0, idC = 0;
vector<int> c(n);
c[cen[0]] = -1;
int maxsz = 0;
for (auto u : graph[cen[0]]) {
dfs(u, cen[0], graph, c, C);
maxsz = max(maxsz, sz[u]);
if (maxsz == sz[u]) idC = C;
C++;
}
vector<pair<int, int> > q;
for (int i = 0; i < n; i++) {
if (i != cen[0] && c[i] != idC) q.push_back({c[i], i});
}
for (int i = 0; i < n; i++) {
if (i != cen[0] && c[i] == idC) q.push_back({INF, i});
}
q.push_back({INF + 1, cen[0]});
sort((q).begin(), (q).end());
vector<int> ans(n);
for (int i = 0; i < n; i++) {
ans[q[i].second] = q[(i + maxsz) % q.size()].second;
}
for (int i = 0; i < n; i++) {
assert(c[ans[i]] != c[i]);
cout << ans[i] + 1 << " ";
}
}
}
| 2 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
constexpr double EPS = (1e-10);
constexpr double INF = 252521;
#define equals(a, b) (fabs(a - b) < EPS)
std::vector<int> par, rnk;
void init(int n)
{
par.resize(n); rnk.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i;
rnk[i] = 0;
}
}
int find(int x)
{
if (par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x == y) return;
if (rnk[x] < rnk[y]) {
par[x] = y;
} else {
par[y] = x;
if (rnk[x] == rnk[y]) {
rnk[x]++;
}
}
}
bool same(int x, int y)
{
return (find(x) == find(y));
}
struct Edge {
int u, v;
double w;
Edge() {}
Edge(int u, int v, double w) :
u{u}, v{v}, w{w} {}
};
struct Data {
int u, v;
double c, t;
Data() {}
Data(int u, int v, double c, double t) :
u{u}, v{v}, c{c}, t{t} {}
};
int N, M;
std::vector<Data> d;
std::vector<Edge> es;
double kruskal()
{
sort(es.begin(), es.end(), [](const Edge& e1, const Edge& e2) {
return e1.w > e2.w;
});
init(N);
double tw = 0;
for (Edge& e : es) {
if (!same(e.u, e.v)) {
unite(e.u, e.v);
} else {
tw += e.w;
}
}
return tw;
}
bool c(double x)
{
for (int i = 0; i < M; i++) {
es[i].u = d[i].u;
es[i].v = d[i].v;
es[i].w = d[i].t - x * d[i].c;
}
return kruskal() >= 0;
}
double solve()
{
double lb = 0, ub = INF;
for (int i = 0; i < 50; i++) {
double mid = (lb + ub) / 2;
if (c(mid)) {
lb = mid;
} else {
ub = mid;
}
}
return ub;
}
int main()
{
scanf("%d %d", &N, &M);
d.resize(M); es.resize(M);
for (int i = 0; i < M; i++) {
scanf("%d %d %lf %lf", &d[i].u, &d[i].v, &d[i].c, &d[i].t);
}
auto res = solve();
printf("%.15f\n", (equals(res, INF) ? 0 : res));
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
#include <numeric>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define Rep1(i,k,n) for(int i=k ; i<=n ; i++)
#define rrep(i,n) for(int i=n ; i>=0 ; i--)
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
//#define int ll
typedef long long ll;
const int md = 1000000007;
const int INF = 1<<30;
using namespace std;
//vi prime(1000000,0);
vector<bool> isp;
void makePrime(int n){
isp.assign(n+1,true);
int id = 0;
for(int i=2 ; i <= n ; i++){
if(!isp[i]) continue;
for(int k = i*2 ; k <= n ; k += i){
isp[k] = false;
}
/*
prime[id] = i;
id++;
*/
//prime.pb(i);
}
//Sort(prime);
}
ll check(int a, int b){
ll ans = 0;
if(a-b <= 2) ans++;
int s;
if((a-b)%2 == 0) s = a-b+1;
else s = a - b;
for(int i = max(s,3); i <= min(a+b,999983) ; i += 2) {
if(isp[i]) ans++;
}
return ans;
}
int main(){
makePrime(999983);
//cout << prime.size() << endl;
//cout << binary_search(ALL(prime),2) << endl;
int n;
while(true){
cin >> n;
if(n == 0) break;
ll ans = 0;
rep(i,n){
int a,b; cin >> a >> b;
//cout << check(a,b) << endl;
int tmp = check(a,b);
//cout << "check: " << tmp << endl;
ans += check(a,b)-1;
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void fast();
void debug(int*, int);
void test();
void solve();
#define ll long long int
#define u_int unsigned int
#define u_long unsigned long
#define fr first
#define sd second
#define pb push_back
#define vi vector<int>
#define vl vector<long long int>
#define vvi vector<vector<int> >
#define vvl vector<vector<long long int> >
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sfor(i, s, e) for(int i = s; i < e; i++)
#define rfor(i, s, e) for(int i = s; i > e; i--)
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void debug(int *a, int size){
cout << "[DEBUG]\t";
sfor(i, 0, size) cout << a[i] << " ";
cout << "\n";
}
void test(){
int t; cin >> t;
while(t--) solve();
}
//////////////////////////////////////////////////////////////////
int arr[200010];
int odd[100010];
int even[100010];
void solve(){
int n; cin >> n;
sfor(i, 0, n) cin >> arr[i];
int j = 0;
for(int i = 0; i < n; i++){
even[j] = arr[i];
if(i != n - 1)
odd[j] = arr[++i];
j++;
}
int sodd = (n)/2;
int seven = (n + 1)/2;
sort(odd, odd + sodd);
sort(even, even + seven);
int i = 0; j = 0;
//debug(arr, n);
j = 0;
for(int i = 0; i < n; i++){
arr[i] = even[j];
if(i != n - 1)
arr[++i] = odd[j];
j++;
}
//debug(arr, n);
sfor(k, 1, n){
if(arr[k-1] > arr[k]){
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main(){
fast();
test();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int tot[100100];
int maxd = 0;
vector<vector<int> > g;
void dfs(int v, int d, int p) {
maxd = ((maxd < d) ? (d) : (maxd));
tot[d]++;
for (int i = (0); i < (g[v].size()); ++i) {
int to = g[v][i];
if (to != p) {
dfs(to, d + 1, v);
}
}
}
int use[100100];
inline bool check(int k, int w) {
int l = 1, r = 1;
int sum = tot[l];
while (sum < k) {
r++;
sum += tot[r];
}
long long price = 0;
int need = k;
for (int i = r; i >= l; --i) {
int v = ((need < tot[i]) ? (need) : (tot[i]));
use[i] = v;
price += v * 1ll * (r - i);
need -= v;
}
if (price <= w) return true;
while (1) {
if (r == maxd) break;
sum += tot[r + 1];
while (1) {
if (l == r) break;
if (sum - tot[l] >= k) {
price -= (r - l) * 1ll * use[l];
sum -= tot[l];
l++;
} else {
int v = k - (sum - tot[l]);
price -= (r - l) * 1ll * (use[l] - v);
use[l] = v;
break;
}
}
r++;
use[r] = ((tot[r] < k) ? (tot[r]) : (k));
price += k - use[r];
if (price <= w) return true;
}
return false;
}
int main() {
int n, k, p;
scanf("%d%d%d", &n, &k, &p);
g.resize(n);
for (int i = (0); i < (n - 1); ++i) {
int v1, v2;
scanf("%d%d", &v1, &v2);
v1--;
v2--;
g[v1].push_back(v2);
g[v2].push_back(v1);
}
dfs(0, 0, -1);
int res = 0;
int le = 1, ri = ((n - 1 < k) ? (n - 1) : (k));
while (le <= ri) {
int m = (le + ri) / 2;
if (check(m, p)) {
res = m;
le = m + 1;
} else {
ri = m - 1;
}
}
cout << res << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const string dest[16] = {
"KIHABARA",
"KIHABAR",
"KIHABRA",
"KIHABR",
"KIHBARA",
"KIHBAR",
"KIHBRA",
"KIHBR",
"AKIHABARA",
"AKIHABAR",
"AKIHABRA",
"AKIHABR",
"AKIHBARA",
"AKIHBAR",
"AKIHBRA",
"AKIHBR"};
string src;
int main() {
cin >> src;
for (int i = 0; i < 16; i++) {
if (src == dest[i]) {
puts("YES");
return 0;
}
}
puts("NO");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 100005;
int P[MAXV];
int R[MAXV];
int S[MAXV];
void init(int N) {
for (int i = 0; i < N; i++) {
P[i] = i;
R[i] = 0;
S[i] = 1;
}
}
int rep(int i) {
if (P[i] != i) P[i] = rep(P[i]);
return P[i];
}
bool unio(int a, int b) {
a = rep(a);
b = rep(b);
if (a == b) return false;
if (R[a] < R[b]) {
P[a] = b;
S[b] += S[a];
} else {
P[b] = a;
S[a] += S[b];
}
if (R[a] == R[b]) R[a]++;
return true;
}
int N, M;
vector<int> fam[MAXV];
vector<int> inc[MAXV];
int out[MAXV];
int topo(int who) {
if (fam[who].size() == 0) return 0;
int proc = 0;
queue<int> q;
for (int v : fam[who])
if (out[v] == 0) q.push(v);
while (!q.empty()) {
int v = q.front();
q.pop();
proc++;
for (int p : inc[v]) {
--out[p];
if (out[p] == 0) q.push(p);
}
}
if (proc == fam[who].size()) return fam[who].size() - 1;
return fam[who].size();
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M;
init(N);
for (int i = 0; i < M; i++) {
int A, B;
cin >> A >> B;
A--;
B--;
inc[B].push_back(A);
out[A]++;
unio(A, B);
}
for (int i = 0; i < N; i++) {
int who = rep(i);
fam[who].push_back(i);
}
int ans = 0;
for (int i = 0; i < N; i++) {
int edge = topo(i);
ans += edge;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.1415926535897;
const int N = 1010;
struct circle {
int x, y, r;
long double s() { return 1. * r * r * pi; }
bool operator<(const circle &y) const { return r < y.r; }
} a[N];
long double sqr(long double x) { return x * x; }
long double dis(int i, int j) {
return sqrt(sqr(a[i].x - a[j].x) + sqr(a[i].y - a[j].y));
}
int n, k, fa[N], b[N], ne[N], fi[N];
void add(int x, int y) {
fa[y] = x;
b[++k] = y;
ne[k] = fi[x];
fi[x] = k;
}
bool in(int x, int y) { return dis(x, y) + a[x].r <= a[y].r; }
long double dfs(int x, int ind) {
long double res = (ind <= 2) || (~ind & 1) ? a[x].s() : -a[x].s();
for (int j = fi[x]; j; j = ne[j]) res += dfs(b[j], ind + 1);
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].r);
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = n; i >= 1; i--)
for (int j = i + 1; j <= n; j++)
if (in(i, j)) {
add(j, i);
break;
}
long double ans = 0;
for (int i = 1; i <= n; i++)
if (fa[i] == i) ans += dfs(i, 1);
printf("%.9Lf", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v[2];
int n, x;
int done[2][2010];
int go(int who) {
memset(done, 0, sizeof(done));
int ret = 0, cap = x;
while (1) {
int mx = -1, pos;
for (int i = 0; i < v[who].size(); i++) {
if (done[who][i]) continue;
if (v[who][i].first <= cap && v[who][i].second > mx) {
mx = v[who][i].second;
pos = i;
}
}
if (mx == -1) return ret;
ret++;
done[who][pos] = 1;
cap += mx;
who = 1 - who;
}
}
int main() {
cin >> n >> x;
for (int i = 0; i < n; i++) {
int t, h, m;
cin >> t >> h >> m;
v[t].push_back(make_pair(h, m));
}
cout << max(go(0), go(1)) << endl;
return 0;
}
| 1 |
#include<iostream>
using namespace std;
int main()
{
string S;
cin>>S;
cout<<S.substr(0,3)<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n[3] = {0, 0, 0}, first = 0;
string s;
cin >> s;
for (int i = 0; i < s.size(); i += 2) {
++n[s[i] - '1'];
}
if (n[0]) {
cout << '1';
++first;
--n[0];
}
if (n[1] && !first) {
cout << '2';
++first;
--n[1];
}
if (n[2] && !first) {
cout << '3';
--n[2];
}
for (int i = 0; i < 3; ++i) {
for (int o = 0; o < n[i]; ++o) {
cout << '+' << i + 1;
}
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int tp, ck, n, i, s, d, ans = 3333, g[3333] = {};
vector<pair<int, int> > f[3333];
void dfs2(int x, int k, int r) {
tp += k;
ck = max(ck, r);
g[x] = 1;
r = max(r, 0);
for (int j = 0; j < f[x].size(); j++)
if (!g[f[x][j].first]) {
if (f[x][j].second == 1) {
dfs2(f[x][j].first, 1, r + 1);
}
if (f[x][j].second == 0) {
dfs2(f[x][j].first, 0, r - 1);
}
}
}
int main() {
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d%d", &s, &d);
f[s].push_back(make_pair(d, 0));
f[d].push_back(make_pair(s, 1));
}
for (i = 1; i <= n; i++) {
memset(g, 0, sizeof(g));
tp = 0;
ck = 0;
dfs2(i, 0, 0);
ans = min(ans, tp - ck);
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
using namespace std;
long long read() {
long long a = 0;
char b = 1, c;
do
if ((c = getchar()) == 45) b = -1;
while (c < 48 || c > 57);
do a = (a << 3) + (a << 1) + (c & 15);
while ((c = getchar()) > 47 && c < 58);
return a * b;
}
void write(long long x) {
if (x < 0) putchar('-'), x = -x;
char a[10], s = 0;
do a[s++] = x % 10 | 48;
while (x /= 10);
do putchar(a[--s]);
while (s);
}
void solve() {
int n = read(), k = read();
int ar[n];
for (auto &i : ar) i = read();
int start = 0;
int end = n - 1;
int moves = 0;
int iters = 0;
while (iters < n) {
if (ar[start] <= k) {
++moves;
++start;
} else if (ar[end] <= k) {
++moves;
--end;
} else {
break;
}
++iters;
}
write(moves);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define mo 1000000007ll
#define int long long
#define N 2
int ans,n,m,k;
char s[1010][1010],tp[1010][1010];
struct no{
int a[N][N];
};
no m1(no x,no y){
no z;
memset(z.a,0,sizeof(z.a));
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j]%mo)%mo;
return z;
}
no qp(no x,int n){
no r;
memset(r.a,0,sizeof(r.a));
for(int i=0;i<2;i++)
r.a[i][i]=1;
while(n){
if(n&1)r=m1(r,x);
x=m1(x,x);
n>>=1;
}
return r;
}
int qp(int x,int y){
int r=1;
while(y){
if(y&1)r=r*x%mo;
x=x*x%mo;
y>>=1;
}
return r;
}
int ck(){
for(int i=1;i<=n;i++)
if(s[i][1]=='#'&&s[i][m]=='#')return 1;
return 0;
}
void tu(){
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
tp[m-j+1][i]=s[i][j];
swap(n,m);
memcpy(s,tp,sizeof(s));
}
signed main(){
cin>>n>>m>>k;
int s1=0,s2=0,s3=0;
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++)
if(s[i][j]=='#')s1++;
}
int a=ck();
tu();
int b=ck();
tu();
if(a&&b){
puts("1");
return 0;
}
else if(!a&&!b){
printf("%lld",qp(s1,k-1));
return 0;
}
else if(b)tu();
for(int i=1;i<=n;i++)
s3+=(s[i][1]=='#'&&s[i][m]=='#');
for(int i=1;i<=n;i++)
for(int j=1;j<m;j++)
s2+=(s[i][j]=='#'&&s[i][j+1]=='#');
no tp;
memset(tp.a,0,sizeof(tp.a));
tp.a[0][0]=s1;
tp.a[0][1]=s2;
tp.a[1][1]=s3;
tp=qp(tp,k-1);
cout<<(tp.a[0][0]-tp.a[0][1]+mo)%mo;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
int i,j,cnt=0;
int k,t;
cin>>k>>t;
int a[t];
for(i=0;i<t;i++)cin>>a[i];
sort(a,a+t,greater<int>());
if(a[0]-1<=k-a[0])cout<<0;
else cout<<a[0]-1-(k-a[0]);
} | 0 |
#include <stdio.h>
#include <algorithm>
int a[100000 + 5];
int n;
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; ++ i) {
int x;
scanf("%d", &x); ++ x;
a[x] ++;
a[x - 1] ++;
a[x + 1] ++;
}
printf("%d\n", *std::max_element(a, a + 100000 + 2));
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int a,b,c,x,ans=0;
int main()
{
cin>>a>>b>>c>>x;
for(int i=0;i<=a;i++)
{
for(int j=0;j<=b;j++)
{
for(int k=0;k<=c;k++)
{
if(i*500+j*100+k*50==x)ans++;
}
}
}
cout<<ans;
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long gcd(long long a, long long b) { return (a == 0) ? b : gcd(b % a, a); }
long long cnt1(long long x) {
long long cnt = 0;
while (x > 0) x &= x - 1, cnt++;
return cnt;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (long long i = 0; i < v.size(); i++) is >> v[i];
return is;
}
template <class T>
ostream &operator<<(ostream &os, vector<T> v) {
for (long long i = 0; i < v.size(); i++) os << v[i] << ' ';
os << '\n';
return os;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
cin >> a;
sort(a.begin(), a.end());
long long s = accumulate(a.begin(), a.end(), 0);
long long i = 0;
while (2 * s < 9 * n) {
s = s - a[i] + 5;
i++;
}
cout << i;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ll w,h,x,y;
cin>>w>>h>>x>>y;
cout<<w*h/2.<<" "<<(x*2==w&&y*2==h)<<endl;
}
| 0 |
#include <bits/stdc++.h>
template <class C>
inline void log_c(const C& c) {}
template <class C>
inline int sz(const C& c) {
return static_cast<int>(c.size());
}
using namespace std;
using num = int64_t;
const std::string eol = "\n";
using pii = pair<int, int>;
num sum = 0;
vector<vector<int>> adj;
vector<bool> visited;
const num mod = 1000000000 + 7;
inline num pow_by_mod(num n, num exp) {
num ax = 1;
for (; exp; --exp) {
ax = (ax * n) % mod;
}
return ax;
}
inline int tree_size(int u, int p) {
int ans = 1;
visited.at(u) = true;
for (int v : adj.at(u)) {
if (v == p) continue;
ans += tree_size(v, u);
}
return ans;
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
adj.resize(n);
visited.resize(n);
for (int k = 0; k < (n - 1); ++k) {
int u, v, x;
cin >> u >> v >> x;
if (x == 0) {
--u;
--v;
adj.at(u).push_back(v);
adj.at(v).push_back(u);
}
}
num bad_path_count = 0;
for (int u = 0; u < (n); ++u) {
if (visited.at(u)) continue;
num sz = tree_size(u, u);
bad_path_count = (bad_path_count + pow_by_mod(sz, m)) % mod;
}
const num path_count = pow_by_mod(n, m);
const num good_path_count = (path_count - bad_path_count + mod) % mod;
cout << good_path_count << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4020;
int a[MAXN];
int calc(int left, int right) {
int ret = 0;
while (left <= right) {
if (!a[left] || !a[right]) {
ret += min(a[left] + a[right], 1000 - a[left] - a[right]);
} else {
ret += 1000 - a[left] - a[right];
}
++left;
--right;
}
return ret;
}
int sum(int left, int right) {
int sum = 0;
for (int i = left; i <= right; ++i) {
sum += a[i];
}
return sum;
}
int main(int argc, char **argv) {
int n;
scanf("%d", &n);
n <<= 1;
int sum = 0;
for (int i = 0; i < n; ++i) {
int r, c;
scanf("%d.%d", &r, &c);
a[i] = c;
sum += c;
}
sort(a, a + n);
int total = 500 * n;
int ans = total - sum;
if (ans < 0) ans = -ans;
for (int i = 0; 2 * i < n; ++i) {
if (!a[i]) {
int cur = total - sum - 1000 * (i + 1);
if (cur < 0) {
cur = -cur;
}
ans = min(ans, cur);
}
}
printf("%d.%03d\n", ans / 1000, ans % 1000);
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int main(){
const int INF=1000000000;
int n,c;
static int dp[2][5000+2][2];
for(int i=0;i<2;i++)for(int j=0;j<5002;j++){dp[i][j][0]=INF; dp[i][j][1]=INF;}
dp[1][1][0]=0;
dp[1][0][1]=0;
scanf("%d",&n);
for(int i=2;i<=n;i++){
int s=i%2,t=1-s;
scanf("%d",&c);
for(int j=0;j<=n/2;j++){
if(j==0)dp[s][j][0]=INF;
else dp[s][j][0]=min(dp[t][j-1][0],dp[t][j-1][1]+c);
dp[s][j][1]=min(dp[t][j][0]+c,dp[t][j][1]);
}
}
printf("%d\n",min(dp[0][n/2][0],dp[0][n/2][1]));
} | 0 |
#include<stdio.h>
#include<algorithm>
using namespace std;
int ret;
int A[11];
int B[11];
int C[11];
int n;
void solve(int a,int b,int c){
if(a==n){
int tmp=0;
for(int i=10;i>=0;i--){
tmp*=10;tmp+=C[i];
}
ret=max(ret,tmp);
return;
}
int t=A[a]-B[a]-b;
if(t<0){
C[a]=t+10;
if(c)solve(a+1,0,c-1);
solve(a+1,1,c);
}else{
C[a]=t;
solve(a+1,0,c);
}
}
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int at=0;
while(a){
A[at]=a%10;
a/=10;at++;
}
n=at;
at=0;
while(b){
B[at]=b%10;b/=10;at++;
}
solve(0,0,c);
printf("%d\n",ret);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
int n;
scanf("%d", &n);
vector<int> p(n);
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
p[i]--;
}
vector<int> b(n);
int cnt_ones = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (b[i] == 1) {
cnt_ones++;
}
}
int cnt_loops = 0;
vector<bool> vis(n, false);
for (int i = 0; i < n; i++) {
if (vis[i]) {
continue;
}
int x = i;
vis[x] = true;
while (p[x] != i) {
x = p[x];
vis[x] = true;
}
cnt_loops++;
}
int cnt1 = cnt_loops;
if (cnt1 == 1) {
cnt1 = 0;
}
int cnt2 = 1;
if (cnt_ones % 2) {
cnt2 = 0;
}
printf("%d\n", cnt1 + cnt2);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline long long ModPow(long long a, long long b, long long mod) {
if (b == 0) return 1;
if (b == 1) return a;
if (b % 2) return (ModPow(a, b - 1, mod) * a) % mod;
long long z = ModPow(a, b / 2, mod);
return (z * z) % mod;
}
int main() {
long long int p, k;
cin >> p >> k;
long long MOD = (long long)1e9 + 7;
if (k == 0) {
long long int ans = 1;
for (int i = 1; i < p; i++) ans = (ans * p) % MOD;
ans = (ans + MOD) % MOD;
cout << ans << endl;
return 0;
}
if (k == 1) {
cout << ModPow(p, p, MOD);
return 0;
}
long long int curr = k;
int q = 0;
while (1) {
curr = (curr * k) % p;
curr = (curr + p) % p;
q++;
if (curr == k) break;
}
int qq = (p - 1) / q;
long long int ans = 1;
for (int i = 1; i <= qq; i++) {
ans = (ans * p) % MOD;
}
ans = (ans + MOD) % MOD;
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
class node {
public:
long long int second;
long long int length;
};
void FastIO() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
long long int a[3 * 100001], ans[3 * 100001], mem[5001][5001];
long long int n, k, i, j, small, large, small_length, large_length;
long long int DP(long long int s, long long int l) {
if (s == 0 && l == 0) return 0;
if (s < 0 || l < 0) return 10000000000;
if (mem[s][l] != -1) return mem[s][l];
long long int first = ans[s * small_length + l * large_length - 1] -
ans[(s - 1) * small_length + l * large_length];
long long int second = ans[s * small_length + l * large_length - 1] -
ans[(s)*small_length + (l - 1) * large_length];
mem[s][l] = min(DP(s - 1, l) + first, DP(s, l - 1) + second);
return mem[s][l];
}
int main() {
FastIO();
cin >> n >> k;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < 5001; i++) {
for (j = 0; j < 5001; j++) mem[i][j] = -1;
}
sort(a, a + n);
ans[0] = 0;
for (i = 1; i < n; i++) ans[i] = a[i] - a[i - 1] + ans[i - 1];
small_length = n / k;
large_length = n / k + 1;
large = n % k;
small = k - large;
cout << DP(small, large) << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5001;
int dp[MAXN][MAXN], p[MAXN][MAXN];
void Solve() {
int n;
cin >> n;
string s1, s2;
cin >> s1 >> s2;
vector<int> ind[4];
for (int i = 0; i < n; ++i) {
ind[(s2[i] - '0') * 2 + (s1[i] - '0')].emplace_back(i);
}
for (int x1 = 0; x1 <= ind[1].size(); ++x1) {
for (int x2 = 0; x2 <= ind[2].size(); ++x2) {
int x0 = (ind[0].size() + ind[1].size()) - x1 - x2;
if ((x0 & 1) || x0 < 0) continue;
x0 /= 2;
int x3 = n / 2 - x0 - x1 - x2;
if (x3 < 0 || x3 > ind[3].size() || x0 > ind[0].size()) continue;
for (int i = 0; i < x0; ++i) cout << ind[0][i] + 1 << ' ';
for (int i = 0; i < x1; ++i) cout << ind[1][i] + 1 << ' ';
for (int i = 0; i < x2; ++i) cout << ind[2][i] + 1 << ' ';
for (int i = 0; i < x3; ++i) cout << ind[3][i] + 1 << ' ';
return;
}
}
cout << -1;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
Solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int ncr[1001][1001];
void ncrdp(void) {
for (int i = 0; i < 1001; i++) {
ncr[i][0] = 1;
ncr[i][i] = 1;
}
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < i; j++) {
ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % 1000000007;
}
}
}
long long fast_power(long long base, long long power) {
long long result = 1;
while (power > 0) {
if (power % 2 == 1) {
result = (result * base) % 1000000007;
}
base = (base * base) % 1000000007;
power = power / 2;
}
return result;
}
bool prime[5000000];
void SieveOfEratosthenes(int n) {
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int operation(long long int var1, long long int var2) {
return min(var1, var2);
}
void build(long long int n, long long int l, long long int r,
long long int tree[], long long int a[]) {
if (l == r) {
tree[n] = a[l];
} else {
build(2 * n, l, (l + r) / 2, tree, a);
build(2 * n + 1, (l + r) / 2 + 1, r, tree, a);
tree[n] = operation(tree[2 * n], tree[2 * n + 1]);
}
}
void update(long long int n, long long int l, long long int r,
long long int tree[], long long int a[], long long int node,
long long int update_value) {
if (node >= l && node <= r) {
if (l == r) {
if (l == node) {
tree[n] = update_value;
}
} else {
update(2 * n, l, (l + r) / 2, tree, a, node, update_value);
update(2 * n + 1, (l + r) / 2 + 1, r, tree, a, node, update_value);
tree[n] = operation(tree[2 * n], tree[2 * n + 1]);
}
}
}
void query(long long int n, long long int l, long long int r,
long long int tree[], long long int a[], long long int ql,
long long int qr, long long int &min_value) {
if (l >= ql && r <= qr) {
min_value = operation(min_value, tree[n]);
} else if ((l <= ql && ql <= r) || (l <= qr && qr <= r)) {
query(2 * n, l, (l + r) / 2, tree, a, max(ql, l), min(qr, r), min_value);
query(2 * n + 1, (l + r) / 2 + 1, r, tree, a, max(ql, l), min(qr, r),
min_value);
}
}
long long int tree[1000000];
int gcdExtended(int a, int b, int *x, int *y) {
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1, &y1);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int get_prime(int x) {
while (x > 1) {
if (prime[x]) {
return x;
}
x--;
}
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> v(n);
for (int i = 0; i < (n); i++) cin >> v[i];
long long int moves = 0, start = 0, iteration = 0;
map<long long int, bool> m;
m[0] = true;
for (int i = 1; i <= n; i++) {
int j;
for (j = 0; j < n; j++) {
if (v[j] == i) break;
}
while (!m[j] && v[j - 1] >= i) {
swap(v[j], v[j - 1]);
m[j] = true;
j--;
}
}
for (int i = 0; i < (n); i++) cout << v[i] << " ";
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 3;
const int maxm = 1e6 + 3;
const int mod = 1e9 + 7;
const int oo = 1061109567;
const long long loo = 4557430888798830399;
const long double pi = 3.141592653589793238462643383279502884;
void input() {
if (fopen("abc"
".inp",
"r")) {
freopen(
"abc"
".inp",
"r", stdin);
freopen(
"abc"
".out",
"w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, k, last = 0, sz = 1;
string s;
int link[maxn], len[maxn], to[maxn][26], terminal[maxn];
long long occ[maxn], word[maxn];
void ins(int c) {
int id = sz++;
len[id] = len[last] + 1;
for (; last != -1 && !to[last][c]; last = link[last]) to[last][c] = id;
if (last != -1) {
int p = to[last][c];
if (len[p] == len[last] + 1)
link[id] = p;
else {
int q = sz++;
len[q] = len[last] + 1;
link[q] = link[p];
memcpy(to[q], to[p], sizeof(to[p]));
for (; last != -1 && to[last][c] == p; last = link[last]) to[last][c] = q;
link[id] = link[p] = q;
}
}
last = id;
}
void dfs(int u) {
if (occ[u] != 0) return;
occ[u] += terminal[u];
word[u] += terminal[u];
for (int i = 0; i < 26; ++i) {
int v = to[u][i];
if (!v) continue;
dfs(v);
occ[u] += occ[v];
word[u] += occ[v] + word[v];
}
}
int main() {
input();
cin >> s >> k;
int n = s.size();
if (k > 1ll * n * (n + 1) / 2) {
cout << "No such line.";
return 0;
}
link[0] = -1;
for (int i = 0; i < s.size(); ++i) ins(s[i] - 'a');
while (last) {
terminal[last] = 1;
last = link[last];
}
dfs(0);
string ans = "";
int u = 0;
while (k > 0) {
long long cur = 0;
for (int i = 0; i < 26; ++i) {
int v = to[u][i], pre = cur;
if (!v) continue;
cur += word[v];
if (cur >= k) {
u = v;
k -= pre + occ[v];
ans += char('a' + i);
break;
}
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const long long INF = 1000000000000000000;
vector<pair<int, int> > v(8);
vector<int> sq(4), par(4);
double di(pair<int, int> a, pair<int, int> b) {
double res = 0;
res = sqrt((double)(a.first - b.first) * (a.first - b.first) +
(a.second - b.second) * (a.second - b.second));
return res;
}
bool isSquare(vector<int> ind) {
vector<vector<double> > d(4, vector<double>(4));
for (int _n(4), i(0); i < _n; i++) {
for (int _n(4), j(0); j < _n; j++) {
d[i][j] = (di(v[ind[i]], v[ind[j]]));
}
}
bool ok = false;
if (fabs(d[0][1] - d[0][2]) < EPS && fabs(d[0][1] - d[2][3]) < EPS &&
fabs(d[0][1] - d[1][3]) < EPS && fabs(d[1][2] - d[0][3]) < EPS) {
ok = true;
}
if (fabs(d[0][2] - d[0][1]) < EPS && fabs(d[0][2] - d[1][3]) < EPS &&
fabs(d[0][2] - d[2][3]) < EPS && fabs(d[2][1] - d[0][3]) < EPS) {
ok = true;
}
if (fabs(d[0][3] - d[0][2]) < EPS && fabs(d[0][3] - d[2][1]) < EPS &&
fabs(d[0][3] - d[3][1]) < EPS && fabs(d[3][2] - d[0][1]) < EPS) {
ok = true;
}
if (fabs(d[0][1] - d[0][3]) < EPS && fabs(d[0][1] - d[3][2]) < EPS &&
fabs(d[0][1] - d[1][2]) < EPS && fabs(d[1][3] - d[0][2]) < EPS) {
ok = true;
}
return ok;
}
bool isPar(vector<int> ind) {
vector<vector<double> > d(4, vector<double>(4));
for (int _n(4), i(0); i < _n; i++) {
for (int _n(4), j(0); j < _n; j++) {
d[i][j] = (di(v[ind[i]], v[ind[j]]));
}
}
bool ok = false;
if (fabs(d[0][1] - d[2][3]) < EPS && fabs(d[0][2] - d[1][3]) < EPS &&
fabs(d[0][3] - d[1][2]) < EPS) {
ok = true;
}
if (fabs(d[0][2] - d[1][3]) < EPS && fabs(d[0][1] - d[2][3]) < EPS &&
fabs(d[0][3] - d[2][1]) < EPS) {
ok = true;
}
if (fabs(d[0][3] - d[2][1]) < EPS && fabs(d[0][2] - d[3][1]) < EPS &&
fabs(d[0][1] - d[3][2]) < EPS) {
ok = true;
}
if (fabs(d[0][1] - d[3][2]) < EPS && fabs(d[0][3] - d[1][2]) < EPS &&
fabs(d[0][2] - d[1][3]) < EPS) {
ok = true;
}
return ok;
}
int main() {
int n, m;
for (int _n(8), i(0); i < _n; i++) {
pair<int, int> a;
cin >> a.first >> a.second;
v[i] = a;
}
vector<int> b(8);
for (int _n(4), i(0); i < _n; i++) b[i] = 1;
sort((b).begin(), (b).end());
int ii = 0, jj = 0;
bool ok = false;
do {
for (int _n(8), i(0); i < _n; i++) {
if (b[i])
sq[ii++] = i;
else
par[jj++] = i;
}
if (isSquare(sq) && (isSquare(par) || isPar(par))) {
ok = true;
break;
}
ii = 0;
jj = 0;
} while (next_permutation(b.begin(), b.end()));
if (ok) {
cout << "YES" << endl;
for (int _n(4), i(0); i < _n; i++) {
if (i) cout << " ";
cout << sq[i] + 1;
}
cout << endl;
for (int _n(4), i(0); i < _n; i++) {
if (i) cout << " ";
cout << par[i] + 1;
}
cout << endl;
} else
cout << "NO" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[111];
int id[111][111];
int vis[111];
int main() {
int n, m, k, q;
while (cin >> n >> k) {
m = n / k;
for (int i = 0; i < n; i++) {
cin >> a[i];
vis[i] = 0;
}
scanf("%d", &q);
double minv = 1 << 30, maxv = -1 << 30;
for (int i = 0; i < q; i++) {
int sum = 0;
for (int j = 0; j < m; j++) {
cin >> id[i][j];
vis[id[i][j] - 1] = 1;
sum += a[id[i][j] - 1];
}
if (double(sum) / m < minv) {
minv = double(sum) / m;
}
if (double(sum) / m > maxv) {
maxv = double(sum) / m;
}
}
vector<int> v;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
v.push_back(a[i]);
}
}
sort(v.begin(), v.end());
if (v.size() >= m && (n - v.size()) < k * m) {
int sum = 0;
for (int i = 0; i < m; i++) {
sum += v[i];
}
if (double(sum) / m < minv) {
minv = double(sum) / m;
}
if (double(sum) / m > maxv) {
maxv = double(sum) / m;
}
reverse(v.begin(), v.end());
sum = 0;
for (int i = 0; i < m; i++) {
sum += v[i];
}
if (double(sum) / m < minv) {
minv = double(sum) / m;
}
if (double(sum) / m > maxv) {
maxv = double(sum) / m;
}
}
printf("%.9lf %.9lf\n", minv, maxv);
}
}
| 2 |
#include <bits/stdc++.h>
#define upd(x,y) x=x+y>=mu?x+y-mu:x+y;
const int mu=1000000007;
int n,a[22][22],dp[22][1<<21];
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=0;j<n;j++) scanf("%d",&a[i][j]);
dp[0][0]=1;
for (int i=0;i<n;i++)
for (int j=0;j<(1<<n);j++){
if (!dp[i][j]) continue;
for (int k=0;k<n;k++)
if (a[i+1][k] && (j&(1<<k))==0)
upd(dp[i+1][j|(1<<k)],dp[i][j]);
}
printf("%d\n",dp[n][(1<<n)-1]);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
struct Op {
int r;
int first;
Op(int r, int first) {
this->r = r;
this->first = first;
}
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<int> vals(n);
for (__typeof(vals.begin()) val = vals.begin(); val != vals.end(); ++val)
scanf("%d", val);
deque<Op> d;
while (m--) {
int r, first;
scanf("%d %d", &r, &first);
while ((!d.empty()) and (d.back().first <= first)) {
d.pop_back();
}
d.push_back(Op(r, first));
}
vector<int> s_arr(vals.begin(), vals.begin() + d.front().first);
sort(s_arr.begin(), s_arr.end());
int i = 0, j = s_arr.size() - 1;
int curr_r = d.front().r;
int pos = j;
while (pos >= 0) {
if ((!d.empty()) and (pos == d.front().first - 1)) {
curr_r = d.front().r;
d.pop_front();
}
if (curr_r == 1) {
vals[pos--] = s_arr[j--];
} else {
vals[pos--] = s_arr[i++];
}
}
for (int idx = 0; idx < vals.size(); ++idx) {
if (idx) printf(" ");
printf("%d", vals[idx]);
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n;
int cnt[N];
void solve() {
long long k, n, a, b;
cin >> k >> n >> a >> b;
if (k - (n - 1) * b <= b) {
cout << -1 << endl;
return;
}
long long c = k / a;
if (c * a >= k) c--;
long long d = (k - n * b) / (a - b);
if (d * (a - b) >= (k - n * b)) d--;
cout << min(n, min(c, d)) << endl;
}
int main() {
int T = 1;
scanf("%d", &T);
while (T--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
int N, X;
vector<int> res;
void solve() {
int n = 0;
if (X < (1 << N))
n = (1 << N) / 2 - 1;
else
n = (1 << N) - 1;
res.resize(n);
vector<int> yong;
for (int i = 1; i < (1 << N); i <<= 1) {
if (i <= X && X < 2 * i) continue;
yong.push_back(i);
}
for (int i = (0); i < (int)(((int)((yong).size()))); i++) {
int bg = (1 << i) - 1, d = (1 << (i + 1));
for (int j = bg; j < n; j += d) res[j] = yong[i];
}
printf("%d\n", ((int)((res).size())));
if (((int)((res).size())) == 0) return;
for (int i = (0); i < (int)(((int)((res).size())) - 1); i++)
printf("%d ", res[i]);
printf("%d\n", res[((int)((res).size())) - 1]);
return;
}
int main() {
scanf("%d%d", &N, &X);
solve();
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
vector<int> adj[100010];
int p[100010];
int lev[100010];
int stp[100010][19];
int stm[100010], etm[100010];
int dtm = 0;
vector<int> b[100010];
void dfs(int u, int par, int lv) {
stm[u] = dtm++;
b[lv].push_back(stm[u]);
lev[u] = lv;
stp[u][0] = par;
for (int i = 1; i < 19; i++) {
if (stp[u][i - 1] != -1) {
stp[u][i] = stp[stp[u][i - 1]][i - 1];
}
}
for (int v : adj[u]) {
dfs(v, u, lv + 1);
}
etm[u] = dtm++;
}
int getAncestor(int u, int x) {
for (int i = 19 - 1; i >= 0; i--) {
if ((1 << i) <= x) {
u = stp[u][i];
if (u == -1) {
return u;
}
x = x - (1 << i);
}
}
return u;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
if (p[i] != 0) {
adj[p[i]].push_back(i);
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < 19; j++) {
stp[i][j] = -1;
}
}
for (int i = 1; i <= n; i++) {
if (p[i] == 0) {
dfs(i, -1, 0);
}
}
int m;
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, r;
scanf("%d %d", &u, &r);
int t = getAncestor(u, r);
if (t == -1) {
printf("0 ");
continue;
}
int lv = lev[u];
int s = 0, e = (int)b[lv].size() - 1, x = -1;
while (s <= e) {
int m = (s + e) / 2;
if (b[lv][m] > stm[t]) {
x = m;
e = m - 1;
} else {
s = m + 1;
}
}
if (x == -1) {
printf("0 ");
continue;
}
s = 0;
e = (int)b[lv].size() - 1;
int y = -1;
while (s <= e) {
int m = (s + e) / 2;
if (b[lv][m] < etm[t]) {
y = m;
s = m + 1;
} else {
e = m - 1;
}
}
if (y == -1) {
printf("0 ");
continue;
}
printf("%d ", max(0, y - x));
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 5e5 + 10;
const long long MOD = 998244353;
const long long MAXnum = 2e5;
const long double eps = 1e-8;
const long double inf = 1e20;
const long double PI = acos(-1);
inline long long read() {
long long s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
inline void print(long long x) {
if (x < 0) putchar('-'), x = x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
struct Edge {
long long to, w, next;
} edges[MAXN];
long long head[MAXN], cnt = 0;
void add(long long u, long long v, long long w) {
edges[++cnt].w = w;
edges[cnt].to = v;
edges[cnt].next = head[u];
head[u] = cnt;
}
long long ind[MAXN];
priority_queue<long long, vector<long long>, greater<long long> > que, que2;
long long temp[MAXN];
void solve() {
while (!que.empty()) que.pop();
while (!que2.empty()) que2.pop();
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
ind[i] = 0;
head[i] = 0;
}
cnt = 0;
for (long long i = 1; i <= n; i++) {
long long k = 0;
cin >> k;
for (long long j = 0; j < k; j++) cin >> temp[j];
sort(temp, temp + k);
for (long long j = 0; j < k; j++) {
long long v = temp[j];
add(v, i, 1);
ind[i]++;
}
}
for (long long i = 1; i <= n; i++) {
if (ind[i] == 0) {
que.push(i);
}
}
long long cntt = 0, now = 0, ans = 1;
while (!que.empty() || !que2.empty()) {
if (que.empty()) {
while (!que2.empty()) {
long long x = que2.top();
que2.pop();
que.push(x);
}
ans++;
}
long long x = que.top();
que.pop();
now = x;
cntt++;
for (long long i = head[x]; i; i = edges[i].next) {
long long v = edges[i].to;
ind[v]--;
if (ind[v] == 0) {
if (v > now)
que.push(v);
else
que2.push(v);
}
}
}
if (cntt != n) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, c, i, cl, cr, p[50], t[50], tim, m;
while (scanf("%d%d", &n, &c) != EOF) {
for (i = 0; i < n; i++) scanf("%d", &p[i]);
for (i = 0; i < n; i++) scanf("%d", &t[i]);
for (i = 0, cl = 0, tim = 0; i < n; i++) {
tim += t[i];
m = (p[i] - c * tim);
if (m < 0) m = 0;
cl += m;
}
for (i = n - 1, cr = 0, tim = 0; i > -1; i--) {
tim += t[i];
m = (p[i] - c * tim);
if (m < 0) m = 0;
cr += m;
}
if (cl == cr)
printf("Tie\n");
else if (cr > cl)
printf("Radewoosh\n");
else
printf("Limak\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pi pair<int,int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#include "string"
const int N = 2e5 + 5;
int xx[4]={0,0,1,-1};
int yy[4] ={1,-1,0,0};
int32_t main(){
boost;
int n,m,k;
cin>>n>>m>>k;
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
char c[n+1][m+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
cin>>c[i][j];
}
int dp[n+1][m+1];
memset(dp,1,sizeof dp);
dp[x1][y1] = 0;
queue<array<int,2>>q;
q.push({x1,y1});
while(!q.empty()) {
array<int,2> p = q.front();
q.pop();
if(p[0]==x2 && p[1]==y2)
break;
for(int i=0;i<4;i++){
for(int j=1;j<=k;j++){
int x = p[0] + xx[i]*j;
int y = p[1] + yy[i]*j;
if(x<1 or x>n or y<1 or y>m or c[x][y]=='@')
break;
if(dp[x][y]<dp[p[0]][p[1]]+1)
break;
if(dp[x][y] > dp[p[0]][p[1]]+1){
dp[x][y] = dp[p[0]][p[1]]+1;
q.push({x,y});
}
}
}
}
if(dp[x2][y2] == dp[0][0])
cout<<-1<<endl;
else
cout<<dp[x2][y2]<<endl;
}
| 0 |
#include<cstdio>
using namespace std;
int n;
int p[20010],a[20010],b[20010];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&p[i]);
for (int i=1;i<=n;i++)
a[i]=1,b[i]=-1;
for (int i=1;i<=n;i++)
a[p[i]]+=i,b[p[i]+1]-=i;
for (int i=1;i<=n;i++)
a[i]+=a[i-1],b[i]+=b[i-1];
for (int i=1;i<=n;i++)
printf("%d%c",a[i]+500000000," \n"[i==n]);
for (int i=1;i<=n;i++)
printf("%d%c",b[i]+500000000," \n"[i==n]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int r, c;
int main() {
scanf("%d%d", &r, &c);
int ans = 0;
if (r == 1 || c == 1)
ans = r * c;
else if (r == 2 && c == 2)
ans = 4;
else if (r * c == 6)
ans = 4;
else {
if (r == 2 || c == 2) {
int mx = max(r, c);
int tot = mx % 4;
tot = min(tot, 2) * 2;
tot += 4 * (mx / 4);
ans = max(ans, tot);
}
int black = (r / 2 + r % 2) * (c / 2 + c % 2) + (r / 2) * (c / 2);
int white = (r / 2) * (c / 2 + c % 2) + (c / 2) * (r / 2 + r % 2);
ans = max(ans, max(black, white));
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);
long long power(long long b, long long e, long long m) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b % m, e / 2, m) % m;
return power(b * b % m, e / 2, m);
}
long long power(long long b, long long e) {
if (e == 0) return 1;
if (e & 1) return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
template <typename T, typename U>
static inline void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T &x, U y) {
if (x < y) x = y;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << '(' << p.first << "," << p.second << ')';
}
const int NC = 1000005, MOD = M;
long long inv[NC], fac_inv[NC], fac[NC];
void initialize() {
long long i;
inv[1] = 1;
for (i = 2; i <= NC - 2; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
fac[0] = fac[1] = 1;
for (i = 2; i <= NC - 2; i++) fac[i] = i * fac[i - 1] % MOD;
fac_inv[0] = fac_inv[1] = 1;
for (i = 2; i <= NC - 2; i++) fac_inv[i] = inv[i] * fac_inv[i - 1] % MOD;
}
long long ncr(long long n, long long r) {
if (n < r) return 0;
return (fac[n] * fac_inv[r] % MOD) * fac_inv[n - r] % MOD;
}
int _runtimeTerror_() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
long long ans = 0;
for (int i = 0; i <= n - 2; ++i) {
if (i + 1 > m) break;
long long u = ncr(m - 1, i) * fac[i] % M;
u = (u * power(m, n - i - 2, M)) % M;
u = (u * ncr(n - 2, i)) % M;
if (i + 2 < n) u = (u * (i + 2) % M * power(n, n - i - 3, M)) % M;
ans = (ans + u) % M;
}
cout << ans << "\n";
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
initialize();
int TESTS = 1;
while (TESTS--) _runtimeTerror_();
return 0;
}
| 4 |
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <string>
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define REP(i, x, y) for(int i = (int)x; i <= (int)y; i ++)
#define FOR(i, x, y) for(int i = (int)x; i < (int)y; i ++)
#define PER(i, x, y) for(int i = (int)x; i >= (int)y; i --)
#define trace(x) cerr << #x << " " << x << endl;
#define dprintf(...) fprintf(stderr, __VA__ARGS__)
#define dln() fprintf(stderr, "\n")
using namespace std;
typedef long long LL;
typedef long double db;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
const int N = 100005;
const int P = 1e9 + 7;
const int inf = 1e9;
const LL Inf = 1e15;
inline int IN(){
char ch = getchar(); int x = 0, f = 0;
while(ch < '0' || ch > '9') ch = getchar(), f = (ch == '-');
while(ch >= '0' && ch <= '9'){
x = (x << 1) + (x << 3) + ch - 48;
ch = getchar();
}
return f ? (-x) : x;
}
inline int Pow(int x, int y, int p){
int an = 1;
for(; y; y >>= 1, x = (LL)x * x % p) if(y & 1) an = (LL)an * x % p;
return an;
}
template<typename T> inline void chkmin(T &a, const T &b) {if(a > b) a = b;}
template<typename T> inline void chkmax(T &a, const T &b) {if(a < b) a = b;}
int n, A, B, C;
int fac[10005], ifac[10005];
int Comb(int x, int y){
if(y < 0 || x < y) return 0;
return (LL)fac[x] * ifac[y] % P * ifac[x - y] % P;
}
int AA = 0;
int ask(int w){
//\sum{a[i]} = B / 2
//\sum{i * a[i]} = w
//sum of \prod{a[i]!}
if(B == 0) return w == 0;
return (LL)AA * Comb(B / 2 + w - 1, B / 2 - 1) % P;
}
int main(){
cin >> n >> A >> B >> C;
if(B & 1){
puts("0");
return 0;
}
ifac[0] = 1;
REP(i, 1, 10000) ifac[i] = (LL)ifac[i - 1] * Pow(i, P - 2, P) % P;
REP(i, 0, 10000) fac[i] = Pow(ifac[i], P - 2, P);
AA = ifac[B / 2];
int ans = 0;
REP(a, 0, A) for(int b = 0; 3 * b <= C; b ++){
int ww = C - A + a - 3 * b;
if(ww < 0) continue;
int cf = (LL)fac[A + b + B / 2] * ifac[a] % P * ifac[A - a] % P * ifac[b] % P;
cf = (LL)cf * ask(ww) % P;
ans = (ans + cf) % P;
}
printf("%d\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M;
cout << abs(N*M - (N+N+M+M-4)) << "\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-6;
const int INF = 1000000000;
int gauss(vector<vector<long double> > a, vector<long double>& ans) {
int n = (int)a.size();
int m = (int)a[0].size() - 1;
vector<int> where(m, -1);
for (int col = 0, row = 0; col < m && row < n; ++col) {
int sel = row;
for (int i = row; i < n; ++i)
if (abs(a[i][col]) > abs(a[sel][col])) sel = i;
if (abs(a[sel][col]) < EPS) continue;
for (int i = col; i <= m; ++i) swap(a[sel][i], a[row][i]);
where[col] = row;
for (int i = 0; i < n; ++i)
if (i != row) {
long double c = a[i][col] / a[row][col];
for (int j = col; j <= m; ++j) a[i][j] -= a[row][j] * c;
}
++row;
}
ans.assign(m, 0);
for (int i = 0; i < m; ++i)
if (where[i] != -1) ans[i] = a[where[i]][m] / a[where[i]][i];
for (int i = 0; i < n; ++i) {
long double sum = 0;
for (int j = 0; j < m; ++j) sum += ans[j] * a[i][j];
if (abs(sum - a[i][m]) > EPS) return 0;
}
for (int i = 0; i < m; ++i)
if (where[i] == -1) return INF;
return 1;
}
int n, m;
int deg[1 << 20];
int main() {
scanf("%d %d", &n, &m);
memset(deg, 0, sizeof(deg));
for (int i = 0; i < m; ++i) {
int a, b;
scanf("%d %d", &a, &b);
--a;
--b;
++deg[a];
++deg[b];
}
vector<vector<long double> > a(4);
vector<long double> ans;
a[0] = vector<long double>(5);
a[0][0] = a[0][1] = a[0][2] = a[0][3] = 1;
a[0][4] = (long double)n * (n - 1) * (n - 2) / 6;
a[1] = vector<long double>(5);
a[1][0] = 0;
a[1][1] = 1;
a[1][2] = 2;
a[1][3] = 3;
a[1][4] = (long double)m * (n - 2);
a[2] = vector<long double>(5);
a[2][0] = 3;
a[2][1] = 2;
a[2][2] = 1;
a[2][3] = 0;
a[2][4] = (long double)(n - 2) * ((long double)n * (n - 1) / 2 - m);
long double sm = 0;
for (int i = 0; i < n; ++i) {
sm += (long double)deg[i] * (deg[i] - 1) / 2;
}
a[3] = vector<long double>(5);
a[3][0] = a[3][1] = 0;
a[3][2] = 1;
a[3][3] = 3;
a[3][4] = sm;
int k = gauss(a, ans);
if (k != 1) {
}
long long tot = (long long)n * (long long)(n - 1) * (long long)(n - 2) / 6;
long long sl = (long long)(ans[1] + ans[2] + ans[3] + 1e-6);
sl = tot - sl;
printf("%lld\n", sl);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
vector<long long> G[maxn];
vector<long long> G1[maxn];
long long n, m, k;
long long x, y;
vector<long long> p;
vector<long long> d;
const long long INF = 1e9;
void dijk() {
priority_queue<pair<long long, long long>,
vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
pq;
long long u = p.size();
u = p[u - 1];
d[u] = 0;
pq.push({0, u});
while (!pq.empty()) {
pair<long long, long long> a = pq.top();
pq.pop();
if (d[a.second] < a.first) {
continue;
}
for (long long i = 0; i < G1[a.second].size(); i++) {
if (d[G1[a.second][i]] > d[a.second] + 1) {
d[G1[a.second][i]] = d[a.second] + 1;
pq.push({d[G1[a.second][i]], G1[a.second][i]});
}
}
}
}
signed main() {
scanf("%lld", &(n));
scanf("%lld", &(m));
for (long long i = 0; i < m; i++) {
scanf("%lld", &(x));
scanf("%lld", &(y));
G[x].push_back(y);
G1[y].push_back(x);
}
d.resize(n + 1);
fill(d.begin(), d.end(), INF);
scanf("%lld", &(k));
for (long long i = 0; i < k; i++) {
scanf("%lld", &(x));
p.push_back(x);
}
dijk();
long long ma = 0, mi = 0, r;
for (long long i = 0; i + 1 < p.size(); i++) {
x = p[i];
long long ans = 0;
for (long long j = 0; j < G[x].size(); j++) {
y = G[x][j];
if (d[x] == d[y] + 1) {
ans++;
r = y;
}
}
if (ans == 1) {
if (r == p[i + 1]) {
} else {
mi++;
ma++;
}
} else {
if (d[r] < d[p[i + 1]]) {
mi++;
ma++;
} else if (d[r] == d[p[i + 1]]) {
ma++;
}
}
}
cout << mi << ' ' << ma << '\n';
}
| 4 |
Subsets and Splits