solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<bits/stdc++.h>
using namespace std;
int main(){
long long a;
cin>>a;
if(a%2==1) a=a*2;
cout<<a;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double EPS = 1e-13;
long long n, m, rnk[102], rsum;
double prob[102][100005];
double sm[100005];
double ans;
void solve() {
scanf("%lld%lld", &n, &m);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &rnk[i]);
rsum += rnk[i];
}
prob[0][0] = 1;
for (long long i = 1; i <= n; i++) {
if (i == 1)
sm[0] = 1.0;
else
sm[0] = 0.0;
for (long long j = 1; j <= n * m; j++) sm[j] = sm[j - 1] + prob[i - 1][j];
long long R = rnk[i];
double tmp = 0.0;
for (long long j = i; j <= n * m; j++) {
prob[i][j] = sm[j - 1] - (j - m > 0 ? (sm[j - m - 1]) : 0) -
(j - R >= 0 ? prob[i - 1][j - R] : 0);
prob[i][j] /= (m - 1);
tmp += prob[i][j];
}
}
for (long long i = n; i < rsum; i++) ans += prob[n][i];
ans *= (m - 1);
ans += 1.0 + EPS;
printf("%.10f", ans);
return;
}
signed main() {
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int ans[MAXN];
int main() {
int n, M, T;
scanf("%d%d%d", &n, &M, &T);
vector<int> pts;
for (int i = 0; i < n; i++) {
int h, m, s, r;
scanf("%2d:%2d:%2d", &h, &m, &s);
r = h * 60 * 60 + m * 60 + s;
pts.push_back(r);
}
sort(pts.begin(), pts.end());
deque<pair<int, int>> cur;
int r = 0;
bool mx_reached = false;
for (int i = 0; i < n; i++) {
while (!cur.empty() && cur.front().first + T <= pts[i]) {
cur.pop_front();
}
if (cur.size() < M) {
ans[i] = ++r;
cur.push_back(make_pair(pts[i], r));
if (cur.size() == M) mx_reached = true;
} else {
auto item = cur.back();
cur.pop_back();
item.first = pts[i];
cur.push_back(item);
ans[i] = item.second;
}
}
if (!mx_reached) {
printf("No solution");
} else {
printf("%d\n", r);
for (int i = 0; i < n; i++) {
printf("%d\n", ans[i]);
}
}
}
| 4 |
#include "bits/stdc++.h"
using namespace std;
int count(int y, int m, int d) {
int sum = 0;
for (int i = 1; i < y; i++) {
if (i % 3 == 0) sum += 20 * 10;
else sum += (20 + 19) * 5;
}
for (int i = 1; i < m; i++) {
if (y % 3 == 0) sum += 20;
else if (i % 2 == 1) sum += 20;
else sum += 19;
}
sum += (d - 1);
return sum;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int y,m, d;
scanf("%d %d %d", &y, &m, &d);
printf("%d\n", count(1000, 1, 1) - count(y, m, d));
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
long long n;
string a, b;
long long A[N];
long long B[N];
vector<pair<long long, long long> > ans;
long long root;
long long aum;
void dfs(long long pos) {
long long add = aum * ((pos - root) % 2 == 0 ? 1 : -1);
if (root != pos) {
if (ans.size() == N - 10) return;
if (!(0 <= A[pos + 1] + add && A[pos + 1] + add <= 9)) {
dfs(pos + 1);
}
if (ans.size() == N - 10) return;
A[pos] += add;
A[pos + 1] += add;
ans.push_back({pos + 1, add});
} else {
while (A[pos] != B[pos]) {
if (ans.size() == N - 10) return;
if (!(0 <= A[pos + 1] + add && A[pos + 1] + add <= 9)) {
dfs(pos + 1);
}
if (ans.size() == N - 10) return;
A[pos] += add;
A[pos + 1] += add;
ans.push_back({pos + 1, add});
}
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(10);
cout << fixed;
cin >> n >> a >> b;
for (long long i = 0; i < n; i++) {
A[i] = a[i] - '0';
}
for (long long i = 0; i < n; i++) {
B[i] = b[i] - '0';
}
long long ant = 0;
long long rpta = 0;
for (long long i = 0; i < n - 1; i++) {
ant = B[i] - (A[i] + ant);
rpta += abs(ant);
}
if (ant + A[n - 1] != B[n - 1]) {
cout << -1 << endl;
return 0;
}
for (long long i = 0; i < n - 1; i++) {
if (A[i] != B[i]) {
aum = B[i] - A[i];
aum = aum / abs(aum);
root = i;
dfs(i);
}
}
cout << rpta << endl;
for (pair<long long, long long> x : ans) {
cout << x.first << " " << x.second << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<string> pril, sush, glag;
void Init() {
pril.push_back("lios");
pril.push_back("liala");
sush.push_back("etr");
sush.push_back("etra");
glag.push_back("initis");
glag.push_back("inites");
}
pair<int, bool> Check(string str) {
pair<int, bool> res;
for (int i = 0; i < 2; i++) {
if (str.length() >= pril[i].length() &&
str.substr(str.length() - pril[i].length(), pril[i].length()) ==
pril[i])
return make_pair(0, i);
}
for (int i = 0; i < 2; i++) {
if (str.length() >= sush[i].length() &&
str.substr(str.length() - sush[i].length(), sush[i].length()) ==
sush[i])
return make_pair(1, i);
}
for (int i = 0; i < 2; i++) {
if (str.length() >= glag[i].length() &&
str.substr(str.length() - glag[i].length(), glag[i].length()) ==
glag[i])
return make_pair(2, i);
}
return make_pair(-1, 0);
}
int main() {
Init();
int f[3];
for (int i = 0; i < 3; i++) f[i] = 0;
char s;
string str;
bool inStr = false, sol = true;
int type = -1;
int step = -1;
int res = 0;
bool pol = false;
while ((s = getchar()) != EOF) {
if (s >= 'a' && s <= 'z') {
if (!inStr) inStr = true;
str.push_back(s);
} else if (inStr) {
inStr = false;
pair<int, bool> r = Check(str);
if (r.first == -1) {
sol = false;
break;
}
if (type == -1) {
type = r.first;
pol = r.second;
f[type]++;
} else {
if (pol != r.second)
sol = false;
else if (type + 1 == r.first) {
type = r.first;
f[type]++;
} else if (type == r.first) {
if (type == 1) sol = false;
f[type]++;
} else
sol = false;
if (!sol) break;
}
str.clear();
}
}
if (!sol)
printf("NO");
else {
if (inStr) {
inStr = false;
pair<int, bool> r = Check(str);
if (r.first == -1) {
sol = false;
}
if (type == -1) {
type = r.first;
pol = r.second;
f[type]++;
} else {
if (pol != r.second)
sol = false;
else if (type + 1 == r.first) {
type = r.first;
f[type]++;
} else if (type == r.first) {
if (type == 1) sol = false;
f[type]++;
} else
sol = false;
}
str.clear();
}
int sum = 0;
for (int i = 0; i < 3; i++) sum += f[i];
if (sum == 1)
printf("YES");
else {
if (sum == f[0] || sum == f[1] || sum == f[2])
printf("NO");
else
printf("YES");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, d, h;
cin >> n >> d >> h;
if (d > (2 * h))
cout << "-1" << endl;
else if (d == 1 && h == 1 && n > 2)
cout << "-1" << endl;
else {
for (int i = (h + 1); i >= 2; i--) {
cout << i << " " << i - 1 << endl;
}
long long int k = (d - h - 1);
long long int x = n, f = 0, fl = 0;
while (k > 0) {
cout << x << " " << x - 1 << endl;
x--;
k--;
fl = 1;
}
for (int i = (h + 2); i <= (n - (max(f, (d - h)))); i++) {
cout << i << " " << h << endl;
}
if (d - h >= 1) cout << x << " " << 1 << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long L = 21;
const long long N = 2e5 + 7;
const long long INF = 1e9 + 1;
const long long mod = 1e9 + 7;
int n;
pair<int, int> a[N];
int get(int d, int st) {
vector<int> res;
if (st == 2) {
res.push_back(a[1].second);
}
for (int i = st + 1; i <= n; i++) {
if (a[i].first - a[st].first == d) {
st = i;
} else {
res.push_back(a[i].second);
}
}
if (res.size() > 1) {
return -1;
}
if (res.size() == 1) {
return res[0];
}
return a[n].second;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
a[i].second = i;
}
if (n == 3) {
cout << 1;
return 0;
}
sort(a + 1, a + n + 1);
vector<int> res;
res.push_back(get(a[2].first - a[1].first, 1));
res.push_back(get(a[3].first - a[1].first, 1));
res.push_back(get(a[3].first - a[2].first, 2));
for (auto x : res) {
if (x != -1) {
cout << x << endl;
return 0;
}
}
cout << -1;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 20;
const double eps = 1e-8;
int a[N];
long long l[N];
long long r[N];
long long t[N];
map<int, int> q;
int main() {
long long n;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d", a + i);
long long ans = 0;
for (int i = 1; i <= n; i++) {
l[i] = q[a[i]];
for (int j = 0; j < 31; j++) {
if ((a[i] & (1LL << j)))
t[j] = i;
else
l[i] = max(l[i], t[j]);
}
q[a[i]] = i;
}
for (int i = 0; i < 31; i++) t[i] = n + 1;
for (int i = n; i >= 0; i--) {
r[i] = n + 1;
for (int j = 0; j < 31; j++) {
if ((a[i] & (1LL << j)))
t[j] = i;
else
r[i] = min(r[i], t[j]);
}
}
for (int i = 1; i <= n; i++) {
ans -= (i - l[i]) * (r[i] - i);
}
ans += n * (n + 1LL) / 2LL;
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
int inp() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while (c >= '0' && c <= '9') {
sum = sum * 10 + c - '0';
c = getchar();
}
return sum;
}
struct Node {
int a;
int l;
} s[110];
bool cmp(Node a, Node b) { return a.a < b.a; }
int f[110][110][2];
int main() {
int n = inp();
for (int i = 1; i <= n; i++) {
s[i].a = inp();
s[i].l = inp();
}
std::sort(s + 1, s + n + 1, cmp);
s[0].a = -2147483647;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= i; j++) {
for (int u = 0; u < 2; u++) {
int r = s[j].a + (u * s[j].l);
int max = -2147483647;
std::pair<int, int> p;
for (int k = i + 1; k <= n; k++) {
for (int l = 0; l < 2; l++) {
int rb = s[k].a + (l * s[k].l);
int lb = std::max(r, s[k].a - (l ^ 1) * s[k].l);
if (rb > max) {
max = rb;
p = std::make_pair(k, l);
}
f[k][p.first][p.second] =
std::max(f[k][p.first][p.second], f[i][j][u] + max - lb);
}
}
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= i; j++)
for (int u = 0; u < 2; u++) ans = std::max(ans, f[i][j][u]);
printf("%d\n", ans);
}
| 5 |
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int n;
const int MAX = 32768;
int solve(int sum,int cnt,int num){
if(sum == n) return 1;
if(sum+num*num > n|| cnt == 4) return 0;
int res = 0;
res+=solve(sum+num*num,cnt+1,num);
res+=solve(sum,cnt,num+1);
return res;
}
int main(){
while(cin >> n && n != 0){
cout << solve(0,0,1) << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, base[20];
base[0] = 1;
for (int i = 0; i < 18; i++) base[i + 1] = base[i] * 10;
while (cin >> a >> b) {
long long ans = a, now = a, t = a;
int c = 0;
while (a - now <= b) {
ans = now;
c++;
if (t < 10) break;
if (t % 10 == 9) {
t /= 10;
continue;
}
t /= 10;
t--;
now = base[c] - 1 + base[c] * t;
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>
#include <bitset>
#include <stack>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = array<int, 2>;
using Q = array<P, 3>;
using D = array<int, 64>;
D resD;
bool dfs(vector<Q> node, D data) {
int m = (int)node.size();
if (!m) {
resD = data;
return true;
}
int now = -1;
D d = D();
for (int i = 0; i < m; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
if (!data[node[i][j][k]]) {
d[node[i][j][k] & ~(32)]++;
}
}
}
}
int ma = -1;
for (int i = 0; i < 32; i++) {
if (ma <= d[i]) {
ma = d[i];
now = i;
}
}
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < 3; j++) {
// for (int k = 0; k < 2; k++) {
// if (!data[node[i][j][k]]) {
// now = node[i][j][k];
// }
// }
// }
// }
assert(now != -1);
{
//now = true
data[now] = 1;
data[now ^ 32] = -1;
vector<Q> n_node;
bool err = false;
for (int i = 0; i < m; i++) {
bool ok = false;
int ec = 0;
for (int j = 0; j < 3; j++) {
if (data[node[i][j][0]] == 1 and data[node[i][j][1]] == 1) {
ok = true;
break;
}
if (data[node[i][j][0]] == -1 or data[node[i][j][1]] == -1) {
ec++;
}
}
if (ok) continue;
if (ec == 3) {
err = true;
break;
}
n_node.push_back(node[i]);
}
if (!err and dfs(n_node, data)) return true;
}
{
//now = false
data[now] = -1;
data[now ^ 32] = 1;
vector<Q> n_node;
bool err = false;
for (int i = 0; i < m; i++) {
bool ok = false;
int ec = 0;
for (int j = 0; j < 3; j++) {
if (data[node[i][j][0]] == 1 and data[node[i][j][1]] == 1) {
ok = true;
break;
}
if (data[node[i][j][0]] == -1 or data[node[i][j][1]] == -1) {
ec++;
}
}
if (ok) continue;
if (ec == 3) {
err = true;
break;
}
n_node.push_back(node[i]);
}
if (!err and dfs(n_node, data)) return true;
}
return false;
}
bool solve() {
int m;
cin >> m;
if (!m) return false;
vector<Q> v(m);
for (int i = 0; i < 3; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
for (int k = 0; k < 2; k++) {
if ('A' <= s[3*j+1+k] && s[3*j+1+k] <= 'Z') {
v[j][i][k] = s[3*j+1+k] - 'A';
}
if ('a' <= s[3*j+1+k] && s[3*j+1+k] <= 'z') {
v[j][i][k] = s[3*j+1+k] - 'a' + 32;
}
}
}
}
auto u = dfs(v, D());
if (!u) {
cout << "-1" << endl;
return true;
}
vector<char> result;
for (int i = 0; i < 32; i++) {
if (resD[i+32] == 1) {
result.push_back(i + 'A');
}
}
cout << result.size() << " ";
for (char c: result) {
cout << c << " ";
}
cout << endl;
return true;
}
int main() {
ios::sync_with_stdio(false);
while (solve()) {}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
void Per(string s, int &x, int &y) {
stringstream ss;
ss << (s[0] - 'a');
x = atoi(ss.str().c_str()) + 1;
ss.str("");
ss << s[1];
y = 8 - atoi(ss.str().c_str()) + 1;
}
bool Prov(int x, int y) {
if (a[y][x] == 0)
return true;
else {
if (a[y][x + 1] == 0) return true;
if (a[y][x - 1] == 0) return true;
if (a[y + 1][x] == 0) return true;
if (a[y + 1][x + 1] == 0) return true;
if (a[y + 1][x - 1] == 0) return true;
if (a[y - 1][x] == 0) return true;
if (a[y - 1][x + 1] == 0) return true;
if (a[y - 1][x - 1] == 0) return true;
if ((a[y][x + 1] > 3) && (a[y][x + 1] != 9)) return true;
if ((a[y][x - 1] > 3) && (a[y][x - 1] != 9)) return true;
if ((a[y + 1][x] > 3) && (a[y + 1][x] != 9)) return true;
if ((a[y + 1][x + 1] > 3) && (a[y + 1][x + 1] != 9)) return true;
if ((a[y + 1][x - 1] > 3) && (a[y + 1][x - 1] != 9)) return true;
if ((a[y - 1][x] > 3) && (a[y - 1][x] != 9)) return true;
if ((a[y - 1][x + 1] > 3) && (a[y - 1][x + 1] != 9)) return true;
if ((a[y - 1][x - 1] > 3) && (a[y - 1][x - 1] != 9)) return true;
return false;
}
}
int main() {
int i, j;
for (i = 0; i < 10; i++)
for (j = 0; j < 10; j++) a[i][j] = 0;
int x1, x2, x3, x4, y1, y2, y3, y4;
string s;
cin >> s;
Per(s, x1, y1);
cin >> s;
Per(s, x2, y2);
cin >> s;
Per(s, x3, y3);
cin >> s;
Per(s, x4, y4);
a[y3][x3] = 9;
for (i = y1; i < 10; i++) {
if (a[i][x1] == 9)
break;
else
a[i][x1] = 1;
}
for (i = y1; i > -1; i--) {
if (a[i][x1] == 9)
break;
else
a[i][x1] = 1;
}
for (i = x1; i < 10; i++) {
if (a[y1][i] == 9)
break;
else
a[y1][i] = 1;
}
for (i = x1; i > -1; i--) {
if (a[y1][i] == 9)
break;
else
a[y1][i] = 1;
}
a[y1][x1] = 7;
for (i = y2; i < 10; i++) {
if (a[i][x2] == 9)
break;
else
a[i][x2] = 1;
}
for (i = y2; i > -1; i--) {
if (a[i][x2] == 9)
break;
else
a[i][x2] = 1;
}
for (i = x2; i < 10; i++) {
if (a[y2][i] == 9)
break;
else
a[y2][i] = 1;
}
for (i = x2; i > -1; i--) {
if (a[y2][i] == 9)
break;
else
a[y2][i] = 1;
}
a[y2][x2] = 8;
if (((x1 == x2) && ((x3 > x1) && (x3 > x2) || (x3 < x1) && (x3 < x2))) ||
((y1 == y2) && ((y3 > y1) && (y3 > y2) || (y3 < y1) && (y3 < y2)))) {
a[y1][x1] = 9;
a[y2][x2] = 9;
}
a[y3][x3 + 1] = 9;
a[y3][x3 - 1] = 9;
a[y3 + 1][x3] = 9;
a[y3 + 1][x3 + 1] = 9;
a[y3 + 1][x3 - 1] = 9;
a[y3 - 1][x3] = 9;
a[y3 - 1][x3 + 1] = 9;
a[y3 - 1][x3 - 1] = 9;
for (i = 0; i < 10; i++) {
a[0][i] = -1;
a[9][i] = -1;
a[i][0] = -1;
a[i][9] = -1;
}
bool f = Prov(x4, y4);
if (f)
cout << "OTHER\n";
else
cout << "CHECKMATE\n";
return 0;
}
| 2 |
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <queue>
typedef long long ll;
const ll LL_MAX (1LL<<60);
#define rep(i,s,e) for(ll i=(s); i<(e); i++)
using namespace std;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
ll cnt(0);
for(ll i=0; i<(1<<n); i++)
{
cout << cnt << ":";
rep(j,0,n)
if ((i & (1<<j)) > 0)
cout << " " << j;
cnt++;
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 * 1000 + 10;
bool mark[MAXN];
int has[MAXN];
vector<int> adj[MAXN];
vector<pair<int, pair<int, int> > > ans;
void dfs(int x) {
mark[x] = true;
vector<int> t, check;
for (__typeof((adj[x]).begin()) i = (adj[x]).begin(); i != (adj[x]).end();
i++)
if (!mark[*i]) check.push_back(*i);
for (__typeof((check).begin()) i = (check).begin(); i != (check).end(); i++) {
int v = *i;
if (!mark[v]) dfs(v);
if (has[v] != -1) {
ans.push_back(make_pair(x, make_pair(v, has[v])));
has[v] = -1;
} else
t.push_back(v);
}
while (((int)(t).size()) > 1) {
int a = t.back();
t.pop_back();
int b = t.back();
t.pop_back();
ans.push_back(make_pair(a, make_pair(x, b)));
}
if (((int)(t).size()))
has[x] = t[0];
else
has[x] = -1;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
if (m & 1) {
printf("No solution\n");
return 0;
}
for (int i = 0; i < (m); i++) {
int x, y;
scanf("%d%d", &x, &y);
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(0);
for (int i = 0; i < (((int)(ans).size())); i++)
printf("%d %d %d\n", ans[i].first + 1, ans[i].second.first + 1,
ans[i].second.second + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<set<char>> groups;
bool check(const string& s1, const string& s2) {
if (s1.length() != s2.length()) return true;
int len = s1.length();
for (int i = 0; i < len; i++) {
bool b = false;
for (auto group : groups) {
if (group.find(s1[i]) != group.end() && group.find(s2[i]) != group.end())
b = true;
}
if (!b) return true;
}
return false;
}
int main() {
for (char c = '0'; c <= '9'; c++) {
set<char> g;
g.insert(c);
if (c == '0') g.insert('O'), g.insert('o');
if (c == '1') g.insert('l'), g.insert('L'), g.insert('I'), g.insert('i');
groups.emplace_back(g);
}
groups.emplace_back(set<char>());
groups.back().insert('_');
for (char c = 'a'; c <= 'z'; c++) {
groups.emplace_back(set<char>());
groups.back().insert(c);
groups.back().insert(c + 'A' - 'a');
}
string s;
cin >> s;
int n;
cin >> n;
bool ans = true;
for (int i = 0; i < n && ans; i++) {
string ss;
cin >> ss;
if (!check(s, ss)) ans = false;
}
cout << (ans ? "Yes" : "No") << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int32_t main() {
long long int n;
cin >> n;
vector<long long int> v;
long long int k = 1;
for (long long int i = 0; i <= 11; i++) {
v.push_back(k);
k *= 10;
}
long long int count = 0;
for (long long int i = 1; i < v.size(); i++) {
if (n >= v[i]) {
count += (v[i] - v[i - 1]) * (i);
} else {
count += (n - v[i - 1] + 1) * (i);
cout << count << endl;
return 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n, i, j, k;
cin >> n;
k = n - 1;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
i = 0;
while (i < n) {
long long mx = 1e9;
long long p = i;
for (j = i; j < n; j++) {
if (a[j] < mx) {
p = j;
mx = a[j];
}
}
j = p;
for (; j > i; j--) {
swap(a[j], a[j - 1]);
}
if (p != i)
i = p;
else
i++;
if (i == n - 1) break;
}
for (auto x : a) cout << x << " ";
cout << endl;
;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
int n1, m1, n2, m2;
int ar1[60][60], ar2[60][60];
void oku() {
int i, j;
char c;
scanf(" %d %d", &n1, &m1);
for (i = 1; i <= n1; i++)
for (j = 1; j <= m1; j++) {
scanf(" %c", &c);
ar1[i][j] = c - '0';
}
scanf(" %d %d", &n2, &m2);
for (i = 1; i <= n2; i++)
for (j = 1; j <= m2; j++) {
scanf(" %c", &c);
ar2[i][j] = c - '0';
}
}
void islem() {
int i, j, k, l, x, y, max = -1, say, tuti, tutj;
for (x = -100; x <= 100; x++)
for (y = -100; y <= 100; y++) {
say = 0;
for (i = 1; i <= n1; i++)
for (j = 1; j <= m1; j++)
if (i + x <= n2 && j + y <= m2 && i + x >= 1 && j + y >= 1)
say += (ar1[i][j] * ar2[i + x][j + y]);
if (say > max) {
max = say;
tuti = x;
tutj = y;
}
}
printf("%d %d\n", tuti, tutj);
}
int main() {
oku();
islem();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while (T--) {
int n, m;
cin >> n >> m;
vector<pair<int, int>> v;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
sort(v.begin(), v.end(), [](pair<int, int> &p1, pair<int, int> &p2) {
int x = p1.second - p1.first + 1;
int y = p2.second - p2.first + 1;
if (x < y) return true;
if (x > y) return false;
return (p1.first < p2.first);
});
int res = v[0].second - v[0].first + 1;
cout << res << endl;
int a[n + 1], j = 0;
memset(a, -1, sizeof a);
int c = 0;
for (int i = 1; i <= n; i++) {
cout << c % res << " ";
c++;
}
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long A, B, l, t, m;
long long t1;
inline bool possible(long long z) {
long long tn = t1 + (z - 1) * B;
return (z * (t1 + tn) / 2 <= min(m, z) * t && tn <= t);
}
int main() {
int n;
cin >> A >> B >> n;
for (int i = 0; i < n; i++) {
cin >> l >> t >> m;
long long z;
t1 = A + (l - 1) * B;
long long left = 1, right = 1500000;
while (left <= right) {
z = (left + right) / 2;
if (z == 0 || !possible(z) && possible(z - 1)) {
break;
} else if (possible(z))
left = z + 1;
else
right = z - 1;
}
if (z <= 1)
cout << -1 << endl;
else
cout << z + l - 2 << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int ID(char x) { return x - '0'; }
struct trie {
public:
int root = 0;
int cnt = 0;
vector<int> val;
vector<vector<int>> ch;
trie() {
ch.push_back(vector<int>(2, -1));
val.push_back(0);
cnt = 0;
}
void add(string s) {
int now = root;
for (int i = (0); i < (s.length()); i++) {
int x = ID(s[i]);
if (ch[now][x] == -1) {
ch[now][x] = ++cnt;
ch.push_back(vector<int>(2, -1));
val.push_back(0);
}
now = ch[now][x];
}
val[now]++;
}
int match(string s) {
int now = root;
for (int i = (0); i < (s.length()); i++) {
int x = ID(s[i]);
if (ch[now][x] == -1) return 0;
if ((now = ch[now][x]) == -1) return 0;
}
if (val[now]) return 1;
return 0;
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
int n;
cin >> n;
trie rt = trie();
vector<string> cs;
int zz, oo, zo, oz;
zz = oo = zo = oz = 0;
for (int i = (0); i < (n); i++) {
string s;
cin >> s;
rt.add(s);
if (s.back() == s[0]) {
if (s[0] == '0')
zz++;
else
oo++;
} else {
if (s.back() == '0')
oz++;
else
zo++;
}
reverse(s.begin(), s.end());
cs.push_back(s);
}
int canzo = 0;
int canoz = 0;
vector<int> can[2];
for (int i = (0); i < (n); i++) {
string x = cs[i];
if (x.back() == x[0]) continue;
if (rt.match(x) == 0) {
if (x.back() == '0')
canzo++, can[0].push_back(i);
else
canoz++, can[1].push_back(i);
}
}
if (zo == 0 && oz == 0) {
if (oo != 0 && zz != 0)
cout << -1 << endl;
else
cout << 0 << endl;
} else {
if (zo == oz)
cout << 0 << endl;
else {
if (zo > oz) {
zo -= oz;
if (canzo >= zo) {
cout << zo / 2 << endl;
for (int i = (0); i < (zo / 2); i++) cout << can[0][i] + 1 << " ";
cout << endl;
} else
cout << -1 << endl;
} else {
oz -= zo;
if (canoz >= oz) {
cout << oz / 2 << endl;
for (int i = (0); i < (oz / 2); i++) cout << can[1][i] + 1 << " ";
cout << endl;
} else
cout << -1 << endl;
}
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1, M = 4e5 + 2;
int n;
int b[N], c[N];
long long d[1000000];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> b[i], c[i] = i - b[i], d[c[i] + M] += 1ll * b[i];
long long ans = 0;
for (int i = 1; i <= n; ++i) ans = max(ans, d[c[i] + M]);
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int mat[111][111];
int main() {
int n, k, i, j;
scanf("%d", &n);
scanf("%d", &k);
memset(mat, 0, sizeof(mat));
if (k > n * n)
cout << "-1" << endl;
else {
if (k != 0) {
for (i = 0; i < n; i++) {
if (k >= (2 * (n - i) - 1)) {
for (j = i; j < n; j++) {
mat[i][j] = 1;
mat[j][i] = 1;
}
k = k - (2 * (n - i) - 1);
} else {
mat[i][i] = 1;
k--;
for (j = i + 1; j < n; j++) {
if (k == 0) break;
if (k == 1) {
k--;
mat[i + 1][i + 1] = 1;
break;
} else {
mat[i][j] = 1;
mat[j][i] = 1;
k -= 2;
}
}
}
if (k == 0) break;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) cout << mat[i][j] << " ";
cout << endl;
}
cout << endl;
}
return 0;
}
| 1 |
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
bool st[N];
int main(){
int n,k,r,s,p;
cin>>n>>k>>r>>s>>p;
char t[N];
cin>>t;
int sum=0;
for(int i=0;i<n;i++){
if(st[i]==false){
if(i+k<n && t[i]==t[i+k]){
st[i+k]=true;
}
if(t[i]=='r') sum+=p;
else if(t[i]=='s') sum+=r;
else if(t[i]=='p') sum+=s;
}
}
cout<<sum<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long k, c[1002], a[1002][1002], sum = 0, f[1002];
int main() {
a[0][1] = 1;
a[1][1] = 1;
a[1][2] = 2;
for (long long i = 2; i < 1002; i++) {
a[0][i] = 1;
a[1][i] = i;
for (long long j = 1; j <= i; j++) {
a[j][i] = (a[j - 1][i - 1] + a[j][i - 1]) % 1000000007;
}
}
scanf("%I64d", &k);
for (long long i = 1; i <= k; i++) {
scanf("%I64d", &c[i]);
}
sum = c[1];
f[1] = 1;
for (long long i = 2; i <= k; i++) {
if (sum > 1)
f[i] = (f[i - 1] * a[sum][sum + c[i] - 1]) % 1000000007;
else if (sum == 1)
f[i] = (f[i - 1] * (c[i])) % 1000000007;
sum += c[i];
}
printf("%I64d\n", f[k]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 9;
long long sum[N];
vector<pair<long long, long long>> seg[4 * N];
bool remo(long long rt) {
long long l = seg[rt].size();
if (l < 3) return false;
long long m1, m2, m3, c1, c2, c3;
m3 = seg[rt][l - 1].first;
c3 = seg[rt][l - 1].second;
m2 = seg[rt][l - 2].first;
c2 = seg[rt][l - 2].second;
m1 = seg[rt][l - 3].first;
c1 = seg[rt][l - 3].second;
return (c3 - c1) * (m1 - m2) >= (c2 - c1) * (m1 - m3);
}
void add(long long rt, pair<long long, long long> x) {
long long m = x.first;
long long c = x.second * x.first - sum[x.second];
seg[rt].push_back({m, c});
while (remo(rt)) {
auto p = seg[rt].back();
seg[rt].pop_back();
seg[rt].pop_back();
seg[rt].push_back(p);
}
}
long long query(long long rt, long long x) {
long long l = 0, r = seg[rt].size() - 1;
while (r - l >= 2) {
long long md = (l + r) / 2;
long long y1 = seg[rt][md].first * x + seg[rt][md].second;
long long y2 = seg[rt][md + 1].first * x + seg[rt][md + 1].second;
if (y1 <= y2)
r = md;
else
l = md;
}
long long mn = LLONG_MAX;
while (l <= r) {
mn = min(mn, seg[rt][l].first * x + seg[rt][l].second);
l++;
}
return mn;
}
void update(long long rt, long long l, long long r,
pair<long long, long long> x) {
if (l > x.second || r < x.second) return;
add(rt, x);
if (l == r) return;
long long md = (l + r) / 2;
update(rt * 2, l, md, x);
update(rt * 2 + 1, md + 1, r, x);
}
long long query(long long rt, long long l, long long r, long long i,
long long j, long long x) {
if (l > j || r < i) return LLONG_MAX;
if (l >= i && r <= j) return query(rt, x);
long long md = (l + r) / 2;
return min(query(rt * 2, l, md, i, j, x),
query(rt * 2 + 1, md + 1, r, i, j, x));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<pair<long long, long long>> vc;
for (int i = 0; i < 4 * n; i++) seg[i].push_back({0, 0});
sum[0] = 0;
for (int i = 1; i <= n; i++) {
long long x;
cin >> x;
sum[i] = sum[i - 1] + x;
vc.push_back({x, i});
}
sort(vc.begin(), vc.end());
for (auto x : vc) update(1, 1, n, x);
long long q;
cin >> q;
while (q--) {
long long x, y;
cin >> x >> y;
cout << query(1, 1, n, y - x + 1, y, x - y) + sum[y] << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long k;
int q;
int solve(long long x) {
long long low = 1, high = 5e8, m, last, mx;
while (low <= high) {
m = (low + high) / 2;
long long p = 0, sum = 0, len = 1;
while (m - p > 0) {
long long l = m - p, r = max(0LL, m - p * 10 - 8);
sum += len * (r + l) * (l - r + 1) / 2;
p = p * 10 + 9;
len++;
}
if (sum <= x)
low = m + 1, last = m, mx = sum;
else
high = m - 1;
}
x -= mx;
if (x == 0) return last % 10;
low = 1, high = 5e8;
while (low <= high) {
m = (low + high) / 2;
long long len = 1, g = m, sum = 0, p = 9;
while (g > 0) {
sum += min(g, p) * len;
g -= p;
p = p * 10;
len++;
}
if (sum >= x)
high = m - 1, last = m, mx = sum;
else
low = m + 1;
}
while (mx > x) last /= 10, mx--;
return last % 10;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> q;
while (q--) {
long long k;
cin >> k;
cout << solve(k) << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
double a, d;
cin >> n >> a >> d;
double t[100005], v[100005];
for (int i = 0; i < n; i++) cin >> t[i] >> v[i];
double ans[100005];
for (int i = 0; i < n; i++) {
double cur = min(sqrt(2 * d / a), v[i] / a);
double val = t[i] + (d - .5 * a * cur * cur) / v[i] + cur;
if (!i)
ans[i] = val;
else
ans[i] = max(ans[i - 1], val);
}
for (int i = 0; i < n; i++)
cout << setprecision(10) << fixed << ans[i] << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100001, maxK = 100;
int T[maxN][maxK];
vector<int> G[maxN], vs[maxK];
queue<int> Q;
int main() {
int n, m, k, s;
scanf("%d%d%d%d", &n, &m, &k, &s);
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
vs[a - 1].push_back(i);
for (int j = 0; j < k; j++) T[i][j] = n + 1;
}
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < k; i++) {
for (int v : vs[i]) Q.push(v), T[v][i] = 0;
while (!Q.empty()) {
int v = Q.front();
Q.pop();
for (int u : G[v]) {
if (T[u][i] > T[v][i] + 1) {
T[u][i] = T[v][i] + 1;
Q.push(u);
}
}
}
}
for (int i = 1; i <= n; i++) {
int* p = &T[i][0];
sort(p, p + k);
int res = 0;
for (int j = 0; j < s; j++) res += p[j];
printf("%d ", res);
}
printf("\n");
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main(){
int a,cou;
while(cin>>a){
cou=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
for(int q=0;q<10;q++){
if(i+j+k+q==a)cou++;
}
}
}
}
cout<<cou<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >>N;
vector<int> a(N),b(N),c(20000000,0);
for(int i=0;i<N;i++){
cin >> a[i];
c[a[i]]++;
}
for(int i=1;i<c.size();i++){
c[i]+=c[i-1];
}
for(int i=N-1;i>=0;i--){
int x=c[a[i]];
b[x-1]=a[i];
c[a[i]]--;
}
for(int i=0;i<N;i++){
cout<<b[i];
if(i!=N-1)cout<<" ";
}
cout<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
class Graph {
public:
vector<vector<int> > adj;
int n;
int dab, maxdist, arg;
vector<bool> seen;
Graph() {}
Graph(int nn) {
n = nn;
seen.assign(n, false);
adj.resize(n);
}
void add(int a, int b) {
adj[a].push_back(b);
adj[b].push_back(a);
}
void dfs(int a, int b, int dist) {
if (seen[a]) return;
seen[a] = true;
if (a == b) dab = dist;
if (dist > maxdist) {
maxdist = dist;
arg = a;
}
for (auto v : adj[a]) {
dfs(v, b, dist + 1);
}
}
vector<int> dist(int a, int b) {
dab = 0, maxdist = 0, arg = -1;
seen.assign(n, false);
dfs(a, b, 0);
seen.assign(n, false);
dfs(arg, -1, 0);
return vector<int>{dab, maxdist};
}
};
void solve() {
int n, a, b, da, db;
cin >> n >> a >> b >> da >> db;
a--;
b--;
Graph g(n);
for (int i = (0); i < (n - 1); i++) {
int u, v;
cin >> u >> v;
u--;
v--;
g.add(u, v);
}
vector<int> ret = g.dist(a, b);
int dab = ret[0];
int dia = ret[1];
if (dab <= da) {
cout << "Alice\n";
} else if (dia <= 2 * da) {
cout << "Alice\n";
} else if (db <= 2 * da) {
cout << "Alice\n";
} else {
cout << "Bob\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _t;
cin >> _t;
while (_t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int Ans = 1;
vector<int> Ed[10001];
vector<bool> Vis(10001);
vector<int> Col(10001);
int N;
void dfs(int ind, int col) {
Vis[ind] = true;
if (Col[ind] != col) Ans++;
for (int i = 0; i != Ed[ind].size(); i++) {
if (!Vis[Ed[ind][i]]) dfs(Ed[ind][i], Col[ind]);
}
return;
}
int main(void) {
int n;
cin >> n;
for (int i = 0; i != n; i++) {
Ed[i].clear();
Vis[i] = false;
}
for (int i = 1; i != n; i++) {
int ed;
cin >> ed;
Ed[i].push_back(ed - 1);
Ed[ed - 1].push_back(i);
}
for (int i = 0; i != n; i++) cin >> Col[i];
dfs(0, Col[0]);
cout << Ans << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b, long long p) {
long long res = 1;
a %= p;
for (; b; b >>= 1) {
if (b & 1) res = res * a % p;
a = a * a % p;
}
return res;
}
template <typename T>
T gcd(T x, T y) {
while (y) {
T t = x % y;
x = y;
y = t;
}
return x;
}
template <typename T>
T lsM(T x, T y) {
return x / gcd(x, y) * y;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
char s[155];
int best[155][155], full[155][155], t[155][155][155], val[155], n;
int main() {
memset(full, -1, sizeof full);
memset(t, -1, sizeof t);
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
scanf("%s", s + 1);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) t[i][j][0] = 0;
for (int len = 1; len <= n; len++)
for (int l = 1; l + len - 1 <= n; ++l) {
int r = l + len - 1;
if (l == r) {
if (val[1] == -1) {
best[l][r] = 0;
full[l][r] = -1;
t[l][r][0] = -1;
t[l][r][1] = 0;
} else {
best[l][r] = val[1];
full[l][r] = val[1];
t[l][r][0] = val[1];
t[l][r][1] = 0;
}
} else {
chkmax(t[l][r][1], max(full[l][r - 1], full[l + 1][r]));
for (int i = l + 1; i < r; i++)
if (full[l][i - 1] != -1 && full[i + 1][r] != -1)
chkmax(t[l][r][1], full[l][i - 1] + full[i + 1][r]);
for (int i = 2; i <= len; i++) {
for (int j = l + 1; j <= r; j++) {
if (full[l][j - 1] != -1 && t[j][r][i] != -1)
chkmax(t[l][r][i], full[l][j - 1] + t[j][r][i]);
if (full[j][r] != -1 && t[l][j - 1][i] != -1)
chkmax(t[l][r][i], full[j][r] + t[l][j - 1][i]);
}
if (s[l] == s[r]) chkmax(t[l][r][i], t[l + 1][r - 1][i - 2]);
}
for (int i = 1; i <= len; i++)
if (val[i] != -1 && t[l][r][i] != -1)
chkmax(full[l][r], t[l][r][i] + val[i]);
t[l][r][0] = full[l][r];
chkmax(best[l][r], full[l][r]);
for (int i = l; i < r; i++)
chkmax(best[l][r], best[l][i] + best[i + 1][r]);
}
}
printf("%d\n", best[1][n]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100005], p;
long long ans;
vector<int> e[100005], b[2];
map<int, int> mp[2];
int dfs(int u, int d) {
int deep = d;
b[d & 1].push_back(a[u]);
mp[d & 1][a[u]]++;
for (int i = 0; i < (int)e[u].size(); ++i)
deep = max(deep, dfs(e[u][i], d + 1));
return deep;
}
int main(void) {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 2; i <= n; ++i) {
scanf("%d", &p);
e[p].push_back(i);
}
int d = dfs(1, 0);
int res = 0;
for (int i = 0; i < (int)b[d & 1].size(); ++i) res ^= b[d & 1][i];
if (res == 0) {
int tot = b[d & 1].size();
ans += (long long)tot * (tot - 1) / 2;
tot = b[!(d & 1)].size();
ans += (long long)tot * (tot - 1) / 2;
}
for (map<int, int>::iterator it = mp[!(d & 1)].begin();
it != mp[!(d & 1)].end(); ++it) {
int temp = res ^ it->first;
ans += (long long)(it->second) * mp[d & 1][temp];
}
printf("%I64d\n", ans);
}
| 5 |
#include <iostream>
#include <vector>
#include <queue>
using namespace std ;
int main(){
int n ; cin >> n ;
priority_queue<int> pq ;
long long ans = 0 ;
vector<vector<int>> vec(n,vector<int>(0)) ;
for(int i=0;i<n;i++){
int m ; cin >> m ;
for(int j=0;j<m;j++){
int a ; cin >> a ;
vec.at(i).push_back(a) ;
}
}
for(int i=n-1;i>=0;i--){
for(int j=0;j<vec.at(i).size();j++){
pq.push(vec.at(i).at(j)) ;
}
ans += pq.top() ; pq.pop() ;
}
cout << ans << endl ;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << "Mahmoud\n";
} else {
cout << "Ehab\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
int n;
cin >> n;
cout << n << " ";
for (int(i) = (2); (i) < (1000010); (i)++)
if (n % i == 0 && isprime(i))
while (n % i == 0) {
n /= i;
cout << n << ' ';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
long long a[200005], x;
struct heap {
long long data;
int indx;
} h[200005];
void heapify(int i) {
int l, r, s;
long long tmp;
l = 2 * i + 1;
r = 2 * i + 2;
s = i;
if (l < n && h[l].data < h[i].data) s = l;
if (r < n && h[r].data < h[s].data) s = r;
if (s != i) {
tmp = h[i].data;
h[i].data = h[s].data;
h[s].data = tmp;
tmp = h[i].indx;
h[i].indx = h[s].indx;
h[s].indx = tmp;
heapify(s);
}
}
void build() {
for (int i = 0; i < n; i++) {
h[i].data = llabs(a[i]);
h[i].indx = i;
}
for (int i = n / 2 - 1; i >= 0; i--) heapify(i);
}
int main() {
int z = 0, ng = 0;
scanf("%d%d%I64d", &n, &k, &x);
for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
for (int i = 0; i < n; i++) {
if (a[i] < 0)
ng++;
else if (a[i] == 0)
z++;
}
if (z != 0) {
if (ng % 2 == 0) {
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
a[i] = -1 * x;
ng++;
k--;
z--;
break;
}
}
}
if (k < z) {
for (int i = 0; i < n; i++) printf("%I64d ", a[i]);
return 0;
} else {
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
a[i] = x;
k--;
z--;
}
}
}
}
build();
if (ng % 2 == 0) {
int s = 1, in;
in = h[0].indx;
s = s * (a[in] / llabs(a[in]));
while (k) {
if (x > h[0].data) {
s = -1 * s;
h[0].data = x - h[0].data;
a[in] = s * h[0].data;
k--;
break;
} else {
h[0].data = h[0].data - x;
a[in] = s * h[0].data;
k--;
}
}
}
heapify(0);
while (k) {
int in = h[0].indx, s;
s = a[in] / llabs(a[in]);
h[0].data += x;
a[in] = s * h[0].data;
k--;
heapify(0);
}
for (int i = 0; i < n; i++) printf("%I64d ", a[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct domino {
int u;
int l;
int uh;
int lh;
};
int main() {
int n, eu = 0, ou = 0, el = 0, ol = 0;
cin >> n;
int sum1 = 0, sum2 = 0;
struct domino a[101];
for (int i = 0; i < n; i++) {
cin >> a[i].u >> a[i].l;
sum1 += a[i].u;
sum2 += a[i].l;
if (a[i].u % 2 == 0) {
eu++;
a[i].uh = 0;
} else if (a[i].u % 2 == 1) {
ou++;
a[i].uh = 1;
}
if (a[i].l % 2 == 0) {
el++;
a[i].lh = 0;
}
if (a[i].l % 2 == 1) {
ol++;
a[i].lh = 1;
}
}
if (sum1 % 2 == 0 && sum2 % 2 == 0) {
cout << 0;
return 0;
}
if (ou % 2 == 1 && ol % 2 == 0) {
for (int i = 0; i < n; i++)
if (a[i].uh != a[i].lh) {
swap(a[i].u, a[i].l);
break;
}
} else if (ol % 2 == 1 && ou % 2 == 0) {
for (int i = 0; i < n; i++)
if (a[i].uh != a[i].lh) {
swap(a[i].u, a[i].l);
break;
}
} else if (ol % 2 == 1 && ou % 2 == 1) {
for (int i = 0; i < n; i++)
if (a[i].uh != a[i].lh) {
swap(a[i].u, a[i].l);
break;
}
}
sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++) {
sum1 += a[i].u;
sum2 += a[i].l;
}
if (sum1 % 2 == 0 && sum2 % 2 == 0) {
cout << 1;
return 0;
}
cout << -1;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
class FLCR {
public:
inline long long int mul(long long int a, long long int b) {
return (1LL * a * b) % 1000000007LL;
}
inline long long int nc2(long long int n) {
return mul(mul(n, n - 1), (1000000007LL + 1) / 2);
}
inline long long int add(long long int a, long long int b) {
return (a + b) % 1000000007LL;
}
inline long long int sq(long long int a) { return mul(a, a); }
long long int fact[1000011LL], ifact[1000011LL];
void process() {
fact[0] = ifact[0] = 1LL;
for (long long int i = 1; i < 1000011LL; i++) fact[i] = mul(fact[i - 1], i);
}
FLCR() { process(); }
} F;
class BIT {
public:
long long int* tree;
BIT() { tree = new long long int[1000011LL + 1]; }
void set(long long int x, long long int v) {
while (x <= 1000011LL) {
tree[x] += v;
x += (x & -x);
}
}
long long int get(long long int x) {
long long int res = 0;
while (x) {
res += tree[x];
x -= (x & -x);
}
return res;
}
long long int moreThan(long long int x) { return get(1000011LL) - get(x); }
} B;
long long int A[1000011LL];
long long int inv[1000011LL];
long long int across[1000011LL];
int main() {
long long int n;
scanf("%lld", &n);
for (long long int i = 0; i < n; i++) scanf("%lld", A + i);
across[0] = inv[0] = 0;
B.set(A[0], 1);
for (long long int i = 1; i < n; i++) {
inv[i] = inv[i - 1] + B.moreThan(A[i]);
long long int moreThan = B.get(n) - B.get(A[i - 1]);
long long int smallerThan = A[i - 1] - 1 - B.get(A[i - 1] - 1);
B.set(A[i], 1);
across[i] = across[i - 1] + smallerThan - moreThan;
}
long long int ans = 0;
for (long long int i = 0; i < n; i++) {
B.set(A[i], -1);
long long int smallerThan = B.get(A[i]);
long long int currAns = F.mul(
F.mul(smallerThan, F.mul(smallerThan - 1, (1000000007LL + 1) / 2)),
F.fact[n - 1 - i]);
currAns =
F.add(currAns, F.mul(F.mul(smallerThan, F.fact[n - 1 - i]),
F.add(across[i], ((i == 0) ? 0 : inv[i - 1]))));
currAns = F.add(
currAns, F.mul(F.mul(smallerThan,
F.mul((1000000007LL + 1) / 2, F.fact[n - i - 1])),
F.nc2(n - i - 1)));
ans = F.add(ans, currAns);
}
ans = F.add(ans, inv[n - 1]);
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool compare(const T &a, const T &b) {
return a.second < b.second;
}
long long arr[500][500];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long cnt = 1;
long long n;
cin >> n;
long long dow = 1;
for (long long i = 0; i < n; i++) {
if (dow) {
for (long long j = 0; j < n; j++) {
arr[j][i] = cnt++;
}
dow = 0;
} else {
for (long long j = n - 1; j >= 0; j--) {
arr[j][i] = cnt++;
}
dow = 1;
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int start[50 + 10][5];
int main() {
int n, m, ans = 10000;
string a;
cin >> n >> m;
for (int i = 0; i < 50 + 10; i++) {
for (int k = 0; k < 5; k++) {
start[i][k] = -1;
}
}
for (int i = 0; i < n; i++) {
cin >> a;
if (a.find_first_of("1234567890") != string::npos) {
start[i][0] =
min(a.find_first_of("1234567890"), m - a.find_last_of("1234567890"));
}
if (a.find_first_of("qwertyuiopasdfghjklzxcvbnm") != string::npos) {
start[i][1] = min(a.find_first_of("qwertyuiopasdfghjklzxcvbnm"),
m - a.find_last_of("qwertyuiopasdfghjklzxcvbnm"));
}
if (a.find_first_of("*&#") != string::npos) {
start[i][2] = min(a.find_first_of("*&#"), m - a.find_last_of("*&#"));
}
}
for (int i = 0; i < n; i++) {
for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) {
if (start[i][0] == -1 || start[k][1] == -1 || start[j][2] == -1 ||
i == j || k == i || k == j) {
continue;
}
ans = min(ans, start[i][0] + start[k][1] + start[j][2]);
}
}
}
cout << ans;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int n,f,t,i,r;
vector<int> g[1<<18];
int dfs(int v, int p) {
int c0 = 0, a = 0;
for(int i : g[v]) if(i != p) {
t = dfs(i, v);
t ? a += t : ++c0;
}
return a + c0 - (c0>0);
}
int main() {
for(i=(cin>>n,n);--i;) {
cin>>f>>t;
g[f].push_back(t);
g[t].push_back(f);
}
while(r < n && g[r].size()<3) r++;
cout << (r==n?1:dfs(r, r));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, kq = 0;
int main() {
cin >> n;
for (int i = 1; i <= sqrt(n); i++) {
long long s = 3ll * i * (i + 1) / 2 - i;
if (s <= n) {
if ((n - s) % 3 == 0) kq++;
} else
break;
}
cout << kq;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const long long INF = (long long)1e9 + 7;
long long fact[N];
long long mypow(long long a, long long x, long long mod) {
long long ans = 1;
while (x) {
if (x & 1) {
ans = (ans * a) % mod;
}
a = (a * a) % mod;
x >>= 1;
}
return ans;
}
long long C(long long m, long long n) {
long long a = fact[n];
long long b = fact[m] * fact[n - m] % INF;
return a * mypow(b, INF - 2, INF) % INF;
}
void init() {
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = i * fact[i - 1] % INF;
}
}
int main() {
init();
int n;
vector<int> a, b;
cin >> n;
a.resize(n);
b.resize(n);
for (int i = 0; i < n; i++) cin >> a[i];
if (n == 1) {
cout << a[0] << endl;
return 0;
}
if (n <= 3) {
int op = 1;
while (n) {
for (int i = 0; i < n - 1; i++) {
b[i] = (a[i] + op * a[i + 1] + INF) % INF;
op *= -1;
}
n--;
a = b;
}
cout << (a[0] + INF) % INF << endl;
} else {
int op = 1;
while (n & 1) {
for (int i = 0; i < n - 1; i++) {
b[i] = (a[i] + op * a[i + 1] + INF) % INF;
op *= -1;
}
n--;
a = b;
}
long long lhs = 0, rhs = 0;
for (int i = 0; i <= n / 2 - 1; i++) {
lhs = (lhs + a[i * 2] * C(i, n / 2 - 1) % INF + INF) % INF;
rhs = (rhs + a[i * 2 + 1] * C(i, n / 2 - 1) % INF + INF) % INF;
}
long long ans = 0;
if ((n % 4 == 2 && op == 1) || (n % 4 == 0 && op == -1)) {
ans = ((lhs + rhs) + INF) % INF;
} else {
ans = ((lhs - rhs) + INF) % INF;
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#define pii pair<int, int>
class Compare1 {
public:
bool operator() (pii a, pii b) {
return (a.second < b.second or (a.first > b.first and a.second == b.second));
}
};
void testCase() {
int nbRequests;
cin >> nbRequests;
bool customerServed [nbRequests];
for (int i = 0; i < nbRequests; i++) customerServed[i] = false;
priority_queue<pii, vector<pii>, Compare1> polycarpQueue;
queue<int> monoCarpQueue;
int nbClients = 0;
for (int i = 0; i < nbRequests; i++) {
int typ;
cin >> typ;
if (typ == 1) {
int locPriority;
cin >> locPriority;
polycarpQueue.push(pii(nbClients, locPriority));
monoCarpQueue.push(nbClients);
nbClients++;
}
else if (typ == 2) {
while (customerServed[monoCarpQueue.front()]) monoCarpQueue.pop();
// cout << "|";
cout << monoCarpQueue.front()+1 << " ";
// cout << "|" << endl;
customerServed[monoCarpQueue.front()] = true;
}
else {
while (customerServed[polycarpQueue.top().first]) polycarpQueue.pop();
// cout << "|";
cout << polycarpQueue.top().first+1 << " ";
// cout << "|" << endl;
customerServed[polycarpQueue.top().first] = true;
}
}
}
int main() {
int n = 1;
for (int i = 0; i < n; i++) testCase();
int dummy = 0;
dummy++;
} | 3 |
#include <bits/stdc++.h>
using namespace std;
int plane[30][30];
int bias = 10;
char s[20];
bool eval() {
for (int i = bias; i < bias + 10; i++)
for (int j = bias; j < bias + 10; j++) {
int s1(0), s2(0), s3(0), s4(0);
for (int k = -2; k <= 2; k++) {
s1 += plane[i + k][j];
s2 += plane[i][j + k];
s3 += plane[i + k][j + k];
s4 += plane[i + k][j - k];
}
if (s1 >= 9 || s2 >= 9 || s3 >= 9 || s4 >= 9) return true;
}
return false;
}
int main() {
memset(plane, 0, sizeof(plane));
for (int i = 0; i < 10; i++) {
scanf("%s", &s);
for (int j = 0; j < 10; j++) {
if (s[j] == 'X')
plane[i + bias][j + bias] = 2;
else if (s[j] == '.')
plane[i + bias][j + bias] = 1;
}
}
bool ans = eval();
if (ans)
printf("YES\n");
else
printf("NO\n");
}
| 2 |
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
int bit[200000];
vector<int>v;
void add(int k, int x) {
while (k <= v.size()) {
bit[k] += x;
k += k&-k;
}
}
int sum(int k) {
int res = 0;
while (k) {
res += bit[k];
k -= k&-k;
}
return res;
}
int a[100000], b[100000], c[100000], d[100000];
struct st {
int y, t, id;
};
int main() {
int n; scanf("%d", &n);
vector<st>xs;
rep(i, n) {
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
if (a[i] > c[i])swap(a[i], c[i]);
if (b[i] > d[i])swap(b[i], d[i]);
v.push_back(a[i]); v.push_back(c[i]);
if (a[i] == c[i]) {
xs.push_back({ b[i],0,i });
xs.push_back({ d[i],2,i });
}
else xs.push_back({ b[i],1,i });
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
sort(xs.begin(), xs.end(), [](st&a, st&b) {
if (a.y == b.y)return a.t < b.t;
return a.y < b.y;
});
rep(i, n) {
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1;
}
int ans = 0;
for (st&p : xs) {
if (p.t == 0)add(a[p.id], 1);
else if (p.t == 1) {
ans += sum(c[p.id]);
if (a[p.id])ans -= sum(a[p.id] - 1);
}
else add(a[p.id], -1);
}
printf("%d\n", ans);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long p = 0;
while (powl(2, p + 2) - 1 < n) p += 2;
n -= (powl(2, p) - 1);
long long num = (n + 2) / 3;
num--;
long long num2 = 0, num3 = 0;
for (long long i = 0; powl(2, i) <= num; i += 2) {
if (!(num & ((long long)1 << i)) && !(num & ((long long)1 << (i + 1))))
continue;
if ((num & ((long long)1 << i)) && (num & ((long long)1 << (i + 1)))) {
num2 += (long long)((long long)1 << i);
num3 += (long long)((long long)1 << (i + 1));
} else if ((num & ((long long)1 << i))) {
num2 += (long long)((long long)1 << (i + 1));
num3 += (long long)((long long)1 << (i + 1));
num3 += (long long)((long long)1 << i);
} else {
num3 += (long long)((long long)1 << (i));
num2 += (long long)((long long)1 << (i + 1));
num2 += (long long)((long long)1 << i);
}
}
long long ans = 0;
if (n % (long long)3 == (long long)1) {
ans += num;
ans += (long long)((long long)1 << p);
} else if (n % (long long)3 == (long long)2) {
ans += num2;
ans += (long long)((long long)2 << p);
} else {
ans += num3;
ans += (long long)((long long)3 << p);
}
cout << ans << '\n';
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#define read(n) scanf("%lld",&n)
#define write(x) printf("%lld",x)
#define F(i,n) for(int i = 0;i < n;i++)
#define ll long long
#define MX 3005
#define MOD 1000000007
#define INF 1000000009
#define set_dp(dp) memset(dp,-1,sizeof(dp))
using namespace std;
ll t, n, a[MX];
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
read(t);
while(t--){
read(n);
ll ans, sum = 0;
F(i, n) read(a[i]), sum += a[i];
for(int i = n; i > 0; i--){
if(sum % i) continue;
ll req = sum / i, cur = 0;
bool ok = 1;
for(int j = 0; j < n; j++){
if(cur < req) cur += a[j];
if(cur > req){
ok = 0;
break;
}
else if(cur == req) cur = 0;
}
if(ok){
ans = i;
break;
}
}
cout<<n - ans<<endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
bool ok(vector<ll>& a, unordered_map<ll, int>& cnt, int i, int j) {
for (int k = i; k <= j; k++) {
cnt[a[k]] -= 1;
}
bool good = true;
for (auto& p : cnt) {
if (p.second > 1) {
good = false;
break;
}
}
for (int k = i; k <= j; k++) {
cnt[a[k]] += 1;
}
return good;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
unordered_map<ll, int> cnt;
for (int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]] += 1;
}
bool good = true;
for (auto& p : cnt) {
if (p.second > 1) {
good = false;
break;
}
}
if (good) {
cout << 0 << endl;
return 0;
}
int best = INT_MAX / 2;
for (int i = 0; i < n; i++) {
int left = i;
int right = n;
while (left < right) {
int mid = (left + right) / 2;
if (ok(a, cnt, i, mid))
right = mid;
else
left = mid + 1;
}
if (ok(a, cnt, i, right)) best = min(best, right - i + 1);
}
cout << best << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename... T>
void read(T&... args) {
((cin >> args), ...);
}
template <typename... T>
void write(T&&... args) {
((cout << args << " "), ...);
}
template <typename... T>
void writen(T&&... args) {
((cout << args << "\n"), ...);
}
long long bin(long long a, long long b, long long mod) {
if (b == 0) return 1;
if (b % 2 == 0) return bin((a * a) % mod, b / 2, mod) % mod;
return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod;
}
long long n, k;
int a[100005], b[100005];
bool func(long long mid) {
long long k1 = k;
for (int i = 1; i < n + 1; i++) {
if (a[i] * mid > b[i]) {
k1 -= a[i] * mid - b[i];
if (k1 < 0) {
return false;
}
}
}
if (k1 >= 0) {
return true;
}
return false;
}
void solve() {
read(n, k);
long long lo = 0, hi = 2000000000;
for (int i = 1; i < n + 1; i++) {
read(a[i]);
}
for (int i = 1; i < n + 1; i++) {
read(b[i]);
}
while (hi - lo > 1) {
long long mid = (hi + lo) / 2;
if (func(mid)) {
lo = mid;
} else {
hi = mid - 1;
}
}
if (func(hi)) {
cout << hi << " ";
} else {
cout << lo << " ";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000 * 1000 * 1000;
int T, n;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
printf("%d\n", max(0, n - 2));
}
return 0;
}
| 2 |
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<stack>
#include<unordered_map>
#include<bitset>
#include<limits>
#include<complex>
#include<array>
#include<numeric>
#include<functional>
#include<random>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,m,n) for(ll (i)=(ll)(m);i<(ll)(n);i++)
#define REP(i,n) rep(i,0,n)
#define all(hoge) (hoge).begin(),(hoge).end()
typedef pair<ll, ll> P;
constexpr long double m_pi = 3.1415926535897932L;
constexpr ll MOD = 1000000007;
constexpr ll INF = 1LL << 61;
constexpr long double EPS = 1e-10;
template<typename T> using vector2 = vector<vector<T>>;
template<typename T> using vector3 = vector<vector2<T>>;
typedef vector<ll> Array;
ll l[20][202020];
ll r[20][202020];
ll dp1[20][202020];
ll dp2[20][202020];
int main() {
ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
ll n, k;
cin >> n >> k;
Array x(n);
REP(i, n)cin >> x[i];
REP(i, n)dp1[0][i] = l[0][i] = lower_bound(all(x), x[i] + k) - x.begin();
REP(i, n)dp2[0][i] = r[0][i] = upper_bound(all(x), x[i] - k) - x.begin() - 1;
REP(i, 19)REP(j, n)l[i + 1][j] = l[i][min(n - 1, l[i][j])];
REP(i, 19)REP(j, n)r[i + 1][j] = r[i][max(0LL, r[i][j])];
REP(i, 19)REP(j, n)dp1[i + 1][j] = dp1[i][j] + dp1[i][l[i][j]];
REP(i, 19)REP(j, n)dp2[i + 1][j] = dp2[i][j] + dp2[i][r[i][j]];
ll q;
cin >> q;
while (q--) {
ll L, R;
cin >> L >> R;
L--;
ll ans = R - L;
ll x = L, y = R - 1;
for (int i = 19; i >= 0; i--) {
if (l[i][x] < R)ans += (1LL << i) - dp1[i][x], x = l[i][x];
if (r[i][y] >= L)ans += dp2[i][y], y = r[i][y];
}
cout << ans << "\n";
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> pll;
pll arr[100005],arr1[100005];
vector<pair<ll,pll> > vect;
int root[100005];
int _rank[100005];
int findroot(int a){
while(a!=root[a])
a=root[a];
return a;
}
void uni(int a, int b){
int x=findroot(a);
int y=findroot(b);
if (_rank[x]>_rank[y]){
root[y]=x;
}
else{
root[x]=y;
if (_rank[x]==_rank[y])
_rank[y]+=1;
}
}
int main(){
for(int i=0;i<100005;i++)
root[i]=i;
int n; ll a,b;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lld%lld",&a,&b);
arr[i].f=a; arr[i].s=i;
arr1[i].f=b; arr1[i].s=i;
}
sort(arr,arr+n);
for(int i=0;i<n-1;i++){
vect.pb(mp((arr[i+1].f-arr[i].f),mp(arr[i+1].s,arr[i].s)));
}
sort(arr1,arr1+n);
for(int i=0;i<n-1;i++){
vect.pb(mp((arr1[i+1].f-arr1[i].f),mp(arr1[i+1].s,arr1[i].s)));
}
ll ans=0;
int len=vect.size();
sort(vect.begin(),vect.end());
for(int i=0;i<len;i++){
if(findroot(vect[i].s.f)!=findroot(vect[i].s.s)){
uni(vect[i].s.f,vect[i].s.s);
ans+=vect[i].f;
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
pair<int, int> P[200005];
int p, r, t;
int ned[305][305];
int num[305];
int grid[305][305];
int used[305];
int bo[305];
int check(int t) {
memset(used, 0, sizeof(used));
memset(grid, 0, sizeof(grid));
memset(bo, 0, sizeof(bo));
for (int i = 1; i <= t; ++i) {
++used[P[i].second];
++grid[P[i].first][P[i].second];
}
int tot = 0;
while (1) {
int flag = 0;
for (int i = 1; i <= p; ++i)
if (!bo[i]) {
int mark = 0;
for (int j = 1; j <= r; ++j)
if (grid[i][j] + max(num[j] - used[j], 0) < ned[i][j])
mark = 1;
if (mark == 0) {
flag = i;
break;
}
}
if (flag == 0) break;
bo[flag] = 1;
++tot;
for (int j = 1; j <= r; ++j)
used[j] -= grid[flag][j];
}
return tot == p;
}
int main() {
scanf("%d%d%d", &p, &r, &t);
for (int i = 1; i <= r; ++i) {
scanf("%d", &num[i]);
}
for (int i = 1; i <= p; ++i)
for (int j = 1; j <= r; ++j)
scanf("%d", &ned[i][j]);
for (int i = 1; i <= t; ++i) {
scanf("%d%d", &P[i].first, &P[i].second);
}
// cout << check(0) << endl;
if (check(t)) {
puts("-1");
}
else {
int head = 0, tail = t;
while (head < tail - 1) {
int mid = (head + tail) >> 1;
if (check(mid)) head = mid;
else tail = mid;
}
cout << tail << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void in(int &x) {
char ch;
int minus = 0;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
;
if (ch == '-')
minus = 1, x = 0;
else
x = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - '0';
if (minus) x = -x;
}
void out(int x) {
char hc[30];
int len, minus = 0;
if (x < 0) minus = 1, x = -x;
len = 0;
hc[len++] = x % 10 + '0';
while (x /= 10) hc[len++] = x % 10 + '0';
if (minus) putchar('-');
for (int i = len - 1; i >= 0; i--) putchar(hc[i]);
}
const int N = 500 + 5;
const int L = 1000000;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
char str[L];
int n, m;
int vis[N][N];
bool valid[L];
int x[L], y[L], ans[L];
int sx, sy, len;
inline bool inside(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; }
int main() {
scanf("%d %d %d %d", &n, &m, &sx, &sy);
scanf("%s", str + 1);
len = strlen(str + 1);
int nx = sx, ny = sy, sign = 0;
vis[nx][ny] = ++sign;
for (int i = 1; i <= len; i++) {
int u;
if (str[i] == 'D') u = 0;
if (str[i] == 'R') u = 1;
if (str[i] == 'U') u = 2;
if (str[i] == 'L') u = 3;
int tx = nx + dx[u];
int ty = ny + dy[u];
if (inside(tx, ty)) {
nx = tx;
ny = ty;
if (vis[tx][ty] == 0) {
vis[tx][ty] = ++sign;
valid[i] = 1;
} else {
valid[i] = 0;
}
} else {
valid[i] = 0;
}
}
ans[0] = 1;
for (int i = 1; i <= len; i++) {
if (valid[i] == 0)
ans[i] = 0;
else if (vis[x[i]][y[i]] == -1)
ans[i] = 0;
else
ans[i] = 1;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (vis[i][j] == 0) cnt++;
ans[len] = cnt;
if (valid[len] == 1) ans[len]++;
for (int i = 0; i <= len; i++) {
printf("%d", ans[i]);
if (i == len)
putchar('\n');
else
putchar(' ');
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void *wmem;
char memarr[96000000];
template <class T>
inline void walloc1d(T **arr, int x, void **mem = &wmem) {
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]);
(*arr) = (T *)(*mem);
(*mem) = ((*arr) + x);
}
inline void rd(int &x) {
int k;
int m = 0;
x = 0;
for (;;) {
k = getchar();
if (k == '-') {
m = 1;
break;
}
if ('0' <= k && k <= '9') {
x = k - '0';
break;
}
}
for (;;) {
k = getchar();
if (k < '0' || k > '9') {
break;
}
x = x * 10 + k - '0';
}
if (m) {
x = -x;
}
}
inline void wt_L(char a) { putchar(a); }
inline void wt_L(long long x) {
int s = 0;
int m = 0;
char f[20];
if (x < 0) {
m = 1;
x = -x;
}
while (x) {
f[s++] = x % 10;
x /= 10;
}
if (!s) {
f[s++] = 0;
}
if (m) {
putchar('-');
}
while (s--) {
putchar(f[s] + '0');
}
}
template <class T>
struct Heap {
int size;
T *val;
void malloc(const int N) {
val = (T *)std::malloc(N * sizeof(T));
size = 0;
}
void walloc(const int N, void **mem = &wmem) {
walloc1d(&val, N, mem);
size = 0;
}
void free() { std::free(val); }
void init() { size = 0; }
void up() {
int n = size - 1;
int m;
while (n) {
m = (n - 1) / 2;
if (val[m] <= val[n]) {
break;
}
swap(val[m], val[n]);
n = m;
}
}
void down() {
int n = 0;
int m;
for (;;) {
m = 2 * n + 1;
if (m >= size) {
break;
}
if (m + 1 < size && val[m] > val[m + 1]) {
m++;
}
if (val[m] >= val[n]) {
break;
}
swap(val[m], val[n]);
n = m;
}
}
T top() { return val[0]; }
T pop() {
T res = val[0];
size--;
if (size > 0) {
val[0] = val[size];
down();
}
return res;
}
T push(const T x) {
val[size++] = x;
up();
return x;
}
};
int N;
int A[300000];
char chk[300000];
int main() {
wmem = memarr;
int i;
int j;
long long res = 0;
Heap<int> hp;
rd(N);
{
int Lj4PdHRW;
for (Lj4PdHRW = (0); Lj4PdHRW < (N); Lj4PdHRW++) {
rd(A[Lj4PdHRW]);
}
}
{
int cTE1_r3A;
int RZTsC2BF = 0;
int FmcKpFmN;
int xr20shxY;
int WYIGIcGE;
for (cTE1_r3A = (0); cTE1_r3A < (((N)-1) + 1); cTE1_r3A++) {
xr20shxY = A[cTE1_r3A];
if (RZTsC2BF == 0 || FmcKpFmN > xr20shxY) {
FmcKpFmN = xr20shxY;
RZTsC2BF = 1;
WYIGIcGE = cTE1_r3A;
}
}
j = WYIGIcGE;
}
for (i = (0); i < (j + 1); i++) {
A[i] = 0;
}
for (i = (0); i < (19); i++) {
chk[(1 << i) - 1] = 1;
}
hp.malloc(N);
hp.init();
for (i = (N)-1; i >= (0); i--) {
hp.push(A[i]);
if (chk[i]) {
res += hp.pop();
}
}
wt_L(res);
wt_L('\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const int mod = 1e9 + 7;
vector<int> side[maxn];
int a[maxn];
long long s[maxn];
int cnt[maxn], tot[maxn];
bool f[maxn];
void dfs(int u, int fa) {
s[u] = a[u];
for (int i = (0), iend = (side[u].size() - 1); i <= iend; i++) {
int v = side[u][i];
if (v == fa) continue;
dfs(v, u);
s[u] += s[v];
}
}
long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); }
long long dp[maxn];
int main() {
int n;
cin >> n;
for (int i = (1), iend = (n); i <= iend; i++) scanf("%d", &a[i]);
for (int i = (2), iend = (n); i <= iend; i++) {
int p;
scanf("%d", &p);
side[i].push_back(p);
side[p].push_back(i);
}
dfs(1, 0);
for (int i = (1), iend = (n); i <= iend; i++)
if (s[1] / gcd(s[1], s[i]) <= n) cnt[s[1] / gcd(s[1], s[i])]++;
for (int i = (1), iend = (n); i <= iend; i++)
for (int j = i; j <= n; j += i) tot[j] += cnt[i];
for (int i = (1), iend = (n); i <= iend; i++)
if (tot[i] == i) f[i] = 1;
dp[1] = 1;
for (int i = (1), iend = (n); i <= iend; i++) {
dp[i] *= f[i];
dp[i] %= mod;
for (int j = 2 * i; j <= n; j += i) dp[j] = dp[j] + dp[i];
}
long long ans = 0;
for (int i = (1), iend = (n); i <= iend; i++) ans = (ans + dp[i]) % mod;
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int arr[4194304 + 1];
bool Exists[4194304 + 20] = {0};
int F[4194304 + 10][2] = {0};
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
Exists[arr[i]] = 1;
F[arr[i]][0] = arr[i];
}
for (int i = 1; i <= 22; i++) {
for (int mask = 0; mask < 4194304; mask++) {
if ((mask & (1 << (i - 1))) != 0) {
if ((mask ^ (1 << (i - 1))) <= 4194304)
F[mask][i % 2] = F[(mask ^ (1 << (i - 1)))][(i + 1) % 2];
} else {
F[mask][i % 2] = F[mask][(i + 1) % 2];
if ((mask ^ (1 << (i - 1))) <= 4194304 && F[mask][i % 2] == 0) {
F[mask][i % 2] = F[(mask ^ (1 << (i - 1)))][(i + 1) % 2];
}
}
}
}
for (int i = 0; i < n; i++) {
if (F[arr[i]][(22) % 2] == 0)
printf("-1 ");
else
printf("%d ", F[arr[i]][(22) % 2]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
int length1, length2;
length1 = a.size();
length2 = b.size();
vector<int> v, num;
for (int i = 0; i < length1; i++) {
num.push_back(a[i] - '0');
}
for (int i = 0; i < length2; i++) {
v.push_back(b[i] - '0');
}
sort(v.begin(), v.end(), greater<int>());
int j = 0;
for (int i = 0; i < length1; i++) {
if ((num[i] < v[j]) && (j < length2)) {
num[i] = v[j];
j++;
}
}
for (int i = 0; i < length1; i++) {
cout << num[i];
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long cnt[200009];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(cnt, -1, sizeof cnt);
long long i, j, k, l, m, n, a, b;
long long mn = 0;
string s;
cin >> s;
n = s.size();
stack<long long> st;
a = -1, b = -3;
for (i = 0; i < n; i++) {
if (s[i] == '(') st.push(i);
if (s[i] == '[') st.push(i);
if (s[i] == ')') {
if (st.size() && s[st.top()] == '(') {
cnt[st.top()] = i;
st.pop();
} else {
while (st.size()) st.pop();
}
}
if (s[i] == ']') {
if (st.size() && s[st.top()] == '[') {
cnt[st.top()] = i;
st.pop();
} else {
while (st.size()) st.pop();
}
}
}
long long pok;
for (i = n - 2; i >= 0; i--) {
if (cnt[i] != -1 && cnt[i] + 1 < n) {
cnt[i] = max(cnt[i], cnt[cnt[i] + 1]);
}
}
for (i = 0; i < n;) {
if (s[i] == ')' || s[i] == ']' || cnt[i] == -1) {
i++;
continue;
}
pok = 0;
for (j = i; j <= cnt[i]; j++) {
if (s[j] == '[') pok++;
}
if (pok > mn) {
mn = pok;
a = i;
b = cnt[i];
}
i = j;
}
cout << mn << '\n';
for (i = a; i <= b; i++) cout << s[i];
cout << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
if (b == 1) return a;
long long int c = power(a, b / 2);
if (b % 2 == 0) return ((c % 1000000007) * (c % 1000000007)) % 1000000007;
return ((((c % 1000000007) * (c % 1000000007)) % 1000000007) *
(a % 1000000007)) %
1000000007;
}
vector<long long int> v[100005];
int vis[100005] = {0};
long long int a[100005], s[100005], n;
void dfs(long long int x, long long int sum) {
if (s[x] != -1) {
if (s[x] < sum) {
cout << "-1\n";
exit(0);
}
}
if (v[x].size() == 0) {
if (s[x] == -1) {
a[x] = 0;
}
} else {
long long int mn = 1000000007;
for (auto z : v[x]) mn = min(mn, s[z]);
if (mn == -1) mn = 0;
if (s[x] == -1) {
a[x] = mn;
s[x] = mn;
} else
a[x] = mn + s[x];
sum = max(sum, s[x]);
for (auto z : v[x]) {
dfs(z, sum);
if (v[z].size() == 0 && s[z] == -1) {
a[z] = 0;
s[z] = s[x];
continue;
}
a[z] = s[z] - a[x];
if (a[z] < 0) a[z] = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int k, t, c = 0, i, j, m, cs = 0;
t = 1;
while (t--) {
cin >> n;
for (i = 1; i < n; i++) {
cin >> m;
v[m - 1].push_back(i);
}
for (i = 0; i < n; i++) {
cin >> s[i];
}
m = s[0];
dfs(0, 0);
if (a[0] != m || s[0] != m)
cout << "-1" << a[0];
else {
for (i = 0; i < n; i++) {
c += a[i];
}
cout << c;
}
if (t != 0) cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, p;
cin >> n >> p;
vector<int> arcosA((n + 1), 0);
vector<pair<int, int>> parejas;
unordered_map<int, int> interseccion[n + 1];
for (int i = 1; i <= n; ++i) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
++arcosA[x];
++arcosA[y];
++interseccion[x][y];
parejas.push_back(make_pair(x, y));
}
vector<int> arr = arcosA;
sort(arr.begin() + 1, arr.end());
int j = n;
long long total = 0;
for (int i = 1; i < n; ++i) {
if (j < (i + 1)) j = (i + 1);
while (j > (i + 1) && (arr[i] + arr[j - 1]) >= p) --j;
if (arr[i] + arr[j] >= p) total += (n - j + 1);
}
unordered_set<int> revisado[n + 1];
for (auto e : parejas) {
if (arcosA[e.first] + arcosA[e.second] >= p &&
revisado[e.first].find(e.second) == revisado[e.first].end()) {
if (arcosA[e.first] + arcosA[e.second] - interseccion[e.first][e.second] <
p)
--total;
revisado[e.first].insert(e.second);
}
}
cout << total << '\n';
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i,n) for(int i=0; i<(int)(n); i++)
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
LL modpow(LL x, LL n){
LL r=1;
while(n){
if(n&1) r=r*x%mod;
x=x*x%mod;
n>>=1;
}
return r;
}
LL modinv(LL x){
LL m=mod, u=1, v=0;
while(m){
LL t=x/m;
x-=t*m; swap(x,m);
u-=t*v; swap(u,v);
}
u%=mod;
if(u<0) u+=mod;
return u;
}
int main(){
int N;
cin >> N;
vector<LL> A(N);
rep(i,N) cin >> A[i];
sort(A.begin(),A.end());
vector<LL> Ac(N);
rep(i,N) Ac[i]=A[i];
LL A_Max=A[N-1];
vector<LL> D(A_Max+1);
rep(i,N){
for(LL j=2; j*j<=A[i]; j++){
int num=0;
while(Ac[i]%j==0){
Ac[i]/=j;
num++;
}
if(D[j]<num) D[j]=num;
}
if(!D[Ac[i]]) D[Ac[i]]=1;
}
LL ans=0, lc=1;
for(LL i=2; i<=A_Max; i++){
lc*=modpow(i,D[i]);
lc%=mod;
}
rep(i,N){
ans+=lc*modinv(A[i]);
ans%=mod;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long powers[1000];
int main() {
ios_base::sync_with_stdio(0);
string imput, number;
cin >> imput;
number = "";
powers[0] = 1;
powers[1] = 2;
for (int i = 2; i < 1000; i++) {
powers[i] = (powers[i - 1] * 2) % 1000003;
}
for (int i = 0; i < imput.size(); i++) {
switch (imput[i]) {
case '>':
number += "1000";
break;
case '<':
number += "1001";
break;
case '+':
number += "1010";
break;
case '-':
number += "1011";
break;
case '.':
number += "1100";
break;
case ',':
number += "1101";
break;
case '[':
number += "1110";
break;
case ']':
number += "1111";
break;
}
}
long long ans = 0;
for (int i = number.size() - 1; i >= 0; i--) {
if (number[i] == '1') {
ans += powers[number.size() - 1 - i];
ans %= 1000003;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int b[100005][3];
int a[300005];
int mp[300005];
vector<int> ans1, ans2;
int vis[300005];
int cmp(const int &a, const int &b) { return mp[a] < mp[b]; }
int main() {
int i, j, n, m, k, q, p1, p2;
scanf("%d", &n);
for (i = 1; i <= 3 * n; i++) {
scanf("%d", &a[i]);
mp[a[i]] = i;
}
for (i = 1; i <= n; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
sort(b[i], b[i] + 3, cmp);
vis[b[i][0]] = 1;
}
scanf("%d", &k);
if (vis[k]) {
for (i = 1; i <= n; i++) {
if (mp[b[i][0]] <= mp[k]) {
if (b[i][0] == k) {
q = max(b[i][1], b[i][2]);
}
for (j = 0; j <= 2; j++) ans1.push_back(b[i][j]);
} else {
for (j = 0; j <= 2; j++) ans2.push_back(b[i][j]);
}
}
sort(ans1.begin(), ans1.end());
int flag = 0;
for (i = 0; i < ans1.size(); i++) {
if (ans1[i] != k) {
if (flag++) printf(" ");
printf("%d", ans1[i]);
}
if (ans1[i] == q) break;
}
for (i++; i < ans1.size(); i++) {
if (ans1[i] != k) ans2.push_back(ans1[i]);
}
sort(ans2.begin(), ans2.end());
for (i = 0; i < ans2.size(); i++) {
if (flag++) printf(" ");
printf("%d", ans2[i]);
}
} else {
int flag = 0;
for (i = 1; i <= 3 * n; i++) {
if (i != k) {
if (flag++) printf("");
printf("%d ", i);
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int p[2503];
vector<pair<int, int> > a[102];
int root(int x) { return p[x] < 0 ? x : p[x] = root(p[x]); }
void mer(int x, int y) {
if ((x = root(x)) == (y = root(y))) return;
if (p[x] > p[y]) swap(x, y);
p[x] += p[y];
p[y] = x;
}
int main() {
ios::sync_with_stdio(false);
mt19937 rng(328);
int n, z = 0, e[52] = {}, f[52] = {};
string s[52] = {};
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i];
clock_t cl = clock();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (s[i][j] == '#') z++, e[i] = 1, f[j] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (s[i][j] == '#') {
for (int k = 0; k < n; k++)
for (int l = 0; l < n; l++)
if (s[k][l] == '#') {
if (i != k || j != l) {
a[abs(i - k) + abs(j - l)].push_back({i * n + j, k * n + l});
}
}
}
int g = 0, h = 0;
for (int i = 0; i < n; i++) g += e[i], h += f[i];
if (g == n || h == n) {
cout << n;
return 0;
}
while (clock() - cl < 700) {
for (int i = 0; i < n * n; i++) p[i] = -1;
for (int i = 0; i < n + n; i++) shuffle(a[i].begin(), a[i].end(), rng);
for (int i = 0; i < n + n; i++) {
for (pair<int, int> j : a[i])
if (root(j.first) != root(j.second)) {
mer(j.first, j.second);
int u[2503] = {}, v[2503] = {}, w[2503] = {}, x[2503] = {}, t = 0;
for (int k = 0; k < n * n; k++) u[k] = w[k] = 328;
for (int k = 0; k < n; k++)
for (int l = 0; l < n; l++)
if (s[k][l] == '#') {
int y = root(k * n + l);
u[y] = min(u[y], k);
v[y] = max(v[y], k);
w[y] = min(w[y], l);
x[y] = max(x[y], l);
}
for (int k = 0; k < n * n; k++)
if (u[k] != 328) {
t += max(v[k] - u[k] + 1, x[k] - w[k] + 1);
}
z = min(z, t);
}
}
}
cout << z;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
int arr[t];
for (int i = 0; i < t; i++) {
cin >> arr[i];
}
sort(arr, arr + t);
for (int i = 0; i < t; i++) cout << arr[i] << " ";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, T;
cin >> n >> T;
int ans[n];
vector<int> x, y, z;
int tmp;
int flag = 0;
for (int i = 0; i < n; ++i) {
cin >> tmp;
if (T % 2 == 0 and tmp == T / 2) {
ans[i] = (flag++) % 2;
} else if (tmp * 2 < T)
ans[i] = 0;
else
ans[i] = 1;
}
for (auto it : ans) {
cout << it << ' ';
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, t, x;
struct comp {
double x, y;
comp(double xx = 0, double yy = 0) { x = xx, y = yy; };
};
comp operator+(comp a, comp b) { return comp(a.x + b.x, a.y + b.y); }
comp operator-(comp a, comp b) { return comp(a.x - b.x, a.y - b.y); }
comp operator*(comp a, comp b) {
return comp(a.x * b.x - a.y * b.y, a.x * b.y + b.x * a.y);
}
int lim;
int top;
int rev[200010];
const double pi = acos(-1);
void FFT(comp *A, int type) {
for (int i = 0; i <= lim; ++i) {
if (i < rev[i]) swap(A[i], A[rev[i]]);
}
for (int mid = 1; mid < lim; mid <<= 1) {
comp wn = comp(cos(pi / mid), type * sin(pi / mid));
int len = mid * 2;
for (int i = 0; i < lim; i += len) {
comp w = comp(1, 0);
for (int j = 0; j < mid; ++j, w = w * wn) {
comp x = A[i + j], y = w * A[i + j + mid];
A[i + j] = x + y;
A[i + j + mid] = x - y;
}
}
}
if (type == -1) {
for (int i = 0; i <= lim; ++i) {
A[i].x = A[i].x / lim;
}
}
}
void pre() {
lim = 1;
int tmp = 0;
while (lim <= 4 * t) lim = lim * 2, tmp++;
for (int i = 0; i <= lim; ++i) {
rev[i] = (rev[i >> 1] >> 1 | (i & 1) << (tmp - 1));
}
}
comp a[200010], b[200010];
void mul(double *A, double *B, double *C) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(C, 0, sizeof(C));
for (int i = 0; i < 2 * t; ++i) {
a[i].x = A[i + 1];
}
for (int i = 0; i < t; ++i) {
b[i].x = B[i + 1];
}
FFT(a, 1), FFT(b, 1);
for (int i = 0; i <= lim; ++i) {
a[i] = a[i] * b[i];
}
FFT(a, -1);
for (int i = 0; i <= t; ++i) {
C[i] = a[t + i - 1].x;
}
}
struct qwq {
int v;
double w;
int nxt;
} edge[200010];
int cnt = -1;
int head[200010];
void add(int u, int v, double w) {
edge[++cnt].nxt = head[u];
edge[cnt].v = v;
edge[cnt].w = w;
head[u] = cnt;
}
double G[110][110];
void pre_dis() {
for (int k = 1; k <= n; ++k) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
}
}
}
}
double p[110][40010];
double dp[110][40010];
double now[40010];
bool vis[110];
queue<int> q;
void spfa() {
for (int i = 1; i < n; ++i) {
for (int j = 0; j <= t; ++j) {
dp[i][j] = 1145e4;
}
for (int j = t + 1; j <= 2 * t; ++j) {
dp[i][j] = G[n][i] + x;
}
}
for (int j = 0; j <= t; ++j) dp[n][j] = 0;
for (int j = t + 1; j <= 2 * t; ++j) dp[n][j] = x;
q.push(n);
vis[n] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
mul(dp[u], p[i], now);
bool changed = false;
for (int j = 0; j <= t; ++j) {
if ((now[j] / 100000.0) + edge[i].w < dp[v][j]) {
dp[v][j] = (now[j] / 100000.0) + edge[i].w;
changed = true;
}
}
if (changed && !vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
}
int main() {
memset(head, -1, sizeof(head));
scanf("%d%d%d%d", &n, &m, &t, &x);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
G[i][j] = 1145e4;
}
}
for (int i = 1; i <= m; ++i) {
int u, v;
double w;
scanf("%d%d%lf", &u, &v, &w);
add(v, u, w);
G[v][u] = w;
for (int j = t; j >= 1; --j) {
scanf("%lf", &p[cnt][j]);
}
}
pre_dis();
pre();
spfa();
printf("%lf\n", dp[1][0]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int qian[maxn] = {0};
int num[maxn] = {0};
int pre[maxn] = {0};
int main() {
int n, c;
cin >> n >> c;
int Max = 0;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
if (a == c)
qian[i] = qian[i - 1] + 1;
else {
qian[i] = qian[i - 1];
num[a] = max(1, num[a] + 1 - qian[i] + qian[pre[a]]);
pre[a] = i;
Max = max(num[a], Max);
}
}
printf("%d\n", Max + qian[n]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
long long p[m];
for (long long i = 0; i < m; i++) {
long long x;
cin >> x >> p[i];
}
sort(p, p + m, greater<long long>());
long long cur;
long long ans = 0;
for (long long i = 1; i <= m; i++) {
if (i & 1)
cur = (i * (i - 1)) / 2 + 1;
else
cur = (i * (i - 1)) / 2 + (i / 2);
if (cur <= n)
ans += p[i - 1];
else
break;
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, t[111111];
void upd(int x, int val) {
for (int i = x; i <= n; i = (i | (i + 1))) {
t[i] += val;
}
}
int get(int x) {
int res = 0;
for (int i = x; i > 0; i = (i & (i + 1)) - 1) {
res += t[i];
}
return res;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
upd(i, x);
}
int m;
cin >> m;
for (int i = 1; i <= m; i++) {
int l, r;
cin >> l >> r;
int sum = (get(r) - get(l - 1));
cout << sum / 10 << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long update(vector<long long> &tree, int index, int a, int b, int x,
long long value) {
if (x >= a and x < b) {
if (a == b - 1)
tree[index] = value;
else
tree[index] = update(tree, 2 * index, a, (a + b) / 2, x, value) +
update(tree, 2 * index + 1, (a + b) / 2, b, x, value);
}
return tree[index];
}
long long get(vector<long long> &tree, int index, int a, int b, int l, int r) {
if (r <= a or l >= b) return 0;
if (l <= a and r >= b) return tree[index];
return get(tree, 2 * index, a, (a + b) / 2, l, r) +
get(tree, 2 * index + 1, (a + b) / 2, b, l, r);
}
int main() {
ios_base::sync_with_stdio(false);
long long n, k, a, b, q, tot = 1;
cin >> n >> k >> a >> b >> q;
while (tot < n) tot *= 2;
tot *= 2;
vector<long long> treea(tot, 0), treeb(tot, 0);
for (int i = 0; i < q; i++) {
long long t, di, ai;
cin >> t;
if (t == 1) {
cin >> di >> ai;
int newv = min(a, treea[tot / 2 + di - 1] + ai);
update(treea, 1, 0, tot / 2, di - 1, newv);
newv = min(b, treeb[tot / 2 + di - 1] + ai);
update(treeb, 1, 0, tot / 2, di - 1, newv);
} else if (t == 2) {
cin >> di;
cout << get(treeb, 1, 0, tot / 2, 0, di - 1) +
get(treea, 1, 0, tot / 2, di + k - 1, tot / 2)
<< endl;
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, u, r;
vector<long long> a, b, p, k;
long long ans = -1000000000000;
void func(long long turn, bool isxor) {
long long i;
if ((u - turn + 1) % 2 == 0) {
long long sum = 0;
for (i = 0; i < n; i++) sum += a[i] * k[i];
ans = max(ans, sum);
}
if (turn > u) {
return;
}
vector<long long> temp = a;
if (!isxor) {
for (i = 0; i < n; i++) a[i] = a[i] ^ b[i];
func(turn + 1, true);
a = temp;
}
for (i = 0; i < n; i++) {
a[i] = temp[p[i] - 1] + r;
}
func(turn + 1, false);
a = temp;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> u >> r;
a.resize(n);
b.resize(n);
p.resize(n);
k.resize(n);
long long i;
for (i = 0; i < n; ++i) cin >> a[i];
for (i = 0; i < n; ++i) cin >> b[i];
for (i = 0; i < n; ++i) cin >> k[i];
for (i = 0; i < n; ++i) cin >> p[i];
func(1, false);
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int A[100000], G[100000], C[10000], T[100000], ac[100000], gc[100000],
cc[10000], tc[100000];
int main() {
string s;
int a = 0, g = 0, c = 0, t = 0, ac1 = 0, gc1 = 0, cc1 = 0, tc1 = 0;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'A') A[a++] = i;
if (s[i] == 'G') G[g++] = i;
if (s[i] == 'C') C[c++] = i;
if (s[i] == 'T') T[t++] = i;
}
for (int i = 1; i <= a; i++) {
if (A[i] - A[i - 1] == 1)
ac[ac1]++;
else
ac1++;
}
for (int i = 1; i <= g; i++) {
if (G[i] - G[i - 1] == 1)
gc[gc1]++;
else
gc1++;
}
for (int i = 1; i <= c; i++) {
if (C[i] - C[i - 1] == 1)
cc[cc1]++;
else
cc1++;
}
for (int i = 1; i <= t; i++) {
if (T[i] - T[i - 1] == 1)
tc[tc1]++;
else
tc1++;
}
int cs = 0;
for (int i = 0; i < ac1; i++)
if (ac[i] % 2 == 1) cs++;
for (int i = 0; i < gc1; i++)
if (gc[i] % 2 == 1) cs++;
for (int i = 0; i < cc1; i++)
if (cc[i] % 2 == 1) cs++;
for (int i = 0; i < tc1; i++)
if (tc[i] % 2 == 1) cs++;
cout << cs;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20 + 5;
int board[2][N], n, est[22];
long long solver[22][2];
inline void merged() {
int num = 1 << n;
for (int lvl = int(1); lvl < int(n + 1); ++lvl) {
int ato = 1 << lvl;
int ito = ato >> 1;
int sig = (lvl - 1) & 1;
for (int i = 0; i < num; i += ato) {
int c1 = 0, c2 = 0;
while (c1 < ito && c2 < ito) {
int p1 = i + c1;
int p2 = i + ito + c2;
if (board[sig][p1] > board[sig][p2])
board[lvl & 1][i + c1 + c2] = board[sig][p2], c2++,
solver[lvl][0] += ito - c1;
else
board[lvl & 1][i + c1 + c2] = board[sig][p1], c1++;
}
while (c1 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + c1], c1++;
while (c2 < ito)
board[lvl & 1][i + c1 + c2] = board[sig][i + ito + c2], c2++;
}
}
}
inline void mergei() {
int num = 1 << n;
for (int lvl = int(1); lvl < int(n + 1); ++lvl) {
int ato = 1 << lvl;
int ito = ato >> 1;
int sig = (lvl - 1) & 1;
for (int i = 0; i < num; i += ato) {
int c1 = 0, c2 = 0;
while (c1 < ito && c2 < ito) {
int p1 = i + c1;
int p2 = i + ito + c2;
if (board[sig][p1] > board[sig][p2])
board[lvl & 1][i + c1 + c2] = board[sig][p2], c2++,
solver[lvl][1] += ito - c1;
else
board[lvl & 1][i + c1 + c2] = board[sig][p1], c1++;
}
while (c1 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + c1], c1++;
while (c2 < ito)
board[lvl & 1][i + c1 + c2] = board[sig][i + ito + c2], c2++;
}
}
}
inline long long query(int k) {
est[k] = 1 - est[k];
long long ret = 0;
int e = 0;
for (int i = int(n); i >= int(0); i--) {
e ^= est[i];
ret += solver[i][e];
}
return ret;
}
int main() {
scanf("%d", &n);
vector<int> v((1 << n));
for (int i = int(0); i < int(1 << n); ++i) scanf("%d", &v[i]);
for (int i = int(0); i < int(1 << n); ++i) board[0][i] = v[i];
merged();
reverse((v).begin(), (v).end());
for (int i = int(0); i < int(1 << n); ++i) board[0][i] = v[i];
mergei();
int q;
scanf("%d", &q);
while (q--) {
int k;
scanf("%d", &k);
printf("%lld\n", query(k));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int INF = 0x3f3f3f3f;
int d, n, m;
struct Sta {
int x, p;
Sta() {}
Sta(int x, int p) : x(x), p(p) {}
void read() { scanf("%d%d", &x, &p); }
bool operator<(const Sta& c) const { return x < c.x; }
} p[N];
struct Node {
int Min, Minid;
} node[N << 2];
void pushup(int x) {
node[x].Min = min(node[(x << 1)].Min, node[(x << 1) | 1].Min);
if (node[x].Min == node[(x << 1) | 1].Min)
node[x].Minid = node[(x << 1) | 1].Minid;
if (node[x].Min == node[(x << 1)].Min) node[x].Minid = node[(x << 1)].Minid;
}
void build(int L = 1, int R = m, int x = 1) {
if (L == R) {
node[x].Min = p[L].p;
node[x].Minid = L;
return;
}
int mid = (L + R) >> 1;
build(L, mid, (x << 1));
build(mid + 1, R, (x << 1) | 1);
pushup(x);
}
int Min, Minid;
void getMin(int l, int r, int L = 1, int R = m, int x = 1) {
if (L >= l && R <= r) {
if (Min > node[x].Min) {
Min = node[x].Min;
Minid = node[x].Minid;
}
return;
}
int mid = (L + R) >> 1;
if (l <= mid) getMin(l, r, L, mid, (x << 1));
if (r > mid) getMin(l, r, mid + 1, R, (x << 1) | 1);
}
long long solve() {
int s = 0;
long long ans = 0;
int yu = 0;
while (s < m) {
int v = upper_bound(p + s + 1, p + m + 1, Sta(p[s].x + n, 0)) - p - 1;
if (v == s) return -1;
Min = INF;
getMin(s + 1, v);
if (Min > p[s].p) {
ans += 1LL * (n - yu) * p[s].p;
yu = n;
yu -= p[Minid].x - p[s].x;
s = Minid;
} else {
for (int j = s + 1; j <= Minid; j++) {
if (p[j].p <= p[s].p) {
int dis = p[j].x - p[s].x;
if (yu >= dis) {
yu -= dis;
} else {
ans += 1LL * (dis - yu) * p[s].p;
yu = 0;
}
s = j;
break;
}
}
}
}
return ans;
}
int main() {
scanf("%d%d%d", &d, &n, &m);
p[0] = Sta(0, 0);
for (int i = 1; i <= m; i++) p[i].read();
p[++m] = Sta(d, 0);
sort(p, p + m + 1);
build();
printf("%lld\n", solve());
return 0;
}
| 4 |
#include<iostream>
using namespace std;
int main(void){
int N;
cin >> N;
int ans=0;
for(int i=0;i<N;i++){
int a;
cin >> a;
while(a%2==0){
ans++;
a/=2;
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
long long n, m;
cin >> n >> m;
vector<int> v(n);
vector<long long> pref(n + 1);
pref[0] = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
pref[i + 1] = pref[i] + v[i];
}
vector<int> ans;
for (int i = 0; i < n; i++) {
long long sum = v[i] + v[0];
if (sum >= m && i < n - 1) continue;
bool f = 1;
int cnt = 0;
if (i % 2 == 0) cnt += v[i / 2];
for (int l = 0; l < min(30, (i / 2 + 1)); l++) {
if (pref[i - l + 1] - pref[l] + cnt != sum * (i / 2 - l + 1)) {
f = 0;
break;
}
}
cnt = 0;
if ((n - i) % 2 == 0) cnt += v[(n + i) / 2];
if (f && i < n - 1)
for (int l = 0; l < min((long long)30, ((n - i) / 2)); l++) {
if (pref[n - l] - pref[l + i + 1] + cnt !=
(sum + m) * ((n - i) / 2 - l)) {
f = 0;
break;
}
}
if (f) ans.push_back(sum % m);
}
sort(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (int i : ans) cout << i << ' ';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,"Line:",__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
void print(ll x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<endl;
#else
cout<<"Yes"<<endl;
#endif
if(ex)exit(0);
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<endl;
#else
cout<<"No"<<endl;
#endif
if(ex)exit(0);
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
int mask(int i){
return (int(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static random_device rd;
static mt19937_64 gen(rd());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
using uint=unsigned;
using ull=unsigned long long;
//initfact();
//const uint mod=998244353;
const uint mod=1000000007;
//uint mod=1;
struct mint{
uint v;
mint(ll vv=0){s(vv%mod+mod);}
mint& s(uint vv){
v=vv<mod?vv:vv-mod;
return *this;
}
mint operator-()const{return mint()-*this;}
mint& operator+=(const mint&rhs){return s(v+rhs.v);}
mint&operator-=(const mint&rhs){return s(v+mod-rhs.v);}
mint&operator*=(const mint&rhs){
v=ull(v)*rhs.v%mod;
return *this;
}
mint&operator/=(const mint&rhs){return *this*=rhs.inv();}
mint operator+(const mint&rhs)const{return mint(*this)+=rhs;}
mint operator-(const mint&rhs)const{return mint(*this)-=rhs;}
mint operator*(const mint&rhs)const{return mint(*this)*=rhs;}
mint operator/(const mint&rhs)const{return mint(*this)/=rhs;}
mint pow(int n)const{
mint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
mint inv()const{return pow(mod-2);}
/*mint inv()const{
int x,y;
int g=extgcd(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return mint(x);
}*/
friend ostream& operator<<(ostream&os,const mint&m){
return os<<m.v;
}
bool operator<(const mint&r)const{return v<r.v;}
bool operator==(const mint&r)const{return v==r.v;}
bool operator!=(const mint&r)const{return v!=r.v;}
};
const int vmax=(1<<21)+10;
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
fact[0]=1;
rng(i,1,vmax){
fact[i]=fact[i-1]*i;
}
finv[vmax-1]=fact[vmax-1].inv();
for(int i=vmax-2;i>=0;i--){
finv[i]=finv[i+1]*(i+1);
}
for(int i=vmax-1;i>=1;i--){
invs[i]=finv[i]*fact[i-1];
}
}
mint choose(int n,int k){
return fact[n]*finv[n-k]*finv[k];
}
mint binom(int a,int b){
return fact[a+b]*finv[a]*finv[b];
}
mint catalan(int n){
return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
vi p,s;
int c;
unionfind(int n):p(n,-1),s(n,1),c(n){}
int find(int a){
return p[a]==-1?a:(p[a]=find(p[a]));
}
//set b to a child of a
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
p[b]=a;
s[a]+=s[b];
c--;
return true;
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int a){
return s[find(a)];
}
};
const int L=15;
mint dp[1<<L][L+1];
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
initfact();
int n;cin>>n;
int q;cin>>q;
vc<pi> es;
vi vs;
rep(_,q){
int a,b;cin>>a>>b;
a--;
vs.pb(a);
vs.pb(b);
es.eb(a,b);
}
mkuni(vs);
int s=vs.size();
unionfind uf(s);
for(auto e:es){
int a=lwb(vs,e.a);
int b=lwb(vs,e.b);
uf.unite(a,b);
}
vi idx(s,-1);
int h=0;
{
rep(i,s)if(uf.find(i)==i)
idx[i]=h++;
}
dmp(vs);
dmp(idx);
mint base=1;
base*=mint(10).pow(vs[0]);
base*=mint(10).pow(n-vs[s-1]);
vvc<mint> g(h,vc<mint>(h,1));
rep(i,s-1){
int d=vs[i+1]-vs[i];
mint w=(mint(10).pow(d)-1)/9;
int a=idx[uf.find(i)];
int b=idx[uf.find(i+1)];
if(a==b){
base*=w+1;
}else{
base*=w;
w=(w+1)/w;
g[a][b]*=w;
g[b][a]*=w;
}
}
dmp(base);
dmp(g);
rep(bit,1<<h){
mint w=1;
rep(i,h)if(bit&1<<i)rep(j,i)if(bit&1<<j)
w*=g[i][j];
dp[bit][popcount(bit)]=w;
}
rep(i,h)rep(bit,1<<h)if(!(bit&1<<i)){
rep(j,L+1)
dp[bit^1<<i][j]+=dp[bit][j];
}
rep(bit,1<<h){
mint z[L+1];
rep(i,h+1)z[i]=dp[bit][i];
rep(_,8){
per(i,h+1){
rng(j,1,h-i+1)
dp[bit][i+j]+=dp[bit][i]*z[j];
dp[bit][i]*=z[0];
}
}
}
rep(i,h)per(bit,1<<h)if(bit&1<<i){
rep(j,h+1)
dp[bit][j]-=dp[bit^1<<i][j];
}
cout<<base*dp[mask(h)][h]/9<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[1010][1010];
long long s[1010], sSqr[1010];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
s[i] += a[i][j];
sSqr[i] += 1LL * a[i][j] * a[i][j];
}
long long sumV, sumVSqr;
for (int i = 1; i < k - 1; i++)
if (s[i] - s[i - 1] == s[i + 1] - s[i])
{
sumV = s[i] - s[i - 1];
sumVSqr = (sSqr[i - 1] + sSqr[i + 1]) / 2 - sSqr[i];
break;
}
int bad = -1;
for (int i = 1; i < k - 1; i++)
if (s[i] != s[0] + sumV * i)
{
bad = i;
break;
}
long long expectedSSqr = (sSqr[bad - 1] + sSqr[bad + 1]) / 2 - sumVSqr;
long long diffS = sumV - (s[bad] - s[bad - 1]);
for (int j = 0; j < n; j++)
{
long long newA = a[bad][j] + diffS;
long long newSSqr = sSqr[bad] - 1LL * a[bad][j] * a[bad][j] + newA * newA;
if (newSSqr == expectedSSqr)
{
cout << bad << ' ' << newA << endl;
cout.flush();
return 0;
}
}
assert(0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MAXn = 100;
long long a[MAXn];
long long t[MAXn][MAXn];
int n;
bool check() {
long long s = 0;
for (int i = 0, __e = n; i < __e; i++) s += t[0][i];
for (int i = 0, __e = n; i < __e; i++) {
long long x = 0;
for (int j = 0, __e = n; j < __e; j++) x += t[i][j];
if (x != s) return false;
}
for (int i = 0, __e = n; i < __e; i++) {
long long x = 0;
for (int j = 0, __e = n; j < __e; j++) x += t[j][i];
if (x != s) return false;
}
long long x = 0;
for (int i = 0, __e = n; i < __e; i++) x += t[i][i];
if (x != s) return false;
x = 0;
for (int i = 0, __e = n; i < __e; i++) x += t[i][n - 1 - i];
if (x != s) return false;
return true;
}
long long EX;
map<long long, int> mp;
long long csum[10], rsum[10];
void bt(int lvl) {
int r = lvl / n, c = lvl % n;
if (r == n - 1) {
if (check()) {
long long sum = 0;
for (int i = 0, __e = n; i < __e; i++) sum += t[0][i];
cout << sum << endl;
for (int i = 0, __e = n; i < __e; i++) {
for (int j = 0, __e = n; j < __e; j++) cout << t[i][j] << " ";
cout << endl;
}
exit(0);
}
return;
}
if (c != n - 1) {
for (__typeof(mp.begin()) _ind = (mp).begin(); _ind != (mp).end(); _ind++)
for (bool _flag = true; _flag; _flag = false)
for (__typeof(*(mp).begin()) &el = *_ind; _flag; _flag = false) {
long long x = el.first;
bool ok = true;
if (el.second == 0) continue;
t[r][c] = x;
csum[c] += x;
rsum[r] += x;
if (r == n - 2) {
t[n - 1][c] = EX - csum[c];
if (mp.count(t[n - 1][c]) == 0 || mp[t[n - 1][c]] == 0)
ok = false;
else
mp[t[n - 1][c]]--;
}
if (ok && mp[x] > 0) {
mp[x]--;
bt(lvl + 1);
mp[x]++;
}
csum[c] -= x;
rsum[r] -= x;
if (r == n - 2 && ok) mp[t[n - 1][c]]++;
}
} else {
t[r][c] = EX - rsum[r];
if (mp.count(t[r][c]) == 0 || mp[t[r][c]] == 0) return;
csum[c] += t[r][c];
rsum[r] += t[r][c];
bool ok = true;
if (r == n - 2) {
t[n - 1][c] = EX - csum[c];
if (mp.count(t[n - 1][c]) == 0 || mp[t[n - 1][c]] == 0)
ok = false;
else
mp[t[n - 1][c]]--;
}
if (ok && mp[t[r][c]] > 0) {
mp[t[r][c]]--;
bt(lvl + 1);
mp[t[r][c]]++;
}
csum[c] -= t[r][c];
rsum[r] -= t[r][c];
if (r == n - 2 && ok) mp[t[n - 1][c]]++;
}
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
int m = n * n;
for (int i = 0, __e = m; i < __e; i++) cin >> a[i], mp[a[i]]++;
if (n == 1) {
cout << a[0] << endl;
cout << a[0] << endl;
return 0;
}
long long p = 0, q = 0;
for (int i = 0, __e = m; i < __e; i++) {
q += a[i];
p += q / n;
q %= n;
}
EX = p;
bt(0);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long MOD = 1e9 + 7;
long long h[26][N], p[N];
int a[30], b[30];
char s[N + 10];
long long get(int i, int l, int r) {
return (h[i][r] - h[i][l - 1] * p[r - l + 1]) % MOD;
}
int main() {
int n, m, x, y, len;
while (scanf("%d%d", &n, &m) != EOF) {
p[0] = 1;
for (int i = 1; i <= n; i++) p[i] = (p[i - 1] * 2) % MOD;
scanf(" %s", s + 1);
for (int i = 0; i < 26; i++)
for (int j = 1; j <= n; j++)
h[i][j] = (h[i][j - 1] * 2 + (s[j] == 'a' + i)) % MOD;
while (m--) {
scanf("%d%d%d", &x, &y, &len);
for (int i = 0; i < 26; i++) {
a[i] = (get(i, x, x + len - 1) + MOD) % MOD;
b[i] = (get(i, y, y + len - 1) + MOD) % MOD;
}
sort(a, a + 26);
sort(b, b + 26);
bool flag = true;
for (int i = 0; i < 26; i++)
if (a[i] != b[i]) {
flag = false;
break;
}
printf("%s\n", flag ? "YES" : "NO");
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int MAX_N=262144;
vector<vector<int64_t>> seg(MAX_N*2-1,vector<int64_t>(2));
int64_t qu(int t,int a,int b,int k,int l, int r){
if(r<=a||b<=l){
return -1;
}
else if(a<=l&&r<=b){
return seg[k][t];
}
else{
int64_t X=qu(t,a,b,k*2+1,l,(l+r)/2);
int64_t Y=qu(t,a,b,k*2+2,(l+r)/2,r);
return max(X,Y);
}
}
void chmax(int t,int64_t a,int b){
int D=MAX_N-1+b;
seg[D][t]=a;
while(D>0){
D--;
D/=2;
seg[D][t]=max(seg[2*D+1][t],seg[2*D+2][t]);
}
}
int main(){
int64_t N,Q;
cin>>N>>Q;
int64_t Z=(N-2)*(N-2);
chmax(0,N,N-2);
chmax(1,N,N-2);
for(int i=0;i<Q;i++){
int t,b,T;
cin>>t>>b;
t--;
T=(t+1)%2;
int l=0,r=N;
while(r-l>1){
int D=(l+r)/2;
if(qu(T,0,D,0,0,MAX_N)>=b-1){
r=D;
}
else{
l=D;
}
}
Z-=l;
chmax(t,l,b-2);
}
cout<<Z<<endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int M,N;
string S[130];
int dp[(1<<11)][(1<<11)];
bool check(int st,int fr){
int mem[130]={};
int n = __builtin_popcount(st);
for(int i=0;i<M;i++){
if( st & (1<<i) ) {
for(int j=0;j<N;j++){
if( S[j][i] == '0' && !(fr & (1<<i)) ){
mem[j]++;
} else if( S[j][i] == '1' && (fr & (1<<i) ) ) {
mem[j]++;
}
}
}
}
int cnt=0;
for(int j=0;j<N;j++){
if( mem[j] == n ) cnt++;
}
if( cnt == 1 ) return true;
return false;
}
int solve(int st,int fr){
if( dp[st][fr] != -1 ) return dp[st][fr];
if( st>0 && check(st,fr) ) return dp[st][fr] = 0;
if( st == (1<<M) - 1 ) return dp[st][fr] = 0;
int ret = 100000;
for(int i=0;i<M;i++){
if( (st&(1<<i)) ) continue;
ret = min( ret,
max( solve(st|(1<<i),fr|(1<<i))+1,
solve(st|(1<<i),fr)+1 ) );
}
return dp[st][fr] = ret;
}
int main() {
while( cin >> M >> N && ( M||N ) ){
memset(dp,-1,sizeof(dp));
for(int i=0;i<N;i++) cin >> S[i];
if( N == 1) cout << 0 << endl;
else cout << solve( 0, 0 ) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int pop_count(int bits){
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}
int N, M;
vector<pair<int, int>> edges[50];
int mask[50];
bool dfs_mask(int i, int p, int target, int ei, int b){
bool ok = (i==target);
for(auto& e : edges[i]) if(e.first != p) ok |= dfs_mask(e.first, i, target, e.second, b);
if(ok && ei >= 0) mask[ei] |= 1<<b;
return ok;
}
int main(){
cin >> N;
for(int i=0; i<N-1; i++){
int a, b;
cin >> a >> b;
edges[a-1].emplace_back(b-1, i);
edges[b-1].emplace_back(a-1, i);
}
cin >> M;
for(int i=0; i<M; i++){
int a, b;
cin >> a >> b;
dfs_mask(a-1, -1, b-1, -1, i);
}
int64_t ans = 0;
for(int b=0; b<(1<<M); b++){
int64_t res = 1;
for(int i=0; i<N-1; i++) if(!(b & mask[i])) res *= 2;
if(pop_count(b)%2) res *= -1;
ans += res;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <stdio.h>
int main(){
int n, m, tl[10001], tr[10001], i, j, k, max, data[20005] ;
while(1){
scanf("%d %d", &n, &m);
if(n==0 && m==0) break;
for(i=0;i<n;i++){
scanf("%d", &tl[i]);
}
tl[i]=1000001;
for(i=0;i<m;i++){
scanf("%d", &tr[i]);
}
tr[i]=1000001;
j=0;
k=0;
data[0]=0;
for(i=1;i<m+n+1;i++){
if(tl[j]<=tr[k]){
data[i]=tl[j];
j++;
}
else if(tl[j]>tr[k]){
data[i]=tr[k];
k++;
}
}
max=-1;
for(i=0;i<m+n;i++){
//printf("%d ", data[i]);
if(max<data[i+1]-data[i]) max=data[i+1]-data[i];
}
printf("%d\n", max);
//printf("\n");
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005,M=1e9+7;
ll fac[N]= {1,1},inv[N]= {1,1},f[N]= {1,1},pos[N];
ll C(ll a,ll b) {
if(b>a)
return 0;
return fac[a]*inv[b]%M*inv[a-b]%M;
}
void init() { //快速计算阶乘的逆元
for(int i=2; i<N; i++) {
fac[i]=fac[i-1]*i%M;
f[i]=(M-M/i)*f[M%i]%M;
inv[i]=inv[i-1]*f[i]%M;
}
}
int main() {
init();
int n;
scanf("%d",&n);
n++;
long long m,x;
for(int i = 1; i <= n; i++) {
scanf("%lld", &x);
if (pos[x]) {
m = n - (i - pos[x] + 1);
break;
}
pos[x] = i;
}
for(int i=1; i<=n; i++) {
printf("%lld\n", (C(n, i) - C(m, i-1)+M) % M);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[1000010];
int main() {
long long n, ans = 0, j;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[x]++;
}
ans = n;
for (int i = 0; i <= 1000000; i++) {
a[i + 1] += a[i] / 2;
ans -= a[i] / 2;
}
while (a[1000001] != 0) {
ans -= a[1000001] / 2;
a[1000001] = a[1000001] / 2;
}
printf("%I64d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300001;
int a[3][N];
int next_pos[3][N];
map<long long, int> last;
struct data {
int c, v;
data(int c, int v) : c(c), v(v) {}
friend bool operator<(data e, data r) {
if (min(e.c, e.v) < min(r.c, r.v)) {
return 0;
}
if (min(e.c, e.v) > min(r.c, r.v)) {
return 1;
}
if (e.c < r.c) {
return 0;
}
if (e.c > r.c) {
return 1;
}
return e.v > r.v;
}
};
priority_queue<data> m_queue;
map<data, int> vals;
bool add(data t, int val) {
if (max(t.c, t.v) >= 1 << 30) {
return 0;
}
if (vals.count(t)) {
vals[t] = max(vals[t], val);
} else {
vals[t] = val;
m_queue.push(t);
}
return 1;
}
int main() {
int q, w, e, r, t = 0, numV;
cin >> numV;
for (w = 0; w < 2; w++) {
for (e = 0; e < numV; e++) {
cin >> a[w][e];
}
a[w][numV] = 0;
}
for (e = 0; e < numV; e++) {
a[2][e] = a[0][e] + a[1][e];
}
a[2][numV] = 0;
for (w = 0; w < 3; w++) {
last.clear();
long long sum = 0;
int cur_min = 1 << 30;
for (e = numV; e >= 0; e--) {
sum += a[w][e];
if (last.count(sum)) {
cur_min = min(cur_min, last[sum]);
}
next_pos[w][e] = cur_min;
last[sum] = e;
}
}
m_queue.push(data(0, 0));
vals[data(0, 0)] = 0;
int ans = 0;
while (!m_queue.empty()) {
data c = m_queue.top();
m_queue.pop();
int val = vals[c];
ans = max(ans, val);
if (c.c < c.v) {
if (!add(data(next_pos[0][c.c], c.v), val + 1)) {
add(data(c.c, next_pos[1][c.v]), val + 1);
}
} else {
if ((!add(data(c.c, next_pos[1][c.v]), val + 1)) || (c.c == c.v)) {
add(data(next_pos[0][c.c], c.v), val + 1);
}
}
if (c.c != c.v) {
add(data(max(c.c, c.v), max(c.c, c.v)), val);
} else {
int mx_next = next_pos[2][max(c.c, c.v)];
add(data(mx_next, mx_next), val + 1);
}
}
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, x[2010], y[2010];
char str[2010];
bool vis[2010];
struct Node {
long long x, y;
Node operator+(const Node &u) const { return (Node){x + u.x, y + u.y}; }
Node operator-(const Node &u) const { return (Node){x - u.x, y - u.y}; }
} node[2010], last;
inline long long cj(Node u, Node v) { return u.x * v.y - v.x * u.y; }
int main() {
long long i, j, t, mn, mx;
cin >> n;
t = 1;
for (i = 1; i <= n; i++) {
scanf("%lld%lld", &node[i].x, &node[i].y);
if (node[i].x < node[t].x ||
node[i].x == node[t].x && node[i].y > node[t].y)
t = i;
}
vis[t] = 1;
scanf("%s", str + 1);
cout << t << " ";
last = node[t];
for (i = 1; i <= n - 2; i++) {
for (j = 1; j <= n; j++) {
if (vis[j]) continue;
break;
}
t = j;
for (j = t + 1; j <= n; j++) {
if (vis[j]) continue;
if ((cj(node[j] - last, node[j] - node[t]) < 0) ^ (str[i] == 'R')) t = j;
}
printf("%lld ", t);
last = node[t];
vis[t] = 1;
}
for (i = 1; i <= n; i++) {
if (!vis[i]) {
cout << i;
return 0;
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, N, a[2005][2005], mn = 2000000000, mnp, dis[2005], vis[2005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] < mn) {
mn = a[i][j];
mnp = i;
}
}
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++) a[i][j] = a[j][i] = a[i][j] - mn;
for (int i = 1; i <= n; i++) {
dis[i] = a[mnp][i];
for (int j = 1; j <= n; j++)
if (i != j) dis[i] = min(dis[i], a[i][j] * 2);
}
dis[0] = 2000000000;
vis[mnp] = 1;
for (int FUCK = 1; FUCK < n; FUCK++) {
int cur = 0;
for (int i = 1; i <= n; i++)
if (dis[i] <= dis[cur] && !vis[i]) cur = i;
vis[cur] = 1;
for (int i = 1; i <= n; i++)
if (!vis[i]) dis[i] = min(dis[i], dis[cur] + a[i][cur]);
}
for (int i = 1; i <= n; i++) cout << 1ll * mn * (n - 1) + dis[i] << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
long long int n = t;
vector<int> v;
while (t--) {
int x;
cin >> x;
v.push_back(x);
}
long long int ans = 0;
ans = v[0];
for (int i = 1; i < n; ++i) {
ans += abs(v[i] - v[i - 1]);
}
ans += n + n - 1;
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Inf = 1e9;
const int Maxn = 234;
bool isp[22233];
void getp() {
isp[1] = 1;
for (int i = 2; i <= 20000; i++)
if (!isp[i]) {
for (int j = i + i; j <= 20000; j += i) isp[j] = 1;
}
}
int n, s, t, ne, ans;
int in[Maxn], h[Maxn], cur[Maxn];
vector<int> G[Maxn];
pair<int, int> e[200020];
bool done[Maxn];
bool bfs() {
for (int i = s; i <= t; i++) h[i] = i == s ? 0 : Inf;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < G[u].size(); i++) {
if (!e[G[u][i]].first) continue;
int v = e[G[u][i]].second;
if (h[v] == Inf) h[v] = h[u] + 1, q.push(v);
}
}
return h[t] != Inf;
}
int dfs(int u, int a) {
if (u == t || !a) return a;
int flow = 0, b;
for (int &i = cur[u]; i < G[u].size() && a; i++) {
int id = G[u][i], v = e[id].second;
if (h[v] == h[u] + 1 && (b = dfs(v, min(a, e[id].first)))) {
e[id ^ 1].first += b;
e[id].first -= b;
a -= b;
flow += b;
}
}
return flow;
}
void add(int u, int v, int c) {
e[ne] = pair<int, int>(c, v);
G[u].push_back(ne++);
e[ne] = pair<int, int>(0, u);
G[v].push_back(ne++);
}
vector<int> V[Maxn];
int cnt = 0;
void dfs(int u) {
done[u] = 1;
V[cnt].push_back(u);
for (int i = 0; i < G[u].size(); i++) {
int v = e[G[u][i]].second, c = e[G[u][i]].first;
if (v != s && v != t && !done[v] && ((c == 1) ^ (in[u] & 1))) {
dfs(v);
return;
}
}
}
void pt() {
for (int i = 1; i <= n; i++)
if (!done[i]) {
V[++cnt].clear();
dfs(i);
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) {
printf("%d", (int)V[i].size());
for (int j = 0; j < V[i].size(); j++) printf(" %d", V[i][j]);
puts("");
}
}
int main() {
getp();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", in + i);
s = 0;
t = n + 1;
for (int i = 1; i <= n; i++)
if (in[i] & 1) {
add(s, i, 2);
for (int j = 1; j <= n; j++)
if (!isp[in[j] + in[i]]) add(i, j, 1);
} else
add(i, t, 2);
while (bfs()) {
memset(cur, 0, sizeof(cur));
ans += dfs(s, Inf);
}
if (ans != n || !ans)
puts("Impossible");
else
pt();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int C = 26;
void add(int &x, int y) {
x += y;
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
}
int fix(int x) {
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
return x;
}
int pw(int a, int b) {
int ret = 1;
while (b) {
if (b & 1) ret = 1ll * ret * a % MOD;
b >>= 1;
a = 1ll * a * a % MOD;
}
return ret;
}
const int MAXN = 1e6 + 10;
const float INF = 1e18;
template <typename T>
T sq(T x) {
return x * x;
}
long long n, x[MAXN], y[MAXN];
bool can(float R) {
bool first = false;
float lo = -INF, hi = INF;
for (int i = 0; i < n; i++) {
float limit = -sq(y[i]) + 2 * y[i] * R;
if (limit < 0) {
return false;
}
limit = sqrtl(limit);
if (first) {
lo = x[i] - limit;
hi = x[i] + limit;
first = false;
}
lo = max(lo, x[i] - limit);
hi = min(hi, x[i] + limit);
}
return lo < hi;
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
bool pos = false, neg = false;
for (int i = 0; i < n; i++) pos |= y[i] > 0, neg |= y[i] < 0;
if (pos && neg) {
cout << "-1\n";
return;
}
if (neg) {
for (int i = 0; i < n; i++) {
y[i] *= -1;
}
}
float hi = 1;
while (!can(hi)) hi *= 2;
cerr << hi << "\n";
float lo = 0;
for (int rep = 0; rep < 100; rep++) {
float mid = 0.5 * (lo + hi);
if (can(mid)) {
hi = mid;
} else {
lo = mid;
}
}
cout << hi << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
int te = 1;
for (int w = 1; w <= te; w++) {
solve();
}
return 0;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.