solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
string str;
int n;
struct oper {
int d;
string t;
} p[maxn];
long long val[10], wei[10];
int main() {
cin >> str;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i].d >> p[i].t;
}
for (int i = 0; i < 10; i++) val[i] = i, wei[i] = 10;
for (int i = n; i >= 1; i--) {
int l = p[i].t.size();
long long sum_val = 0, sum_wei = 1;
for (int j = 2; j < l; j++) {
sum_val *= wei[p[i].t[j] - '0'];
sum_val += val[p[i].t[j] - '0'];
sum_val %= mod;
sum_wei *= wei[p[i].t[j] - '0'];
sum_wei %= mod;
}
val[p[i].d] = sum_val, wei[p[i].d] = sum_wei;
}
long long ans = 0;
int l = str.size();
for (int i = 0; i < l; i++)
ans *= wei[str[i] - '0'], ans += val[str[i] - '0'], ans %= mod;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long INF = 1e9;
inline long long pwr(long long base, long long n, long long m) {
long long ans = 1;
while (n > 0) {
if (n % 2 == 1) ans = (ans * base) % m;
base = (base * base) % m;
n /= 2;
}
return ans;
}
const int N = 3e5 + 10;
int a[N], l[N], r[N], n;
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++) {
l[i] = i;
int j = i - 1;
while (j >= 0 && a[j] % a[i] == 0) {
l[i] = l[j];
j = l[i] - 1;
}
}
for (int i = n - 1; i >= 0; i--) {
r[i] = i;
int j = i + 1;
while (j < n && a[j] % a[i] == 0) {
r[i] = r[j];
j = r[i] + 1;
}
}
int mx = 0;
for (int i = 0; i < n; i++) mx = max(r[i] - l[i], mx);
set<int> res;
for (int i = 0; i < n; i++) {
if (r[i] - l[i] == mx) res.insert(l[i]);
}
cout << res.size() << " " << mx << "\n";
for (auto i : res) cout << i + 1 << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
vector<int> graf[MAXN];
int n, m, bio[MAXN], flag;
vector<pair<int, int> > edges;
pair<int, int> forbid;
bool dfs(int node) {
bio[node] = flag + 2;
for (int nex : graf[node]) {
if (bio[nex] == flag + 1 || forbid == make_pair(node, nex)) continue;
if (bio[nex] == flag + 2) {
edges.push_back(make_pair(node, nex));
return true;
}
edges.push_back(make_pair(node, nex));
if (dfs(nex)) return true;
edges.pop_back();
}
bio[node] = flag + 1;
return false;
}
bool check() {
flag += 3;
for (int i = 0; i < n; i++) {
if (bio[i] <= flag && dfs(i)) return true;
}
return false;
}
void fin(bool a) {
if (a) {
cout << "YES" << endl;
exit(0);
}
cout << "NO" << endl;
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int a, b;
for (int i = 0; i < m; i++) {
cin >> a >> b;
graf[a - 1].push_back(b - 1);
}
if (!check()) fin(true);
vector<pair<int, int> > myedges = edges;
for (pair<int, int> &nex : myedges) {
edges.clear();
forbid = nex;
if (!check()) fin(true);
}
fin(false);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m;
void solve() {
long long int t, k, z, p, q, u, v, x, y, ct = 0, flag = 0;
cin >> n;
double diagonal = 2 / (sqrt(2 * (1 - cos(3.1415926535897932384626 / n))));
double angle = double(180) / double(2 * n);
double cosAngle = cos(angle * 3.1415926535897932384626 / 180);
cout << fixed << showpoint;
cout << setprecision(8) << diagonal * cosAngle << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int _;
cin >> _;
while (_--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int MOD2 = 1007681537;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline long long isqrt(long long k) {
long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
const int maxn = 1e6 + 5;
int n, k;
int a[maxn];
int cnt[maxn];
int rem[maxn];
void solve() {
cin >> n >> k;
k--;
set<pair<int, int> > st;
for (int i = (0); i < (n); i++) {
cin >> a[i];
a[i]--;
st.insert(make_pair(0, a[i]));
}
st.insert(make_pair(0, k));
for (int i = (0); i < (n); i++) {
int x = a[i];
if (rem[x]) continue;
if ((st.find(make_pair(cnt[x], x)) != st.end())) {
st.erase(make_pair(cnt[x], x));
}
st.insert(make_pair(++cnt[x], x));
while (int((st).size()) && st.begin()->first < cnt[k]) {
int ix = st.begin()->second;
st.erase(st.begin());
rem[ix] = 1;
}
}
if (int((st).size()) == 1) {
cout << -1 << "\n";
return;
}
for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); it++) {
int x = it->second;
if (x != k) {
cout << x + 1 << "\n";
return;
}
}
}
int main() {
int JUDGE_ONLINE = 1;
if (fopen("in.txt", "r")) {
JUDGE_ONLINE = 0;
assert(freopen("in.txt", "r", stdin));
} else {
ios_base::sync_with_stdio(0), cin.tie(0);
}
solve();
if (!JUDGE_ONLINE) {
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long N;
vector<long long> V;
inline void Get(const long long& A, const long long& B, const long long& C,
int k) {
long long L = 0, R = min(1LL << 31, N / B) + 1;
while (R - L > 1) {
long long M = (L + R) >> 1;
if ((unsigned long long)A * M * M + (unsigned long long)B * M +
(unsigned long long)C <=
(unsigned long long)N)
L = M;
else
R = M;
}
if ((unsigned long long)A * L * L + (unsigned long long)B * L +
(unsigned long long)C ==
(unsigned long long)N)
V.push_back((1LL << k) * (2 * L + 1));
}
int main() {
scanf("%I64d\n", &N);
for (int A = 0; A <= 61; ++A) Get(2, (1LL << A + 1) - 1, (1LL << A) - 1, A);
sort(V.begin(), V.end());
if ((int)V.size())
for (int i = 0; i < (int)V.size(); ++i) printf("%I64d\n", V[i]);
else
puts("-1");
fclose(stdin);
fclose(stdout);
return 0;
}
| 2 |
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
int a, b, c, d, e, f;
while(1){
scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if((a|b|c|d|e|f) == 0) break;
a += d;
b += e;
c += f;
int lim = min(a, min(b, c)), ans = 0;
for(int i=0;i<=lim&&i<3;i++) ans = max(ans, i + (a-i) / 3 + (b-i) / 3 + (c-i) / 3);
printf("%d\n", ans);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char x[100][100];
int n, m;
bool f = false, f1 = false;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
if (f == false)
x[i][j] = '#';
else
x[i][j] = '.';
f = !f;
}
for (int i = 1; i < n; i += 2) {
if (f1 == false)
x[i][m - 1] = '#';
else
x[i][0] = '#';
f1 = !f1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << x[i][j];
cout << endl;
}
}
| 1 |
#include <iostream>
#include <vector>
using namespace std;
int main(){
int r,c;
while(cin >> r >> c && r && c){
vector<vector<int> > v(r,vector<int>(c)), v2;
int ans = 0;
for(int i=0;i<r;i++) for(int j=0;j<c;j++) cin >> v[i][j];
for(int i=0;i<(1<<(r-1));i++){
v2 = v;
for(int j=0;j<r-1;j++){
if((i & (1<<j)) != 0){
for(int k=0;k<c;k++) v2[j][k] = (v2[j][k] + 1) % 2;
}
}
int a = 0;
for(int j=0;j<c;j++){
int b = 0;
for(int k=0;k<r;k++){
if(v2[k][j] == 0) b++;
}
a += max(b,r-b);
}
ans = max(ans,a);
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
double a[100005], b[100005], p, q;
int n;
double y, l, r;
double check(double x) {
y = 1000000000;
for (int i = 1; i <= n; i++) y = min(y, (1 - a[i] * x) / b[i]);
return p * x + q * y;
}
int main() {
scanf("%d", &n);
scanf("%lf%lf", &p, &q);
for (int i = 1; i <= n; i++) scanf("%lf%lf", &a[i], &b[i]), y = max(y, a[i]);
l = 0, r = 1 / y;
for (int i = 1; i <= 300; i++) {
double stp = (r - l) / 3;
if (check(l + stp) < check(r - stp))
l += stp;
else
r -= stp;
}
printf("%.8f", check(l));
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main()
{int N, a, c{}; cin >> N;
for (int i{1}; i <= N; ++i) cin >> a, c += i % 2 * (a % 2);
cout << c;} | 0 |
#include<cstdio>
char a[10],b[10];
int ans;
int main()
{
scanf("%s%s",a,b);
for(int i=0;i<3;i++)
if(a[i]==b[i])ans++;
printf("%d",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, bel[25], tag[25], F[1 << 20];
char S[100005], T[100005];
int Getbel(int x) { return bel[x] == x ? x : (bel[x] = Getbel(bel[x])); }
void Merge(int a, int b) {
a = Getbel(a), b = Getbel(b);
if (a != b) bel[a] = b;
}
int main() {
int C;
scanf("%d", &C);
while (C--) {
memset(F, 0, sizeof(F));
memset(tag, 0, sizeof(tag));
scanf("%d%s%s", &n, S, T);
for (int i = 0; i < 20; i++) bel[i] = i;
for (int i = 0; i < n; i++) {
tag[S[i] - 'a'] |= 1 << (T[i] - 'a');
Merge(S[i] - 'a', T[i] - 'a');
}
F[0] = 1;
int ans = 40, mxx = 0;
for (int i = 0; i < 20; i++)
if (bel[i] == i) ans--;
for (int s = 0; s < (1 << 20); s++) {
if (!F[s]) continue;
int cnt = 0;
for (int i = 0; i < 20; i++) {
if (s & (1 << i)) cnt++;
if (s & tag[i]) continue;
F[s | (1 << i)] = 1;
}
mxx = max(mxx, cnt);
}
printf("%d\n", ans - mxx);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> failureFunc(const string &s) {
vector<int> longestPreSuf(s.size() + 1);
longestPreSuf[0] = 0;
longestPreSuf[1] = 0;
for (int i = 2; i <= s.size(); ++i) {
for (int j = longestPreSuf[i - 1];; j = longestPreSuf[j]) {
if (s[j] == s[i - 1]) {
longestPreSuf[i] = j + 1;
break;
}
if (j == 0) {
longestPreSuf[i] = 0;
break;
}
}
}
return longestPreSuf;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string f, t;
cin >> f;
--n;
while (n--) {
cin >> t;
int minSize = min(f.size(), t.size());
int overlap =
failureFunc(t.substr(0, minSize) + '$' + f.substr(f.size() - minSize))
.back();
f += t.substr(overlap);
}
cout << f << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, q;
long long w[N];
struct node {
int l, r;
long long sum, maxx, minn, tag;
} tr[N * 4];
void pushup(int u) {
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
tr[u].minn = min(tr[u << 1].minn, tr[u << 1 | 1].minn);
tr[u].maxx = max(tr[u << 1].maxx, tr[u << 1 | 1].maxx);
}
void pushdown(int u) {
auto &root = tr[u], &lson = tr[u << 1], &rson = tr[u << 1 | 1];
if (root.tag) {
lson.tag = rson.tag = root.tag;
lson.maxx = lson.minn = root.tag;
rson.maxx = rson.minn = root.tag;
lson.sum = root.tag * (lson.r - lson.l + 1);
rson.sum = root.tag * (rson.r - rson.l + 1);
root.tag = 0;
}
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) {
tr[u].sum = tr[u].maxx = tr[u].minn = w[l];
tr[u].tag = 0;
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void update(int u, int l, int r, long long k) {
if (tr[u].minn >= k) return;
if (tr[u].l >= l && tr[u].r <= r && tr[u].maxx < k) {
tr[u].sum = k * (tr[u].r - tr[u].l + 1);
tr[u].minn = tr[u].maxx = tr[u].tag = k;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) update(u << 1, l, r, k);
if (r > mid) update(u << 1 | 1, l, r, k);
pushup(u);
}
long long query(int u, int l, int r, long long &k) {
if (tr[u].minn > k) return 0ll;
if (tr[u].l >= l && tr[u].r <= r && tr[u].sum <= k) {
k -= tr[u].sum;
return 1ll * (tr[u].r - tr[u].l + 1);
}
pushdown(u);
long long res = 0;
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) res += query(u << 1, l, r, k);
if (r > mid) res += query(u << 1 | 1, l, r, k);
return res;
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%lld", &w[i]);
build(1, 1, n);
int op, x;
long long y;
while (q--) {
scanf("%d%d%lld", &op, &x, &y);
if (op == 1)
update(1, 1, x, y);
else
printf("%lld\n", query(1, x, n, y));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int mod = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x, y, z, w, i, j, k, l;
vector<int> v;
cin >> n;
if (!n)
return cout << "YES\n1\n2\n2\n3", 0;
else if (n == 1) {
cin >> x;
cout << "YES\n" << 2 * x << '\n' << 2 * x << '\n' << 3 * x;
return 0;
} else if (n == 2) {
cin >> x >> y;
for (i = 1; i <= 2000; ++i)
for (j = 1; j <= 2000; ++j) {
v.clear();
v.push_back(i), v.push_back(j), v.push_back(x), v.push_back(y);
sort(v.begin(), v.end());
if ((v[1] + v[2]) % 2 == 0 and (v[0] + v[1] + v[2] + v[3]) % 4 == 0 and
(v[0] + v[1] + v[2] + v[3]) / 4 == (v[1] + v[2]) / 2 and
(v[1] + v[2]) / 2 == (v[3] - v[0])) {
cout << "YES\n";
for (auto it : v)
if (it != x and it != y)
cout << it << '\n';
else if (it == x)
x = -1;
else if (it == y)
y = -1;
return 0;
}
}
} else if (n == 3) {
cin >> x >> y >> z;
for (i = 1; i <= 2000; ++i) {
v.clear();
v.push_back(i), v.push_back(x), v.push_back(y), v.push_back(z);
sort(v.begin(), v.end());
if ((v[1] + v[2]) % 2 == 0 and (v[0] + v[1] + v[2] + v[3]) % 4 == 0 and
(v[0] + v[1] + v[2] + v[3]) / 4 == (v[1] + v[2]) / 2 and
(v[1] + v[2]) / 2 == (v[3] - v[0])) {
cout << "YES\n";
for (auto it : v)
if (it != x and it != y and it != z)
cout << it << '\n';
else if (it == x)
x = -1;
else if (it == y)
y = -1;
else if (it == z)
z = -1;
return 0;
}
}
} else {
cin >> x >> y >> z >> w;
v.push_back(x), v.push_back(y), v.push_back(z), v.push_back(w);
sort(v.begin(), v.end());
if ((v[1] + v[2]) % 2 == 0 and (v[0] + v[1] + v[2] + v[3]) % 4 == 0 and
(v[0] + v[1] + v[2] + v[3]) / 4 == (v[1] + v[2]) / 2 and
(v[1] + v[2]) / 2 == (v[3] - v[0])) {
cout << "YES\n";
return 0;
}
}
cout << "NO";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
for (int i = 0; i <= N; i++) if (i * 27 / 25 == N) {
cout << i << endl;
return 0;
}
cout << ":(" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, f[105][3005], g, h, i, j, k, l, m, n;
char s[10005];
int main() {
f[0][0] = 1;
for (i = 1; i <= 100; i++)
for (j = 0; j <= i * 26; j++)
for (k = 0; k < 26 && k <= j; k++) {
(f[i][j] += f[i - 1][j - k]) %= 1000000007;
}
int t;
scanf("%d", &t);
for (; t; t--) {
scanf("%s", s);
l = strlen(s);
h = 0;
for (i = 0; i < l; i++) h = h + s[i] - 'a';
printf("%d\n", f[l][h] - 1);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
int a[110000];
bool e[33];
signed main() {
cin>>N;
for(int i=0;i<N;i++)cin>>a[i];
int x=0;
for(int i=0;i<N;i++){
x^=a[i];
for(int j=0;j<33;j++){
if(a[i]%2==1){
e[j]=true;
break;
}
a[i]/=2;
}
}
bitset<33> xbs(x);
bool sw=0;
int ans=0;
for(int i=32;i>=0;i--){
if(xbs[i]^sw){
if(e[i]){
ans++;
sw=!sw;
}else{
cout<<-1<<endl;
return 0;
}
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int dirx[4] = {0, 1, 0, -1};
const int diry[4] = {1, 0, -1, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
;
long long int t, n, x;
cin >> t;
while (t--) {
cin >> n;
vector<int> odd, even;
for (int i = 0; i < n; i++) {
cin >> x;
if (x % 2)
odd.push_back(x);
else
even.push_back(x);
}
bool flag = false;
if (odd.size() % 2 == 0)
flag = true;
else {
for (int i = 0; i < odd.size(); i++) {
for (int j = 0; j < even.size(); j++) {
if (abs(odd[i] - even[j]) == 1) {
flag = true;
break;
}
}
if (flag) break;
}
}
if (flag)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool graph[3010][3010];
vector<int> graph2[3010], out;
int dis[3010][3010];
vector<pair<int, int> > v1[3010], v2[3010];
int ans;
void bfs(int start) {
memset(dis[start], -1, sizeof(dis[0]));
dis[start][start] = 0;
queue<int> que;
que.push(start);
while (!que.empty()) {
int cur = que.front();
que.pop();
for (int i : graph2[cur]) {
if (dis[start][i] != -1) continue;
dis[start][i] = dis[start][cur] + 1;
que.push(i);
}
}
}
bool check(int a, int b, int c, int d) { return a == c || b == d || a == d; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
while (m--) {
int x, y;
cin >> x >> y;
if (x == y || graph[x][y]) continue;
graph[x][y] = 1;
graph2[x].emplace_back(y);
}
for (int i = 1; i <= n; i++) bfs(i);
auto cmp = [&](pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (dis[i][j] > 0) {
v1[i].push_back({j, dis[i][j]});
v2[j].push_back({i, dis[i][j]});
}
}
for (int i = 1; i <= n; i++) {
sort(v1[i].begin(), v1[i].end(), cmp);
sort(v2[i].begin(), v2[i].end(), cmp);
}
for (int b = 1; b <= n; b++)
for (int c = 1; c <= n; c++) {
if (b == c || dis[b][c] <= 0) continue;
int sizeB = v2[b].size();
int sizeC = v1[c].size();
for (int i = 0; i < min(2, sizeB); i++) {
for (int j = 0; j < min(2, sizeC); j++) {
int a = v2[b][i].first;
int d = v1[c][j].first;
if (check(a, b, c, d)) continue;
int len = dis[a][b] + dis[b][c] + dis[c][d];
if (len > ans) {
ans = len;
out = {a, b, c, d};
}
}
}
}
for (int i : out) cout << i << ' ';
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
int dp[10000010][2];
int main(){
string s; cin >> s;
int N=s.size();
dp[0][0]=0;
dp[0][1]=1;
for(int i=0;i<N;i++){
int d=s[i]-'0';
dp[i+1][0]=min(dp[i][0]+d,dp[i][1]+10-d);
dp[i+1][1]=min(dp[i][0]+d+1,dp[i][1]+10-(d+1));
}
cout << dp[N][0] << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
int k;
cin >> k;
string second;
cin >> second;
long long sum = 0;
vector<int> a;
for (int i = 0; i < second.size(); i++) {
sum += second[i] - 48;
a.push_back(second[i] - 48);
}
sort((a).begin(), (a).end());
if (sum < k) {
int cnt = 0;
for (int i = 0; i < a.size(); i++) {
int d = 9 - a[i];
if (sum + d < k && d > 0) {
sum += d;
cnt++;
} else {
d = k - sum;
if (d != 0) cnt++;
break;
}
}
cout << cnt;
} else
cout << 0;
}
| 2 |
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
VL slide_max(VL a, int k){
VL ret;
deque<int> dq;
REP(i,a.size()){
while (!dq.empty() && a[dq.back()] <= a[i]) dq.pop_back();
dq.push_back(i);
ret.push_back(a[dq.front()]);
if (i - k + 1 >= 0){
if (dq.front() == i - k + 1) dq.pop_front();
}
}
return ret;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
VL a(n);
REP(i,n) scanf("%lld", &a[i]);
VL dp(n);
REP(i,n) dp[i] = a[i];
REP(x,k-1){
VL ma = slide_max(dp, m);
dp[0] = -1e18;
REP(i,n-1) dp[i+1] = ma[i] + a[i+1] * (x + 2);
// REP(i,n) cout << dp[i] << " ";
// cout << endl;
}
ll ma = 0;
REP(i,n) ma = max(ma, dp[i]);
cout << ma << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 300005;
int n, m, q, cc, cnt, top, Top, dfn_cnt, Dfn_cnt, bcc_ct, Bcc_ct, node_ct,
edge_ct, used_ct, times[Maxn], used[Maxn], c[Maxn], father[Maxn], bel[Maxn],
Bel[Maxn], sta[Maxn], Sta[Maxn], dep[Maxn], anc[Maxn][20], node[Maxn],
real_node[Maxn], mas[Maxn], dfn[Maxn], low[Maxn], Dfn[Maxn], Low[Maxn],
head[Maxn];
vector<int> Ve[Maxn], Vt[Maxn];
long long R;
struct edg {
int nxt, to;
} edge[2 * Maxn];
void add(int x, int y) {
edge[++cnt] = (edg){head[x], y};
head[x] = cnt;
}
void Tarjan(int u, int f) {
bool tag = false;
father[u] = f;
dfn[u] = low[u] = ++dfn_cnt;
sta[++top] = u;
for (int i = head[u]; i; i = edge[i].nxt) {
int to = edge[i].to;
if (to == f && !tag) {
tag = true;
continue;
}
if (!dfn[to]) {
Tarjan(to, u);
low[u] = min(low[u], low[to]);
if (low[to] > dfn[u]) {
mas[++bcc_ct] = to;
int x;
do {
x = sta[top--];
bel[x] = bcc_ct;
} while (x != to);
}
} else
low[u] = min(low[u], dfn[to]);
}
}
void Tarjan2(int u, int f) {
bool tag = false;
Dfn[u] = Low[u] = ++Dfn_cnt;
Sta[++Top] = u;
for (vector<int>::iterator it = Vt[u].begin(); it != Vt[u].end(); it++) {
int to = *it;
if (to == f && !tag) {
tag = true;
continue;
}
if (!Dfn[to]) {
Tarjan2(to, u);
Low[u] = min(Low[u], Low[to]);
if (Low[to] > Dfn[u]) {
Bcc_ct++;
int x;
do {
x = Sta[Top--];
Bel[x] = Bcc_ct;
} while (x != to);
}
} else
Low[u] = min(Low[u], Dfn[to]);
}
}
void dfs(int u, int fa, int tim) {
times[u] = tim;
dfn[u] = ++dfn_cnt;
for (vector<int>::iterator it = Ve[u].begin(); it != Ve[u].end(); it++) {
if (*it == fa) continue;
anc[*it][0] = u;
dep[*it] = dep[u] + 1;
dfs(*it, u, tim);
}
}
void init(void) {
for (int j = 1; (1 << j) <= bcc_ct; j++)
for (int i = 1; i <= bcc_ct; i++) anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 19; i >= 0; i--)
if ((dep[x] - dep[y]) & (1 << i)) x = anc[x][i];
if (x == y) return x;
for (int i = 19; i >= 0; i--)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
void build(void) {
sort(node + 1, node + 1 + node_ct,
[](int x, int y) { return dfn[x] < dfn[y]; });
node_ct = unique(node + 1, node + 1 + node_ct) - node - 1;
for (int i = 1; i <= node_ct; i++) {
if (times[node[i]] != times[node[i - 1]]) {
while (top > 1)
Vt[sta[top]].push_back(sta[top - 1]),
Vt[sta[top - 1]].push_back(sta[top]), top--;
top = 0;
if (times[node[i]] != node[i])
sta[++top] = times[node[i]], used[++used_ct] = times[node[i]];
}
if (!top) {
sta[++top] = node[i];
used[++used_ct] = node[i];
continue;
}
int l = lca(sta[top], node[i]);
while (top > 1 && dep[l] < dep[sta[top - 1]])
Vt[sta[top]].push_back(sta[top - 1]),
Vt[sta[top - 1]].push_back(sta[top]), top--;
if (dep[l] < dep[sta[top]])
Vt[sta[top]].push_back(l), Vt[l].push_back(sta[top]), top--;
if (!top || sta[top] != l) sta[++top] = l, used[++used_ct] = l;
sta[++top] = node[i];
used[++used_ct] = node[i];
}
while (top > 1)
Vt[sta[top]].push_back(sta[top - 1]), Vt[sta[top - 1]].push_back(sta[top]),
top--;
}
int rotate(int element) {
element = (element + R - 1) % n + 1;
return element;
}
int main() {
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) {
Tarjan(i, 0);
mas[++bcc_ct] = i;
while (top) bel[sta[top]] = bcc_ct, top--;
}
for (int i = 1; i <= bcc_ct; i++)
if (father[mas[i]])
Ve[bel[father[mas[i]]]].push_back(i),
Ve[i].push_back(bel[father[mas[i]]]);
dfn_cnt = 0;
for (int i = 1; i <= bcc_ct; i++)
if (!anc[i][0]) dfs(i, 0, i);
init();
for (int Case = 1; Case <= q; Case++) {
cc = Bcc_ct = top = dfn_cnt = used_ct = 0;
scanf("%d%d", &node_ct, &edge_ct);
for (int i = 1; i <= node_ct; i++) {
scanf("%d", &node[i]);
node[i] = bel[rotate(node[i])];
}
memcpy(real_node, node, sizeof(int[node_ct + 1]));
int real_node_ct = node_ct;
for (int i = 1; i <= edge_ct; i++) {
int x, y;
scanf("%d%d", &x, &y);
x = bel[rotate(x)], y = bel[rotate(y)];
node[++node_ct] = x, node[++node_ct] = y;
Vt[x].push_back(y), Vt[y].push_back(x);
}
build();
for (int i = 1; i <= used_ct; i++)
if (!Dfn[used[i]]) {
Tarjan2(used[i], 0);
++Bcc_ct;
while (Top) Bel[Sta[Top]] = Bcc_ct, Top--;
}
for (int i = 1; i <= real_node_ct; i++) {
c[Bel[real_node[i]]]++;
if (c[Bel[real_node[i]]] == 1) cc++;
if (cc > 1) {
puts("NO");
goto END;
}
}
puts("YES"), R += Case;
END:
for (int i = 1; i <= used_ct; i++) Vt[used[i]].clear(), Dfn[used[i]] = 0;
for (int i = 1; i <= real_node_ct; i++) c[Bel[real_node[i]]] = 0;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void clear(T &x) {
T t;
x.swap(t);
}
template <class T>
void miz(T &a, T b) {
if (b < a) a = b;
}
template <class T>
void maz(T &a, T b) {
if (b > a) a = b;
}
template <class T>
T gcd(T a, T b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
vector<int> a;
int n, x, z, i, ans = 0;
bool flag = 1;
scanf("%d%d", &n, &z);
a.push_back(0);
while (n--) {
scanf("%d", &x);
a.push_back(x);
if (x == z) flag = 0;
}
if (flag) {
a.push_back(z);
ans++;
}
n = a.size() - 1;
sort(a.begin(), a.end());
int L = lower_bound(a.begin(), a.end(), z) - a.begin(), R;
for (R = L; R < n && a[R + 1] == z; R++)
;
while (1) {
i = (n + 1) / 2;
if (a[i] < z) {
a.push_back(1000000);
n++;
} else if (a[i] > z) {
a.insert(a.begin(), 0);
n++;
} else {
break;
}
ans++;
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct point {
int x, y;
} a[4];
double d(point a, point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double ans;
void gan(point p1, point p2, point p3, point p4) {
double temp = d(p1, p2) + d(p2, p3) + d(p3, p4);
if (temp > ans) {
ans = temp;
a[0] = p1;
a[1] = p2;
a[2] = p3;
a[3] = p4;
}
}
int main() {
cin >> n >> m;
ans = 0;
point p1, p2, p3, p4;
p1.x = p1.y = 0;
p2.x = n;
p2.y = m;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
if ((i != 0 || j != 0) && (i != n || j != m)) {
p3.x = i;
p3.y = j;
p4.x = n - i;
p4.y = m - j;
if (!(p3.x == p4.x && p3.y == p4.y)) {
gan(p1, p2, p3, p4);
gan(p1, p3, p2, p4);
gan(p3, p1, p2, p4);
}
}
}
}
for (int i = 0; i < 4; ++i) {
cout << a[i].x << ' ' << a[i].y << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int inf = 1000001000;
long long INF = 1e18;
long long MOD = INF;
long long mod = INF;
int main() {
int n;
string s;
cin >> n >> s;
int u = n - 1;
for (int(i) = 0; (i) < (n - 1); (i)++) {
if (s[i] > s[i + 1]) {
u = i;
break;
}
}
for (int(i) = 0; (i) < (n); (i)++) {
if (i == u) continue;
cout << s[i];
}
cout << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string s;
bool c=true,d=false;
cin >> s;
for(n=2;n<s.length();n++){
if(isupper(s[n])&&d){
c=false;
}else if(s[n]=='C'){
d=true;
}
}
if(s[0]=='A'&&islower(s[1])&&d&&c&&s[s.length()-1]!='C'){
cout << "AC" << endl;
}else {
cout << "WA" << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 50 + 10;
int n = 50;
ll k;
int main()
{
scanf("%lld", &k);
ll base = k / n + 49;
int l = k % n;
printf("%d\n", n);
for(int i = 1; i <= n - l; ++i) printf("%lld ", base - l);
for(int i = n - l + 1; i <= n; ++i) printf("%lld ", base + 51 - l);
return 0;
} | 0 |
#include<stdio.h>
int main()
{
int k;
scanf("%d", &k);
while(k--) printf("ACL");
printf("\n");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
string s;
cin>>s;
long long ans=0LL,cnt=0LL;
for (int i = 2; i < s.size(); ++i)
{
if (s.at(i-2)=='A')
{
++cnt;
}
else
{
cnt=0LL;
}
if (s.at(i-1)=='B'&&s.at(i)=='C')
{
ans+=cnt;
--cnt;
s.at(i)='A';
++i;
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MOD2 = 15000007;
const long long int MAX = 9187201950435737471;
const int N = 100005;
const int M = 100005;
const int INF = 2;
const double EPS = 1e-9;
const long double PI = acos(-1);
long long int po(long long int x, long long int y) {
x %= MOD;
long long int ret = 1;
while (y > 0) {
if (y & 1) {
ret = (ret * x) % MOD;
}
y = y >> 1ll;
x = (x * x) % MOD;
}
return ret;
}
map<int, vector<int> > mp;
void prc(int x) {
int j = 0;
mp[x].push_back(j);
j++;
while (x) {
x /= 2;
mp[x].push_back(j);
j++;
}
}
void solve(int T) {
int n;
cin >> n;
string a, b;
cin >> a >> b;
string c[] = {"abc", "acb", "bac", "bca", "cab", "cba"};
cout << "YES\n";
for (int i = 0; i < 6; i++) {
string tt = "";
for (int j = 0; j < n; j++) {
tt += c[i];
}
bool ok = 1;
for (int j = 0; j < tt.size() - 1; j++) {
if ((a[0] == tt[j] && a[1] == tt[j + 1]) ||
(b[0] == tt[j] && b[1] == tt[j + 1])) {
ok = 0;
break;
}
}
if (ok == 1) {
cout << tt;
return;
}
}
if (a[0] == b[1] && a[1] == b[0]) {
for (int i = 0; i < n; i++) cout << a[0];
for (char i = 'a'; i < 'd'; i++) {
if (i != a[0] && i != b[0]) {
for (int j = 0; j < n; j++) cout << i;
}
}
for (int i = 0; i < n; i++) cout << b[0];
return;
}
if (a[0] == b[0]) {
for (int i = 0; i < n; i++) cout << a[1];
for (int i = 0; i < n; i++) cout << b[1];
for (int i = 0; i < n; i++) cout << a[0];
return;
}
if (a[1] == b[1]) {
for (int i = 0; i < n; i++) cout << a[1];
for (int i = 0; i < n; i++) cout << b[0];
for (int i = 0; i < n; i++) cout << a[0];
return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
for (int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int dp[21][100005];
int sum[100005];
int main() {
memset(dp, -1, sizeof(dp));
int n, q, a;
scanf("%d%d", &n, &q);
sum[0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
for (int j = 0; j < 20; j++) dp[j][i] = dp[j][i - 1];
sum[i] = sum[i - 1];
bool ok = 0;
for (int j = 19; j >= 0; j--) {
if ((a >> j) & 1) {
if (dp[j][i] == -1) {
dp[j][i] = a;
ok = 1;
break;
} else
a ^= dp[j][i];
}
}
if (!ok) sum[i] = (sum[i] * 2) % mod;
}
for (int i = 1; i <= q; i++) {
int l, x;
scanf("%d%d", &l, &x);
bool ok = 1;
for (int j = 19; j >= 0; j--) {
if ((x >> j) & 1) {
if (dp[j][l] == -1) {
ok = 0;
break;
} else
x ^= dp[j][l];
}
}
if (ok)
printf("%d\n", sum[l]);
else
printf("0\n");
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<int, int> a, pair<int, int> b) { return a.second > b.second; }
int main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> arr(m, {0, 0});
for (int i = 0; i < m; i++) {
cin >> arr[i].first >> arr[i].second;
}
sort(arr.begin(), arr.end(), comp);
int k = 0;
int t = 0;
int i = 0;
while (k != n && i < m) {
int p = min(arr[i].first, n - k);
t += p * arr[i].second;
k += p;
i++;
}
cout << t << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char str[1005];
scanf("%s", str);
int cc = -1;
while (str[++cc] != '.') {
}
if (str[cc - 1] == '9') {
printf("GOTO Vasilisa.\n");
return 0;
}
if (str[cc + 1] >= '5') {
for (int i = 0; i < cc - 1; i++) printf("%c", str[i]);
printf("%c", str[cc - 1] + 1);
} else {
for (int i = 0; i < cc; i++) printf("%c", str[i]);
}
printf("\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
char s[200005], g[200005];
int p[200005], q[200005], ans;
signed main() {
scanf("%s", s + 1);
scanf("%s", g + 1);
int n = strlen(s + 1), m = strlen(g + 1), now = 1;
for (int i = 1; i <= n; i++)
if (now <= m && s[i] == g[now]) q[now++] = i;
now = m;
for (int i = n; i >= 1; i--)
if (now >= 1 && s[i] == g[now]) p[now--] = i;
ans = max(ans, max(p[1] - 1, n - q[m]));
for (int i = 1; i < m; i++)
if (p[i + 1] > q[i]) ans = max(ans, p[i + 1] - q[i] - 1);
printf("%d", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long s[n], c[n];
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++) cin >> c[i];
long long s0, s1, s2, ans = 1e18;
for (int j = 1; j < n; j++) {
bool f1 = 0, f2 = 0;
s0 = c[j];
s1 = 1e10;
s2 = 1e10;
for (int i = j - 1; i >= 0; i--) {
if (s[i] < s[j]) {
f1 = 1;
s1 = min(s1, c[i]);
}
}
for (int k = j + 1; k < n; k++) {
if (s[k] > s[j]) {
f2 = 1;
s2 = min(s2, c[k]);
}
}
if (f1 == 1 && f2 == 1) {
ans = min(ans, s0 + s1 + s2);
}
}
if (ans == 1e18) ans = -1;
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[1000005];
bool c[1000005];
int dp[1000005][20];
bool f[1000005];
int dd(int x, int n) {
for (int i = 0; i < 20; i++) {
if ((1 << i) & n) x = dp[x][i];
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int Z;
cin >> Z;
while (Z--) {
int n, m;
cin >> n >> m;
string s;
for (int i = 0; i < n; i++) {
cin >> s;
for (int j = 0; j < m; j++) c[m * i + j] = s[j] - '0', f[m * i + j] = 0;
}
for (int i = 0; i < n; i++) {
cin >> s;
for (int j = 0; j < m; j++) {
int x = i, y = j;
if (s[j] == 'U')
x--;
else if (s[j] == 'D')
x++;
else if (s[j] == 'L')
y--;
else
y++;
dp[m * i + j][0] = m * x + y;
}
}
for (int k = 1; k < 20; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
dp[i * m + j][k] = dp[dp[i * m + j][k - 1]][k - 1];
int mn = m * n;
int bl = 0, tot = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dest = dd(i * m + j, mn);
if (!c[i * m + j] && !f[dest]) {
f[dest] = 1;
bl++, tot++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int dest = dd(i * m + j, mn);
if (c[i * m + j] && !f[dest]) {
f[dest] = 1;
tot++;
}
}
}
cout << tot << " " << bl << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
int T;
cin >> T;
while (T--) {
int n, a[550];
bool b[550];
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
bool l = 0, k = 0;
for (int i = 1; i <= n; i++) {
cin >> b[i];
if (b[i]) l = 1;
if (!b[i]) k = 1;
}
if (l && k)
cout << "YES";
else if (l || k) {
if (is_sorted(a + 1, a + n + 1))
cout << "YES";
else
cout << "NO";
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int M = 300010;
bool taken[M];
char s[M];
int main(int argc, char** argv) {
int len, m;
scanf("%d%d\n", &len, &m);
scanf("%s\n", s + 1);
s[0] = 'x';
s[len + 1] = 'x';
len += 2;
int out = 0;
memset(taken, 0, sizeof taken);
for (int i = 1; i < len; i++) {
if (s[i] == '.' && s[i - 1] == '.') {
out++;
taken[i] = 1;
}
}
int pos;
char c;
while (m--) {
cin >> pos >> c;
if (c == '.' && s[pos] != '.') {
s[pos] = c;
if (s[pos - 1] == '.') {
out++;
taken[pos] = 1;
}
if (s[pos + 1] == '.' && !taken[pos + 1]) {
out++;
taken[pos + 1] = 1;
}
}
if (c != '.' && s[pos] == '.') {
s[pos] = c;
if (taken[pos]) {
taken[pos] = 0;
out--;
}
if (taken[pos + 1]) {
taken[pos + 1] = 0;
out--;
}
}
cout << out << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400 + 10;
struct Graph {
int id, next, b[MAXN];
} g[MAXN * 2];
int n, k, st[MAXN], f[MAXN][MAXN], size[MAXN];
void Add(int tot, int first, int second) {
g[tot].id = second, g[tot].next = st[first], st[first] = tot;
}
void Tree_dp(int first, int fa) {
size[first] = 1;
f[first][1] = 0;
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].id != fa) {
int second = g[i].id;
Tree_dp(second, first);
for (int j = size[first] + 1; j <= size[first] + size[second]; ++j)
f[first][j] = MAXN;
size[first] += size[second];
for (int j = min(size[first], k); j >= 1; --j) {
g[i].b[j] = 0, ++f[first][j];
for (int k = 1; k <= min(size[second], j - 1); ++k)
if (f[first][j - k] + f[second][k] < f[first][j])
f[first][j] = f[first][j - k] + f[second][k], g[i].b[j] = k;
}
}
}
void DFS(int first, int k, int fa) {
vector<int> s;
for (int i = st[first]; i != -1; i = g[i].next)
if (g[i].id != fa) s.push_back(i);
reverse(s.begin(), s.end());
for (int i = 0; i < (int)s.size(); ++i)
if (!g[s[i]].b[k])
printf("%d ", s[i] / 2);
else
DFS(g[s[i]].id, g[s[i]].b[k], first), k -= g[s[i]].b[k];
}
int main() {
cin >> n >> k;
if (n == k) puts("0"), exit(0);
memset(st, -1, sizeof(st));
for (int i = 1; i < n; ++i) {
int first, second;
cin >> first >> second;
Add(i * 2, first, second);
Add(i * 2 + 1, second, first);
}
int Tohka = MAXN, Mashiro = 0;
if (k <= 10)
for (int i = 1; i <= n; ++i) {
Tree_dp(i, 0);
if (f[i][k] < Tohka) Tohka = f[i][k], Mashiro = i;
}
else
for (int i = 1; i <= n / k * 10; ++i) {
int first = rand() % n + 1;
Tree_dp(first, 0);
if (f[first][k] < Tohka) Tohka = f[first][k], Mashiro = first;
}
cout << Tohka << endl;
Tree_dp(Mashiro, 0);
DFS(Mashiro, k, 0);
fclose(stdin);
fclose(stdout);
return 0;
}
| 4 |
#include<cstdio>
using namespace std;
int a[100050];
int main(){
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
for (int i=2;i<=n;i++)
if (a[i]>a[i-1]) a[i]--;else
if (a[i]<a[i-1]) return printf("No\n"),0;
printf("Yes\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int) (n); i++)
int n, k;
void solve0()
{
if (n == 1) {
cout << k / 2;
cout << endl;
return;
}
cout << k / 2 << " ";
rep(i, n-2) {
cout << k << " ";
}
cout << k << endl;
}
inline void sub(vector<int>& v, int& pos)
{
if (v[pos] == 1) {
v[pos] = 0;
pos--;
} else {
v[pos]--;
for (int i = pos+1; i < n; i++) v[i] = k;
pos = n-1;
}
}
void solve1()
{
vector<int> res(n, k / 2 + 1);
long long a = n / 2;
int pos = n-1;
while (a--) {
sub(res, pos);
}
for (int i = 0; i < pos; i++) {
cout << res[i] << " ";
}
cout << res[pos] << endl;
}
int main()
{
cin >> k >> n;
if (k % 2) solve1();
else solve0();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<string, pair<int, vector<int> > > m;
vector<string> s;
int t;
bool search(string x) {
for (int i = 0; i < s.size(); i++)
if (x == s[i]) return true;
return false;
}
void prep(string x) {
for (int i = 0; i < 60; i++) m[x].second.push_back(0);
}
string check(string x, string y) {
if (m[x].first > m[y].first)
return x;
else if (m[x].first < m[y].first)
return y;
else {
for (int i = 0;; i++) {
if (m[x].second[i] > m[y].second[i])
return x;
else if (m[x].second[i] < m[y].second[i])
return y;
}
}
}
string check2(string x, string y) {
if (m[x].second[0] > m[y].second[0])
return x;
else if (m[x].second[0] < m[y].second[0])
return y;
else if (m[x].first > m[y].first)
return x;
else if (m[x].first < m[y].first)
return y;
else {
for (int i = 1;; i++) {
if (m[x].second[i] > m[y].second[i])
return x;
else if (m[x].second[i] < m[y].second[i])
return y;
}
}
}
int number(int p) {
if (p == 1) return 25;
if (p == 2) return 18;
if (p == 3) return 15;
if (p == 4) return 12;
if (p == 5) return 10;
if (p == 6) return 8;
if (p == 7) return 6;
if (p == 8) return 4;
if (p == 9) return 2;
if (p == 10)
return 1;
else
return 0;
}
int main() {
cin >> t;
for (int i = 1; i <= t; i++) {
int n;
cin >> n;
for (int j = 1; j <= n; j++) {
string x;
cin >> x;
if (!search(x)) s.push_back(x), prep(x);
m[x].first += number(j);
m[x].second[j - 1]++;
}
}
string maxv = s[0], maxx = s[0];
for (int i = 1; i < s.size(); i++) maxv = check(maxv, s[i]);
for (int i = 1; i < s.size(); i++) maxx = check2(maxx, s[i]);
cout << maxv << endl;
cout << maxx << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 200043;
int add(int x, int y) { return (x + y) % MOD; }
int sub(int x, int y) { return add(x, MOD - y); }
int mul(int x, int y) { return (x * 1ll * y) % MOD; }
int binpow(int x, int y) {
int z = 1;
while (y > 0) {
if (y % 2 == 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int inv(int x) { return binpow(x, MOD - 2); }
int fact[N];
int C(int n, int k) { return mul(fact[n], inv(mul(fact[k], fact[n - k]))); }
int main() {
int n, k;
cin >> n >> k;
if (k > n - 1) {
cout << 0 << endl;
return 0;
}
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = mul(i, fact[i - 1]);
int ans = 0;
int c = n - k;
for (int i = c; i >= 0; i--)
if (i % 2 == c % 2)
ans = add(ans, mul(binpow(i, n), C(c, i)));
else
ans = sub(ans, mul(binpow(i, n), C(c, i)));
ans = mul(ans, C(n, c));
if (k > 0) ans = mul(ans, 2);
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int l1, l2, l3, dp[105][105][105];
char s1[105], s2[105], s3[105];
int dfs(int n1, int n2, int n3) {
if (n1 > l1 || n2 > l2) return dp[n1][n2][n3] = 0;
if (~dp[n1][n2][n3]) return dp[n1][n2][n3];
int &re = dp[n1][n2][n3];
re = 0;
if (s1[n1] == s2[n2]) {
if (s1[n1] == s3[n3]) {
if (n3 == l3) {
re = dfs(n1 + 1, n2 + 1, n3);
} else {
if (s1[n1] == s3[1])
re = max(dfs(n1 + 1, n2 + 1, n3), min(dfs(n1 + 1, n2 + 1, n3 + 1) + 1,
dfs(n1 + 1, n2 + 1, 2) + 1));
else
re = max(dfs(n1 + 1, n2 + 1, n3), dfs(n1 + 1, n2 + 1, n3 + 1) + 1);
}
} else
re = dfs(n1 + 1, n2 + 1, 1 + (s1[n1] == s3[1])) + 1;
} else {
re = max(dfs(n1 + 1, n2, n3), dfs(n1, n2 + 1, n3));
}
return re;
}
void write(int n1, int n2, int n3) {
if (n1 > l1 || n2 > l2) return;
if (s1[n1] == s2[n2]) {
if (s1[n1] == s3[n3]) {
if (n3 == l3) {
write(n1 + 1, n2 + 1, n3);
} else {
if (s1[n1] == s3[1] && dp[n1 + 1][n2 + 1][2] + 1 == dp[n1][n2][n3]) {
printf("%c", s1[n1]);
write(n1 + 1, n2 + 1, 2);
} else if (dp[n1 + 1][n2 + 1][n3 + 1] + 1 == dp[n1][n2][n3]) {
printf("%c", s1[n1]);
write(n1 + 1, n2 + 1, n3 + 1);
} else {
write(n1 + 1, n2 + 1, n3);
}
}
} else {
printf("%c", s1[n1]);
write(n1 + 1, n2 + 1, 1 + (s1[n1] == s3[1]));
}
} else {
if (dp[n1][n2][n3] == dp[n1 + 1][n2][n3]) {
write(n1 + 1, n2, n3);
} else {
write(n1, n2 + 1, n3);
}
}
}
int main() {
scanf("%s %s %s", s1 + 1, s2 + 1, s3 + 1);
l1 = strlen(s1 + 1);
l2 = strlen(s2 + 1);
l3 = strlen(s3 + 1);
memset(dp, -1, sizeof(dp));
if (!dfs(1, 1, 1)) return printf("0"), 0;
write(1, 1, 1);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long> b(25);
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < 25; j++) {
if (a[i] & (1 << j)) b[j]++;
}
}
long long ans = 0;
bool found = true;
while (found) {
found = false;
long long num = 0;
for (long long i = 0; i < 25; i++) {
if (b[i]) {
found = true;
b[i]--;
num += (1 << i);
}
}
ans += num * num;
}
cout << ans << "\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int t, n, a, b, c, d;
int main() {
cin >> t;
while (t--) {
cin >> n >> a >> b >> c >> d;
if (((a - b) * n > c + d) || ((a + b) * n < c - d))
cout << "NO\n";
else
cout << "YES\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 100;
int n, m, lastans = 0;
map<int, int> E[N];
int fa[N], sz[N], ans[N];
int top;
struct pi {
int x, y;
} st[N];
struct OP {
int op, x, y;
} o[N];
struct node {
int l, r;
int op, x, y;
vector<OP> id;
} t[N << 2];
int find(int x) { return x == fa[x] ? x : find(fa[x]); }
void link(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
st[++top] = (pi){x, y};
fa[y] = x, sz[x] += sz[y];
}
void undo(int cur) {
while (top > cur) {
int x = st[top].x, y = st[top].y;
top--;
fa[y] = y;
sz[x] -= sz[y];
}
}
void build(int x, int l, int r) {
t[x].l = l, t[x].r = r;
if (l == r) return;
int mid = l + r >> 1;
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
}
void update(int x, int l, int r, OP k) {
if (l <= t[x].l && t[x].r <= r) {
t[x].id.push_back(k);
return;
}
if (t[x].l > r || t[x].r < l) return;
update(x << 1, l, r, k);
update(x << 1 | 1, l, r, k);
}
void solve(int x) {
int cur = top;
for (int i = 0; i < t[x].id.size(); i++)
if (E[t[x].id[i].x][t[x].id[i].y]) link(t[x].id[i].x, t[x].id[i].y);
if (t[x].l == t[x].r) {
int L = t[x].l, R = t[x].r;
if (o[t[x].l].op == 2)
lastans = ans[L] = (find(o[t[x].l].x) == find(o[t[x].l].y));
o[L + 1].x = (o[L + 1].x + lastans - 1) % n + 1;
o[L + 1].y = (o[L + 1].y + lastans - 1) % n + 1;
if (o[L + 1].x > o[L + 1].y) swap(o[L + 1].x, o[L + 1].y);
if (o[L + 1].op == 1) E[o[L + 1].x][o[L + 1].y] ^= 1;
} else {
solve(x << 1);
solve(x << 1 | 1);
}
undo(cur);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;
build(1, 1, m);
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &o[i].op, &o[i].x, &o[i].y);
if (o[i].x > o[i].y) swap(o[i].x, o[i].y);
if (o[i].op == 1) {
int x = o[i].x, y = o[i].y;
if (E[x].count(y)) update(1, E[x][y], i, o[i]);
E[x][y] = i;
x = x % n + 1, y = y % n + 1;
if (x > y) swap(x, y);
if (E[x].count(y)) update(1, E[x][y], i, OP{1, x, y});
E[x][y] = i;
}
}
for (int i = 1; i <= n; i++)
for (auto it : E[i])
if (it.second != m) update(1, it.second, m, (OP){1, i, it.first});
for (int i = 1; i <= n; i++) E[i].clear();
if (o[1].op == 1) E[o[1].x][o[1].y] = 1;
solve(1);
for (int i = 1; i <= m; i++)
if (o[i].op == 2) printf("%d", ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, a[1005], ans[1005][1005];
int bl[1005], br[1005], tp;
int sg[1005];
void print() {
for (int i = 1; i <= (n + 1); ++i) {
int ok = 0;
for (int j = 1; j <= (n); ++j)
if (ans[i][j]) ok = 1;
if (!ok) continue;
for (int j = 1; j <= (n); ++j) printf("%d", ans[i][j]);
puts("");
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= (n); ++i) scanf("%d", a + i);
tp = 1, bl[1] = 1, br[1] = n + 1;
for (int i = 1; i <= (n); ++i) {
int p = 0, s = 0;
for (int j = 1; j <= (tp); ++j)
if (j == tp || s + br[j] - bl[j] + 1 >= a[i]) {
p = j;
break;
} else
s += br[j] - bl[j] + 1;
int z = (s + br[p] - bl[p] + 1 > a[i]) ? a[i] - s : br[p] - bl[p];
for (int j = 1; j <= (p - 1); ++j)
for (int k = bl[j]; k <= (br[j]); ++k) ans[k][i] = 1;
int l = bl[p], r = br[p];
for (int j = p + 1; j <= (tp); ++j) bl[j - 1] = bl[j], br[j - 1] = br[j];
--tp;
for (int k = l; k <= (l + z - 1); ++k) ans[k][i] = 1;
int lf = a[i] - s - z;
for (int k = 1; k <= (n + 1); ++k)
if (lf && sg[k]) ans[k][i] = 1, --lf;
if (lf)
for (int k = 1; k <= (n); ++k)
if ((k < l || k > r) && !ans[k][i]) {
ans[k][i] = 1;
break;
}
if (z > 1) {
bl[++tp] = l;
br[tp] = l + z - 1;
} else
sg[l] = 1;
if (r - l + 1 - z > 1) {
bl[++tp] = l + z;
br[tp] = r;
} else
sg[r] = 1;
}
int cc = 0;
for (int i = 1; i <= (n + 1); ++i) {
int ok = 0;
for (int j = 1; j <= (n); ++j)
if (ans[i][j]) ok = 1;
cc += ok;
}
printf("%d\n", cc);
print();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c;
scanf("%d%d", &n, &c);
int a[n], b[n - 1];
scanf("%d", &a[0]);
for (int i = 1; i < n; i++) {
scanf("%d", &a[i]);
b[i - 1] = a[i - 1] - a[i] - c;
}
int max = 0;
for (int i = 0; i < n - 1; i++) {
if (b[i] > max) max = b[i];
}
printf("%d\n", max);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000100;
const long long P1 = 998244353;
const long long P2 = 1004535809;
const long long E = 3;
long long a[maxn], b[maxn], c[maxn];
int rev[maxn];
int n, k, lim;
long long qpow(long long a, long long b, long long mod) {
long long res = 1;
for (; b > 0; b >>= 1, a = a * a % mod)
if (b & 1) res = res * a % mod;
return res;
}
long long inv(long long a, long long mod) { return qpow(a, mod - 2, mod); }
void Init(int tot) {
lim = 1;
while (lim <= tot) lim <<= 1;
rev[0] = 0;
for (register int i = 1; i < lim; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (lim >> 1));
return;
}
void FFT(long long *A, long long G, long long mod) {
for (register int i = 0; i < lim; i++)
if (i < rev[i]) swap(A[i], A[rev[i]]);
for (register int i = 2; i <= lim; i <<= 1) {
long long wn = qpow(G, (mod - 1) / i, mod);
for (register int j = 0; j < lim; j += i) {
long long w = 1;
for (register int k = 0; k < (i >> 1); k++, w = w * wn % mod) {
long long x = A[j + k], y = A[j + k + (i >> 1)] * w % mod;
A[j + k] = (x + y) % mod;
A[j + k + (i >> 1)] = (x - y + mod) % mod;
}
}
}
return;
}
void DFT(long long *A, long long G, long long mod) {
FFT(A, G, mod);
return;
}
void IDFT(long long *A, long long G, long long mod) {
FFT(A, G, mod);
long long ilim = inv(lim, mod);
for (register int i = 0; i < lim; i++) A[i] = A[i] * ilim % mod;
return;
}
void Polypow(long long *A, long long *C, long long k, long long G,
long long mod) {
long long IG = inv(G, mod);
for (int i = 0; i < lim; i++) C[i] = A[i];
DFT(C, G, mod);
for (int i = 0; i < lim; i++) C[i] = qpow(C[i], k, mod);
IDFT(C, IG, mod);
return;
}
signed main() {
scanf("%d%d", &n, &k);
int num, mnu = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &num);
a[num] = 1;
mnu = max(num, mnu);
}
Init(mnu * k);
Polypow(a, b, k, E, P1);
Polypow(a, c, k, E, P2);
for (int i = 0; i < lim; i++) {
if (b[i] == 0 && c[i] == 0) continue;
printf("%d ", i);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long int ans = 0;
void fun(int x, int n) {
if (x > n) return;
ans++;
fun(x * 10, n);
fun(x * 10 + 1, n);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
fun(1, n);
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string tostr(long long a) {
stringstream rr;
rr << a;
return rr.str();
}
long long pow(long long c, long long d) { return d == 0 ?: c * pow(c, d - 1); }
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)); }
bool compare(pair<long long, pair<long long, long long> > a,
pair<long long, pair<long long, long long> > b) {
if (a.first != b.first) {
return a.first < b.first;
} else {
return a.second.first < b.second.first;
}
}
long long a[10000001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m, k, i, j, x, y, p = 0, s = 0, h, f = 1;
cin >> n;
vector<pair<long long, pair<long long, long long> > > v;
long long event[n + 1];
for (i = 0; i < n; i++) {
cin >> x >> y;
v.push_back({y, {x, i + 1}});
}
sort(v.begin(), v.end(), compare);
for (i = 0; i < v.size(); i++) {
for (j = v[i].second.first; j <= v[i].first; j++) {
if (a[j] == 0) {
a[j] = 1;
event[v[i].second.second] = j;
break;
}
}
}
for (i = 1; i <= n; i++) {
cout << event[i] << " ";
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int t, n, ans, mpos;
struct node {
int a, id;
} x[200010];
bool cmp(node a, node b) { return a.a < b.a; }
int main() {
cin >> t;
while (t--) {
cin >> n;
ans = 0;
for (int i = 1; i <= n; i++) {
cin >> x[i].a;
x[i].id = i;
}
stable_sort(x + 1, x + n + 1, cmp);
mpos = x[n].id;
for (int i = n - 1; i >= 1; i--) {
if (x[i].id > mpos) {
ans++;
mpos = x[i].id;
}
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64777215")
using namespace std;
const long long maxn = 1001;
const long long maxq = 1000001;
const long long maxk = 11;
const long long mo = 5;
const long long maxseg = 262144;
const long long secret_number = -87468267;
const pair<int, int> secret_pair = make_pair(secret_number, secret_number);
const long long inf = 1e18;
const long long mod = 87178291199;
const int iinf = 2147483647 / 2;
const double pi = 3.141592653589793238462643383279502884;
const double eps = 1e-7;
const string something_very_long =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaa";
const string govno = "g";
int maxlen[maxn][maxn][maxk][2];
string a, b;
int n, m, k;
int get_maxlen(int x, int y, int t, bool end);
int get_maxlen(int x, int y, int t);
int get_maxlen_end(int x, int y, int t);
int get_maxlen_notend(int x, int y, int t);
int get_maxlen_end(int x, int y, int t) {
return max(get_maxlen(x - 1, y - 1, t, 1), get_maxlen(x - 1, y - 1, t - 1));
}
int get_maxlen_notend(int x, int y, int t) {
return max(get_maxlen(x - 1, y, t), get_maxlen(x, y - 1, t));
}
int get_maxlen(int x, int y, int t) {
return max(get_maxlen(x, y, t, 0), get_maxlen(x, y, t, 1));
}
int get_maxlen(int x, int y, int t, bool end) {
if (x < 0 || y < 0 || t <= 0) return 0;
if (maxlen[x][y][t][end] != -1) return maxlen[x][y][t][end];
int best;
if (end)
best = a[x] == b[y] ? get_maxlen_end(x, y, t) + 1 : -1;
else
best = get_maxlen_notend(x, y, t);
maxlen[x][y][t][end] = best;
return best;
}
int get_maxlen() {
fill((int*)maxlen, (int*)maxlen + maxn * maxn * maxk * 2, -1);
return get_maxlen(a.length() - 1, b.length() - 1, k);
}
int main() {
cin >> n >> m >> k;
cin >> a >> b;
cout << get_maxlen();
return 0;
}
| 4 |
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define repit(it,u) for(__typeof((u).begin())it=(u).begin();it!=(u).end();++it)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define uniq(u) (u).erase(unique(all(u)),(u).end())
#define ll long
#define long int64_t
#define mp make_pair
#define pb push_back
typedef double number;
typedef vector<vector<number> > matrix;
inline number my_hypot(number x, number y) { return sqrt(x * x + y * y); }
inline void givens_rotate(number& x, number& y, number c, number s) {
number u = c * x + s * y, v = -s * x + c * y;
x = u, y = v;
}
vector<number> givens(matrix A, vector<number> b) {
const int n = b.size();
rep(i, n) repi(j, i + 1, n) {
const number r = my_hypot(A[i][i], A[j][i]);
const number c = A[i][i] / r, s = A[j][i] / r;
givens_rotate(b[i], b[j], c, s);
repi(k, i, n) givens_rotate(A[i][k], A[j][k], c, s);
}
for (int i = n - 1; i >= 0; --i) {
repi(j, i + 1, n) b[i] -= A[i][j] * b[j];
b[i] /= A[i][i];
}
return b;
}
const double eps = 1e-5;
int d;
vector<number> v;
vector<number> solve_eq(int m, int n){
matrix A(d+1,vector<number>(d+1));
vector<number> b(d+1);
int cnt = 0;
rep(i,d+3)if(i != m && i != n){
b[cnt] = v[i];
rep(j,d+1) A[cnt][j] = pow(1.0*i,1.0*j);
cnt++;
}
return givens(A,b);
}
int main(){
while(cin >> d, d){
v.resize(d+3);
int ans = -1;
rep(i,d+3) cin >> v[i];
rep(i,d+3){
repi(j,i+1,d+3){
vector<number> b = solve_eq(i,j);
number e = 0, f = 0;
rep(k,d+1) e += pow(1.0*i,1.0*k) * b[k];
rep(k,d+1) f += pow(1.0*j,1.0*k) * b[k];
if(abs(e-v[i]) > eps && abs(f-v[j]) < eps){
ans = i;
break;
}
if(abs(e-v[i]) < eps && abs(f-v[j]) > eps){
ans = j;
break;
}
}
if(ans >= 0) break;
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
map<long long, map<long long, map<long long, map<long long, int>>>> d;
int go(long long lx, long long ly, long long ax, long long ay, long long bx,
long long by) {
if (lx == 0 && ly == 0) return 1;
if (d[ax][ay][bx][by]) return d[ax][ay][bx][by];
int &ret = d[ax][ay][bx][by];
ret = -1;
long long lax, lay, lbx, lby;
lax = lx - ax;
lay = ly - ay;
lbx = lx - bx;
lby = ly - by;
if (lax >= 0 && lay >= 0) {
if (go(lax, lay, ax, ay, bx + ax, by + ay) == 1) {
printf("A");
return ret = 1;
}
}
if (lbx >= 0 && lby >= 0) {
if (go(lbx, lby, ax + bx, ay + by, bx, by) == 1) {
printf("B");
return ret = 1;
}
}
return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
vector<pair<long long, char>> ans;
long long egcd(long long a, long long b, long long &x, long long &y,
char la = 'A', char lb = 'B') {
if (!b) {
x = 1;
y = 0;
return a;
}
ans.push_back({a / b, la});
long long d = egcd(b, a % b, y, x, lb, la);
y -= x * (a / b);
return d;
}
int main() {
long long x, y;
scanf("%lld%lld", &x, &y);
if (gcd(x, y) != 1)
puts("Impossible");
else {
long long a, b;
egcd(x, y, a, b);
for (int i = 0; i < ans.size(); i++) {
if (i + 1 == ans.size()) --ans[i].first;
if (ans[i].first) printf("%I64d%c", ans[i].first, ans[i].second);
}
puts("");
}
return 0;
}
| 5 |
#include<cstdio>
#include<iostream>
using namespace std;
const int step[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
char a[55][55];
int s[55][55];
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='.')
for(int k=0;k<8;k++)
if(a[i+step[k][0]][j+step[k][1]]=='#') s[i][j]++;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
if(a[i][j]=='#') putchar('#');
else printf("%d",s[i][j]);
puts("");
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int q;
cin >> q;
for (int noob = 0; noob < q; noob++) {
string str;
cin >> str;
if (str[str.size() - 1] == 'o')
cout << "FILIPINO\n";
else if (str[str.size() - 1] == 'u')
cout << "JAPANESE\n";
else
cout << "KOREAN\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1000000;
map<long long int, long long int> idx, Xor[N + 5];
vector<long long int> v[N + 5];
long long int par[N + 5];
void init() {
for (long long int i = 1; i <= N; ++i) {
par[i] = i;
v[i].push_back(i);
Xor[i][i] = 0;
}
}
long long int get_idx(long long int x) {
return (idx.find(x) == idx.end() ? idx[x] = idx.size() : idx[x]);
}
long long int find(long long int x) {
return (x == par[x] ? x : par[x] = find(par[x]));
}
void uni(long long int a, long long int b, long long int val) {
long long int x = find(a), y = find(b);
if (v[x].size() < v[y].size()) swap(x, y), swap(a, b);
par[y] = x;
for (auto it : v[y]) {
Xor[b][it] = Xor[it][b] = Xor[b][y] ^ Xor[it][y];
Xor[x][it] = Xor[it][x] = Xor[it][b] ^ val ^ Xor[a][x];
v[x].push_back(it);
}
}
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
init();
long long int q, t, l, r, i, j, k, x, y, z, a, b, c;
long long int last = 0;
cin >> q;
while (q--) {
cin >> t >> x >> y;
x = x ^ last;
y = y ^ last;
if (x > y) swap(x, y);
y += 1;
x = get_idx(x);
y = get_idx(y);
if (t == 1) {
cin >> z;
z = z ^ last;
if (find(x) == find(y))
continue;
else {
Xor[x][y] = Xor[y][x] = z;
uni(x, y, z);
}
} else {
if (find(x) == find(y)) {
z = find(x);
last = Xor[x][z] ^ Xor[y][z];
} else
last = -1;
cout << last << endl;
if (last == -1) last = 1;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long labs(long long a) { return a < 0 ? (-a) : a; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long min(long long a, long long b) { return a < b ? a : b; }
const int MAXN = 6;
const int MAXK = 26;
const int MAXRES = 5 * 1001;
struct Var {
int a, b;
int c;
};
Var v[MAXK];
int res[MAXRES];
int n, k, t;
bool was1[MAXN];
bool was2[MAXN];
void Solve(int mask) {
memset((was1), 0, sizeof(was1));
memset((was2), 0, sizeof(was2));
int sum = 0;
for (int(i) = 0; (i) < (k); (i)++) {
if (mask & 1) {
if (was1[v[i].a] || was2[v[i].b]) {
return;
}
was1[v[i].a] = 1;
was2[v[i].b] = 1;
sum += v[i].c;
}
mask >>= 1;
}
res[sum]++;
}
int main() {
cin >> n >> k >> t;
if (t == 1) {
cout << 0 << endl;
return 0;
}
memset((res), 0, sizeof(res));
for (int(i) = 0; (i) < (k); (i)++) {
cin >> v[i].a >> v[i].b >> v[i].c;
v[i].a--;
v[i].b--;
}
for (int mask = 0; mask < (1 << k); mask++) {
Solve(mask);
}
int cur = 0;
for (int(i) = 0; (i) < (MAXRES); (i)++) {
cur += res[i];
if (cur >= t) {
cout << i << endl;
return 0;
}
}
return 0;
}
| 6 |
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
while(cin>>str){
int num[10];
for(int i=0;i<10;i++){
num[i] = str[i]-48;
}
for(int j=9;j>=0;j--){
for(int k=0;k<j;k++){
if(num[k]+num[k+1]>9) num[k]=num[k]+num[k+1]-10;
else num[k]=num[k]+num[k+1];
}
}
cout<<num[0]<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int pos, len;
int n, k, m, t;
cin >> n >> k >> m >> t;
pos = k;
len = n;
for (int i = 0; i < t; i++) {
int a, b;
cin >> a >> b;
if (a == 0) {
if (b < pos) {
pos -= b;
len -= b;
} else {
len = b;
}
} else {
if (b <= pos) {
pos++;
len++;
} else {
len++;
}
}
cout << len << " " << pos << '\n';
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
int n,i=1;
cin>>n;
while(1){
int count=0;
for(int j=1;j<i+1;j++){
if(i%j==0) count++;
}
if(count==n) break;
i++;
}
cout<<i<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000100;
const int INF = 1e9 + 10;
struct Node {
int k, a;
friend bool operator<(Node A, Node B) { return A.k < B.k; }
};
Node b[maxn];
int n;
bool check(long long m) {
for (int i = 1; i <= n; i++) {
if ((m - b[i].k) * 2 >= log2(b[i].a)) continue;
return 0;
}
return 1;
}
long long bin(long long l, long long r) {
long long res = r;
while (l <= r) {
long long m = (l + r) >> 1;
if (check(m))
res = min(res, m), r = m - 1;
else
l = m + 1;
}
return res;
}
int main() {
while (cin >> n) {
for (int i = 1; i <= n; i++) scanf("%d%d", &b[i].k, &b[i].a);
sort(b + 1, b + n + 1);
printf("%I64d\n", bin(b[n].k + 1, 2 * INF));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int N = 100100;
using namespace std;
int d[N][2];
int n, a[N];
int mod = 1e9 + 7;
vector<int> v[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
v[p + 1].push_back(i);
}
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) {
d[i][0] = 1;
for (auto y : v[i]) {
d[i][1] = (1ll * d[i][1] * d[y][0]) % mod;
d[i][1] = (d[i][1] + (1ll * d[i][0] * d[y][1]) % mod) % mod;
d[i][0] = (1ll * d[i][0] * d[y][0]) % mod;
}
if (a[i])
d[i][1] = d[i][0];
else
d[i][0] = (d[i][0] + d[i][1]) % mod;
}
cout << d[1][1] << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
static void rmin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static void rmax(T &x, U y) {
if (x < y) x = y;
}
const double eps = 1e-9;
const double pi = acos(-1.0);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int N = 1e2 + 10;
int n;
string s[4][N];
int check(int id, char type) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i + j) % 2 == 0) {
if (s[id][i][j] != type) res++;
} else {
if (s[id][i][j] == type) res++;
}
}
}
return res;
}
void relax(int &y, int x) {
if (y > x) y = x;
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
cin >> s[i][j];
}
}
int ans = INF;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
int cur = check(i, '1') + check(j, '1');
for (int k = 0; k < 4; k++) {
if (k == i || k == j) continue;
cur += check(k, '0');
}
relax(ans, cur);
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
pair<char, int> a[100007];
string s[1007];
pair<int, int> pos[30];
int q;
int n, m;
int sum[1007][1007];
string ans;
bool isvalid(int x, int y, int xx, int yy) {
if (xx >= n || xx < 0 || yy >= m || yy < 0) return false;
if (x == xx) {
if (y > yy) swap(y, yy);
return ((sum[xx + 1][yy + 1] + sum[x][y] - sum[xx + 1][y] -
sum[x][yy + 1]) == 0);
} else if (y == yy) {
if (x > xx) swap(x, xx);
return ((sum[xx + 1][yy + 1] + sum[x][y] - sum[xx + 1][y] -
sum[x][yy + 1]) == 0);
}
}
bool func(int x, int y, int z) {
if (z >= q) return true;
if (a[z].first == 'W') {
int yy = y - a[z].second;
if (isvalid(x, y, x, yy)) return func(x, yy, z + 1);
} else if (a[z].first == 'E') {
int yy = y + a[z].second;
if (isvalid(x, y, x, yy)) return func(x, yy, z + 1);
} else if (a[z].first == 'S') {
int xx = x + a[z].second;
if (isvalid(x, y, xx, y)) return func(xx, y, z + 1);
} else {
int xx = x - a[z].second;
if (isvalid(x, y, xx, y)) return func(xx, y, z + 1);
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < s[i].size(); j++) {
if (s[i][j] >= 'A' && s[i][j] <= 'Z')
pos[s[i][j] - 'A'] = make_pair(i, j);
sum[i + 1][j + 1] =
sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + (s[i][j] == '#');
}
}
cin >> q;
for (int i = 0; i < q; i++) cin >> a[i].first >> a[i].second;
for (int i = 0; i < 26; i++)
if (func(pos[i].first, pos[i].second, 0)) ans += (char)('A' + i);
if (ans.size() == 0)
cout << "no solution" << endl;
else
cout << ans << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=305,mod=998244353;
int n,k,m,a[N],sum[N];
int dp[N][N][N];
//dp[i][j][h] 第i个坑,用了j个1,移动了k次的方案数
char s[N];
int main()
{
scanf("%s%d",s+1,&k);
for(int i=1;s[i];i++) m+=s[i]=='1';
int t=0;
for(int i=1;s[i];i++)
if(s[i]=='0') a[++n]=t,t=0;
else t++;
a[++n]=t;
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
k=min(k,m);
dp[0][0][0]=1;
for(int i=0;i<n;i++)
for(int j=sum[i];j<=sum[n];j++)
for(int h=0;h<=k;h++)
{
if(!dp[i][j][h]) continue;
for(int f=max(j,sum[i+1]);f<=sum[n];f++)
if(f>=sum[i+1]&&h+f-j-a[i+1]<=k)
{
if(i+1==n&&f-j>a[i+1]) break;
int cost=f-j<=a[i+1]?0:f-j-a[i+1];
dp[i+1][f][h+cost]=(dp[i+1][f][h+cost]+dp[i][j][h])%mod;
}
}
ll ans=0;
for(int i=0;i<=k;i++) ans=(ans+dp[n][m][i])%mod;
printf("%lld\n",ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int M = 50 + 10;
int n, m;
char board[100][100];
int cor[8] = {0, 1, 0, -1, 1, 0, -1, 0};
int low[M][M];
int layer[M][M];
int visited[M][M];
int timestamp;
int rootchild;
int sx, sy;
bool ok = false;
void tarjan(pair<int, int> cur, pair<int, int> father) {
int curx = cur.first;
int cury = cur.second;
int fax = father.first;
int fay = father.second;
visited[curx][cury] = 1;
layer[curx][cury] = timestamp;
low[curx][cury] = timestamp;
timestamp++;
for (int k = 0; k < 8; k = k + 2) {
int nxtx = curx + cor[k];
int nxty = cury + cor[k + 1];
if (nxtx >= 0 && nxtx < n && nxty >= 0 && nxty < m &&
board[nxtx][nxty] == '#') {
if (visited[nxtx][nxty] == 0) {
if (curx == sx && cury == sy) {
rootchild++;
}
tarjan(make_pair(nxtx, nxty), make_pair(curx, cury));
low[curx][cury] = min(low[curx][cury], low[nxtx][nxty]);
if (low[nxtx][nxty] >= layer[curx][cury] &&
(curx != sx || cury != sy)) {
ok = true;
}
} else {
if (nxtx != fax || nxty != fay) {
low[curx][cury] = min(low[curx][cury], layer[nxtx][nxty]);
}
}
}
}
}
int main() {
cin >> n >> m;
int cnt = 0;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
board[i][j] = s[j];
if (s[j] == '#') {
cnt++;
sx = i;
sy = j;
}
}
}
if (cnt <= 2) {
cout << "-1" << endl;
} else {
tarjan(make_pair(sx, sy), make_pair(-1, -1));
int ans = 2;
if (ok == true || rootchild >= 2) {
ans = 1;
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef pair< int , int > Pi;
typedef long long int64;
int N;
Pi data[300000];
bool calc(int value){ //閉じこめられる微生物value
priority_queue < Pi, vector< Pi >, greater< Pi > > que; //許容量が小さい順に取り出す(?)
int64 sum = 0; //放出量の和
for(int i = 0; i < N; i++){ //人数分入れる
sum += data[i].first;
que.push( Pi( data[i].second, data[i].first));
while(!que.empty() && (int64)que.top().first * value < sum){
sum -= que.top().second;
que.pop();
}
if(que.size() >= value) return true;
}
return value == 0;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N; i++){
scanf("%d %d", &data[i].first, &data[i].second);
}
sort( data, data + N);
//これわかんないな
int row = 0, high = N;
while(high != row){
int mid = (row + high + 1) >> 1;
if(calc(mid)) row = mid;
else high = mid - 1;
}
printf("%d\n", high);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 500;
int a[N], s[2 * N + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
memset(s, 0, sizeof(s));
for (int i = 0; i < n / 2; ++i) {
int x = a[i], y = a[n - i - 1];
if (x > y) swap(x, y);
s[2] += 2;
s[x + 1]--;
s[x + y]--;
s[x + y + 1]++;
s[y + k + 1]++;
}
int ans = n;
for (int i = 2; i <= 2 * k; ++i) {
s[i] += s[i - 1];
ans = min(ans, s[i]);
}
cout << ans << endl;
}
}
| 2 |
#include<iostream>
using namespace std;
#define M 1000000
#define NIL -1
struct N{ int p,cl,sr;};
N nd[M];
int de[M];
void print(int x){
int i,j;
cout<<"node "<<x<<": parent = "<<nd[x].p<<", depth = ";
cout<<de[x]<<", ";
if(nd[x].p==NIL)cout<<"root, [";
else if(nd[x].cl==NIL)cout<<"leaf, [";
else cout<<"internal node, [";
if(nd[x].cl!=NIL){
cout<<nd[x].cl;
j=nd[x].cl;
while(nd[j].sr!=NIL){
cout<<", ";
cout<<nd[j].sr;
j=nd[j].sr;
}
}
cout<<"]"<<endl;
}
void cde(int x,int p){
de[x]=p;
if(nd[x].cl!=NIL)cde(nd[x].cl,p+1);
if(nd[x].sr!=NIL)cde(nd[x].sr,p);
}
int main(){
int i,j,n,id,k,c,m;
cin>>n;
for(i=0;i<n;i++)nd[i].p=nd[i].cl=nd[i].sr=NIL;
for(i=0;i<n;i++){
cin>>id>>k;
for(j=0;j<k;j++){
cin>>c;
if(j==0)nd[id].cl=c;
else nd[m].sr=c;
m=c;
nd[c].p=id;
}
}
for(i=0;i<n;i++){
if(nd[i].p==NIL)m=i;
}
cde(m,0);
for(i=0;i<n;i++)print(i);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, k;
int b[N], a[N];
int main() {
scanf("%d%d%d", &n, &m, &k);
int ans = n;
k = n - k;
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
for (int i = 1; i < n; ++i) a[i] = b[i + 1] - b[i] - 1;
nth_element(a + 1, a + k, a + n);
ans += accumulate(a + 1, a + k + 1, 0);
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
struct fastIO {
fastIO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
};
int n, ans;
vector<vector<int> > v;
queue<int> q;
int main() {
fastIO();
cin >> n;
v.resize(n);
for (int i = 1; i <= n - 1; i++) {
int j;
cin >> j;
v[j - 1].push_back(i);
}
q.push(0);
while (q.size() > 0) {
int sz = q.size();
if (sz % 2 == 1) ans++;
for (int i = 0; i < sz; i++) {
int fr = q.front();
for (int j = 0; j < v[fr].size(); j++) q.push(v[fr][j]);
q.pop();
}
}
cout << ans;
return 0;
}
| 4 |
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
char st[5010];
int f[5010][5010];
int main()
{
int n;scanf("%d",&n);
scanf("%s",st+1);
int ans=0;
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
{
if (st[i]==st[j]) f[i][j]=f[i-1][j-1]+1;
else f[i][j]=0;
//printf("%d %d %d\n",i,j,f[i][j]);
if (f[i][j]>j-i) f[i][j]=j-i;
if (f[i][j]>ans) ans=f[i][j];
}
printf("%d\n",ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool isUsed(int row, int col, int k) {
if (row == 1 && col == 3) {
cout << "";
}
int seat = (row - 1) * 3 + col;
if (row != 1) {
seat++;
}
if (col == 3 && seat <= k) {
return row == 1;
} else if (col == 4 && row > 1) {
seat--;
}
return seat <= k;
}
int main() {
int k;
cin >> k;
const int rows = 11;
const int cols = 4;
cout << "+";
for (int i = 0; i < rows * 2 + 2; i++) {
cout << "-";
}
cout << "+" << endl;
for (int i = 0; i < cols; i++) {
cout << "|";
for (int j = 0; j < rows; j++) {
if (isUsed(j + 1, i + 1, k)) {
cout << "O";
} else if (i == 2 && j > 0) {
cout << ".";
} else {
cout << "#";
}
cout << ".";
}
if (i == 0) {
cout << "|D|)";
} else if (i == 1) {
cout << "|.|";
} else if (i == 2) {
cout << "..|";
} else if (i == 3) {
cout << "|.|)";
}
cout << endl;
}
cout << "+";
for (int i = 0; i < rows * 2 + 2; i++) {
cout << "-";
}
cout << "+" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string text;
getline(cin, text);
map<char, int> mapping;
for (int i = 0; i < text.size(); i++) {
mapping[text[i]]++;
}
int no_of_i = mapping['i'];
while (no_of_i) {
if ((mapping['n'] / 3 >= no_of_i ||
mapping['n'] >= 3 + (no_of_i - 1) * 2) &&
mapping['e'] / 3 >= no_of_i && mapping['t'] >= no_of_i) {
cout << no_of_i;
return 0;
}
no_of_i--;
}
cout << 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
string in;
int nm = 0;
int nm2 = 0;
int x;
char c;
int main() {
cin >> n >> m;
cin >> in;
for (int i = 0; i < n; i++) {
if (in[i] == '.') nm++;
}
if (in[0] == '.') nm2++;
for (int i = 1; i < n; i++) {
if (in[i] == '.' && in[i - 1] != '.') {
nm2++;
}
}
while (m--) {
cin >> x >> c;
x--;
if (c == '.' && in[x] != '.') {
nm++;
int t = 0;
if (x > 0 && in[x - 1] == '.') {
t++;
}
if (x < n - 1 && in[x + 1] == '.') {
t++;
}
if (t == 0) {
nm2++;
}
if (t == 2) {
nm2--;
}
cout << nm - nm2 << endl;
} else if (c != '.' && in[x] == '.') {
nm--;
int t = 0;
if (x > 0 && in[x - 1] == '.') {
t++;
}
if (x < n - 1 && in[x + 1] == '.') {
t++;
}
if (t == 0) {
nm2--;
}
if (t == 2) {
nm2++;
}
cout << nm - nm2 << endl;
} else {
cout << nm - nm2 << endl;
}
in[x] = c;
}
}
| 3 |
#include<iostream>
#include<vector>
#include<algorithm>
#include<tuple>
using namespace std;
class BIT {
private:
int size_; vector<int>x;
public:
BIT() : size_(0), x(vector<int>()) {};
BIT(int size__) : size_(size__) {
x.resize(size__ + 1);
for (int i = 0; i < size__ + 1; i++)x[i] = 0;
}
int size() { return size_; }
void add(int r, int pp) {
while (r <= size_) { x[r] += pp; r += r&-r; }
}
int sums(int r) {
int cnt1 = 0; while (r >= 1) { cnt1 += x[r]; r -= r&-r; }
return cnt1;
}
int sum(int l, int r) {
return sums(r) - sums(l - 1);//Range [l, r]
}
};
int n, x[5000], y[5000], dir[5000];
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
vector<int>XX, YY, XXX, YYY, XR[3100], YR[3100];//XR=yoko, YR=tate
vector<tuple<int, int, int>>posit;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
char c; cin >> x[i] >> y[i] >> c; swap(x[i], y[i]); XX.push_back(x[i]); YY.push_back(y[i]);
if (c == 'v')dir[i] = 0; if (c == '>')dir[i] = 1; if (c == '^')dir[i] = 2; if (c == '<')dir[i] = 3;
}
sort(XX.begin(), XX.end()); sort(YY.begin(), YY.end()); XXX.push_back(XX[0]); YYY.push_back(YY[0]);
for (int i = 1; i < XX.size(); i++) { if (XX[i - 1] != XX[i])XXX.push_back(XX[i]); }
for (int i = 1; i < YY.size(); i++) { if (YY[i - 1] != YY[i])YYY.push_back(YY[i]); }
for (int i = 0; i < n; i++) {
int pos1 = lower_bound(XXX.begin(), XXX.end(), x[i]) - XXX.begin(); x[i] = pos1 + 1;
int pos2 = lower_bound(YYY.begin(), YYY.end(), y[i]) - YYY.begin(); y[i] = pos2 + 1;
XR[x[i]].push_back(y[i]); YR[y[i]].push_back(x[i]);
posit.push_back(make_tuple(x[i], y[i], dir[i]));
}
vector<BIT> X(n + 2, BIT(n + 2)), Y(n + 2, BIT(n + 2)); sort(posit.begin(), posit.end());
int maxn = 0;
for (int i = 0; i < n; i++) {
vector<tuple<int, int, int>>used;
int cx = x[i], cy = y[i], cd = dir[i];
for (int j = 0; j < n; j++) { X[x[j]].add(y[j], 1); Y[y[j]].add(x[j], 1); used.push_back(make_tuple(x[j], y[j], 0)); }
bool flag = true; sort(used.begin(), used.end());
int cnt2 = 0;
while (true) {
X[cx].add(cy, -1); Y[cy].add(cx, -1);
int pos3 = lower_bound(used.begin(), used.end(), make_tuple(cx, cy, 0)) - used.begin();
get<2>(used[pos3]) = 1;
//--------------DOWN---------------
if (cd == 0) {
int L = cx, R = n + 2, M;
while(true){
M = (L + R) / 2;
int E1 = Y[cy].sum(cx, M - 1);
int E2 = Y[cy].sum(cx, M);
if (E1 <= 0 && E2 >= 1) {
cx = M;
int pos4 = lower_bound(posit.begin(), posit.end(), make_tuple(cx, cy, 0)) - posit.begin(); cd = get<2>(posit[pos4]); break;
}
if (E1 >= 1)R = M;
if (E2 <= 0)L = M;
if (E2 <= 0 && M == n + 1) { flag = false; break; }
}
}
//--------------UP---------------
else if (cd == 2) {
int L = 0, R = cx + 2, M;
while (true) {
M = (L + R) / 2;
int E1 = Y[cy].sum(M - 1, cx);
int E2 = Y[cy].sum(M, cx);
if (E1 >= 1 && E2 <= 0) {
cx = M - 1;
int pos4 = lower_bound(posit.begin(), posit.end(), make_tuple(cx, cy, 0)) - posit.begin(); cd = get<2>(posit[pos4]); break;
}
if (E1 <= 0)R = M;
if (E2 >= 1)L = M;
if (E1 <= 0 && M == 0) { flag = false; break; }
}
}
//--------------RIGHT---------------
else if (cd == 1) {
int L = cy, R = n + 2, M;
while (true) {
M = (L + R) / 2;
int E1 = X[cx].sum(cy, M - 1);
int E2 = X[cx].sum(cy, M);
if (E1 <= 0 && E2 >= 1) {
cy = M;
int pos4 = lower_bound(posit.begin(), posit.end(), make_tuple(cx, cy, 0)) - posit.begin(); cd = get<2>(posit[pos4]); break;
}
if (E1 >= 1)R = M;
if (E2 <= 0)L = M;
if (E2 <= 0 && M == n + 1) { flag = false; break; }
}
}
//--------------LEFT---------------
else if (cd == 3) {
int L = 0, R = cy + 2, M;
while (true) {
M = (L + R) / 2;
int E1 = X[cx].sum(M - 1, cy);
int E2 = X[cx].sum(M, cy);
if (E1 >= 1 && E2 <= 0) {
cy = M - 1;
int pos4 = lower_bound(posit.begin(), posit.end(), make_tuple(cx, cy, 0)) - posit.begin(); cd = get<2>(posit[pos4]); break;
}
if (E1 <= 0)R = M;
if (E2 >= 1)L = M;
if (E1 <= 0 && M == 0) { flag = false; break; }
}
}
cnt2++;
if (flag == false) { break; }
}
for (int j = 0; j < n; j++) {
int a1 = get<0>(used[j]), a2 = get<1>(used[j]), a3 = get<2>(used[j]);
if (a3 == 1)continue;
X[a1].add(a2, -1); Y[a2].add(a1, -1);
}
maxn = max(maxn, cnt2);
}
cout << maxn << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 10;
const long long int mod = 1e9 + 7;
const long long int level = 20;
long long int dp[505][505];
int main() {
ios::sync_with_stdio(false);
long long int n;
cin >> n;
long long int arr[n + 1];
for (long long int i = 1; i < n + 1; i++) {
cin >> arr[i];
dp[i][i] = 1;
}
for (long long int len = 2; len < n + 1; len++) {
for (long long int i = 1; i < n - len + 2; i++) {
long long int j = i + len - 1;
if (len == 2) {
if (arr[i] == arr[j])
dp[i][j] = 1;
else
dp[i][j] = 2;
continue;
}
long long int ans = INT_MAX;
for (long long int k = i; k < j; k++) {
ans = min(ans, dp[i][k] + dp[k + 1][j]);
}
dp[i][j] = ans;
if (arr[i] == arr[j]) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
}
}
cout << dp[1][n] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
const int MAXN = 1005;
const int MAXM = 200005;
vector<int> gph[MAXN];
int adj[MAXN][MAXN], vis[MAXN];
int n, m;
int s[MAXM], e[MAXM];
void dfs(int r, int v){
vis[v] = 1;
adj[r][v]++;
for(auto &i : gph[v]){
if(i == r) continue;
if(!vis[i] && adj[r][i] < 2){
dfs(r, i);
}
}
}
int main(){
cin >> n >> m;
for(int i=0; i<m; i++){
scanf("%d %d",&s[i],&e[i]);
gph[s[i]].push_back(e[i]);
}
for(int i=1; i<=n; i++){
for(auto &j : gph[i]){
if(adj[i][j] < 2){
memset(vis, 0, sizeof(vis));
dfs(i, j);
}
}
}
for(int i=0; i<m; i++){
if(adj[e[i]][s[i]] == 0){
puts(adj[s[i]][e[i]] > 1 ? "diff" : "same");
}
else{
puts(adj[s[i]][e[i]] > 1 ? "same" : "diff");
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
const T& max(const T& a, const T& b, const T& c) {
return max(a, max(b, c));
}
template <class T>
const T& min(const T& a, const T& b, const T& c) {
return min(a, min(b, c));
}
long long stoi(string& str) {
istringstream second(str);
long long i;
second >> i;
return i;
}
string itos(long long i) {
stringstream second;
second << i;
return second.str();
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long poww(long long a, long long b) {
long long res = 1;
for (int i = 1; i <= b; i++) res *= a;
return res;
}
long long sumOfDigs(string s) {
long long sum = 0;
for (int i = 0; i < s.length(); i++) sum += s[i] - '0';
return sum;
}
long long sumOfDigs(long long n) {
return (n < 10 ? n : n % 10 + sumOfDigs(n / 10));
}
long long mod = poww(10, 9) + 7;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
using namespace std;
void t() { freopen("test.txt", "r", stdin); }
int a[1000001], b[1000001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
;
for (int j = 1; j <= n; j++) cin >> b[j];
long long cnt = 0;
for (int i = 1; i <= n; i++) {
if (b[i] > 2 * a[i] || b[i] < 2) {
cnt--;
continue;
}
long long x = b[i] / 2;
long long y = b[i] - x;
cnt += (x * y);
}
cout << cnt << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int n, ans;
string s, ss;
int f[11][4][10][100005];
int N = 100005;
int type(char ch) {
if (ch == 'A') return 0;
if (ch == 'T') return 1;
if (ch == 'G') return 2;
return 3;
}
void upd(int j, int typ, int id, int pos, int val) {
for (; pos < N; pos |= pos + 1) f[j][typ][id][pos] += val;
}
int sum(int j, int typ, int id, int x) {
int res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) res += f[j][typ][id][x];
return res;
}
int get(int j, int typ, int id, int l, int r) {
return sum(j, typ, id, r) - sum(j, typ, id, l - 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
s = "#" + s;
for (int i = 1; i < s.size(); i++) {
for (int j = 1; j <= 10; j++) {
upd(j, type(s[i]), i % j, i, 1);
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
int ty;
cin >> ty;
if (ty == 1) {
int x;
char c;
cin >> x >> c;
if (s[x] != c) {
for (int j = 1; j <= 10; j++) {
upd(j, type(s[x]), x % j, x, -1);
upd(j, type(c), x % j, x, 1);
}
s[x] = c;
}
} else {
int l, r;
cin >> l >> r >> ss;
ans = 0;
for (int j = 0; j < ss.size(); j++, l++) {
ans += get(ss.size(), type(ss[j]), l % ss.size(), l, r);
}
cout << ans << "\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void make_unique(vector<int> &a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
}
int Set(int N, int cur) { return N = N | (1 << cur); }
int Reset(int N, int cur) { return N = N & ~(1 << cur); }
bool Check(int N, int cur) { return (bool)(N & (1 << cur)); }
long long GCD(long long a, long long b) {
if (b == 0) return a;
return GCD(b, a % b);
}
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
long long POW(long long a, long long p) {
long long res = 1, x = a;
while (p) {
if (p & 1) res = (res * x);
x = (x * x);
p >>= 1;
}
return res;
}
int N, M;
int A[1000005];
int cnt[1000005];
int DP[1000005];
void solve() {
for (int i = 1; i <= M; i++) {
cnt[i] = DP[i] = 0;
}
for (int i = 0; i < N; i++) {
if (A[i] <= M) cnt[A[i]]++;
}
int lcm = 1, mxLen = 0;
for (int d = 1; d <= M; d++) {
for (int v = d; v <= M; v += d) {
DP[v] += cnt[d];
if (DP[v] > mxLen) {
lcm = v;
mxLen = DP[v];
} else if (DP[v] == mxLen) {
if (v < lcm) lcm = v;
}
}
}
printf("%d %d\n", lcm, mxLen);
bool f = false;
if (mxLen == 0) return;
for (int i = 0; i < N; i++) {
if (lcm % A[i] == 0) {
if (f == false) {
f = true;
printf("%d", i + 1);
} else {
printf(" %d", i + 1);
}
}
}
printf("\n");
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
solve();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
string a[100005];
set<int> b;
pair<int, int> c[2000005];
void mark(int l, int r, int i) {
while (1) {
auto it = b.lower_bound(l);
if (it == b.end() || *it > r) break;
c[*it] = {i, *it - l};
b.erase(it);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n, h = 0;
cin >> n;
for (int i = 1; i <= 2000000; i++) b.insert(i);
for (int i = 1; i <= n; i++) {
cin >> a[i];
int t;
cin >> t;
while (t--) {
int x;
cin >> x;
int r = x + a[i].size() - 1;
mark(x, r, i);
h = max(h, r);
}
}
for (int i = 1; i <= h; i++) {
if (c[i].first == 0)
cout << 'a';
else
cout << a[c[i].first][c[i].second];
}
cout << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
const double eps = 1e-8;
int A[55][55], t[55][55];
void mul(int (&a)[55][55], int (&b)[55][55], int (&c)[55][55]) {
int t[55][55];
memset(t, 0, sizeof(A));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j])
for (int k = 1; k <= n; k++) t[i][k] ^= a[i][j] & b[j][k];
memcpy(c, t, sizeof(A));
}
bool check(int (&a)[55][55]) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] && i != j) return 0;
return 1;
}
void makei(int (&a)[55][55]) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) a[i][j] = i == j;
}
void pow(int (&a)[55][55], long long p, int (&b)[55][55]) {
int ret[55][55], t[55][55];
memcpy(t, a, sizeof(A));
makei(ret);
for (; p; p >>= (long long)1, mul(t, t, t))
if (p & (long long)1) mul(t, ret, ret);
memcpy(b, ret, sizeof(A));
}
long long x;
long long d[20000];
bool no[20000];
int nn;
int main() {
srand(39);
scanf("%d", &n);
if (n >= 48) srand(1);
for (int i = 1; i < n; i++) A[i][i + 1] = 1;
x = ((long long)1 << (long long)n) - 1;
for (long long i = sqrt(x + eps); i; i--)
if (x % i == 0) {
d[++nn] = i;
if (i > 1) d[++nn] = x / i;
}
sort(d + 1, d + nn + 1);
for (int i = 1; i <= nn; i++)
for (int j = i + 1; j <= nn; j++)
if (d[j] % d[i] == 0) {
no[i] = 1;
break;
}
for (;;) {
for (int i = 1; i <= n; i++) A[n][i] = rand() & 1;
A[n][n] = 1;
pow(A, x, t);
if (check(t)) {
bool ok = true;
for (int i = nn; i; i--)
if (!no[i]) {
pow(A, d[i], t);
if (check(t)) {
ok = false;
break;
}
}
if (ok) {
for (int i = n; i; i--) printf("%d ", A[n][i]);
putchar('\n');
for (int i = 1; i < n; i++) printf("0 ");
puts("1");
return 0;
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
char m[510][510];
int a[510][510];
int r1, c1, rr, cr, sum;
int main() {
int r, c, q;
while (~scanf("%d%d", &r, &c)) {
for (int i = 0; i < r; i++) scanf("%s", m[i]);
a[0][0] = 0;
for (int i = 1; i < c; i++) {
a[0][i] = a[0][i - 1];
if (m[0][i] == '.' && m[0][i - 1] == '.') a[0][i]++;
}
for (int i = 1; i < r; i++) {
a[i][0] = a[i - 1][0];
if (m[0][i] == '.' && m[0][i - 1] == '.') a[i][0]++;
}
for (int i = 1; i < r; ++i) {
for (int j = 1; j < c; ++j) {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
if (m[i][j] == '.') {
if (m[i - 1][j] == '.') a[i][j]++;
if (m[i][j - 1] == '.') a[i][j]++;
}
}
}
scanf("%d", &q);
while (q--) {
scanf("%d%d%d%d", &r1, &c1, &rr, &cr);
r1--;
c1--;
rr--;
cr--;
int sum = a[rr][cr] - a[r1][cr] - a[rr][c1] + a[r1][c1];
for (int i = c1; i < cr; i++) {
if (m[r1][i] == '.' && m[r1][i + 1] == '.') sum++;
}
for (int i = r1; i < rr; i++) {
if (m[i][c1] == '.' && m[i + 1][c1] == '.') sum++;
}
printf("%d\n", sum);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 256;
int n, A[MAXN], indeg[MAXN];
vector<int> G[MAXN];
bool r[MAXN];
int sim(int id) {
memset(r, false, sizeof r);
memset(indeg, 0, sizeof indeg);
for (int i = (0); i < (int)(n); i++)
for (int j = (0); j < (int)(G[i].size()); j++) indeg[G[i][j]]++;
int ans = 0, k = 0;
while (k < n) {
bool ok = true;
while (ok) {
ok = false;
for (int i = (0); i < (int)(n); i++) {
if (!r[i] && indeg[i] == 0 && A[i] == id) {
r[i] = true;
for (int j = (0); j < (int)(G[i].size()); j++) indeg[G[i][j]]--;
ok = true;
k++;
}
}
}
if (k == n) break;
ans++;
id = (id + 1) % 3;
}
return ans;
}
int main() {
while (scanf("%d", &n) >= 1) {
for (int i = (0); i < (int)(n); i++) {
scanf("%d", &A[i]);
--A[i];
}
for (int i = (0); i < (int)(n); i++) {
int k, a;
scanf("%d", &k);
for (int j = (0); j < (int)(k); j++) {
scanf("%d", &a);
G[--a].push_back(i);
}
}
printf("%d\n", min(min(sim(0), sim(1)), sim(2)) + n);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100500;
const int N = 50;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
long long n, p, w, d;
scanf("%lld%lld%lld%lld", &n, &p, &w, &d);
long long s = w * d / gcd(w, d);
long long nw = s / w;
long long nd = s / d;
long long a = p / w;
long long b;
long long yu = p % w;
int flag = 0;
for (long long i = 0; i < min(nw, a + 1); i++) {
if ((yu + i * w) % d == 0) {
flag = 1;
a -= i;
b = (yu + i * w) / d;
break;
}
}
if (flag == 0) {
printf("-1\n");
return 0;
} else {
long long r = a / nw;
long long l = 0;
while (l <= r) {
long long mid = (l + r) / 2;
if ((a - mid * nw + b + mid * nd) <= n) {
printf("%lld %lld %lld\n", a - mid * nw, b + mid * nd,
n - a + mid * nw - b - mid * nd);
return 0;
} else {
r = mid - 1;
}
}
printf("-1\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int i, j, tmp = 0;
string s, s1;
cin >> s;
cin >> s1;
long long int a[27] = {0}, a1[27];
for (i = 0; i < s.size(); i++) {
long long int ta = s[i] - 'a' + 1;
long long int ta1 = s1[i] - 'a' + 1;
if (a[ta] != 0 && a[ta1] != 0) {
if (a[ta] != ta1 || a[ta1] != ta) {
tmp = 1;
break;
}
} else if (a[ta] == 0 && a[ta1] == 0) {
a[ta] = ta1;
a[ta1] = ta;
} else {
tmp = 1;
break;
}
}
if (tmp == 0) {
vector<pair<char, char> > v;
for (i = 1; i < 27; i++) {
if (a[i] != 0) {
v.push_back(make_pair((char)(i + 96), (char)(a[i] + 96)));
a[a[i]] = 0;
}
}
long long int cnt = 0;
for (i = 0; i < v.size(); i++) {
if (v[i].first != v[i].second) cnt++;
}
cout << cnt << "\n";
for (i = 0; i < v.size(); i++) {
if (v[i].first != v[i].second)
cout << v[i].first << " " << v[i].second << "\n";
}
} else
cout << -1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N], ans[N];
int b[N];
int ask(int x) {
int ans = 0;
for (; x; x -= x & -x) ans = max(ans, b[x]);
return ans;
}
void add(int x, int c) {
for (; x <= n; x += x & -x) b[x] = max(b[x], c);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
memset(b, 0, sizeof(b));
for (int i = n; i >= 1; i--) {
ans[i] = ask(a[i] - 1);
ans[i] = (ans[i] == 0 ? -1 : ans[i] - i - 1);
add(a[i], i);
}
for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++)cin>>v[i];
map<int,int> m;
int ans=0;
for(int i=n-1;i>=0;i--){
ans+=m[v[i]+i];
m[i-v[i]]++;
}
cout<<ans<<endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a;
while(cin>>a,a){
vector<unordered_set<string>>V(50);
for(int b=0;b<a;b++){
string c;cin>>c;
string d="a";d[0]=c[0];
for(int e=1;e<c.length();e++){
if(c[e-1]=='a'||c[e-1]=='i'||c[e-1]=='u'||c[e-1]=='e'||c[e-1]=='o'){
d+=c[e];
}
}
for(int e=1;e<=50;e++){
V[e-1].insert(d.substr(0,e));
}
}
for(int f=1;f<=50;f++){
if(V[f-1].size()==a){
cout<<f<<endl;
goto l;
}
}
puts("-1");
l:;
}
} | 0 |
#include<iostream>
using namespace std;
int main(){
string s;
cin>>s;
if(s=="A")cout<<"T";
else if(s=="T")cout<<"A";
else if(s=="G")cout<<"C";
else cout<<"G";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int> > in(n);
vector<int> out(n);
for (int i = 0; i < n; ++i) {
cin >> in[i].first;
in[i].second = i;
}
sort(in.begin(), in.end());
int type;
bool valid = true;
int outCont = 1;
for (int i = 0; i < n; ++i) {
type = n - in[i].first;
if (type <= 0) {
valid = false;
break;
}
for (int j = i; j < type + i; j++) {
if (in[j].first != in[i].first) {
valid = false;
break;
}
out[in[j].second] = outCont;
}
if (!valid) {
break;
}
outCont++;
i += type - 1;
}
if (!valid) {
cout << "Impossible";
} else {
cout << "Possible\n";
for (int i = 0; i < n; ++i) {
cout << out[i] << " ";
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n4 = 0, n7 = 0;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '4') n4++;
if (s[i] == '7') n7++;
}
if (n4 == 0 && n7 == 0) {
cout << "-1" << endl;
return 0;
}
if (n4 > n7) {
cout << "4" << endl;
return 0;
}
if (n4 < n7) {
cout << "7" << endl;
return 0;
}
if (n4 == n7) {
cout << "4" << endl;
return 0;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, long long> > Q[3];
int Pr[3];
int Cap[3];
int E[100005];
int N;
long long ans = 0;
void push(int k, int man, long long t) {
if (k == 3) {
ans = max(ans, t - E[man]);
return;
}
while ((int)(Q[k]).size() && Q[k].front().second + Pr[k] <= t) {
pair<int, long long> p = Q[k].front();
Q[k].pop();
push(k + 1, p.first, p.second + Pr[k]);
}
if ((int)(Q[k]).size() == Cap[k]) {
pair<int, long long> p = Q[k].front();
Q[k].pop();
push(k + 1, p.first, p.second + Pr[k]);
Q[k].push(make_pair(man, p.second + Pr[k]));
} else {
Q[k].push(make_pair(man, t));
}
}
void Solve() {
for (int i = 0; i < 3; ++i) {
scanf("%d", Cap + i);
}
for (int i = 0; i < 3; ++i) {
scanf("%d", Pr + i);
}
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
scanf("%d", E + i);
push(0, i, E[i]);
}
for (int k = 0; k < 3; ++k) {
while (!Q[k].empty()) {
pair<int, long long> p = Q[k].front();
Q[k].pop();
push(k + 1, p.first, p.second + Pr[k]);
}
}
printf("%I64d\n", ans);
}
int main() {
Solve();
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.