solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-10;
template <typename T, typename S>
vector<T>& operator<<(vector<T>& a, S b) {
a.push_back(b);
return a;
}
template <typename T>
void operator>>(vector<T>& a, int b) {
while (b--)
if (!a.empty()) a.pop_back();
}
bool isprime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
long long b_pow(long long x, long long n) {
return n ? b_pow(x * x, n / 2) * (n % 2 ? x : 1) : 1ll;
}
string itos(int n) {
stringstream ss;
ss << n;
return ss.str();
}
int e[10000001];
int main() {
ios_base::sync_with_stdio(false);
for (int i = 1; i * i <= 10000000; i++) {
for (int j = i * i; j <= 10000000; j += i * i) {
e[j] = max(i * i, e[j]);
}
}
int a, n;
cin >> a >> n;
long long p = 0;
for (int i = a; i < a + n; i++) {
if (e[i] == 0) {
p += i;
} else
p += i / e[i];
}
cout << p << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 50;
long long n, m, x, y, sum[maxn], dp[maxn][2];
char c[maxn][maxn];
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m >> x >> y;
for (long long i = 1; i <= n; i++) {
cin >> (c[i] + 1);
}
for (long long i = 1; i <= m; i++) {
for (long long j = 1; j <= n; j++) {
sum[i] += (c[j][i] == '#');
}
}
for (long long i = 1; i <= m; i++) {
sum[i] += sum[i - 1];
}
memset(dp, 0x3f3f3f3f, sizeof(dp));
dp[0][0] = dp[0][1] = 0LL;
for (long long i = x; i <= y; i++) {
dp[i][0] = n * i - sum[i];
dp[i][1] = sum[i];
}
for (long long i = x + 1; i <= m; i++) {
for (long long j = i - x + 1; j >= max(i - y + 1, 1LL); j--) {
dp[i][0] =
min(dp[i][0], dp[j - 1][1] + n * (i - j + 1) - sum[i] + sum[j - 1]);
dp[i][1] = min(dp[i][1], dp[j - 1][0] + sum[i] - sum[j - 1]);
}
}
cout << min(dp[m][0], dp[m][1]);
}
| 3 |
#include <iostream>
using namespace std;
int main() {
int a, b, ans;
for (int i = 0; i < 7; ++i) {
cin >> a >> b;
cout << a - b << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int n;
int a[MX], b[MX];
int Tree[3 * MX][2];
void Build(int node, int start, int end) {
if (start == end) {
Tree[node][0] = a[start];
Tree[node][1] = b[start];
} else {
int mid = (start + end) >> 1;
Build(node * 2, start, mid);
Build(node * 2 + 1, mid + 1, end);
Tree[node][0] = max(Tree[node * 2][0], Tree[node * 2 + 1][0]);
Tree[node][1] = min(Tree[node * 2][1], Tree[node * 2 + 1][1]);
}
}
pair<int, int> out;
void Query(int node, int start, int end, int i, int j) {
if (start > end || i > j)
return;
else if (i == start && end == j) {
out.first = max(out.first, Tree[node][0]);
out.second = min(out.second, Tree[node][1]);
} else {
int mid = (start + end) >> 1;
Query(node * 2, start, mid, i, min(mid, j));
Query(node * 2 + 1, mid + 1, end, max(mid + 1, i), j);
}
}
int f(int start, int end) {
out.first = INT_MIN;
out.second = INT_MAX;
Query(1, 0, n - 1, start, end);
return out.first - out.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
Build(1, 0, n - 1);
long long ans = 0;
for (int l = 0; l < n; l++) {
int rmin = -1, rmax = -1;
int low = l, high = n - 1;
while (low <= high) {
int mid = (low + high) >> 1;
int val = f(l, mid);
if (val < 0)
low = mid + 1;
else if (val > 0)
high = mid - 1;
else {
rmin = mid;
high = mid - 1;
}
}
if (rmin == -1) continue;
low = l, high = n - 1;
while (low <= high) {
int mid = (low + high) >> 1;
int val = f(l, mid);
if (val < 0)
low = mid + 1;
else if (val > 0)
high = mid - 1;
else {
rmax = mid;
low = mid + 1;
}
}
ans += (rmax - rmin + 1);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s = 0, a[200001] = {0};
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
if (n == 1) {
s = 0;
} else {
int m = 0, y = 0, f = 0;
m = a[0];
if (a[0] == 1) {
y = 1;
} else
f = 1;
for (int i = 1; i < n; ++i) {
if (a[i] == 1) {
if (a[i - 1] == 1) {
m++;
} else {
s = max(s, m);
m = 1;
}
if (f == 0) y++;
} else
f = 1;
}
if (a[n - 1] == 1) m += y;
s = max(s, m);
}
cout << s << endl;
return 0;
}
| 2 |
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int i, q;
string str;
cin >> str >> q;
for (i=0; i<q; i++) {
string f, g;
int a, b;
cin >> f >> a >> b;
if (f=="replace") {
cin >> g;
str.replace(a, b-a+1, g);
} else if (f=="reverse") {
reverse(str.begin()+a, str.begin()+b+1);
} else if (f=="print") {
cout << string(&str[a], &str[b+1]) << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f2f1f0f;
const long long LINF = 1ll * INF * INF;
const int MAX_M = 33, MAX_N = 65, MAX_S = 2550;
int M, Mr[MAX_M];
bool Dy[MAX_M][MAX_N][MAX_S];
int From[MAX_M][MAX_N][MAX_S];
inline bool isCan(int j, int s) { return s >= j * (j - 1) / 2; }
vector<int> Val;
void getList(int m, int j, int s) {
if (m == 0) return;
int i = From[m][j][s];
Val.push_back(Mr[m]);
if (i == m) return getList(m, j - 1, s - Mr[m]);
return getList(m - 1, j - 1, s - Mr[m]);
}
int Ans[MAX_N][MAX_N];
int main() {
cin >> M;
for (int i = 1; i <= M; i++) scanf("%d", &Mr[i]);
sort(Mr + 1, Mr + M + 1);
Dy[0][0][0] = true;
for (int i = 1; i <= M; i++) {
for (int j = 0; j < MAX_N; j++)
for (int s = 0; s < MAX_S; s++) {
if ((Dy[i - 1][j][s] || Dy[i][j][s]) && isCan(j + 1, s + Mr[i])) {
Dy[i][j + 1][s + Mr[i]] = true;
From[i][j + 1][s + Mr[i]] = (Dy[i][j][s] ? i : i - 1);
}
}
}
for (int j = 0; j < MAX_N; j++)
if (Dy[M][j][j * (j - 1) / 2]) {
getList(M, j, j * (j - 1) / 2);
reverse((Val).begin(), (Val).end());
int N = ((int)(Val).size());
vector<pair<int, int> > W;
for (int i = 0; i < N; i++) W.push_back(pair<int, int>(Val[i], i));
for (int i = N - 1; i >= 0; i--) {
int ix = W[i].second;
int l = i - W[i].first;
sort(W.begin(), W.begin() + i);
for (int j = i - 1; j >= 0; j--) {
int jx = W[j].second;
if (l > 0) {
W[j].first--, l--;
Ans[jx][ix] = 1;
} else
Ans[ix][jx] = 1;
}
}
printf("%d\n", N);
for (int i = 0; i < N; i++, puts(""))
for (int j = 0; j < N; j++) printf("%d", Ans[i][j]);
return 0;
}
puts("=(");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long INF = (long long)5e18;
const int MOD = 998244353;
int _abs(int x) { return x < 0 ? -x : x; }
int add(int x, int y) {
x += y;
return x >= MOD ? x - MOD : x;
}
int sub(int x, int y) {
x -= y;
return x < 0 ? x + MOD : x;
}
void Add(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void Sub(int &x, int y) {
x -= y;
if (x < 0) x += MOD;
}
void Mul(int &x, int y) { x = (long long)(x) * (y) % MOD; }
int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = (long long)(ret) * (x) % MOD;
x = (long long)(x) * (x) % MOD;
y >>= 1;
}
return ret;
}
void checkmin(int &x, int y) {
if (x > y) x = y;
}
void checkmax(int &x, int y) {
if (x < y) x = y;
}
void checkmin(long long &x, long long y) {
if (x > y) x = y;
}
void checkmax(long long &x, long long y) {
if (x < y) x = y;
}
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * f;
}
const int N = 1001;
const int M = 500005;
int first[N], nxt[M], point[M], w[M], cur[N], e = 0, tot = 0;
int S, T, a[N], id[N][N];
namespace Flow {
void add_edge(int x, int y, int z) {
point[e] = y;
w[e] = z;
nxt[e] = first[x];
first[x] = e++;
}
void add(int x, int y, int z) {
add_edge(x, y, z);
add_edge(y, x, 0);
id[x][y] = e - 1;
id[y][x] = e - 1;
}
int vis[N], dep[N];
bool bfs() {
queue<int> q;
while (!q.empty()) q.pop();
for (int i = 1; i <= tot; i++) vis[i] = 0;
vis[S] = 1;
q.push(S);
dep[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = first[u]; i != -1; i = nxt[i])
if (w[i]) {
int to = point[i];
if (vis[to]) continue;
dep[to] = dep[u] + 1;
vis[to] = 1;
q.push(to);
}
}
return vis[T];
}
int dfs(int u, int flow) {
if (u == T) return flow;
int ret = flow;
for (int &i = cur[u]; i != -1; i = nxt[i])
if (w[i]) {
int to = point[i];
if (dep[to] != dep[u] + 1) continue;
int tmp = dfs(to, min(w[i], ret));
if (tmp) w[i] -= tmp, w[i ^ 1] += tmp, ret -= tmp;
if (!ret) break;
}
return flow - ret;
}
int Dinic() {
int ret = 0;
while (bfs()) {
for (int i = 1; i <= tot; i++) cur[i] = first[i];
ret += dfs(S, inf);
}
return ret;
}
} // namespace Flow
int n, bl[M];
void init() {
memset(first, -1, sizeof(first));
n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 2; i < M; i++) {
if (!bl[i])
for (int j = i + i; j < M; j += i) bl[j] = 1;
}
tot = n;
S = ++tot;
T = ++tot;
for (int i = 1; i <= n; i++) {
if (a[i] & 1) {
Flow::add(S, i, 2);
for (int j = 1; j <= n; j++)
if (!bl[a[i] + a[j]]) {
Flow::add(i, j, 1);
}
} else
Flow::add(i, T, 2);
}
}
int vis[N];
vector<vector<int> > ans;
int main() {
init();
int F = Flow::Dinic();
if (F != n) {
puts("Impossible");
return 0;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
vector<int> v;
int x = i;
while (1) {
vis[x] = 1;
v.push_back(x);
int to = -1;
for (int j = 1; j <= n; j++)
if (w[id[x][j]] && !vis[j]) {
to = j;
break;
}
if (to == -1) break;
x = to;
}
ans.push_back(v);
}
printf("%d\n", (int)(ans).size());
for (auto &v : ans) {
printf("%d ", (int)(v).size());
for (auto &u : v) printf("%d ", u);
puts("");
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k,sum=0;
cin>>n>>k;
int a[n+1],bj[210000];
memset(bj,0,sizeof(bj));
for(int i=1;i<=n;i++){
cin>>a[i];
bj[a[i]]++;
}
sort(bj+1,bj+n+1);
for(int i=1;i<=n-k;i++){
sum+=bj[i];
}
cout<<sum;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[1000], b[1000];
int main() {
int n, i, j, k, l, ans = 0;
cin >> n;
double x, y;
cin >> x >> y;
int a[n], b[n];
for (i = 0; i < n; i++) cin >> a[i] >> b[i];
double f[n];
for (i = 0; i < n; i++) {
if (a[i] != x)
f[i] = (b[i] - y) / (a[i] - x);
else
f[i] = INT_MAX;
}
sort(f, f + n);
for (i = 0; i < n; i++) {
ans++;
while (f[i + 1] == f[i]) i++;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int n, x[maxn], p[maxn];
int cnt1, cnt2, ans;
char ch;
bool check(int mid) {
int ptr = 1;
for (int i = 1; i <= cnt1; i++) {
int tmp = x[ptr];
if (ptr > cnt2) {
return true;
}
if ((p[i] - tmp) > mid) {
return false;
}
if (tmp <= p[i]) {
while (ptr <= cnt2 &&
(x[ptr] - tmp + min(p[i] - tmp, abs(x[ptr] - p[i]))) <= mid) {
ptr++;
}
} else {
while (ptr <= cnt2 && x[ptr] - p[i] <= mid) {
ptr++;
}
}
}
if (ptr <= cnt2) {
return false;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
cnt1 = 0, cnt2 = 0;
for (int i = 0; i < n; i++) {
cin >> ch;
if (ch == 'P') {
p[++cnt1] = i;
}
if (ch == '*') {
x[++cnt2] = i;
}
}
int l = 0, r = n << 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[200007];
int b[200007];
int res[200007];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int i = 1;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int b;
cin >> b;
a[i] = {0, b};
}
int q;
cin >> q;
int z = q;
while (q--) {
int p;
cin >> p;
int l, v;
if (p == 1) {
cin >> l >> v;
a[l - 1] = {i, v};
i++;
} else {
cin >> v;
b[i - 1] = max(b[i - 1], v);
}
}
for (int k = z; k >= 0; k--) {
b[k] = max(b[k + 1], b[k]);
}
for (int k = 0; k < n; k++) {
res[k] = max(a[k].second, b[a[k].first]);
cout << res[k] << " ";
}
cout << ("") << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, x, y;
long long mod = 1000000007;
vector<long long> vec;
vector<long long> mult(vector<long long> a, vector<long long> b) {
vector<long long> res;
res.push_back(a[0] * b[0] + a[1] * b[2]);
res[0] %= mod;
res[0] += mod;
res[0] %= mod;
res.push_back(a[0] * b[1] + a[1] * b[3]);
res[1] %= mod;
res[1] += mod;
res[1] %= mod;
res.push_back(a[2] * b[0] + a[3] * b[2]);
res[2] %= mod;
res[2] += mod;
res[2] %= mod;
res.push_back(a[2] * b[1] + a[3] * b[3]);
res[3] %= mod;
res[3] += mod;
res[3] %= mod;
return res;
}
vector<long long> binp(vector<long long> v, long long po) {
if (po == 1) return v;
if (po % 2 == 0) {
vector<long long> cur = binp(v, po / 2);
return mult(cur, cur);
} else {
po--;
vector<long long> rres = binp(v, po);
return mult(rres, vec);
}
}
int main() {
cin >> x >> y >> n;
y %= mod;
y += mod;
y %= mod;
x %= mod;
x += mod;
x %= mod;
if (n == 1) {
cout << x << endl;
return 0;
}
if (n == 2) {
cout << y << endl;
return 0;
}
n -= 2;
vec.push_back(0);
vec.push_back(-1);
vec.push_back(1);
vec.push_back(1);
vector<long long> vv = binp(vec, n);
long long res = vv[1] * x + vv[3] * y;
res %= mod;
res += mod;
res %= mod;
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;
int d[17];
int dk[101];
int g[101][101];
int co[1<<17];
int main(){
int n,m,k;
cin>>n>>m>>k;
memset(dk,-1,sizeof(dk));
rep(i,m){
cin>>d[i];
--d[i];
dk[d[i]]=i;
}
int vi;
rep(i,n){
rep(j,k){
cin>>vi;
--vi;
g[i][j]=vi;
}
}
rep(i,1<<m) co[i]=1000100010;
co[(1<<m)-1]=0;
queue<int> q;
q.push((1<<m)-1);
while(!q.empty()){
int b=q.front();q.pop();
rep(i,k){
int nb=0;
rep(j,m){
if(b&(1<<j)){
if(dk[g[d[j]][i]]>=0) nb|=(1<<dk[g[d[j]][i]]);
}
}
if(co[nb]>co[b]+1){
co[nb]=co[b]+1;
if(nb==0){
cout<<co[nb]<<endl;
return 0;
}
q.push(nb);
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n,res=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int p=0;
sort(a,a+n,greater<int>());
for(int i=0;i<n-1;i++){
res=res+(long long)(a[p]);
if(i%2==0){
p++;
}
}
cout<<res<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 70000 + 5;
const int P = 29;
const int P2 = 31;
const int mod = 1e9 + 7;
const int mod2 = 1e9 + 9;
int n;
char s[N][10];
map<pair<int, int>, int> M;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%s", s[i]);
for (int j = 0; j < 9; j++) {
long long str1 = 0, str2 = 0;
for (int k = j; k < 9; k++) {
str1 = (str1 * P + s[i][k] - '0' + 1) % mod;
str2 = (str2 * P2 + s[i][k] - '0' + 1) % mod2;
M[{str1, str2}]++;
}
}
}
for (int i = 1; i <= n; i++) {
int ans = -1;
string ss = "";
for (int j = 0; j < 9; j++) {
long long str1 = 0, str2 = 0;
for (int k = j; k < 9; k++) {
str1 = (str1 * P + s[i][k] - '0' + 1) % mod;
str2 = (str2 * P2 + s[i][k] - '0' + 1) % mod2;
M[{str1, str2}]--;
}
}
for (int j = 0; j < 9; j++) {
long long str1 = 0, str2 = 0;
string str = "";
for (int k = j; k < 9; k++) {
str += s[i][k];
str1 = (str1 * P + s[i][k] - '0' + 1) % mod;
str2 = (str2 * P2 + s[i][k] - '0' + 1) % mod2;
if (M[{str1, str2}] == 0 and (ans == -1 or k - j + 1 < ans)) {
ans = k - j + 1;
ss = str;
}
}
}
for (int j = 0; j < 9; j++) {
long long str1 = 0, str2 = 0;
for (int k = j; k < 9; k++) {
str1 = (str1 * P + s[i][k] - '0' + 1) % mod;
str2 = (str2 * P2 + s[i][k] - '0' + 1) % mod2;
M[{str1, str2}]++;
}
}
puts(ss.c_str());
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
long long t = 1;
while (t--) {
long long n, d, l;
cin >> n >> d >> l;
long long a[n];
memset(a, 0, sizeof(a));
for (long long i = 0; i < n - 1; i++) {
if (d < 1) {
d = 1 - d;
a[i] = 1;
} else {
d = l - d;
a[i] = l;
}
}
if (d >= 1 && d <= l) {
a[n - 1] = d;
for (long long i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
| 2 |
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
using namespace std;
#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;
int gcd(int a, int b){
if(b==0) return a;
return gcd(b,a%b);
}
int lcm(int a, int b){
return a*b/gcd(a,b);
}
vs f(string s){
vs ret;
int l = 0;
rep(r,s.size())if(s[r] == '"'){
ret.push_back(s.substr(l,r-l));
l = r+1;
}
ret.push_back(s.substr(l));
return ret;
}
signed main(void) {
string in;
while(cin >> in, in != "."){
vs s = f(in);
cin >> in;
vs t = f(in);
//rep(i,s.size())cout << s[i] << endl;
//rep(i,t.size())cout << t[i] << endl;
int ans = 0;
if(s.size() != t.size()){
ans = 2;
}else{
rep(i,s.size())if(s[i] != t[i]){
if(i%2)ans++;
else ans = 2;
}
}
if(ans >= 2){
cout << "DIFFERENT" << endl;
}else if(ans == 1){
cout << "CLOSE" << endl;
}else{
cout << "IDENTICAL" << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> buyMap;
map<int, int> sellMap;
int main() {
int n, s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
char d;
int p, q;
cin >> d >> p >> q;
if (d == 'B')
buyMap[p] = (buyMap[p]) ? buyMap[p] + q : q;
else
sellMap[p] = (sellMap[p]) ? sellMap[p] + q : q;
}
map<int, int>::reverse_iterator rit;
int startIndex = s > sellMap.size() ? sellMap.size() : s;
int index = sellMap.size();
for (rit = sellMap.rbegin(); rit != sellMap.rend(); ++rit) {
if (startIndex < index--) continue;
cout << "S " << rit->first << " " << rit->second << endl;
}
index = 0;
for (rit = buyMap.rbegin(); rit != buyMap.rend(); ++rit) {
cout << "B " << rit->first << " " << rit->second << endl;
if (++index == s) break;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
char ch[4000010];
inline string get() {
scanf("%s", ch);
return string(ch);
}
inline void read(long long *a, long long n) {
for (long long i = 0; i < n; i++) a[i] = in();
}
template <class blank>
inline void out(blank x) {
cout << x << "\n";
exit(0);
}
template <class blank, class blank2>
inline void smin(blank &a, blank2 b) {
a = a <= b ? a : b;
}
template <class blank, class blank2>
inline void smax(blank &a, blank2 b) {
a = a >= b ? a : b;
}
const long long maxn = 1e6 + 10;
const long long maxm = 4e6 + 10;
const long long maxlg = 20;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long inf = 2e18 + 10;
const double eps = 1e-9;
const long long SQ = 320;
long long dp[2][SQ][SQ];
int32_t main() {
long long n = in(), m = in(), l = in();
if (n > m) out(0);
dp[0][0][0] = 1;
for (long long i = 1; i <= m; i++) {
long long fl = i & 1;
long long prefl = fl ^ 1;
for (long long j = 0; j <= n; j++)
for (long long k = 0; k <= j; k++)
(dp[fl][j][k] += (i == l ? 0 : dp[prefl][j][k]) +
(j && k ? dp[prefl][j - 1][k - 1] : 0)) %= mod;
for (long long j = 0; j <= n; j++)
for (long long k = 0; k <= j; k++)
(dp[fl][j][k] += dp[fl][j][k + 1]) %= mod, dp[prefl][j][k] = 0;
}
long long res = dp[m & 1][n][0];
for (long long i = 1; i <= n; i++) res = res * i % mod;
cout << res << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const int MAXN = 5000;
const int B = 250;
const int BNUM = MAXN / B;
int n, m;
pair<int, int> ps[MAXN];
int x[MAXN], y[MAXN];
vector<int> bucket[BNUM];
int main() {
while(cin >> n >> m) {
for(int i = 0; i < n; ++i) {
cin >> ps[i].first >> ps[i].second;
}
sort(ps, ps+n);
fill(bucket, bucket + BNUM, vector<int>());
for(int i = 0; i < n; ++i) {
x[i] = ps[i].first;
y[i] = ps[i].second;
bucket[i/B].push_back(y[i]);
}
for(int b = 0; b < BNUM; ++b) {
sort(bucket[b].begin(), bucket[b].end());
}
while(m--) {
int s, t, u, v; cin >> s >> t >> u >> v;
int l = lower_bound(x, x+n, s) - x;
int r = upper_bound(x, x+n, u) - x;
int res = 0;
while(l < r && l % B != 0) {
res += (t <= y[l] && y[l] <= v);
++l;
}
while(l < r && r % B != 0) {
--r;
res += (t <= y[r] && y[r] <= v);
}
while(l < r) {
int b = l / B;
res += upper_bound(bucket[b].begin(), bucket[b].end(), v)
- lower_bound(bucket[b].begin(), bucket[b].end(), t);
l += B;
}
cout << res << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int n;
map<int, int> m;
scanf("%d", &n);
int x;
for (int i = 0; i < n * n; i++) {
scanf("%d", &x);
m[x]++;
}
vector<int> a;
map<int, int>::iterator it;
map<int, int>::iterator it1;
int GCD;
int ak;
for (int i = 0; i < n; i++) {
it = m.end();
it--;
ak = it->first;
it->second--;
if (it->second == 0) m.erase(it);
for (int j = 0; j < a.size(); j++) {
GCD = gcd(ak, a[j]);
it1 = m.find(GCD);
it1->second -= 2;
if (it1->second == 0) m.erase(it1);
}
a.push_back(ak);
}
for (int i = 0; i < n; i++) {
if (i > 0) printf(" ");
printf("%d", a[i]);
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[103], n, x, y, m, ans;
int main() {
cin >> x >> y >> n;
m = x * y;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int x, j;
cin >> x;
for (j = 0; j < n; j++) {
if (a[j] == x) break;
}
ans = ans + j + 1;
for (int k = j - 1; k >= 0; k--) a[k + 1] = a[k];
a[0] = x;
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct stud {
stud(){};
stud(int xx, int yy) : x(xx), pos(yy) {}
int x, pos;
bool operator<(const stud s) const { return x < s.x; }
} f[200005], b[200005];
int n, s;
vector<pair<int, int> > ans;
priority_queue<stud> q, qq;
bool solve() {
int i, j;
ans.clear();
while (!q.empty()) q.pop();
while (!qq.empty()) qq.pop();
for (i = 0; i < n; i++)
if (f[i].x > 0) q.push(f[i]);
stud cur;
while (!q.empty()) {
cur = q.top();
q.pop();
if (cur.x > q.size()) return 0;
int m = 0;
while (cur.x--) {
b[m] = q.top();
int xx = cur.pos;
int yy = b[m].pos;
ans.push_back(make_pair(xx, yy));
b[m].x--;
m++;
q.pop();
}
for (i = 0; i < m; i++)
if (b[i].x) q.push(b[i]);
}
return 1;
}
int main() {
int i, j;
while (~scanf("%d %d", &n, &s)) {
for (i = 0; i < n; i++) {
scanf("%d", &f[i].x);
f[i].pos = i + 1;
}
if (solve()) {
printf("Yes\n");
printf("%d\n", ans.size());
for (i = 0; i < ans.size(); i++) {
printf("%d %d\n", ans[i].first, ans[i].second);
}
} else
printf("No\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
int h, m;
cin >> h >> m;
cout << (24 * 60) - (h * 60 + m);
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int wait;
const long long INF = 10000005;
const int N = 1e5 + 5;
int MAX = 10000000;
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long ans = -(long long)1ll << 60, l_idx = 0, r_idx = 0;
int n;
cin >> n;
long long arr[n];
long long conSum[n + 1];
conSum[0] = 0;
map<long long, vector<long long> > make_pair;
for (int i = 0; i < n; i++) {
cin >> arr[i];
make_pair[arr[i]].push_back(i);
(arr[i] >= 0) ? conSum[i + 1] = conSum[i] + arr[i]
: conSum[i + 1] = conSum[i];
}
for (auto p : make_pair) {
long long l, r;
if (p.second.size() >= 2) {
l = p.second[0];
r = p.second[p.second.size() - 1];
} else
continue;
long long x = conSum[r] - conSum[l + 1] + arr[l] * 2;
if (x >= ans)
l_idx = l, r_idx = r, ans = conSum[r] - conSum[l + 1] + arr[l] * 2;
}
cout << ans << " ";
vector<long long> cut;
for (long long i = 0; i < n; i++)
if (i < l_idx || i > r_idx || arr[i] < 0 && i > l_idx && i < r_idx)
cut.push_back(i + 1);
cout << cut.size() << endl;
for (int i = 0; i < cut.size(); i++) cout << cut[i] << " ";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3 * 100 + 4;
const int M = 100 * 1000 + 5;
int n, m, k;
long double ans, C[N], P[M];
void solve() {
C[0] = 1;
for (int i = 1; i <= n; i++) C[i] = C[i - 1] * ((long double)(n - i + 1) / i);
P[0] = 1;
for (int i = 1; i <= m; i++)
P[i] = P[i - 1] * ((long double)(k - i + 1) / (m - i + 1));
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
if (n * (i + j) - (i * j) <= k)
ans += C[i] * C[j] * P[n * (i + j) - (i * j)];
}
int main() {
ios::sync_with_stdio(false);
cout << setprecision(12) << fixed;
cin >> n >> m >> k;
solve();
cout << min((long double)1e99, ans) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[199999], n, val, cou = 0, fao = 0;
cin >> n;
for (int i = 1; i <= 2 * n; i++) {
cin >> val;
if (!arr[val]) {
arr[val] = 1;
cou++;
} else {
cou--;
}
fao = max(cou, fao);
}
cout << fao << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string second;
string t;
cin >> second;
cin >> t;
map<char, vector<int>> pos;
int n = second.length();
int m = t.length();
for (int i = 0; i < n; i++) {
pos[second[i]].push_back(i);
}
int idx = -1;
int ans = 1;
int flag = 0;
for (auto &it : t) {
if (!pos.count(it)) {
flag = 1;
break;
}
auto nxt = upper_bound(pos[it].begin(), pos[it].end(), idx);
if (nxt == pos[it].end()) {
ans++;
idx = pos[it][0];
} else
idx = *nxt;
}
if (flag)
cout << "-1\n";
else
cout << ans << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
const int max_N = 1e5 + 21;
char str[max_N], tmp[max_N];
int n, right[max_N];
template <size_t size>
struct Kmp {
int fail[size];
template <typename T>
inline void get_fail(T S, int len) {
fail[1] = 0;
for (int i = 2, j; i <= len; ++i) {
for (j = fail[i - 1]; j && S[j + 1] != S[i]; j = fail[j])
;
fail[i] = S[j + 1] == S[i] ? ++j : 0;
}
}
template <typename T>
inline void matching(T A, int lenA, T B, int lenB, int* right) {
get_fail(B, lenB);
for (int i = 1, j = 0; i <= lenA; ++i) {
while (j && A[i] != B[j + 1]) j = fail[j];
if (A[i] == B[j + 1]) ++j;
if (!right[j]) right[j] = lenA - i + j;
if (j == lenB) return;
}
}
};
template <size_t size>
struct Manacher {
int r[2][size];
template <typename T>
inline void build(T S, int len, int type) {
register int i, j, k;
int* R = r[type];
for (i = 1, j = 0; i <= len; i += k, j = std::max(j - k, 0)) {
while (i - j > 0 && i + j + type <= len && S[i - j] == S[i + j + type])
++j;
R[i] = j;
for (k = 1; k < j && R[i - k] != R[i] - k; ++k)
R[i + k] = std::min(R[i - k], R[i] - k);
}
}
};
Manacher<max_N> manacher;
template <size_t size, size_t size_log>
struct SparseTable {
int max[size][size_log], bit[size];
inline int slt(int x, int y) {
return manacher.r[0][x] > manacher.r[0][y] ? x : y;
}
inline void init(int n) {
for (int i = 2; i <= n; ++i) bit[i] = bit[i >> 1] + 1;
for (int i = 1; i <= n; ++i) max[i][0] = i;
for (int j = 1; j <= bit[n]; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
max[i][j] = slt(max[i][j - 1], max[i + (1 << (j - 1))][j - 1]);
}
}
}
inline int rmq(int l, int r) {
int i = bit[r - l + 1];
return slt(max[l][i], max[r - (1 << i) + 1][i]);
}
};
Kmp<max_N> kmp;
SparseTable<max_N, 17> st;
int ans;
std::vector<std::pair<int, int>> vec;
inline int query(int L, int R) {
int l = 1, r = (R - L + 1) & 1 ? (R - L + 2) / 2 : (R - L + 1) / 2;
int ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
int tmp = st.rmq(L + mid - 1, R - mid + 1);
if (manacher.r[0][tmp] >= mid) {
ans = tmp;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
inline void solve() {
int x = st.rmq(1, n);
ans = manacher.r[0][x] * 2 - 1;
vec.push_back({x - manacher.r[0][x] + 1, x + manacher.r[0][x] - 1});
for (int i = 1; i <= n; ++i) {
if (right[i] <= 2 * i) continue;
int middle = query(i + 1, right[i] - i);
if (!~middle) {
puts("WTF");
exit(0);
}
int R = manacher.r[0][middle];
R = std::min(R, std::min(middle - i, right[i] - i - middle + 1));
int ml = middle - R + 1, mr = middle + R - 1;
int len = i * 2 + (mr - ml + 1);
if (len > ans) {
ans = len;
vec.clear();
vec.push_back({right[i] - i + 1, right[i]});
vec.push_back({ml, mr});
vec.push_back({1, i});
}
}
}
int main() {
scanf("%s", str + 1);
n = strlen(str + 1);
strcpy(tmp + 1, str + 1);
std::reverse(str + 1, str + 1 + n);
kmp.matching(tmp, n, str, n, right);
for (int i = 1; i <= n; ++i)
if (!right[i]) right[i] = i;
manacher.build(str, n, 0);
manacher.build(str, n, 1);
st.init(n);
solve();
printf("%d\n", (int)vec.size());
for (auto& x : vec)
printf("%d %d\n", n - x.second + 1, x.second - x.first + 1);
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main(){
long long int a = 1, b = 1;
int n; cin >> n;
for(int i=0;i<n;++i){
long long int c = a + b;
a = b;
b = c;
}
cout << a << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
int xceptor() {
cin >> s;
int L = s.length(), mxind = 0;
char qq = 'z';
for (int i = 25; i >= 0; i--) {
qq = 'a' + i;
for (int j = 0; j < L; j++) {
if (s[j] == qq && j >= mxind) {
cout << s[j];
mxind = j;
}
}
}
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
return xceptor();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, arr[1001], ans = 1e3, temp1, temp2;
cin >> n >> m;
for (int i = 0; i < m; i++) cin >> arr[i];
sort(arr, arr + m);
for (int i = 0; i <= m - n; i++) {
for (int j = i; j < i + n; j++) {
if (j == i) temp1 = arr[j];
if (j == i + n - 1) temp2 = arr[i + n - 1];
}
if (temp2 - temp1 < ans) ans = temp2 - temp1;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x, y;
cin >> x >> y;
if (x == 0 && y == 1) {
cout << "yes" << endl;
return 0;
}
if (y != 0) y--;
if (y != 0) {
if (x > y) {
x = x - y;
if (x % 2 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
} else if (x == y) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} else
cout << "No" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int k, sum[200005], mid[200005];
string s, t;
int main() {
cin >> k;
cin >> s >> t;
s = '0' + s;
t = '0' + t;
for (int i = k; i >= 1; i--) {
sum[i] += (s[i] - 'a' + t[i] - 'a');
sum[i - 1] = sum[i] / 26;
sum[i] = sum[i] % 26;
}
for (int i = 0; i <= k; i++) {
sum[i + 1] += (26 * (sum[i] % 2));
sum[i] = sum[i] / 2;
}
for (int i = 1; i <= k; i++) cout << (char)(sum[i] + 'a');
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int lights, pushed_buttons;
cin >> lights >> pushed_buttons;
int *buttons = new int[pushed_buttons];
int *pulbs = new int[lights];
for (int i = 0; i < lights; i++) {
pulbs[i] = -1;
}
for (int i = 0; i < pushed_buttons; i++) {
cin >> buttons[i];
for (int l = buttons[i] - 1; l < lights; l++) {
if (pulbs[l] == -1) {
pulbs[l] = buttons[i];
}
}
}
for (int i = 0; i < lights; i++) {
cout << pulbs[i] << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m;
char st[100000 + 5];
long long f[100000 + 5][2][2][2], ans = 0;
int main() {
n = read();
m = read();
scanf("%s", st + 1);
f[0][0][0][0] = 1;
for (int i = (1); i <= (n); ++i)
for (int j = (0); j <= (1); ++j)
for (int k = (0); k <= (1); ++k)
for (int l = (0); l <= (1); ++l)
for (int x = (0); x <= (m - 1); ++x) {
int J = max(i - 1 - k, i - 2 - j + (x == st[i - 1] - 'a'));
int K = max(i - 1 - k + (x == st[i] - 'a'), i - 1 - l);
K = max(K, J);
int L = max(i - 1 - l + (x == st[i + 1] - 'a'), K);
J = max(J, 0);
K = max(K, 0);
L = max(L, 0);
if (i - 1 - J > 1 || i - K > 1 || i - L > 1) continue;
f[i][i - 1 - J][i - K][i - L] += f[i - 1][j][k][l];
}
for (int j = (0); j <= (1); ++j)
for (int l = (0); l <= (1); ++l) ans += f[n][j][1][l];
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
int n, k;
char a[100000 + 50];
int G[100000 + 50];
bool bo = 0;
scanf("%d%d", &n, &k);
getchar();
for (int i = 1; i <= n; i++) {
scanf("%c", &a[i]);
}
int l = 1, r = n, pos, next[100000 + 50];
if (k <= n / 2)
pos = 0;
else
pos = 1;
int kk = 0;
G[0] = -1000000007;
while (l <= r) {
if (a[l] != a[r]) {
if (pos == 1) {
G[++kk] = r;
if (r == k) bo = 1;
next[r] = l;
} else {
if (l == k) bo = 1;
G[++kk] = l;
next[l] = r;
}
}
l++, r--;
}
if (!bo) {
G[++kk] = k;
}
sort(G + 1, G + kk + 1);
int ans = 0;
int last;
int fa = lower_bound(G + 1, G + kk + 1, k) - G;
if (abs(G[1] - G[fa]) >= abs(G[kk] - G[fa])) {
last = G[fa];
for (int i = fa + 1; i <= kk; i++) {
ans +=
min(26 - abs(a[G[i]] - a[next[G[i]]]), abs(a[G[i]] - a[next[G[i]]]));
;
ans += abs(G[i] - last);
last = G[i];
}
last = G[kk];
for (int i = fa - 1; i >= 1; i--) {
ans +=
min(26 - abs(a[G[i]] - a[next[G[i]]]), abs(a[G[i]] - a[next[G[i]]]));
ans += abs(G[i] - last);
last = G[i];
}
} else {
last = G[fa];
for (int i = fa - 1; i >= 1; i--) {
ans +=
min(26 - abs(a[G[i]] - a[next[G[i]]]), abs(a[G[i]] - a[next[G[i]]]));
ans += abs(G[i] - last);
;
last = G[i];
}
last = G[1];
for (int i = fa + 1; i <= kk; i++) {
ans +=
min(26 - abs(a[G[i]] - a[next[G[i]]]), abs(a[G[i]] - a[next[G[i]]]));
ans += abs(G[i] - last);
;
last = G[i];
}
}
if (bo) ans += min(26 - abs(a[k] - a[next[k]]), abs(a[next[k]] - a[k]));
cout << ans << endl;
return 0;
}
| 3 |
#include<stdio.h>
long long s, r;
int n;
int main(){
int i, a;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&a);
if(a==0){
r += s/2;
s=0;
}
s+=a;
}
printf("%lld\n",r+s/2);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
while (cin >> n >> k) {
char m[4][n];
bool available = 0;
if (k % 2 == 0) {
available = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (i > 0 && j > 0 && i < n - 1 && j < 3 && k-- > 0) {
m[j][i] = '#';
} else {
m[j][i] = '.';
}
}
}
} else {
available = 1;
for (int i = 0; i < 4; i++) {
m[i][n / 2] = '.';
}
m[1][n / 2] = '#';
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n / 2; j++) {
if (i > 0 && j > 0 && i < 3 && j < n - 1 && (k -= 2) > 0) {
m[i][j] = '#';
m[i][n - j - 1] = '#';
} else {
m[i][j] = '.';
m[i][n - j - 1] = '.';
}
}
}
}
if (available) {
cout << "YES" << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
cout << m[i][j];
}
cout << endl;
}
} else {
cout << "NO" << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 1000010 * 2;
int n, m;
vector<int> vec[maxn];
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
while (k--) {
int x;
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= m; i++) {
sort(vec[i].begin(), vec[i].end());
}
sort(vec + 1, vec + m + 1);
long long res = 1;
long long cnt = 1;
for (int i = 1; i < m; i++) {
if (vec[i] == vec[i + 1])
cnt++;
else {
while (cnt > 0) res = res * cnt % mod, cnt--;
cnt = 1;
}
}
while (cnt > 0) res = res * cnt % mod, cnt--;
cout << res << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
char c;
int bx = -1, by = -1, ex, ey;
for (int i = (1); i != (n + 1); ++i) {
for (int j = (1); j != (m + 1); ++j) {
cin >> c;
if (c == 'B') {
if (bx < 0) {
bx = i;
by = j;
}
ex = i;
ey = j;
}
}
}
cout << (bx + ex) / 2 << ' ' << (by + ey) / 2 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
using namespace std;
double __begin;
template <typename T1, typename T2, typename T3>
struct triple {
T1 a;
T2 b;
T3 c;
triple(){};
triple(T1 _a, T2 _b, T3 _c) : a(_a), b(_b), c(_c) {}
};
template <typename T1, typename T2, typename T3>
bool operator<(const triple<T1, T2, T3> &t1, const triple<T1, T2, T3> &t2) {
if (t1.a != t2.a)
return t1.a < t2.a;
else
return t1.b < t2.b;
}
template <typename T1, typename T2, typename T3>
inline std::ostream &operator<<(std::ostream &os, const triple<T1, T2, T3> &t) {
return os << "(" << t.a << ", " << t.b << ", " << t.c << ")";
}
inline int bits_count(int v) {
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
inline int bits_count(long long v) {
int t = v >> 32;
int p = (v & ((1LL << 32) - 1));
return bits_count(t) + bits_count(p);
}
unsigned int reverse_bits(register unsigned int x) {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16));
}
inline int sign(int x) { return (x >> 31) | (-x >> 31); }
inline bool ispow2(int x) { return (x != 0 && (x & (x - 1)) == 0); }
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
bool first = true;
os << "[";
for (unsigned int i = 0; i < v.size(); i++) {
if (!first) os << ", ";
os << v[i];
first = false;
}
return os << "]";
}
template <typename T>
inline std::ostream &operator<<(std::ostream &os, const std::set<T> &v) {
bool first = true;
os << "[";
for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T1, typename T2>
inline std::ostream &operator<<(std::ostream &os, const std::map<T1, T2> &v) {
bool first = true;
os << "[";
for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ", ";
os << *ii;
first = false;
}
return os << "]";
}
template <typename T, typename T2>
void printarray(T a[], T2 sz, T2 beg = 0) {
for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
cout << endl;
}
inline long long binpow(long long x, long long n) {
long long res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
inline long long powmod(long long x, long long n, long long _mod) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % _mod;
x = (x * x) % _mod;
n >>= 1;
}
return res;
}
inline long long gcd(long long a, long long b) {
long long t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline int gcd(int a, int b) {
int t;
while (b) {
a = a % b;
t = a;
a = b;
b = t;
}
return a;
}
inline long long lcm(int a, int b) { return a / gcd(a, b) * (long long)b; }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
inline long long lcm(long long a, long long b, long long c) {
return lcm(lcm(a, b), c);
}
inline long long lcm(int a, int b, int c) {
return lcm(lcm(a, b), (long long)c);
}
inline long long max(long long a, long long b) { return (a > b) ? a : b; }
inline int max(int a, int b) { return (a > b) ? a : b; }
inline double max(double a, double b) { return (a > b) ? a : b; }
inline long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline long long min(long long a, long long b) { return (a < b) ? a : b; }
inline int min(int a, int b) { return (a < b) ? a : b; }
inline double min(double a, double b) { return (a < b) ? a : b; }
inline long long min(long long a, long long b, long long c) {
return min(a, min(b, c));
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
template <class T>
inline void getar(T a, int n, int m) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; ++j) {
scanf("%d", &a[i][j]);
}
}
inline void getar(int *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d", a + ii);
}
}
inline void getar(pair<int, int> *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%d%d", &a[ii].first, &a[ii].second);
}
}
inline void getar(long long *a, int n) {
for (int ii = 0; ii < n; ii++) {
scanf("%I64d", a + ii);
}
}
template <class T>
inline void cinarr(T a, int n) {
for (int i = 0; i < n; ++i) cin >> a[i];
}
template <class T>
inline bool scan_d(T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(int x) {
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
int n;
long long dp[4005][4005], c[4005][4005];
int main() {
dp[0][0] = dp[1][1] = 1;
for (int i = 2; i < 4005; i++) {
dp[i][1] = dp[i - 1][i - 1];
for (int j = 2; j <= i; j++) {
dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % 1000000007;
}
}
for (int i = 1; i < 4005; i++) {
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++) {
c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % 1000000007;
}
}
while (scanf("%d", &n) != EOF) {
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += c[n][i] * dp[i][i];
ans %= 1000000007;
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 50005;
int n, m, K;
long long ans[N];
struct ff {
int a, b, c, d;
} A[N];
struct gg {
int x, l, r, w;
bool operator<(const gg& k) const { return x < k.x; }
} B[N * 2];
vector<int> v;
namespace SGT {
int tg[N * 8], sum[N * 8];
void upd(int k1, int k2, int k3) {
if (tg[k1]) {
sum[k1] = v[k3] - v[k2 - 1];
} else {
if (k2 != k3)
sum[k1] = sum[k1 * 2] + sum[k1 * 2 + 1];
else
sum[k1] = 0;
}
}
void bud(int k1, int k2, int k3) {
tg[k1] = 0, sum[k1] = 0;
if (k2 == k3) {
return;
}
int mid = (k2 + k3) >> 1;
bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3);
upd(k1, k2, k3);
}
void mdf(int k1, int k2, int k3, int k4, int k5, int k6) {
if (k2 > k5 || k3 < k4) return;
if (k4 <= k2 && k3 <= k5) {
tg[k1] += k6;
upd(k1, k2, k3);
return;
}
int mid = (k2 + k3) >> 1;
mdf(k1 * 2, k2, mid, k4, k5, k6), mdf(k1 * 2 + 1, mid + 1, k3, k4, k5, k6);
upd(k1, k2, k3);
}
} // namespace SGT
long long sol(int len) {
int cnt = 0;
for (int i = (1); i <= (m); ++i) {
int a = (A[i].a + len - 1) / len;
int c = A[i].c / len;
int b = (A[i].b + len - 1) / len;
int d = A[i].d / len;
B[++cnt] = (gg){a, b, d, 1};
B[++cnt] = (gg){c + 1, b, d, -1};
}
auto get = [&](int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
};
{
v.clear();
for (int i = (1); i <= (cnt); ++i)
v.push_back(B[i].l - 1), v.push_back(B[i].r);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = (1); i <= (cnt); ++i)
B[i].l = get(B[i].l), B[i].r = get(B[i].r);
}
sort(B + 1, B + 1 + cnt);
SGT::bud(1, 0, ((int)(v).size()) - 1);
long long res = 0;
for (int i = 1, j; i <= cnt; i = j) {
j = i;
res += 1LL * (B[i].x - B[i - 1].x) * SGT::sum[1];
while (j <= cnt && B[i].x == B[j].x) {
SGT::mdf(1, 0, ((int)(v).size()) - 1, B[j].l, B[j].r, B[j].w);
++j;
}
}
return res;
}
int main() {
scanf("%d%d%d", &n, &m, &K);
for (int i = (1); i <= (m); ++i) {
scanf("%d%d%d%d", &A[i].a, &A[i].b, &A[i].c, &A[i].d);
}
for (int i = (0); i <= (30); ++i) {
if ((1 << i) > K) break;
ans[i] = sol(1 << i);
}
for (int i = (0); i <= (30); ++i) {
if ((1 << i) > K) break;
if ((ans[i] - ans[i + 1]) & 1) {
puts("Hamed");
exit(0);
}
}
puts("Malek");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int e, r, i, count, u, v, q, test, w;
map<long long int, long long int> mp, ans;
cin >> q;
ans.clear();
for (int i = 0; i < q; i++) {
cin >> test;
mp.clear();
if (test == 1) {
cin >> u >> v >> w;
e = u;
r = v;
while (u != 1) {
mp[u] = mp[u] + 1;
u = u / 2;
}
while (v != 1) {
mp[v] = mp[v] + 1;
v = v / 2;
}
u = e;
v = r;
while (u != 1) {
if (mp[u] == 2) break;
ans[u] = ans[u] + w;
u = u / 2;
}
while (v != 1) {
if (mp[v] == 2) break;
ans[v] = ans[v] + w;
v = v / 2;
}
} else {
cin >> u >> v;
e = u;
r = v;
mp.clear();
while (u != 1) {
mp[u] = mp[u] + 1;
u = u / 2;
}
while (v != 1) {
mp[v] = mp[v] + 1;
v = v / 2;
}
u = e;
v = r;
count = 0;
while (u != 1) {
if (mp[u] == 2) break;
count = count + ans[u];
u = u / 2;
}
while (v != 1) {
if (mp[v] == 2) break;
count = count + ans[v];
v = v / 2;
}
cout << count << "\n";
}
}
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int main()
{
string S;
int w;
cin >> S >> w;
for(int i = 0; i < S.size(); ++i){
if(i%w == 0) cout << S[i];
}
cout << endl;
}
| 0 |
#include <iostream>
#include <string>
using namespace std;
string getCode1( char c ){
string ret;
switch( c ){
case ' ': ret = "101"; break;
case '\'': ret = "000000"; break;
case ',': ret = "000011"; break;
case '-': ret = "10010001"; break;
case '.': ret = "010001"; break;
case '?': ret = "000001"; break;
case 'A': ret = "100101"; break;
case 'B': ret = "10011010"; break;
case 'C': ret = "0101"; break;
case 'D': ret = "0001"; break;
case 'E': ret = "110"; break;
case 'F': ret = "01001"; break;
case 'G': ret = "10011011"; break;
case 'H': ret = "010000"; break;
case 'I': ret = "0111"; break;
case 'J': ret = "10011000"; break;
case 'K': ret = "0110"; break;
case 'L': ret = "00100"; break;
case 'M': ret = "10011001"; break;
case 'N': ret = "10011110"; break;
case 'O': ret = "00101"; break;
case 'P': ret = "111"; break;
case 'Q': ret = "10011111"; break;
case 'R': ret = "1000"; break;
case 'S': ret = "00110"; break;
case 'T': ret = "00111"; break;
case 'U': ret = "10011100"; break;
case 'V': ret = "10011101"; break;
case 'W': ret = "000010"; break;
case 'X': ret = "10010010"; break;
case 'Y': ret = "10010011"; break;
case 'Z': ret = "10010000"; break;
}
return ret;
}
char getCode2( string& s ){
char c = 0;
for ( int i = 0; i < 5; ++i ){
c += ( ( s[ 4 - i ] - '0' ) << i );
}
if ( c < 26 ){
c += 'A';
}else{
switch( c ){
case 26: c = ' '; break;
case 27: c = '.'; break;
case 28: c = ','; break;
case 29: c = '-'; break;
case 30: c = '\''; break;
case 31: c = '?'; break;
}
}
return c;
}
void firstEnc( string& is ){
string os;
string::iterator it = is.begin();
while ( it != is.end() ){
os += getCode1( *it );
it = is.erase( it );
}
is = os;
}
void secondEnc( string& is ){
string os;
if ( is.size() % 5 ){
is.append( 5 - is.size() % 5 , '0' );
}
while ( is.size() ){
string set( is, 0, 5 );
os += getCode2( set );
is.erase( 0, 5 );
}
is = os;
}
int main(){
string s;
while ( getline( cin, s ) ){
firstEnc( s );
secondEnc( s );
cout << s << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
ll T;
cin>>T;
while(T--)
{
ll n, k;
cin>>n>>k;
vector<ll> arr;
for(ll i=1; i<=k; i++)
{
arr.push_back(i);
}
ll x = 2*k-n;
for(ll i=k-1; i>=x; i--){
arr.push_back(i);
}
vector<ll> ans;
set<ll> st;
for(ll i=arr.size()-1; i>=0; i--)
{
if(st.find(arr[i])==st.end()){
ans.push_back(arr[i]);
}
st.insert(arr[i]);
}
reverse(ans.begin(), ans.end());
for(ll i=0; i<ans.size(); i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, sum;
int main() {
scanf("%d%d", &n, &m);
if (n - m > 1 || m - 2 * n > 2)
printf("-1\n");
else {
if (n - m == 1) {
printf("0");
n--;
}
for (int i = 0; i < min(m - n, n); i++) {
printf("110");
}
for (int i = 0; i < 2 * n - m; i++) {
printf("10");
}
for (int i = 0; i < m - 2 * n; i++) printf("1");
printf("\n");
}
return 0;
}
| 3 |
#include<iostream>
#include<algorithm>
#include<cstdio>
#define INF 10000000
using namespace std;
int main(){
int n,m,p,a,b;
double cost[30][257],ticket[8],edge[30][30];
bool done[30];
while(cin >>n>>m>>p>>a>>b,n||m||p||a||b){
for(int i=0; i<m; i++) done[i] = false;
for(int i=0; i<m; i++) for(int j=0; j<30; j++) edge[i][j] = -1;
for(int i=0; i<m; i++) for(int j=0; j<(1<<n); j++) cost[i][j] = INF;
for(int i=0; i<n; i++) cin >>ticket[i];
for(int i=0; i<p; i++){
int x,y; double z;
cin >>x>>y>>z;x--;y--;
edge[x][y] = edge[y][x] = z;
}
a--;b--;
cost[a][0] = 0;
for(;;){
int next=-1;double n_c=INF;
for(int i=0; i<m; i++){
if(done[i]) continue;
for(int j=0; j<(1<<n); j++){
if(n_c > cost[i][j]){
n_c = cost[i][j];
next = i;
}
}
}
if(next == -1) break;
done[next] = true;
for(int i=0; i<m; i++){
if(edge[next][i] == -1 || done[i]) continue;
for(int j=0; j<(1<<n); j++){
if(cost[next][j] == INF) continue;
for(int k=0; k<n; k++){
if((j&(1<<k)) == 0) cost[i][j|(1<<k)] = min(cost[i][j|(1<<k)],cost[next][j]+edge[next][i]/ticket[k]);
}
}
}
}
double ans = INF;
for(int i=0; i<(1<<n); i++) ans = min(ans,cost[b][i]);
if(ans == INF) cout <<"Impossible"<<endl;
else printf("%.3lf\n",ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n;
bool p[300005];
bool c[300005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
cout << "1 ";
long long ans = 1;
c[n + 1] = true;
for (long long i = 1; i <= n; i++) {
long long x;
cin >> x;
ans++;
p[x] = true;
if (c[x + 1] == true) {
while (p[x] == true && x >= 1) {
c[x] = true;
ans--;
x--;
}
cout << ans << " ";
continue;
}
cout << ans << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void print(int n) {
if (n == 0) {
putchar('0');
putchar('\n');
} else if (n == -1) {
putchar('-');
putchar('1');
putchar('\n');
} else {
char buf[11];
buf[10] = '\n';
int i = 9;
while (n) {
buf[i--] = n % 10 + '0';
n /= 10;
}
while (buf[i] != '\n') putchar(buf[++i]);
}
}
int read() {
int cc = getc(stdin);
for (; cc < '0' || cc > '9';) cc = getc(stdin);
int ret = 0;
for (; cc >= '0' && cc <= '9';) {
ret = ret * 10 + cc - '0';
cc = getc(stdin);
}
return ret;
}
long long power(long long num, long long g) {
if (g == 0) return 1;
if (g % 2 == 1) return (num * power((num * num), g / 2));
return power((num * num), g / 2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<int> vec[n + 1];
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
vec[a].push_back(b);
}
int b[n + 1];
for (int i = 1; i <= n; i++) {
b[i] = vec[i].size();
}
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int j = 1; j <= n; j++) {
int ans1 = INT_MAX;
for (int k = 0; k < b[j]; k++) {
ans1 = min(ans1,
(j + n - i) % n + (vec[j][k] + n - j) % n + (b[j] - 1) * n);
}
if (b[j] > 0) ans = max(ans, ans1);
}
cout << ans << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[107];
int mem[107][10007];
int n, m;
int mini = 107;
void func(int day, int total, int now) {
if (total >= m) {
mini = min(mini, day);
return;
}
if (total <= mem[day][now]) {
return;
} else {
mem[day][now] = total;
}
int i;
for (i = 0; now + i < n; i++) {
if (a[now + i] - i > 0) total += a[now + i] - i;
func(day + 1, total, now + i + 1);
}
}
int main() {
int i, j;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
memset(mem, -1, sizeof(mem));
sort(a, a + n, greater<int>());
func(0, 0, 0);
if (mini == 107)
printf("-1\n");
else
printf("%d\n", mini);
return 0;
}
| 4 |
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main(){
double a,b,c,d,e,f;
while( cin>>a>>b>>c>>d>>e>>f){
double x,y;
y=(c*d-a*f)/(b*d-a*e);
x=(c-b*y)/a;
//cout<<x<<' '<<y<<endl;
printf("%.3lf %.3lf",x,y);
cout<<endl;
}
}
| 0 |
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
cout<<"100 100\n";
int A,B;
cin>>A>>B;A--,B--;
vector<vector<char>> C(100,vector<char>(100,'#'));
for(int i=50;i<100;i++)
for(int j=0;j<100;j++)
C[i][j]='.';
for(int i=0;i<100;i+=2){
for(int j=0;j<100;j+=2){
if(A==0)break;
C[i][j]='.',A--;
}
if(A==0)break;
}
for(int i=51;i<100;i+=2){
for(int j=0;j<100;j+=2){
if(B==0)break;
C[i][j]='#',B--;
}
if(B==0)break;
}
for(int i=0;i<100;i++){
for(int j=0;j<100;j++)
cout<<C[i][j];
cout<<'\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cout << *i << " ";
cout << endl;
}
template <class T>
void chmin(T &t, T f) {
if (t > f) t = f;
}
template <class T>
void chmax(T &t, T f) {
if (t < f) t = f;
}
int in() {
int x;
scanf("%d", &x);
return x;
}
const int R = 4;
const int S = R * R / 2;
int uf[4000010];
int root(int x) { return (uf[x] < 0) ? x : (uf[x] = root(uf[x])); }
bool conn(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return 0;
if (uf[x] > uf[y]) swap(x, y);
uf[x] += uf[y];
uf[y] = x;
return 1;
}
int N;
inline int _(int x, int y) { return x * N + y; }
int A[2010][2010];
int B[2010][2010];
int C[2010][2010];
vector<pair<int, int> > ps[4000010];
int main() {
int x, y;
int r;
for (; ~scanf("%d", &N);) {
for (x = 0; x < N; ++x)
for (y = 0; y < N; ++y) {
A[x][y] = in();
}
for (x = 0; x < N; ++x)
for (y = 0; y < N; ++y) {
B[x + 1][y + 1] = B[x + 1][y] + B[x][y + 1] - B[x][y] + A[x][y];
}
for (x = 0; x + R <= N; ++x)
for (y = 0; y + R <= N; ++y) {
C[x][y] = B[x + R][y + R] - B[x + R][y] - B[x][y + R] + B[x][y];
}
memset(uf, ~0, sizeof(uf));
for (x = 0; x + R <= N; ++x)
for (y = 0; y + R <= N; ++y)
if (C[x][y] > S) {
if (x)
if (C[x - 1][y] > S) conn(_(x - 1, y), _(x, y));
if (y)
if (C[x][y - 1] > S) conn(_(x, y - 1), _(x, y));
}
for (r = 0; r < N * N; ++r) {
ps[r].clear();
}
for (x = 0; x + R <= N; ++x)
for (y = 0; y + R <= N; ++y)
if (C[x][y] > S) {
r = root(_(x, y));
ps[r].push_back(make_pair(x, y));
}
int ansC = 0, ansS = 0;
for (r = 0; r < N * N; ++r)
if (!ps[r].empty()) {
int sz = ps[r].size(), i;
if (sz < 30) continue;
double ox = 0, oy = 0;
for (i = 0; i < sz; ++i) {
x = ps[r][i].first;
y = ps[r][i].second;
ox += x;
oy += y;
}
ox /= sz;
oy /= sz;
double rad = 0;
for (i = 0; i < sz; ++i) {
x = ps[r][i].first;
y = ps[r][i].second;
chmax(rad, (x - ox) * (x - ox) + (y - oy) * (y - oy));
}
double ratio = sz / rad;
(ratio > 2.7) ? ++ansC : ++ansS;
}
printf("%d %d\n", ansC, ansS);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, i, j, k, ans, s0, s1;
cin >> n;
vector<long long> dp(n, 0);
s0 = 0;
s1 = 0;
ans = 0;
for (i = 0; i < n; i++) {
if (i % 2 == 0)
dp[i] = 1 + s1, s0 = (s0 + dp[i]) % 1000000007;
else
dp[i] = 1 + s0, s1 = (s1 + dp[i]) % 1000000007;
ans = (ans + dp[i]) % 1000000007;
}
cout << ans << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll mod = 1E9;
const int inf = 2E9;
vector<int> LisNlogK(int n, vector<int>& Sequence) {
vector<int> I(n + 1, inf), ans(n, 0);
I[0] = -inf;
int LisLength = 0;
for (int i = 0; i < n; i++) {
int low, high, mid;
low = 0;
high = LisLength;
while (low <= high) {
mid = (low + high) / 2;
if (I[mid] <= Sequence[i])
low = mid + 1;
else
high = mid - 1;
}
I[low] = Sequence[i];
if (LisLength < low) LisLength = low;
ans[i] = low;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
int n, t;
cin >> n >> t;
vector<int> tmp(n), a, cnt(310, 0);
for (int i = 0; i < n; i++) {
cin >> tmp[i];
cnt[tmp[i]]++;
}
for (int i = 0; i < n; i++) {
a.insert(a.end(), tmp.begin(), tmp.end());
}
vector<int> b = a;
reverse(b.begin(), b.end());
for (int i = 0; i < n * n; i++) {
b[i] = -b[i];
}
if (t <= 2 * n) {
a.insert(a.end(), a.begin(), a.end());
vector<int> LISa = LisNlogK(2 * n * n, a);
cout << *max_element(LISa.begin(), LISa.begin() + (t * n)) << endl;
} else {
vector<int> LISa = LisNlogK(n * n, a);
vector<int> LISb = LisNlogK(n * n, b);
reverse(LISb.begin(), LISb.end());
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (a[j] >= a[i]) {
for (int k = 0; k < n; k++)
if (a[k] >= a[i] && a[k] <= a[j]) {
ans = max(ans, LISa[n * (n - 1) + i] + (t - 2 * n) * cnt[a[k]] +
LISb[j]);
}
}
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using lint=long long;
inline int popcount(int x){
x=((x>>1)&0x55555555)+(x&0x55555555);
x=((x>>2)&0x33333333)+(x&0x33333333);
x=((x>>4)+x)&0x0f0f0f0f;
x+=(x>>8);
x+=(x>>16);
return x&0x3f;
}
lint modpow(lint a,lint k,int m){
lint r=1;
for(lint x=a%m;k>0;k>>=1,x=x*x%m) if(k&1) r=r*x%m;
return r;
}
int f(int a){
if(a==0) return 0;
return f(a%popcount(a))+1;
}
int main(){
int n;
string s; cin>>n>>s;
int pc=0;
rep(i,n) if(s[i]=='1') pc++;
lint fs[2]={};
rep(i,n){
fs[0]=(fs[0]*2+s[i]-'0')%(pc+1);
if(pc>1) fs[1]=(fs[1]*2+s[i]-'0')%(pc-1);
}
rep(i,n){
int ans;
if(s[i]=='0'){
ans=f((fs[0]+modpow(2,n-1-i,pc+1))%(pc+1))+1;
}
else{
if(pc==1){
ans=0;
}
else{
ans=f((fs[1]-modpow(2,n-1-i,pc-1)+pc-1)%(pc-1))+1;
}
}
printf("%d\n",ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int a[51][51], b[51][51];
int A[51][51], B[51][51];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, pair<int, int> > > vec;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> a[i][j], vec.push_back({a[i][j], {i, j}});
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> b[i][j], vec.push_back({b[i][j], {i, j}});
sort((vec).begin(), (vec).end());
for (auto it : vec) {
int i = it.second.first, j = it.second.second;
if (A[it.second.first][it.second.second] == 0) {
bool good = 1;
if (j != m - 1 && A[i][j + 1] != 0 && it.first >= A[i][j + 1]) good = 0;
if (i != n - 1 && A[i + 1][j] != 0 && it.first >= A[i + 1][j]) good = 0;
if (j != 0 && A[i][j - 1] != 0 && it.first <= A[i][j - 1]) good = 0;
if (i != 0 && A[i - 1][j] != 0 && it.first <= A[i - 1][j]) good = 0;
if (!good) {
if (B[it.second.first][it.second.second] == 0) {
bool g2 = 1;
if (j != m - 1 && B[i][j + 1] != 0 && it.first >= B[i][j + 1]) g2 = 0;
if (i != n - 1 && B[i + 1][j] != 0 && it.first >= B[i + 1][j]) g2 = 0;
if (j != 0 && B[i][j - 1] != 0 && it.first <= B[i][j - 1]) g2 = 0;
if (i != 0 && B[i - 1][j] != 0 && it.first <= B[i - 1][j]) g2 = 0;
if (!g2) {
cout << "Impossible";
return 0;
}
B[i][j] = it.first;
} else {
cout << "Impossible";
return 0;
}
} else {
A[i][j] = it.first;
}
} else if (B[it.second.first][it.second.second] == 0) {
bool g2 = 1;
if (j != m - 1 && B[i][j + 1] != 0 && it.first >= B[i][j + 1]) g2 = 0;
if (i != n - 1 && B[i + 1][j] != 0 && it.first >= B[i + 1][j]) g2 = 0;
if (j != 0 && B[i][j - 1] != 0 && it.first <= B[i][j - 1]) g2 = 0;
if (i != 0 && B[i - 1][j] != 0 && it.first <= B[i - 1][j]) g2 = 0;
if (!g2) {
cout << "Impossible";
return 0;
}
B[i][j] = it.first;
} else {
cout << "Impossible";
return 0;
}
}
cout << "Possible";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, x, c = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> x;
if (x % 2 == 0)
cout << (x / 2 - 1) << endl;
else
cout << x / 2 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const long long INF = 1e18 + 5;
vector<vector<int> > A;
vector<int> vis;
vector<int> C;
int dfs(int u) {
vis[u] = true;
int ans = C[u];
for (auto v : A[u]) {
if (!vis[v]) {
ans = min(ans, dfs(v));
}
}
return ans;
}
int main() {
int n, m;
cin >> n >> m;
C.resize(n + 1);
for (int in = 1; in < 1 + n; in++) cin >> C[in];
A.resize(n + 1);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
A[u].push_back(v);
A[v].push_back(u);
}
long long ans = 0;
vis.resize(n + 1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans += dfs(i);
}
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100];
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
reverse(a, a + n);
int ans = 0;
for (int i = 0; i < n; ++i) {
m -= a[i];
ans++;
if (m <= 0) break;
}
cout << ans << endl;
}
| 1 |
#include<bits/stdc++.h>
#define ll long long
#define FIO "agc58F"
using namespace std;
const int N=2e3+5,K=1e4+5,S=1e6+5;
bitset<K>f[N];
int n,len[N],k,l;
char s[N][K],ans[K];
namespace Z{
int ext[K],nxt[K];
int n,m;
char s[K],t[K];
inline void build(char ss[K],char tt[K],int nn,int mm){
n=nn;m=mm;
for(int i=0;i<n;i++)s[i]=ss[i];
for(int i=0;i<m;i++)t[i]=tt[i];
int mid=0;
nxt[0]=0;
for(int i=1;i<n;i++){
if(nxt[mid]+mid>=i+nxt[i-mid])nxt[i]=nxt[i-mid];
else nxt[i]=max(0,mid+nxt[mid]-i);
while(i+nxt[i]<n&&s[nxt[i]]==s[i+nxt[i]])nxt[i]++;
if(i+nxt[i]>mid+nxt[mid])mid=i;
}
mid=0;
ext[0]=0;
//!!! ext<= min(n,m)
while(t[ext[0]]==s[ext[0]]&&ext[0]<min(n,m))ext[0]++;
for(int i=1;i<m;i++){
if(ext[mid]+mid>=i+nxt[i-mid])ext[i]=nxt[i-mid];
else ext[i]=max(0,mid+ext[mid]-i);
while(ext[i]<n&&i+ext[i]<m&&s[ext[i]]==t[i+ext[i]])ext[i]++;
if(i+ext[i]>mid+ext[mid])mid=i;
}
nxt[0]=n;
/*
puts("_______");
for(int i=0;i<n;i++)putchar(s[i]);puts("");
for(int i=0;i<m;i++)putchar(t[i]);puts("");
for(int i=0;i<n;i++)printf("%d%c",nxt[i],i^n-1?' ':'\n');
for(int i=0;i<m;i++)printf("%d%c",ext[i],i^m-1?' ':'\n');
puts("______");
*/
//ext[i] = lcp (ans[i],cur)
}
inline int cmp(int pos){
int d=ext[pos];
if(pos+d>=min(n,m))return 0;
return s[pos+d]<t[d]?-1:1;
}
}
#define pii pair<int,bool>
#define xx first
#define yy second
bitset<K> ok,vis;
pii a[K];
int st[K],t;
inline int cmpc(char a,char b){return a<b?-1:1;}
inline int cmp(pii x,pii y){
if(0)printf("cmping %d,%d:%d,%d\n",x.xx,x.yy,y.xx,y.yy);
if(x==y)return 0;
if(!x.yy&&!y.yy)return 0;
if(!x.yy){
if(x.xx<=y.xx)return 0;
/*
ans->t
cur->s
ans
xxxxxxx
ans,cur
xxxx____
lcp::
t s
*/
/*
ab
t ab
s aaa
0 1
2 0
ab
()aaa
*/
const int lcp=min(Z::ext[y.xx],x.xx-y.xx);
if(lcp==x.xx-y.xx)return 0;
return cmpc(Z::t[y.xx+lcp],Z::s[lcp]);
}else{
if(!y.yy){
/*
c
cu
0 1
2 0
*/
if(x.xx>=y.xx)return 0;
int lcp=Z::ext[x.xx];
//!!! not t[x]
//but ext[x]!!!
//printf("my lcp=%d will cmp(t[%d]=%c,s[%d]=%c)\n",lcp, y.xx+lcp,Z::t[y.xx+lcp],lcp,Z::s[lcp]);
if(lcp==Z::n)return 0;
return cmpc(Z::s[lcp],Z::t[x.xx+lcp]);
/*
xxx___
xxxxxxx
*/
}
/*
(empty)aaa
ab aaa
xxx___
xxxx___
*/
//printf("Ext[%d]=%d\n",x.xx,Z::ext[x.xx]);
int lcp=min(Z::ext[x.xx],min(y.xx-x.xx,Z::n));
//printf("lcp=%d n=%d y-x=%d\n",lcp,Z::n,y.xx-x.xx);
if(lcp==Z::n)return 0;
if(lcp<y.xx-x.xx)return cmpc(Z::s[lcp],Z::t[x.xx+lcp]);
//printf("lcp=min(%d,%d)\n",Z::n-y.xx+x.xx,Z::nxt[y.xx-x.xx]);
lcp=min(Z::n-y.xx+x.xx,Z::nxt[y.xx-x.xx]);
if(lcp==Z::n-y.xx+x.xx)return 0;
return cmpc(Z::s[y.xx-x.xx+lcp],Z::s[lcp]);
}
}
inline int cmp(int x,int y){
return cmp(a[x],a[y]);
}
inline void ins(int id,int n){
if(0)printf("===============================\nid=%d\n",id);
Z::build(s[id],ans,n,l);
vis.reset();
for(int j=0;j<=k;j++)if(f[id+1][k-j]){
if(ok[j]){vis[j]=1;a[j]=pii(j,0);}
if(j>=n&&ok[j-n]){
if(vis[j])
a[j]=cmp(a[j],pii(j-n,1))<=0?a[j]:pii(j-n,1);
else vis[j]=1,a[j]=pii(j-n,1);
}
}
if(0)for(int i=0;i<=k;i++)if(vis[i])printf("i=%d %d:%d\n",i,a[i].first,a[i].second);
t=0;
for(int j=0;j<=k;j++)if(vis[j]){
while(t&&cmp(st[t],j)>0)t--;
if(!t||cmp(st[t],j)==0)st[++t]=j;
if(0){
printf("i=%d st:",j);
for(int i=1;i<=t;i++)printf("%d ",st[i]);puts("");
}
}
if(0){
puts("-----\n");
for(int i=0;i<=k;i++)if(vis[i])printf("i=%d %d:%d\n",i,a[i].xx,a[i].yy);
printf("st:");
for(int i=1;i<=t;i++)printf("%d ",st[i]);puts("");
}
l=st[t];
if(a[l].yy)for(int j=0;j<n;j++)ans[a[l].xx+j]=s[id][j];
ok.reset();
while(t)ok[st[t--]]=1;
//ans[l]='\0';printf("id=%d\nans=%s\n",id,ans);
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%s",s[i]);
len[i]=strlen(s[i]);
}
ok[0]=f[n+1][0]=1;
for(int i=n;i;i--)
f[i]=f[i+1]|f[i+1]<<len[i];
for(int i=1;i<=n;i++)ins(i,len[i]);
ans[k]='\0';
printf("%s\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
vector<long long> a(n, 0);
vector<long long> b(n, 0);
for (int i = 0; i < n; i++) {
if (s[i] == 'a') {
a[i] = 1;
} else {
b[i] = 1;
}
if (i != 0) {
a[i] += a[i - 1];
b[i] += b[i - 1];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if ((a[i] - a[j]) == (b[i] - b[j])) {
cout << j + 2 << " " << i + 1 << endl;
return;
}
if (a[i] == b[i]) {
cout << j + 1 << " " << i + 1 << endl;
return;
}
}
}
cout << -1 << " " << -1 << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t-- > 0) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t, n, i, j, l, r;
string s, ans, temp;
cin >> t;
while (t--) {
cin >> n;
cin >> s;
ans = "";
temp = "";
l = 0;
r = n - 1;
while (l < n && s[l] == '0') l++;
while (r >= 0 && s[r] == '1') r--;
if (l > r) {
cout << s << endl;
} else {
for (i = 0; i < l; i++) {
cout << s[i];
}
for (i = r; i < n; i++) {
cout << s[i];
}
cout << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x * f;
}
void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
void writeln(int x) {
write(x);
puts("");
}
int n, m, sum, p[1000010], mp[1000010];
bool cls(int x, int y) {
if (x == 0 || y == 0) return 0;
int x1 = (x - 1) / m + 1, y1 = x % m, x2 = (y - 1) / m + 1, y2 = y % m;
if (!y1) y1 += m;
if (!y2) y2 += m;
if ((x1 - 1 == x2) && (y1 == y2)) return 1;
if ((x1 + 1 == x2) && (y1 == y2)) return 1;
if ((x1 == x2) && (y1 - 1 == y2)) return 1;
if ((x1 == x2) && (y1 + 1 == y2)) return 1;
return 0;
}
int main() {
srand(19890604);
n = read();
m = read();
sum = n * m;
for (;;) {
if (clock() > 1.9 * CLOCKS_PER_SEC) break;
for (int i = 1; i <= sum; ++i) p[i] = i, mp[i] = 0;
random_shuffle(p + 1, p + sum + 1);
bool LF = 1;
for (int i = 1; i <= sum; ++i) {
bool fl = 0;
for (int ci = 0; ci <= 10; ++ci) {
int x = (rand() << 15 | rand()) % n + 1,
y = (rand() << 15 | rand()) % m + 1;
while (mp[(x - 1) * m + y])
x = (rand() << 15 | rand()) % n + 1,
y = (rand() << 15 | rand()) % m + 1;
if ((y > 1) && cls(mp[(x - 1) * m + y - 1], p[i])) continue;
if ((y < m) && cls(mp[(x - 1) * m + y + 1], p[i])) continue;
if ((x > 1) && cls(mp[(x - 2) * m + y], p[i])) continue;
if ((x < n) && cls(mp[x * m + y], p[i])) continue;
mp[(x - 1) * m + y] = p[i];
fl = 1;
break;
}
if (!fl) {
LF = 0;
break;
}
}
if (LF) {
puts("YES");
int pos = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) printf("%d ", mp[pos++]);
puts("");
}
return 0;
}
}
puts("NO");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long ans, n, tot, e[1000001], nt[1000001], hd[1000001], f[500001][21],
a[500001], rt;
void build(long long x, long long y) {
tot++;
e[tot] = y;
nt[tot] = hd[x];
hd[x] = tot;
}
void dfs(long long x, long long fa) {
long long i, mn = a[fa], j;
f[x][0] = fa;
for (j = 1; j <= 20; j++) {
f[x][j] = f[f[x][j - 1]][j - 1];
if (!f[x][j]) break;
mn = min(mn, a[f[x][j]] * (j + 1));
}
mn = min(mn, (j + 1) * a[rt]);
if (fa) ans += mn + a[x];
for (i = hd[x]; i; i = nt[i]) {
if (e[i] == fa) continue;
dfs(e[i], x);
}
}
int main() {
long long i, x, y;
a[0] = 1e18;
rt = 0;
scanf("%lld", &n);
for (i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] < a[rt]) rt = i;
}
for (i = 1; i < n; i++) {
scanf("%lld%lld", &x, &y);
build(x, y);
build(y, x);
}
dfs(rt, 0);
printf("%lld", ans);
}
| 6 |
#include <iostream>
#include <string>
using namespace std;
int main(){
string w,T;
int a=0;
cin>>w;
while(1){
cin>>T;
int b=T.size();
for(int i=0;i<b;i++){
if(T!="END_OF_TEXT") T[i]=tolower(T[i]);
}
if(w==T) a++;
if(T=="END_OF_TEXT") break;
}
cout<<a<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int a, num;
};
int pos[2000];
data d[2000];
bool cmp(data x, data y) { return x.a < y.a; }
int main() {
int n, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &d[i].a);
d[i].num = i;
}
sort(d, d + n, cmp);
pos[d[n - 1].num] = 1;
int k = 1;
for (i = n - 2; i >= 0; i--) {
if (d[i].a == d[i + 1].a) {
pos[d[i].num] = pos[d[i + 1].num];
k++;
} else {
pos[d[i].num] = pos[d[i + 1].num] + k;
k = 1;
}
}
for (i = 0; i < n; i++) printf("%d ", pos[i]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<long long> A(N), B(M);
for (int i = 0; i < N; i++) {
cin >> A[i];
if (i) A[i] += A[i - 1];
}
for (int i = 0; i < M; i++) {
cin >> B[i];
if (i) B[i] += B[i - 1];
}
int i = 0, j = 0;
int ans = 0;
while (i < N && j < M) {
if (A[i] == B[j]) {
i++;
j++;
ans++;
} else if (A[i] < B[j]) {
i++;
} else {
j++;
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
bool cant;
int n;
int move(int i, int j) {
if (i == j) return 0;
if (i || s[i + 1] != '0') {
swap(s[i], s[i + 1]);
return 1 + move(i + 1, j);
} else {
int k = i + 1;
while (k < n && s[k] == '0') k++;
if (k == n) {
cant = true;
return 0;
}
swap(s[k], s[i + 1]);
int a = k - i - 1;
return a + move(i, j);
}
return 0;
}
int check(char x, char y) {
int ans = 0;
int i = n - 1;
while (1) {
if (s[n - 1] == y) break;
while (i >= 0 && s[i] != y) i--;
if (i < 0) return -1;
int a = move(i, n - 1);
if (cant) {
i--;
cant = false;
continue;
} else {
ans += a;
break;
}
}
i = n - 2;
while (1) {
if (s[n - 2] == x) break;
while (i >= 0 && s[i] != x) i--;
if (i < 0) return -1;
int a = move(i, n - 2);
if (cant) {
i--;
cant = false;
continue;
} else {
ans += a;
break;
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
int ans = 1e5, a;
string s1;
cin >> (s1);
;
n = s1.size();
cant = false;
s = s1;
if ((a = check('0', '0')) != -1) ans = min(ans, a);
s = s1;
if ((a = check('2', '5')) != -1) ans = min(ans, a);
s = s1;
if ((a = check('5', '0')) != -1) ans = min(ans, a);
s = s1;
if ((a = check('7', '5')) != -1) ans = min(ans, a);
cout << (ans == 1e5 ? -1 : ans) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string grid[50];
vector<int> locs;
int n, m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool vis[50][50];
int dfs(int i, int j) {
vis[i][j] = true;
int ret = 1;
for (int k = 0; k < 4; ++k) {
int ii = i + dx[k], jj = j + dy[k];
if (ii < 0 || ii >= n || jj < 0 || jj >= m || vis[ii][jj]) continue;
if (grid[ii][jj] == '#') ret += dfs(ii, jj);
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> grid[i];
for (int j = 0; j < m; ++j)
if (grid[i][j] == '#') locs.push_back(50 * i + j);
}
if (locs.size() <= 1) {
cout << -1 << endl;
return 0;
}
memset(vis, false, sizeof vis);
if (dfs(locs[0] / 50, locs[0] % 50) < locs.size()) {
cout << 0 << endl;
return 0;
}
for (int id = 0; id < locs.size(); ++id) {
int i = locs[id] / 50, j = locs[id] % 50;
int i2 = locs[(id + 1) % locs.size()] / 50,
j2 = locs[(id + 1) % locs.size()] % 50;
grid[i][j] = '.';
memset(vis, false, sizeof vis);
if (dfs(i2, j2) < locs.size() - 1) {
cout << 1 << endl;
return 0;
}
grid[i][j] = '#';
}
cout << ((locs.size() == 2) ? -1 : 2) << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
int in;
int maxin = 1;
int acc = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> in;
acc += in;
maxin = max(maxin, in);
}
int res = max(acc * 2 / n + 1, maxin);
cout << res << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
cout << (n - 2) * (n - 2) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N, K;
double M, V1, V2;
int main() {
scanf("%d%lf%lf%lf%d", &N, &M, &V1, &V2, &K);
N = (N + K - 1) / K;
N = (N << 1) - 1;
printf("%.10f\n", M * (V2 * N + V1) / (V1 * N + V2) / V2);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d, h;
cin >> n >> d >> h;
if (d > 2 * h || d < h || (n > 2 && h + d == 2)) {
cout << -1;
return 0;
}
if (h == d) {
for (int i = 1; i < h + 1; i++) cout << i << ' ' << i + 1 << '\n';
int make_pair = n - h - 1;
for (int j = h + 2; make_pair > 0; make_pair--, j++) {
cout << h << ' ' << j << '\n';
}
return 0;
}
int make_pair = n - h - 1, dd = d - h, raz = 0;
raz = make_pair / dd;
for (int i = 1; i < h + 1; i++) cout << i << ' ' << i + 1 << '\n';
int it = h + 2;
for (int i = 0; i < raz; i++)
for (int j = 0; j < dd; j++) {
if (j == 0)
cout << 1;
else
cout << it - 1;
cout << ' ' << it << '\n';
it++;
}
if (make_pair % dd) {
for (int j = 0; j < make_pair % dd; j++) {
if (j == 0)
cout << 1;
else
cout << it - 1;
cout << ' ' << it << '\n';
it++;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int n, a, x, b, y, flag = 0;
scanf("%d %d %d %d %d", &n, &a, &x, &b, &y);
while (a != x && b != y) {
if (a != x) {
a = (a + 1) % n;
if (!a) a = n;
}
if (b != y) {
b--;
if (!b) b = n;
}
if (a == b) {
flag = 1;
break;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[11];
char s[5];
int x, y, ans;
a[0] = 2, a[1] = 7, a[2] = 2, a[3] = 3, a[4] = 3, a[5] = 4, a[6] = 2,
a[7] = 5, a[8] = 1, a[9] = 2;
scanf("%s", s);
x = s[0] - '0';
y = s[1] - '0';
ans = a[x] * a[y];
printf("%d\n", ans);
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main(){
int a,b;
int cou = -1;
while(1){
int count = 0;
cin >> a >> b;
if(a == 0 && b == 0) break;
if(cou != -1) cout << endl;
for(int i = a; i <= b; i++){
cou++;
if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
cout << i << endl;
count++;
}
}
if(count == 0){
cout << "NA" << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
cin >> s;
n = s.size();
if (s[n - 1] == '0' || s[n - 1] == '2' || s[n - 1] == '4' ||
s[n - 1] == '6' || s[n - 1] == '8')
cout << "0";
else
cout << "1";
return 0;
}
| 4 |
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#define int long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(u) for(int i=hd[u],v;v=e[i].v,i;i=e[i].n)
#define REQ(u) for(int i=Hd[u],v;v=E[i].v,i;i=E[i].n)
using namespace std;
const int N=400400,M=N<<1,P=1e9+7;
struct edge{int n,v;}e[M],E[M];
int n,m,u,v,fl,Fl,T,ans;
int hd[N],in[N],vi[N],a[N],b[N],f[N],fa[N],du[N],sz[N],Hd[N];
int iv[N],fc[N],vf[N];
vector<int>ve;
queue<int>q;
void add(int u,int v){e[++fl]=(edge){hd[u],v};hd[u]=fl;du[v]++;}
void Add(int u,int v){E[++Fl]=(edge){Hd[u],v};Hd[u]=Fl;}
void dfs0(int u){
ve.push_back(u);
REP(u)if(in[v] && v!=fa[u]) fa[v]=u,dfs0(v);
}
void dfs1(int u){
vi[u]=1;a[++T]=u;ve.push_back(u);
REP(u) if(!vi[v]) dfs1(v);else if(in[v]) dfs0(v);
}
void dfs2(int u,int ff){
REP(u)if(in[v] && v!=fa[u]){
dfs2(v,u);
if(ff>v) Add(u,v),b[v]=1;
}
}
int dfs3(int u){
int ret=1;sz[u]=f[u]=1;
REQ(u) dfs3(v),f[u]=1ll*f[v]*f[u]%P,sz[u]+=sz[v];
f[u]=1ll*f[u]*iv[sz[u]]%P;
}
int sol(){
int ret=1;
for(int u:ve) Hd[u]=b[u]=0;Fl=0;
FOR(j,1,T)if(a[j-1]>a[j+1]) Add(a[j],a[j+1]),b[a[j+1]]=1;
//for(int u:ve){cout<<u<<':';REQ(u) cout<<v<<' ';cout<<'\n';}
FOR(j,1,T) dfs2(a[j],a[j-1]);
for(int u:ve)if(!b[u]) dfs3(u),ret=1ll*ret*f[u]%P;
return ret;
}
int work(int u){
int ret=0;
T=0;ve.clear();dfs1(u);
//FOR(i,1,T) cout<<a[i]<<' ';cout<<'\n';
a[0]=a[T];a[T+1]=a[1];
(ret+=sol())%=P;
reverse(a,a+T+2);
(ret+=sol())%=P;
return ret;
}
signed main(){
//freopen("robot.in","r",stdin);
//freopen("robot.out","w",stdout);
scanf("%d",&n);m=2*n;
iv[1]=fc[0]=vf[0]=1;
FOR(i,2,m) iv[i]=1ll*(P-P/i)*iv[P%i]%P;
FOR(i,1,m) fc[i]=1ll*fc[i-1]*i%P,vf[i]=1ll*vf[i-1]*iv[i]%P;
FOR(i,1,m) scanf("%d%d",&u,&v),add(u,v+n),add(v+n,u);
FOR(i,1,m) if(!du[i]) return puts("0"),0;
FOR(i,1,m) if(du[i]==1) vi[i]=in[i]=1,q.push(i);
while(q.size()){
u=q.front();q.pop();
REP(u)if(!vi[v] && (--du[v])==1) vi[v]=in[v]=1,q.push(v);
}
ans=1;
FOR(i,1,m)if(!vi[i]) ans=1ll*ans*work(i)%P;
printf("%lld\n",1ll*fc[m]*ans%P);
}/*
4
1 1
1 2
2 1
2 2
3 3
3 4
4 3
4 4
*/
| 0 |
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
struct edge
{
ll x,w;
};
ll n,m,x,y,w,vis[100005],a[100005],b[100005],i,l,r;
vector<edge> s[100005];
bool ok=true;
void dfs(int p)
{
if(ok==false)return;
edge v;
ll i,tmp1,tmp2,u,vv;
vis[p]=1;
for(i=0;i<s[p].size();i++)
{
v=s[p][i];
tmp1=v.w-a[p];
tmp2=-b[p];
if(vis[v.x])
{
if(tmp1!=a[v.x]||tmp2!=b[v.x])
{
if((tmp2==b[v.x]&&tmp1!=a[v.x])||(tmp1-a[v.x])%(b[v.x]-tmp2)!=0)
{
ok=false;
return;
}
u=(tmp1-a[v.x])/(b[v.x]-tmp2);
r=min(r,u);
l=max(l,u);
}
continue;
}
a[v.x]=tmp1;
b[v.x]=tmp2;
dfs(v.x);
if(ok==false)return;
}
}
int main()
{
l=1;r=1e18;
cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>x>>y>>w;
s[x].push_back((edge){y,w});
s[y].push_back((edge){x,w});
}
a[1]=0;
b[1]=1;
dfs(1);
if(ok==false)
{
cout<<"0"<<endl;
return 0;
}
for(i=1;i<=n;i++)
{
if(b[i]==-1)r=min(r,a[i]-1);
else l=max(l,-a[i]+1);
}
cout<<max(1LL*0,r-l+1);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void c_h_g() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
c_h_g();
long long t;
cin >> t;
while (t--) {
string s;
cin >> s;
int r = 0, s1 = 0, p = 0;
for (long long i = 0; i < s.length(); i++) {
if (s[i] == 'R')
r += 1;
else if (s[i] == 'S')
s1 += 1;
else
p += 1;
}
map<int, char> make_pair;
make_pair[r] = 'R';
make_pair[s1] = 'S';
make_pair[p] = 'P';
int z = max(r, max(s1, p));
char q;
if (make_pair[z] == 'R') q = 'P';
if (make_pair[z] == 'S') q = 'R';
if (make_pair[z] == 'P') q = 'S';
string z1 = "";
for (long long i = 0; i < s.length(); i++) z1 += q;
cout << z1 << endl;
}
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int main(){
int a,b,c,d,e,f,N,i;
cin >> N;
for(i=0;i<N;i++){
cin >> a >> b>>c;
d=a*a;
e=b*b;
f=c*c;
if(d==e+f||e==d+f||f==d+e){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b;
int t;
int main(){
cin>>t;
while(t--){
cin>>a>>b;
ll l=1,r=10e9;
if(a>b)swap(a,b);
while(l<r){
ll mid=(l+r+1)/2;
if(mid*mid>=a*b){
r=mid-1;
}else{
l=mid;
}
}
//cout<<l<<endl;
r=l;
while(l*(r+1)<a*b) r++;
ll ans=l*2;
//cout<<l<<" "<<r<<endl;
if(l==r) ans--;
if(a<=l) ans--;
//if(r==l && r==b) ans--;
//if(a==b && l==r && a==l) ans++;
cout<<ans<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k, t;
string s;
cin >> t;
while (t--) {
cin >> n >> k >> s;
int c0 = 0, c1 = 0, cq = 0;
vector<string> a;
int l;
for (l = 0; l <= n - k; l += k) a.push_back(s.substr(l, k));
if (l < n) {
string b = "";
for (int i = l; i < l + k; i++) {
if (i < n)
b.push_back(s[i]);
else
b.push_back('?');
}
a.push_back(b);
}
char c;
for (int j = 0; j < k; ++j) {
c = '?';
for (int i = 0; i < a.size(); ++i) {
if (a[i][j] != '?' && c != a[i][j]) {
if (c == '?')
c = a[i][j];
else {
cout << "NO\n";
goto end;
}
}
}
a[0][j] = c;
}
for (int i = 0; i < k; ++i) {
if (a[0][i] == '0')
c0++;
else if (a[0][i] == '1')
c1++;
else
cq++;
}
if (!(c0 == k / 2 && c1 == k / 2)) {
if (c0 + c1 == k)
cout << "NO\n";
else if (c0 > k / 2 || c1 > k / 2)
cout << "NO\n";
else if (((k / 2 - c0) + (k / 2 - c1)) != cq)
cout << "NO\n";
else
goto next;
continue;
}
next:
cout << "YES\n";
end:;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
long long ipow(long long a, long long b)
{
if(b==0) return 1;
long long ans = ipow(a, b/2);
ans = ans*ans%MOD;
if(b%2==1) ans=ans*a%MOD;
return ans;
}
long long fact(int N)
{
long long ret = 1;
for(int i=1; i<=N; ++i) ret=ret*i%MOD;
return ret;
}
int solve(int N)
{
long long ans = ipow(3, N);
long long nCk = fact(N)*ipow(fact(N/2), 2*(MOD-2))%MOD;;
for(int k=N/2+1; k<=N; ++k)
{
nCk = nCk * ipow(k, MOD-2)%MOD*(N-k+1)%MOD;
ans -= 2*nCk*ipow(2, N-k);
ans %= MOD;
ans += MOD;
ans %= MOD;
}
return ans;
}
int main()
{
int N; scanf("%d", &N);
printf("%d\n", solve(N));
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 10;
const long long mod = 1e9 + 7;
long long n, m;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
long long f[maxn][maxn][5];
long long F[maxn], ans;
long long jc[maxn], c[maxn][maxn];
signed main() {
n = read(), m = read();
jc[0] = f[1][0][0] = f[1][1][2] = 1;
for (long long i = 2; i <= n; i++) {
f[i][0][0] = 1;
for (long long j = 1; j <= i; j++) {
f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1] + f[i - 1][j - 1][0]) % mod;
f[i][j][1] = (f[i - 1][j][2] + f[i - 1][j][3] + f[i - 1][j - 1][2]) % mod;
f[i][j][2] = (f[i - 1][j - 1][0] + f[i - 1][j - 1][1]) % mod;
f[i][j][3] = (f[i - 1][j - 1][2] + f[i - 1][j - 1][3]) % mod;
}
}
for (long long i = 1; i <= n; i++) jc[i] = jc[i - 1] * i % mod;
for (long long i = 0; i <= n; i++)
F[i] = (f[n][i][0] + f[n][i][1]) * jc[n - i] % mod;
c[0][0] = 1;
for (long long i = 1; i <= n; i++) {
c[i][0] = 1;
for (long long j = 1; j <= i; j++)
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
}
for (long long i = m; i <= n; i++)
ans = (ans + ((i - m) & 1 ? -1 : 1) * c[i][m] * F[i] % mod) % mod;
printf("%lld\n", (ans + mod) % mod);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
template <class T>
long long chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
long long chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <class T>
T sqr(T a) {
return a * a;
}
template <class T>
T mmin(T a, T b) {
return a < b ? a : b;
}
template <class T>
T mmax(T a, T b) {
return a > b ? a : b;
}
template <class T>
T aabs(T a) {
return a < 0 ? -a : a;
}
template <class T>
long long dcmp(T a, T b) {
return a > b;
}
template <long long *a>
long long cmp_a(long long first, long long second) {
return a[first] < a[second];
}
namespace io {
const long long SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
qu[55];
long long f, qr;
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char first) {
*oS++ = first;
if (oS == oT) flush();
}
inline bool read(signed &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(long long &first) {
for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
c < '0' || c > '9';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
if (c == '-')
f = -1;
else if (c == EOF)
return 0;
for (first = 0; c <= '9' && c >= '0';
c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++))
first = first * 10 + (c & 15);
first *= f;
return 1;
}
inline bool read(char &first) {
first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
return first != EOF;
}
inline bool read(char *first) {
while ((*first = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++)) == '\n' ||
*first == ' ' || *first == '\r')
if (*first == EOF) return 0;
while (!(*first == '\n' || *first == ' ' || *first == '\r'))
*(++first) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
*first = 0;
return 1;
}
template <typename A, typename... B>
inline bool read(A &first, B &...second) {
return read(first) && read(second...);
}
inline bool write(signed first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(long long first) {
if (!first) putc('0');
if (first < 0) putc('-'), first = -first;
while (first) qu[++qr] = first % 10 + '0', first /= 10;
while (qr) putc(qu[qr--]);
return 0;
}
inline bool write(char first) {
putc(first);
return 0;
}
inline bool write(const char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
inline bool write(char *first) {
while (*first) {
putc(*first);
++first;
}
return 0;
}
template <typename A, typename... B>
inline bool write(A first, B... second) {
return write(first) || write(second...);
}
struct Flusher_ {
~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::putc;
using io ::read;
using io ::write;
vector<pair<long long, long long> > linex[10005];
vector<pair<long long, long long> > lny[10005];
long long s[10005], t[10005];
void add(long long *s, long long first, long long second) {
for (; first <= 10001; first += first & -first) s[first] += second;
}
long long sum(long long first) {
long long second = 0;
for (; first; first ^= first & -first) second += s[first];
return second;
}
signed main() {
long long n;
read(n);
long long PinkRabbit_AK_NOI, PinkRabbitaknoi, praknoi, orzpr;
for (long long i = 1; i <= n; ++i) {
read(PinkRabbit_AK_NOI, PinkRabbitaknoi, praknoi, orzpr);
if (PinkRabbit_AK_NOI > praknoi) swap(PinkRabbit_AK_NOI, praknoi);
if (PinkRabbitaknoi > orzpr) swap(PinkRabbitaknoi, orzpr);
PinkRabbit_AK_NOI += 5001;
PinkRabbitaknoi += 5001;
praknoi += 5001;
orzpr += 5001;
if (PinkRabbit_AK_NOI == praknoi)
linex[PinkRabbit_AK_NOI].push_back(make_pair(PinkRabbitaknoi, orzpr));
else {
lny[PinkRabbit_AK_NOI].push_back(make_pair(PinkRabbitaknoi, praknoi + 1));
lny[praknoi + 1].push_back(
make_pair(-PinkRabbitaknoi, PinkRabbit_AK_NOI));
}
}
long long ans = 0;
for (long long i = 1; i <= 10001; ++i) {
for (auto j : lny[i]) {
if (j.first > 0)
add(t, j.first, 1);
else
add(t, -j.first, -1);
}
if (!linex[i].empty()) {
for (long long j = 1; j <= 10001; ++j) s[j] = t[j];
for (long long j = i + 1; j <= 10001; ++j) {
for (auto k : lny[j])
if (k.first < 0 && k.second <= i) add(s, -k.first, -1);
for (auto second : linex[j])
for (auto first : linex[i]) {
PinkRabbit_AK_NOI = mmax(second.first, first.first);
praknoi = mmin(second.second, first.second);
if (PinkRabbit_AK_NOI <= praknoi) {
long long w = (sum(praknoi) - sum(PinkRabbit_AK_NOI - 1));
ans += w * (w - 1) >> 1;
}
}
}
}
}
write(ans, '\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
int min(int a, int b) { return a < b ? a : b; }
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n, k, p, x, y;
cin >> n >> k >> p >> x >> y;
int totalSum = 0;
int lowerThanMed = 0;
int lowerAllowed = n / 2;
int points[1000];
for (int i = 0; i < k; i++) {
cin >> points[i];
totalSum += points[i];
if (points[i] < y) lowerThanMed++;
}
if (lowerThanMed > lowerAllowed) {
cout << "-1\n";
return 0;
}
int minAdd = min(lowerAllowed - lowerThanMed, n - k);
int mustAdd = minAdd + y * (n - k - minAdd);
if (totalSum + mustAdd > x) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < minAdd; i++) cout << "1 ";
for (int i = 0; i < n - k - minAdd; i++) cout << y << " ";
cout << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
puts("INTERCAL");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int spar[200005][18], l[200005];
long long int maxi[200005][18];
vector<pair<long long int, pair<int, int>>> node, node1;
vector<pair<int, long long int>> g[200005];
map<pair<int, int>, long long int> mp;
int par[200005], rnk[200005];
int Find(int u) {
if (par[u] == u) return u;
return par[u] = Find(par[u]);
}
void union_set(int a, int b) {
if (a != b) {
if (rnk[a] < rnk[b]) swap(a, b);
par[b] = a;
if (rnk[a] == rnk[b]) rnk[a]++;
}
}
long long int fun(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
rnk[i] = 0;
}
int cnt = 0;
long long int ret = 0;
sort(node.begin(), node.end());
for (int i = 0; i < node.size(); i++) {
int u = node[i].second.first, v = node[i].second.second;
long long int w = node[i].first;
int p1 = Find(u);
int p2 = Find(v);
if (Find(u) != Find(v)) {
union_set(p1, p2);
ret += w;
cnt++;
g[u].push_back({v, w});
g[v].push_back({u, w});
if (cnt == n - 1) break;
}
}
return ret;
}
void dfs(int u, int p, int d) {
spar[u][0] = p;
l[u] = d;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].first;
if (v == p) continue;
maxi[v][0] = g[u][i].second;
dfs(v, u, d + 1);
}
}
void init(int n) {
memset(maxi, 0, sizeof(maxi));
memset(spar, -1, sizeof(spar));
}
void sparse(int n) {
for (int i = 1; (1 << i) < n; i++) {
for (int j = 1; j <= n; j++) {
if (spar[j][i - 1] != -1) {
maxi[j][i] = max(maxi[j][i - 1], maxi[spar[j][i - 1]][i - 1]);
spar[j][i] = spar[spar[j][i - 1]][i - 1];
}
}
}
}
long long int LCA(int p, int q, int n) {
long long int mx = 0;
if (p == q) {
return 0;
}
if (l[p] < l[q]) swap(p, q);
int log = log2(n);
for (int i = log; i >= 0; i--) {
if (l[p] - (1 << i) >= l[q]) {
mx = max(maxi[p][i], mx);
p = spar[p][i];
}
}
if (p == q) {
return mx;
}
for (int i = log; i >= 0; i--) {
if ((spar[p][i] != -1) && (spar[p][i] != spar[q][i])) {
mx = max(mx, max(maxi[p][i], maxi[q][i]));
p = spar[p][i];
q = spar[q][i];
}
}
mx = max(mx, max(maxi[p][0], maxi[q][0]));
return mx;
}
int main() {
int n, m, u, v;
long long int w;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
scanf("%d%d%lld", &u, &v, &w);
mp[{u, v}] = w;
node.push_back({w, {u, v}});
node1.push_back({w, {u, v}});
}
long long int tot = fun(n);
init(n);
dfs(1, 1, 0);
sparse(n);
for (int i = 0; i < node1.size(); i++) {
u = node1[i].second.first;
v = node1[i].second.second;
printf("%lld\n", tot - LCA(u, v, n) + mp[{u, v}]);
}
}
| 5 |
#include <bits/stdc++.h>
double EPS = 1e-7;
using namespace std;
int n;
struct PT {
double x, y;
PT() {}
PT(double x, double y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
PT operator*(double c) const { return PT(x * c, y * c); }
PT operator/(double c) const { return PT(x / c, y / c); }
};
PT z[100100];
double dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
PT ProjectPointSegment(PT a, PT b, PT c) {
double r = dot(b - a, b - a);
if (fabs(r) < EPS) return a;
r = dot(c - a, b - a) / r;
if (r < 0) return a;
if (r > 1) return b;
return a + (b - a) * r;
}
double dist2(PT p, PT q) { return dot(p - q, p - q); }
double DistancePointSegment(PT a, PT b, PT c) {
return sqrt(dist2(c, ProjectPointSegment(a, b, c)));
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> z[i].x >> z[i].y;
}
double mn = 1e18;
for (int i = 0; i < n; i++) {
mn = min(mn, hypot(z[i].y - z[(i + 1) % n].y, z[i].x - z[(i + 1) % n].x));
}
for (int i = 0; i < n; i++) {
int i1 = i + 1;
if (i1 >= n) i1 -= n;
int i2 = i + 2;
if (i2 >= n) i2 -= n;
mn = min(mn, DistancePointSegment(z[i], z[i2], z[i1]) / 2);
}
cout << fixed << setprecision(10) << mn << endl;
return 0;
}
| 4 |
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
int W,H;
int g[7][7];
int cg[7][7];
int ly,lx;
bool inside(int y,int x){
return 0<=y&&y<H&&0<=x&&x<W;
}
bool neighbor(int y,int x){
return abs(y-ly)+abs(x-lx)==1;
}
void ff(int y,int x){
if(inside(y,x)&&cg[y][x]==0){
for(int i=0;i<4;i++){
cg[y][x]=1;
ff(y+(~i&i-1),x+(i?2-i:0));
}
}
}
bool dfs(int y,int x,int r){
if(r==1)return neighbor(y,x);
g[y][x]=1;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
if(g[i][j]==0){
// copy(g[0],g[7],cg[0]);
// ff(i,j);
// for(int k=0;k<H;k++){
// for(int l=0;l<W;l++){
// if(cg[k][l]==0){
// goto fail;
// }
// }
// }
for(int i=0;i<4;i++){
int ny=y+(~i&i-1);
int nx=x+(i?2-i:0);
if(inside(ny,nx)&&g[ny][nx]==0){
for(int j=0;j<4;j++){
if(i==j)continue;
int oy=y+(~j&j-1);
int ox=x+(j?2-j:0);
if(inside(oy,ox)&&g[oy][ox]==0&&!neighbor(oy,ox)){
int c=0;
for(int k=0;k<4;k++){
int ony=oy+(~k&k-1);
int onx=ox+(k?2-k:0);
c+=inside(ony,onx)&&g[ony][onx]==0;
}
if(c<2)goto next;
}
}
if(dfs(ny,nx,r-1))return true;
next:
;
}
}
goto fail;
}
}
}
fail:
g[y][x]=0;
return false;
}
int main(){
while(cin>>W>>H,W){
int c=0;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
cin>>g[i][j];
if(g[i][j]==0){
ly=i;
lx=j;
c++;
}
}
}
cout<<((c&&dfs(ly,lx,c))?"Yes":"No")<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e+9;
string second, a[100005], first, mx;
bool comp(string a, string b) {
int n, m;
n = a.size();
m = b.size();
if (n > m) return 1;
if (n < m) return 0;
for (int i = 0; i < n; i++)
if (a[i] < b[i])
return 0;
else if (a[i] > b[i])
return 1;
else
continue;
return 1;
}
int main() {
int n, size = 0, x;
cin >> second;
n = second.size();
a[size] += second[0];
for (int i = 1; i < n; i++) {
if (second[i] != '0') {
size++;
if (size == 1) first = a[0];
if (size == 2) {
mx = a[1];
x = 1;
}
if (size > 2) {
if (comp(a[size - 1], mx)) {
mx = a[size - 1];
x = size - 1;
}
}
}
a[size] += second[i];
}
size++;
if (size == 2) {
mx = a[1];
x = 1;
}
if (size > 2) {
if (comp(a[size - 1], mx)) {
mx = a[size - 1];
x = size - 1;
}
}
int j;
while (true) {
first = a[0];
j = 1;
while (!comp(first, mx) && j <= x) first += a[j++];
if (j == x + 1 || j == 1) break;
mx = a[1];
x = 1;
for (int i = 2; i < j; i++)
if (comp(a[i], mx)) {
mx = a[i];
x = i;
}
}
cout << size - j + 1;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
cout << x << endl;
exit(0);
}
const int maxn = 1e6 + 5;
int n, m;
int a[maxn];
vector<int> g[maxn];
vector<pair<int, pair<int, int>>> edges;
int par[maxn], siz[maxn];
int f(int x) { return par[x] == x ? x : par[x] = f(par[x]); }
void join(int x, int y) {
x = f(x);
y = f(y);
if (x == y) return;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
par[i] = i;
siz[i] = 1;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
edges.push_back({min(a[u], a[v]), {u, v}});
}
auto cmp = [](pair<int, pair<int, int>> p1, pair<int, pair<int, int>> p2) {
return p1.first > p2.first;
};
long double num = 0;
long double den = 0;
sort(edges.begin(), edges.end(), cmp);
for (auto p : edges) {
int wei = p.first;
int u = f(p.second.first);
int v = f(p.second.second);
if (u == v) continue;
long double paths = 1LL * siz[u] * siz[v];
den += paths;
num += 1.0 * wei * paths;
join(u, v);
}
long double ans = num / den;
cout << fixed << setprecision(9) << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " : " << a << endl;
err(++it, args...);
}
const int N = 2 * 1e5 + 10;
vector<pair<int, pair<int, int> > > g[N];
int w[N], ces[N], wyn[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
wyn[i] = -1;
int a, b, we;
cin >> a >> b >> we;
g[a].push_back({i, {b, we}});
g[b].push_back({-i, {a, we}});
w[a] += we;
w[b] += we;
}
queue<int> q;
q.push(1);
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto x : g[node]) {
int ind = abs(x.first), we = x.second.second, neigh = x.second.first;
if (wyn[abs(ind)] != -1) continue;
if (x.first > 0)
wyn[ind] = 0;
else
wyn[ind] = 1;
w[neigh] -= we;
ces[neigh] += we;
if (w[neigh] == ces[neigh] and neigh != n) q.push(neigh);
}
}
for (int i = 1; i <= m; i++) cout << wyn[i] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long abss(long long a) {
if (a < 0) return -a;
return a;
}
long long Case() {
long long M, N, X1, X2, Y1, Y2;
cin >> N >> M >> X1 >> Y1 >> X2 >> Y2;
long long Dx = abss(X1 - X2), Dy = abss(Y1 - Y2);
long long ladX = N - Dx, ladY = M - Dy;
long long Area = ladX * ladY * 2;
long long supX = max(2 * ladX - N, (long long)0),
supY = max(2 * ladY - M, (long long)0);
long long Areasup = supX * supY;
return N * M - (Area - Areasup);
}
int main() {
int T;
cin >> T;
while (T--) cout << Case() << endl;
}
| 2 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
class Euler{
public:
int N, root;
vector<vector<int> > G;
vector<int> l;
vector<int> r;
Euler():N(-1){}
Euler(int N, int root):N(N),root(root),G(N),l(N),r(N){}
void add_edge(int a, int b){
assert( 0 <= a && a < N );
assert( 0 <= b && b < N );
G[a].push_back(b);
G[b].push_back(a);
}
void build(){
int cnt = -1;
dfs( root, root, cnt );
}
void dfs(int x, int px, int &cnt){
l[x] = ++cnt;
for(int nx : G[x] ){
if( nx != px ) dfs( nx, x, cnt );
}
r[x] = cnt;
}
// [ res.first, res.second ]
tuple<int,int> getRange(int x){
return tuple<int,int>( l[x], r[x] );
}
};
class Seg{
public:
int N;
vector<int> S;
Seg(){}
Seg(int n){
N = 1;
while( N < n ) N *= 2;
S.resize( N * 2 - 1, 0 );
}
void add(int a, int b, int l, int r, int k, int w){
if( r <= a || b <= l ) return ;
if( a <= l && r <= b ){
S[k] += w;
return ;
}
add( a, b, l, ( l + r ) / 2, k * 2 + 1 , w );
add( a, b, ( l + r ) / 2, r, k * 2 + 2 , w );
}
// [a, b)
void add(int a, int b, int w){
add( a, b, 0, N, 0, w );
}
int get(int x){
x += N - 1;
int res = S[x];
while( x ){
x = ( x - 1 ) / 2;
res += S[x];
}
return res;
}
};
signed main(){
int N;
cin >> N;
Euler euler( N, 0 );
Seg seg( N );
for(int i=0;i<N;i++){
int k;
cin >> k;
for(int j=0;j<k;j++){
int c;
cin >> c;
euler.add_edge( i, c );
}
}
euler.build();
int Q;
cin >> Q;
while( Q-- ){
int flg;
cin >> flg;
if( flg == 0 ){
int v, w;
cin >> v >> w;
int l, r;
tie( l, r ) = euler.getRange( v );
seg.add( l, r + 1, w );
}
else{
int u;
cin >> u;
int l, r;
tie( l, r ) = euler.getRange( u );
cout << seg.get( l ) << endl;
}
}
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.