solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, a[100010], b[100010], max = 0, sumOr, sumBo;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
sumOr = a[i];
sumBo = b[i];
for (int j = i + 1; j <= n; j++) {
sumOr |= a[j];
sumBo |= b[j];
if (sumOr + sumBo > max) max = sumOr + sumBo;
}
if (sumOr + sumBo > max) max = sumOr + sumBo;
}
cout << max << endl;
}
| 1 |
#include <iostream>
using namespace std;
int main() {
string s;
int k;
cin >> s >> k;
int tmp = 1, buf = 0;
long long ans = 0;
for (int i = 1; i < s.size(); i++) {
if (s[i-1] == s[i]) {
tmp += 1;
}
else {
if (!buf) buf = tmp;
ans += tmp / 2;
tmp = 1;
}
}
if (!buf) {
ans = tmp;
ans = ans * k / 2;
cout << ans << endl;
return 0;
}
ans += tmp / 2;
ans *= k;
if (s.front() == s.back() && buf % 2 && tmp % 2) {
ans += (k - 1);
}
cout << ans << endl;
}
| 0 |
#include <iostream>
using namespace std;
int main(void){
long long n,a[10000],t=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
long long b;
cin>>b;
if(a[i]<b){
t+=(b-a[i])/2;
}else{
t+=b-a[i];
}
}
if(t>=0){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A;
vector<int> B;
for (int i = 0; i < N; i++) {
int temp;
cin >> temp;
if (temp == 0)
A.push_back(i);
else
B.push_back(i);
}
long long ans = 0;
for (int i = 0; i < ((int)(A).size()); i++) {
int pos = lower_bound((B).begin(), (B).end(), A[i]) - B.begin();
ans += pos;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n];
long long k = 0;
long long m = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
if (a[i] >= i + 1 + k) {
long long p = k;
k += a[i] - (i + 1 + p);
}
}
cout << k << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? EOF
: *p1++;
}
inline long long read() {
long long t = 0, f = 1;
char anss = getchar();
while (!isdigit(anss)) {
if (anss == '-') f = -1;
anss = getchar();
}
while (isdigit(anss)) t = t * 10 + anss - '0', anss = getchar();
return t * f;
}
inline void write(long long x) {
if (x < 0) {
putchar('-');
write(-x);
return;
}
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(long long x) {
write(x);
puts("");
}
inline void write_p(long long x) {
write(x);
putchar(' ');
}
const int N = 300005;
struct node {
int l, len;
} p[N];
double sy, p1, p2;
int m, que, sum[N];
inline double Get_l(double x, double y) {
if (x == p1) return p1;
double k = (y - sy) / (x - p1);
return x - y / k;
}
inline double Get_r(double x, double y) {
if (x == p2) return p2;
double k = (y - sy) / (x - p2);
return x - y / k;
}
inline int Get(int l, int r) {
if (l <= r)
return sum[r] - sum[l - 1];
else
return 0;
}
inline int lower(int x) {
int l = 1, r = m, ans = 0;
while (l <= r) {
int mid = l + r >> 1;
if (p[mid].l <= x)
ans = mid, l = mid + 1;
else
r = mid - 1;
}
return ans;
}
inline void Solve(double x, double y) {
double l = Get_l(x, y), r = Get_r(x, y);
int tep = lower((int)l);
int top = lower((int)r);
if (tep == top) {
if (p[tep].l <= l && p[top].l + p[top].len >= r) {
double tans1 = r - l;
printf("%.20lf\n", tans1 / y * (y - sy));
return;
}
}
double tans = Get(tep + 1, top - 1);
if (1.0 * p[tep].l + 1.0 * p[tep].len > l)
tans += (p[tep].l * 1.0 + p[tep].len * 1.0 - l);
if (1.0 * p[top].l + 1.0 * p[top].len > r)
tans += r - 1.0 * p[top].l;
else if (p[top].l > l)
tans += 1.0 * p[top].len;
printf("%.20lf\n", tans / y * (y - sy));
}
int main() {
sy = read();
p1 = read();
p2 = read();
m = read();
for (int i = 1; i <= m; ++i) p[i].l = read(), p[i].len = read() - p[i].l;
for (int i = 1; i <= m; ++i) sum[i] = sum[i - 1] + p[i].len;
que = read();
for (int i = 1; i <= que; ++i) {
int x = read(), y = read();
Solve(x, y);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int gcd(int a, int b) {
int aa = a, bb = b;
while (bb) {
int r = aa % bb;
aa = bb;
bb = r;
}
return aa;
}
inline long long gcd(long long a, long long b) {
long long aa = a, bb = b;
while (bb) {
long long r = aa % bb;
aa = bb;
bb = r;
}
return aa;
}
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline int min(int a, int b) {
if (a < b) return a;
return b;
}
inline long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
inline double min(double a, double b) {
if (a < b) return a;
return b;
}
inline int min(int a, int b, int c) { return min(a, min(b, c)); }
inline long long min(long long a, long long b, long long c) {
return min(a, min(b, c));
}
inline double min(double a, double b, double c) { return min(a, min(b, c)); }
inline int max(int a, int b) {
if (a > b) return a;
return b;
}
inline long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
inline double max(double a, double b) {
if (a > b) return a;
return b;
}
inline int max(int a, int b, int c) { return max(a, max(b, c)); }
inline long long max(long long a, long long b, long long c) {
return max(a, max(b, c));
}
inline double max(double a, double b, double c) { return max(a, max(b, c)); }
inline int read() {
register int x = 0;
register int c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + (c ^ 48), c = getchar();
return x;
}
int n, m, a[105], c;
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) a[i] = read();
sort(a + 1, a + 1 + n, greater<int>());
for (int i = 1; i <= n; ++i) {
if (m > 0) {
m -= a[i];
++c;
} else
break;
}
printf("%d", c);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const long long MAXLL = 0x0FFFFFFFFFFFFFFFll;
const int MAXINT = 0x0FFFFFFF;
const int MAXN = 100005;
int n;
int a[MAXN];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
set<int> st;
set<int>::iterator it;
st.clear();
for (int i = 0; i < n; i++) {
st.insert(a[i]);
it = st.find(a[i]);
it++;
if (it != st.end()) st.erase(it);
}
cout << st.size() << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<char>>> dp(2,
vector<vector<char>>(501,
vector<char>(501, false)));
bool load(int32_t l1, int32_t l2, int32_t l3) {
if (l2 < 0 || l3 < 0) return false;
return dp[l1][l2][l3];
}
int main() {
int32_t n, k;
cin >> n >> k;
vector<int32_t> coins;
coins.reserve(n);
for (int32_t i = 0; i < n; i++) {
int32_t c;
cin >> c;
coins.push_back(c);
}
dp[0][0][0] = true;
for (int32_t l1 = 0; l1 < n; l1++) {
int32_t c = coins[l1];
for (int32_t l2 = 0; l2 <= k; l2++) {
for (int32_t l3 = 0; l3 <= k; l3++) {
dp[(l1 & 1) ^ 1][l2][l3] = load(l1 & 1, l2 - c, l3 - c) |
load(l1 & 1, l2 - c, l3) |
load(l1 & 1, l2, l3);
}
}
}
int32_t cnt = 0;
for (int32_t i = 0; i <= k; i++) {
if (dp[n & 1][k][i]) {
cnt++;
}
}
cout << cnt << '\n';
for (int32_t i = 0; i <= k; i++) {
if (dp[n & 1][k][i]) {
cout << i << ' ';
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 52, INF = 2147483647;
const double eps = 1e-5;
double l = 0.0, r = 1e8, mid;
int n;
double f[MAXN][MAXN][MAXN];
struct rec {
double a, b;
} t[MAXN];
bool cmp(rec x, rec y) { return x.a > y.a; }
void dfs(int i) {
if (i == 0) return;
dfs(i - 1);
if (t[i].a < t[i - 1].a) {
for (int j = 0; j <= n; ++j)
for (int k = 0; k <= n; ++k) {
if (j - k + 1 >= 0)
f[i][j][0] = min(f[i][j][0], f[i - 1][j - k + 1][k]);
if (j - k >= 0)
f[i][j][1] =
min(f[i][j][1], f[i - 1][j - k][k] + t[i].a - mid * t[i].b);
}
} else if (t[i].a == t[i - 1].a) {
for (int j = 0; j <= n; ++j)
for (int k = 0; k <= n; ++k) {
if (k >= 1)
f[i][j][k] =
min(f[i - 1][j][k - 1] + t[i].a - mid * t[i].b, f[i][j][k]);
f[i][j][k] = min(f[i][j][k], f[i - 1][j + 1][k]);
}
}
}
bool judge(double x) {
dfs(n);
for (int i = 0; i <= n; ++i)
for (int j = 0; j <= n; ++j)
if (f[n][i][j] <= eps) return 1;
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lf", &t[i].a);
for (int i = 1; i <= n; ++i) scanf("%lf", &t[i].b);
sort(t + 1, t + 1 + n, cmp);
t[0].a = INF * 1.0;
mid = 9.0;
while (r - l > eps) {
mid = (l + r) / 2;
for (int i = 0; i <= n + 1; ++i)
for (int j = 0; j <= n + 1; ++j)
for (int k = 0; k <= n + 1; ++k) f[i][j][k] = INF * 1.0;
f[0][0][0] = 0.0;
if (judge(mid))
r = mid;
else
l = mid;
}
printf("%lld", (long long)ceil(l * 1000));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, a[310000], dp1[310000], dp2[310000], ans[310000];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
ans[1] = max(ans[1], a[i]);
}
for (int i = 1; i < n; i++)
dp1[min(i, n - i)] = max(dp1[min(i, n - i)], max(a[i], a[i + 1]));
for (int i = 2; i < n; i++)
dp2[min(i - 1, n - i)] = max(dp2[min(i - 1, n - i)],
max(min(a[i - 1], a[i]), min(a[i], a[i + 1])));
for (int i = n / 2; i >= 1; i--) {
ans[i << 1] = max(ans[(i + 1) << 1], dp1[i]);
ans[i << 1 | 1] = max(ans[(i + 1) << 1 | 1], dp2[i]);
}
for (int i = n; i >= 1; i--) printf("%d ", ans[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
long long n;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
}
long long ans = a[0];
for (int j = 1; j < n; ++j) {
int i = 0;
while (a[j] + b[j] * i <= ans) {
i++;
}
ans = a[j] + b[j] * i;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> razl(long long p, long long k) {
vector<long long> a;
while (p > 0) {
a.push_back(p % k);
p /= k;
}
return a;
}
void priv1(vector<long long>& razl, long long k) {
for (int i = 0; i < razl.size(); ++i) {
if (i % 2 == 1) {
if (razl[i] != 0) {
if (i + 1 != razl.size()) {
razl[i + 1]++;
razl[i] = razl[i] - k;
} else {
razl.push_back(1);
razl[i] = razl[i] - k;
}
}
}
}
}
bool check(vector<long long>& razl, long long k) {
for (int i = 0; i < razl.size(); ++i) {
if (razl[i] >= k || razl[i] <= -k) {
return true;
}
}
return false;
}
bool secondCheck(vector<long long>& razl, long long k) {
for (int i = 0; i < razl.size(); ++i) {
if (i % 2 == 0) {
if (razl[i] < 0) {
return true;
}
} else {
if (razl[i] > 0) {
return true;
}
}
}
return false;
}
vector<long long> priv2(vector<long long>& razl, long long k) {
while (check(razl, k)) {
for (int i = 0; i < razl.size(); ++i) {
if (i % 2 == 0) {
if (razl[i] >= k) {
if (i != razl.size() - 1) {
razl[i + 1]++;
razl[i] -= k;
} else {
razl.push_back(1);
razl[i] -= k;
}
}
} else {
if (razl[i] <= -k) {
if (i != razl.size() - 1) {
razl[i + 1]--;
razl[i] += k;
} else {
razl.push_back(-1);
razl[i] += k;
}
}
}
}
if (secondCheck(razl, k)) {
priv1(razl, k);
}
}
return razl;
}
int main() {
long long p, k;
cin >> p >> k;
vector<long long> a = razl(p, k);
priv1(a, k);
priv2(a, k);
cout << a.size() << endl;
for (int i = 0; i < a.size(); ++i) {
if (i % 2 == 0) {
cout << a[i] << ' ';
} else {
cout << -a[i] << ' ';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int tinf = (int)1e9 + 7;
const long long inf = (long long)1e18 + 7;
const int N = 1e5 + 5;
long long log2(long long x) { return log10(x) / log10(2); }
int main() {
{
ios ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
long long n;
cin >> n;
long long a[n], Min = inf;
vector<long long> an;
for (int i = 0; i < n; i++) cin >> a[i], Min = min(Min, a[i]);
for (int i = 0; i < n; i++)
if (a[i] == Min) an.push_back(i);
long long ans = inf;
for (int i = 1; i < an.size(); i++) ans = min(ans, an[i] - an[i - 1]);
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
const int MAXRET = 100000;
int n;
long long tdrink;
int len[MAXN];
int tmx[MAXN];
long long nret, ret[MAXRET];
bool solve() {
nret = 0;
long long t = 0;
long long rem = 0;
for (int i = (0); i < (n); ++i) {
if (tmx[i] < len[i]) return false;
if (rem >= len[i]) {
t += len[i];
rem -= len[i];
continue;
}
if (rem + 2 * (len[i] - rem) <= tmx[i]) {
t += rem + 2 * (len[i] - rem);
rem = 0;
continue;
}
int exceed = rem + 2 * (len[i] - rem) - tmx[i];
int nfull = exceed / tdrink, nlast = exceed % tdrink;
for (int j = 0; j < nfull && nret + j < MAXRET; ++j)
ret[nret + j] = t + rem + tdrink * j;
nret += nfull;
if (nlast != 0) {
if (nret < MAXRET) ret[nret] = t + tmx[i] - nlast;
++nret;
rem = tdrink - nlast;
} else
rem = 0;
t += tmx[i];
}
return true;
}
void run() {
scanf("%d%lld", &n, &tdrink);
for (int i = (0); i < (n); ++i) scanf("%d", &len[i]);
for (int i = (0); i < (n); ++i) scanf("%d", &tmx[i]);
if (!solve()) {
printf("-1\n");
return;
}
printf("%lld\n", nret);
if (nret <= MAXRET) {
for (int i = (0); i < (nret); ++i) {
if (i != 0) printf(" ");
printf("%lld", ret[i]);
}
puts("");
}
}
int main() {
run();
return 0;
}
| 4 |
#include <bits/stdc++.h>
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
long long dp[55];
void work(int l, int r, long long K) {
if (l == r) {
cout << r << " ";
return;
}
if (l > r) return;
int num = r - l + 1;
if (dp[num - 1] >= K) {
cout << l << " ";
work(l + 1, r, K);
} else {
cout << l + 1 << " " << l << " ";
K -= dp[num - 1];
work(l + 2, r, K);
}
}
int Main() {
ios_base::sync_with_stdio(0);
dp[0] = dp[1] = 1;
for (int i = 2; i < 55; i++) dp[i] = dp[i - 1] + dp[i - 2];
int n;
long long K;
cin >> n >> K;
(void)work(1, n, K);
return 0;
}
int main() { return Main(); }
| 4 |
#include <bits/stdc++.h>
using namespace std;
class trio {
public:
long long first, second, third;
};
long long n;
void print(long long mat[60][60]) {
cout << "!\n" << flush;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < n; ++j) {
cout << mat[i][j] << flush;
}
cout << endl << flush;
}
}
int32_t main() {
cin >> n;
long long x;
long long mat1[60][60] = {0};
long long mat2[60][60] = {0};
long long mat3[60][60] = {0};
mat1[0][0] = 1;
mat1[n - 1][n - 1] = 0;
for (long long j = 0; j < n; ++j) {
if (j > 0) {
if (j % 2) {
cout << "? " << 0 + 1 << " " << j - 1 + 1 << " " << 1 + 1 << " "
<< j + 1 << endl
<< flush;
cin >> x;
if (x == 1) {
mat1[1][j] = mat1[0][j - 1];
} else {
mat1[1][j] = 1 ^ mat1[0][j - 1];
}
} else {
cout << "? " << 0 + 1 << " " << j - 2 + 1 << " " << 0 + 1 << " "
<< j + 1 << endl
<< flush;
cin >> x;
if (x == 1) {
mat1[0][j] = mat1[0][j - 2];
} else {
mat1[0][j] = 1 ^ mat1[0][j - 2];
}
}
}
for (long long i = j % 2; i < n - 2; i += 2) {
cout << "? " << i + 1 << " " << j + 1 << " " << i + 2 + 1 << " " << j + 1
<< endl
<< flush;
cin >> x;
if (x == 1) {
mat1[i + 2][j] = mat1[i][j];
} else {
mat1[i + 2][j] = 1 ^ (mat1[i][j]);
}
}
}
mat2[1][0] = 1;
for (long long j = 0; j < n; ++j) {
if (j > 0) {
if (j % 2) {
cout << "? " << 1 + 1 << " " << j - 1 + 1 << " " << 2 + 1 << " "
<< j + 1 << endl
<< flush;
cin >> x;
if (x == 1) {
mat2[2][j] = mat2[1][j - 1];
} else {
mat2[2][j] = 1 ^ (mat2[1][j - 1]);
}
} else {
cout << "? " << 1 + 1 << " " << j - 2 + 1 << " " << 1 + 1 << " "
<< j + 1 << endl
<< flush;
cin >> x;
if (x == 1) {
mat2[1][j] = mat2[1][j - 2];
} else {
mat2[1][j] = 1 ^ mat2[1][j - 2];
}
}
}
for (long long i = ((j % 2) ? 2 : 1); i < n - 2; i += 2) {
cout << "? " << i + 1 << " " << j + 1 << " " << i + 2 + 1 << " " << j + 1
<< endl
<< flush;
cin >> x;
if (x == 1) {
mat2[i + 2][j] = mat2[i][j];
} else {
mat2[i + 2][j] = 1 ^ (mat2[i][j]);
}
}
}
cout << "? " << 0 + 1 << " " << 1 + 1 << " " << 1 + 1 << " " << 2 + 1 << endl
<< flush;
cin >> x;
if (x == 1) {
mat2[0][1] = mat2[1][2];
} else {
mat2[0][1] = 1 ^ (mat2[1][2]);
}
for (long long j = 1; j < n - 2; j += 2) {
cout << "? " << 0 + 1 << " " << j + 1 << " " << 0 + 1 << " " << j + 2 + 1
<< endl
<< flush;
cin >> x;
if (x == 1) {
mat2[0][j + 2] = mat2[0][j];
} else {
mat2[0][j + 2] = 1 ^ (mat2[0][j]);
}
}
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < n; ++j) {
if ((i + j) % 2 == 0) {
mat2[i][j] = mat1[i][j];
} else {
mat1[i][j] = 1 ^ mat2[i][j];
}
}
}
long long arr[100], brr[100];
pair<long long, long long> coor[100];
for (long long i = 0; i < n; ++i) {
arr[i] = mat1[i][0];
brr[i] = mat2[i][0];
coor[i].first = i;
coor[i].second = 0;
}
for (long long i = 1; i < n; ++i) {
arr[n + i - 1] = mat1[n - 1][i];
brr[n + i - 1] = mat2[n - 1][i];
coor[n + i - 1].first = n - 1;
coor[n + i - 1].second = i;
}
long long k = 2 * n - 1, a1 = (arr[0] ^ arr[1] ^ arr[2] ^ arr[3]), j = 3;
if (a1 != 0) {
for (long long i = 4; i < k; ++i) {
a1 ^= arr[i];
a1 ^= arr[i - 4];
if (a1 == 0) {
j = i;
break;
}
}
}
cout << "? " << coor[j - 3].first + 1 << " " << coor[j - 3].second + 1 << " "
<< coor[j].first + 1 << " " << coor[j].second + 1 << endl
<< flush;
cin >> x;
if (x != 0) {
if (brr[j] == brr[j - 3]) {
print(mat2);
} else {
print(mat1);
}
} else {
if (brr[j] != brr[j - 3]) {
print(mat2);
} else {
print(mat1);
}
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int a[111];
bool used[111];
int main()
{
int x,n;
cin>>x>>n;
for(int i=0;i<n;i++){
cin>>a[i];
used[a[i]]=true;
}
int ans=x;
for(int i=1;i<x+ans;i++)
if(!used[i])
ans=min(ans,x-i);
cout<<x-ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void _print(long long t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long int t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
int min(int arg_count, ...) {
int i;
int Min, a;
va_list ap;
va_start(ap, arg_count);
Min = va_arg(ap, int);
for (i = 2; i <= arg_count; i++)
if ((a = va_arg(ap, int)) < Min) Min = a;
va_end(ap);
return Min;
}
int max(int arg_count, ...) {
int i;
int Max, a;
va_list ap;
va_start(ap, arg_count);
Max = va_arg(ap, int);
for (i = 2; i <= arg_count; i++)
if ((a = va_arg(ap, int)) > Max) Max = a;
va_end(ap);
return Max;
}
template <typename T>
T Exgcd(T a, T b, T &x, T &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
T x1, y1;
T d = Exgcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
template <typename T>
T modpow(T x, T n, T p) {
if (n == 0) return 1 % p;
T y = 0;
if (n % 2 == 0) {
y = modpow(x, n / 2, p);
y = (y * y) % p;
} else {
y = x % p;
y = (y * modpow(x, n - 1, p)) % p;
}
return (int)((y + p) % p);
}
template <typename T>
T ModInv(T a, T m) {
T x, y;
Exgcd(a, m, x, y);
return (x % m + m) % m;
}
template <typename T>
bool IsPrime(T n) {
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
const long long maxNN = 100005;
long long fact[maxNN];
void pre(long long M) {
fact[0] = 1;
for (int i = 1; i < maxNN; i++) fact[i] = (i * fact[i - 1]) % M;
}
long long nCk(long long n, long long r, long long M) {
long long ans = 1;
ans = (ans * fact[n]) % M;
ans = (ans * ModInv(fact[n - r], M)) % M;
ans = (ans * ModInv(fact[r], M)) % M;
return M;
}
int find(int n, vector<int> &parent) {
if (parent[n] < 0) return n;
return parent[n] = find(parent[n], parent);
}
void Union(int a, int b, vector<int> &parent, vector<int> &rank) {
int l1 = find(a, parent);
int l2 = find(b, parent);
if (l1 == l2) return;
if (rank[l1] < rank[l2]) {
parent[l1] = l2;
rank[l2] += rank[l1];
} else {
parent[l2] = l1;
rank[l1] += rank[l2];
}
}
int get_component_size(int u, vector<int> rank, vector<int> parent) {
u = find(u, parent);
return rank[u];
}
int func(string a, string b) {
int cnt = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i] != b[i]) cnt++;
}
return cnt;
}
bool iscomposite(string str) {
return str == "25" || str == "27" || str == "22" || str == "35" ||
str == "32" || str == "33" || str == "52" || str == "57" ||
str == "55" || str == "72" || str == "75" || str == "77";
}
bool isin(char ch) {
return ch == '1' || ch == '4' || ch == '6' || ch == '8' || ch == '9';
}
void solve() {
int k;
cin >> k;
string str;
cin >> str;
for (int i = 0; i < k; i++) {
if (isin(str[i])) {
cout << 1 << endl << str[i] << endl;
return;
}
}
for (int i = 0; i < k - 1; i++) {
for (int j = i + 1; j < k; j++) {
string ans = "";
ans = str[i];
ans += str[j];
if (iscomposite(ans)) {
cout << 2 << endl << ans << endl;
return;
}
}
}
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
int n;
scanf("%d", &n);
int sum = 0, mxa = 0;
for (int i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
sum += a;
mxa = std::max(mxa, a);
}
int opt_k = ceil(2.0 * sum / n);
if (opt_k == 2.0 * sum / n) opt_k++;
if (opt_k < mxa) opt_k = mxa;
printf("%d\n", opt_k);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool is_magic(const std::string &x) {
int a = 2;
for (size_t i = 0; i < x.size(); ++i)
if (x[i] == '1') {
a = 0;
} else if (x[i] == '4') {
if (++a > 2) {
return false;
}
} else {
return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
if (is_magic(s)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 5;
long long inf = 5 * 1LL * 1e10;
int a[N];
int n, k;
struct node {
int l, r;
long long mark, v;
} s[N << 2];
void push_down(int i) {
if (s[i].mark) {
s[i << 1].mark = min(s[i << 1].mark, s[i].mark);
s[i << 1 | 1].mark = min(s[i << 1 | 1].mark, s[i].mark);
s[i << 1].v = min(s[i << 1].v, s[i].mark);
s[i << 1 | 1].v = min(s[i << 1 | 1].v, s[i].mark);
s[i].mark = inf;
}
}
void build(int l, int r, int i) {
int mid = l + r >> 1;
s[i].l = l;
s[i].r = r;
if (l == r) {
s[i].mark = inf;
s[i].v = inf;
return;
}
build(l, mid, i << 1);
build(mid + 1, r, i << 1 | 1);
s[i].v = max(s[i << 1].v, s[i << 1 | 1].v);
s[i].mark = inf;
}
void update(int l, int r, long long x, int i) {
int mid = s[i].l + s[i].r >> 1;
if (l <= s[i].l && s[i].r <= r) {
s[i].mark = min(s[i].mark, x);
s[i].v = min(s[i].v, x);
return;
}
push_down(i);
if (l <= mid) update(l, r, x, i << 1);
if (r > mid) update(l, r, x, i << 1 | 1);
s[i].v = max(s[i << 1].v, s[i << 1 | 1].v);
}
long long query(int l, int r, int i) {
int mid = s[i].l + s[i].r >> 1;
if (l <= s[i].l && s[i].r <= r) {
return s[i].v;
}
push_down(i);
long long ans = 0;
if (l <= mid) ans += query(l, r, i << 1);
if (r > mid) ans += query(l, r, i << 1 | 1);
return ans;
}
int main() {
long long dp[105];
while (scanf("%d %d", &n, &k) != EOF) {
char c;
for (int i = 1; i < n + 1; ++i) {
scanf(" %c", &c);
a[i] = c == '1';
}
build(1, n, 1);
for (int i = 1; i < n + 1; ++i) {
long long t1 = query(i - 1, i - 1, 1) + i;
update(i, i, t1, 1);
if (a[i] == 1) {
t1 = query(max(i - k, 1) - 1, max(i - k, 1) - 1, 1) + i;
update(i, i, t1, 1);
update(max(i - k, 1), min(i + k, n), t1, 1);
}
}
printf("%lld\n", query(n, n, 1));
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); }
const int N = 300005;
int n,ans,fa1[N],fa2[N];
vector<int>G1[N],G2[N];
struct BIT{
int d[N],n;
inline void init(int nn){ n = nn,memset(d,0,n+1<<2); }
inline void add(int x){ while (x <= n) ++d[x],x+=x&-x; }
inline void add2(int x){ while (x <= n) --d[x],x+=x&-x; }
inline int ask(int x){
int r=0; while (x)r+=d[x],x-=x&-x; return r;
}
inline int qry(int l,int r){
return ask(r)-ask(l-1);
}
}T2;
struct Seg{
#define lc o<<1
#define rc o<<1|1
int n,val[N<<2],is[N<<2];
inline void Tag(int o,int v){ val[o] = v,is[o] = 1; }
inline void down(int o){ if (is[o]) is[o] = 0,Tag(lc,val[o]),Tag(rc,val[o]); }
inline void Build(int o,int l,int r){
if (o == 1) n = r;
val[o] = is[o] = 0; if (l < r){ int mid = l+r>>1; Build(lc,l,mid),Build(rc,mid+1,r); }
}
int ll,rr,vv;
inline void Add(int o,int l,int r){
if (ll <= l && rr >= r){ Tag(o,vv); return; }
down(o); int mid =l+r>>1; if (ll<=mid) Add(lc,l,mid); if (rr>mid) Add(rc,mid+1,r);
}
inline int ask(int p){
static int mid,o,l,r; o = l = 1,r = n;
while (l < r){
down(o),mid = l+r>>1;
if (p <= mid) o<<=1,r = mid; else o = o<<1|1,l = mid + 1;
}
return val[o];
}
inline void add(int l,int r,int v){
ll =l,rr=r,vv=v; if (l<=r) Add(1,1,n);
}
#undef lc
#undef rc
}T;
int tl[N],tr[N],id[N],Time;
inline void dfs2(int x){
tl[x] = id[x] = ++Time;
for (int i = 0; i < G2[x].size(); ++i) dfs2(G2[x][i]);
tr[x] = Time;
}
int Now;
inline void dfs1(int x){
int cx = 0,cy = 0;
if (!T2.qry(tl[x],tr[x])){
cx = T.ask(id[x]);
if (cx == 0){
cy = x;
++Now;
}
else{
cy = x;
T2.add2(id[cx]);
T.add(tl[cx],tr[cx],0);
}
T2.add(id[x]);
T.add(tl[x],tr[x],x);
}
ans =max(ans,Now);
for (int i = 0; i < G1[x].size(); ++i) dfs1(G1[x][i]);
if (cy == x){
T2.add2(id[x]);
T.add(tl[x],tr[x],0);
if (cx == 0) --Now;
else T2.add(id[cx]),T.add(tl[cx],tr[cx],cx);
}
}
inline void solve(){
int i;
read(n),ans = 0;
for (i = 0; i <= n; ++i) G1[i].clear(),G2[i].clear();
for (i = 2; i <= n; ++i) read(fa1[i]),G1[fa1[i]].push_back(i);
for (i = 2; i <= n; ++i) read(fa2[i]),G2[fa2[i]].push_back(i);
T.Build(1,1,n); T2.init(n);
Time = 0,dfs2(1);
dfs1(1);
cout << ans << '\n';
}
int main(){
int T;
read(T);
while (T--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool asc(pair<string, int>& a, pair<string, int>& b) {
return a.second < b.second;
}
int main() {
int n;
while (cin >> n) {
vector<pair<string, int> > c;
for (int i = 1; i <= n; ++i) {
string s, t;
int r;
cin >> s >> t;
if (t == "rat")
r = i;
else if (t == "child" || t == "woman")
r = i * 100;
else if (t == "man")
r = i * 10000;
else
r = i * 1000000;
c.push_back(make_pair(s, r));
}
sort(c.begin(), c.end(), asc);
for (int i = 0; i < n; ++i) {
cout << c[i].first << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void read(_T &f) {
f = 0;
_T fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') {
fu = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + (c & 15);
c = getchar();
}
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10)
putchar(x + 48);
else
print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x);
putchar(t);
}
const int N = 1e6 + 5, M = 1e6, md = 998244353;
inline int add(int x, int y) {
x += y;
if (x >= md) x -= md;
return x;
}
inline int sub(int x, int y) {
x -= y;
if (x < 0) x += md;
return x;
}
inline int mul(int x, int y) { return 1ll * x * y % md; }
inline int fpow(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = mul(ans, x);
y >>= 1;
x = mul(x, x);
}
return ans;
}
int a[N], fac[N * 2], inv[N * 2], cnt[N];
int n, lim = -1, ans, sum, now;
inline int C(int n, int m) {
if (n < 0 || m < 0 || n < m) return 0;
return mul(fac[n], mul(inv[m], inv[n - m]));
}
int main() {
fac[0] = 1;
for (int i = 1; i <= M * 2; i++) fac[i] = mul(fac[i - 1], i);
inv[M * 2] = fpow(fac[M * 2], md - 2);
for (int i = M * 2; i >= 1; i--) inv[i - 1] = mul(inv[i], i);
read(n);
for (int i = 1; i <= n; i++) read(a[i]);
sort(a + 1, a + n + 1);
for (int i = 2; i <= n; i++)
if (a[i] + 1 < i) {
lim = a[i];
break;
}
if (~lim) {
for (int i = 1; i <= n; i++) ++cnt[a[i]];
for (int i = 0; i <= lim; i++) {
if (i) cnt[i] += cnt[i - 1];
int must = (i == 0 ? 0 : cnt[i - 1]);
ans = add(ans, C(n + i - must - 1, n - 1));
}
print(ans, '\n');
return 0;
}
for (int i = 1; i <= n; i++) ++cnt[a[i] % n], sum += a[i], now += (a[i] % n);
for (int i = 0; i < n; i++) {
int x = (sum - now) / n;
ans = add(ans, C(n + x - 1, n - 1));
now -= n;
now += cnt[i] * n;
}
print(ans, '\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n == 1)
cout << "1 1\n1";
else
cout << (n - 1 << 1) << " 2\n1 2";
}
| 1 |
#include <bits/stdc++.h>
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e1 + 5;
const double eps = 1e-9;
using namespace std;
char a[MAXN][MAXN], b[MAXN][MAXN];
int n, m, ans;
int c[4][5][2] = {{{0, 0}, {0, 1}, {0, 2}, {1, 1}, {2, 1}},
{{0, 1}, {1, 1}, {2, 0}, {2, 1}, {2, 2}},
{{0, 0}, {1, 0}, {1, 1}, {1, 2}, {2, 0}},
{{0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 2}}};
void solve(int x, int y, int cnt) {
if (cnt + (n * m - x * m - y) / 6 <= ans) return;
if (x + 2 == n) {
if (cnt <= ans) return;
ans = cnt;
memcpy(b, a, MAXN * MAXN);
return;
}
if (y + 2 == m) {
solve(x + 1, 0, cnt);
return;
}
for (int i = 0; i < 4; i++) {
int fl = 0;
for (int j = 0; j < 5; j++)
if (a[x + c[i][j][0]][y + c[i][j][1]] != '.') {
fl = 1;
break;
}
if (fl) continue;
for (int j = 0; j < 5; j++) a[x + c[i][j][0]][y + c[i][j][1]] = 'A' + cnt;
solve(x, y + 1, cnt + 1);
for (int j = 0; j < 5; j++) a[x + c[i][j][0]][y + c[i][j][1]] = '.';
}
solve(x, y + 1, cnt);
}
int main() {
scanf("%d", &(n)), scanf("%d", &(m));
if (min(n, m) < 3) {
printf("%d\n", (0));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (j == m - 1)
printf(".\n");
else
printf(".");
return 0;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) a[i][j] = '.';
solve(0, 0, 0);
printf("%d\n", (ans));
for (int i = 0; i < n; i++) printf("%s\n", b[i]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int n, m, a;
int w[N], b[N], fa[N];
int f[N];
vector<int> adj[N];
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
cin >> n >> m >> a;
for (int i = 1; i <= n; i++) cin >> w[i], fa[i] = i;
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
fa[find(u)] = find(v);
}
for (int i = 1; i <= n; i++) adj[find(i)].push_back(i);
for (int i = 1; i <= n; i++) {
if (i == find(i)) {
for (int j = a; j >= 0; j--) {
int W, B;
W = B = 0;
for (int k = 0; k < adj[i].size(); k++) {
W += w[adj[i][k]];
B += b[adj[i][k]];
if (j >= w[adj[i][k]])
f[j] = max(f[j], f[j - w[adj[i][k]]] + b[adj[i][k]]);
}
if (j >= W) f[j] = max(f[j], f[j - W] + B);
}
}
}
cout << f[a] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int spf(int n) {
for (int i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) return i;
}
return n;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
if (n % 2 == 0)
cout << n + 2 * k;
else {
int temp = spf(n);
cout << n + temp + 2 * (k - 1);
}
cout << "\n";
}
}
| 1 |
#include<bits/stdc++.h>
#define MN 3000
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;
}
char s[MN*2+5];
int n,posb[MN+5],posa[MN+5],anum,bnum,match[MN*2+5],no[MN*2+5];
string f[MN*2+5];
string Max(string a,string b)
{
int n=a.length(),m=b.length();
for(int j=0;j<min(n,m);++j)
{
if(a[j]<b[j]) return b;
if(a[j]>b[j]) return a;
}
return n<=m?b:a;
}
int main()
{
n=read();scanf("%s",s+1);
for(int i=1;i<=n<<1;++i)
if(s[i]=='a') posa[++anum]=i;
else posb[++bnum]=i;
for(int i=1;i<=n;++i) match[posa[i]]=posb[i],match[posb[i]]=posa[i],no[posb[i]]=no[posa[i]]=i;
for(int i=n<<1;i;--i)
if(match[i]<i) f[i]=f[i+1];
else if(s[i]=='a') f[i]=Max(f[i+1],string("ab")+f[match[i]+1]);
else
{
int j=no[i],k=no[i];
while(k<n&&posb[k+1]<posa[k]) ++k;
for(int l=i;l<=posa[k];++l)
if(no[l]>=j&&no[l]<=k) f[i]+=s[l];
f[i]=Max(f[i]+f[posa[k]+1],f[i+1]);
//cout<<i<<" "<<j<<" "<<k<<" "<<f[i]<<endl;
}
cout<<f[1];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t, tm1 = "at", tm2 = "dot", a[1000];
int pa = 0;
cin >> s;
int f_at = 0, l = s.length();
for (int i = 0; i < l; i++) {
if (s.substr(i, 2) == "at") {
pa++;
a[pa] = '@';
i += 1;
} else if (s.substr(i, 3) == "dot") {
pa++;
a[pa] = '.';
i += 2;
} else {
pa++;
t = s[i];
a[pa] = t;
}
}
for (int i = 1; i <= pa; i++) {
if (a[i] == "@" && f_at == 0 && i != 1 && i != pa) {
cout << '@';
f_at = 1;
continue;
}
if (a[i] == "@") {
cout << "at";
continue;
}
if (a[i] == "." && i != 1 && i != pa) {
cout << '.';
continue;
}
if (a[i] == ".") {
cout << "dot";
continue;
}
cout << a[i];
}
cin.get();
cin.get();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define INF 1e18
using ll = long long;
int a[20];
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
int n,k;cin>>n>>k;
for(int i=0;i<n;i++)cin>>a[i];
ll ans=INF;
int sz=1<<n;
for(int p=0;p<sz;p++)
{
int m=0;
int cnt=0;
ll cost=0;
for(int i=0;i<n;i++)
{
if(cnt>=k)continue;
if(m<a[i])
{
m=a[i];
cnt++;
continue;
}
if(p&(1<<i))
{
m=m+1;
cost+=m-a[i];
cnt++;
}
}
if(cnt>=k)ans=min(ans,cost);
}
cout<<ans<<"\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
int a[105], num[105];
int main() {
int n, i, j, x, y, ans = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
num[i] = num[i - 1] + a[i];
}
scanf("%d %d", &x, &y);
for (i = 1; i <= n; i++) {
if (num[i] >= x && num[i] <= y && num[n] - num[i] >= x &&
num[n] - num[i] <= y) {
ans = i + 1;
break;
}
}
printf("%d", ans);
scanf(" ");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000 + 10, MAXLEN = 1000000 + 10;
int k, cur, cnt, Np, Ns;
string p, s, Tohka;
char str[MAXLEN];
bool check(int tp, int ts, int ds) {
for (int i = tp, j = ts; i < Np; i += k, j += ds)
if (p[i] != s[j]) return false;
return true;
}
string calc(int w, int num) {
string Houki;
int res = num - 1;
for (int i = min(k - 1, Np - 1); i >= Np % k; --i)
if (res >= w && check(i, res, num))
Houki += "1", --res;
else
Houki += "0";
for (int i = Np % k - 1; i >= 0; --i)
if (res >= 0 && check(i, res, num))
Houki += "1", --res;
else
Houki += "0";
reverse(Houki.begin(), Houki.end());
while ((int)Houki.size() < k) Houki += "0";
if (res < 0)
return Houki;
else
return string();
}
int main() {
gets(str);
p = str;
gets(str);
s = str;
cin >> k;
Np = p.size(), Ns = s.size();
if (k >= Np)
Tohka = calc(0, Ns);
else {
int w = Ns % (Np / k), num = Ns / (Np / k);
if (w > Np % k) puts("0"), exit(0);
for (; w <= Np % k && w <= num; w += Np / k, --num) {
string Houki = calc(w, num);
if (!Houki.size()) continue;
if (!Tohka.size())
Tohka = Houki;
else
Tohka = min(Tohka, Houki);
}
}
if (!Tohka.size())
puts("0");
else
cout << Tohka << endl;
fclose(stdin);
fclose(stdout);
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int md = 1e9 + 7;
const long long inf = 9e18;
using namespace std;
const int mx = 1e9;
int ask(int x, int y) {
cout << "? " << x << " " << y << '\n';
fflush(stdout);
cout.flush();
string rtn;
cin >> rtn;
if (rtn[0] == 'x')
return 0;
else if (rtn[0] == 'y')
return 1;
exit(0);
return 0;
}
void answer(int x) {
cout << "! " << x << '\n';
fflush(stdout);
cout.flush();
}
void solve() {
if (ask(0, 1) == 0) {
answer(1);
return;
}
int st = 1, prev;
while (1) {
prev = st;
st *= 2;
if (ask(st, prev) == 1) break;
}
int lo = prev + 1, hi = st, mid;
while (lo < hi) {
mid = (lo + hi) / 2;
if (ask(mid, prev) == 1)
hi = mid;
else
lo = mid + 1;
}
answer(lo);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
string tt;
cin >> tt;
while (tt == "start") {
solve();
cin >> tt;
}
}
| 4 |
#include<cstdio>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
int h=12,w=6;
char f[12][9];
int rec(int y,int x,bool e)
{
char c=f[y][x]; int ret=1;
f[y][x]='.';
rep(d,4)
{
int ny=y+dy[d],nx=x+dx[d];
if(ny<0||ny>=h||nx<0&&nx>=w)continue;
if(e&&f[ny][nx]=='O')f[ny][nx]='.',ret++;
if(f[ny][nx]==c)ret+=rec(ny,nx,e);
}
if(!e)f[y][x]=c;
return ret;
}
int main()
{
int CS; scanf("%d",&CS);
rep(cs,CS)
{
rep(i,h)scanf("%s",f[i]);
int ans=0;
for(;;ans++)
{
bool e=0;
rep(i,h)rep(j,w)if(f[i][j]!='.'&&f[i][j]!='O')
{
if(rec(i,j,0)>3)e=1,rec(i,j,1);
}
if(!e)break;
rep(j,w)for(int i=10;i>=0;i--)if(f[i][j]!='.')
{
int k=i+1; for(;k<12&&f[k][j]=='.';k++);
swap(f[i][j],f[k-1][j]);
}
}
printf("%d\n",ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int left;
int right;
int parent;
int key;
int l;
int r;
bool leaf;
};
vector<node> G;
vector<double> answ;
vector<int> leftb, an;
int n, k, t, root;
void dfs(int v) {
if (G[v].leaf) {
an.push_back(v);
leftb.push_back(G[v].key);
G[v].l = v;
G[v].r = v;
return;
}
dfs(G[v].left);
an.push_back(G[G[v].left].r);
leftb.push_back(G[v].key);
dfs(G[v].right);
G[v].l = G[G[v].left].l;
G[v].r = G[G[v].right].r;
}
void dfs2(int v, double l = 0.0, double s = 0.0) {
if (G[v].leaf) {
answ[v] = s / l;
return;
}
dfs2(G[v].right, l + 1.0, s + G[G[G[v].left].r].key);
dfs2(G[v].left, l + 1.0, s + G[G[G[v].right].l].key);
}
int main() {
scanf("%d", &n);
G.resize(n);
answ.resize(n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &G[i].parent, &G[i].key);
G[i].leaf = true;
}
for (int i = 0; i < n; i++)
if (G[i].parent == -1)
root = i;
else {
G[i].parent--;
G[G[i].parent].leaf = false;
if (G[i].key > G[G[i].parent].key)
G[G[i].parent].right = i;
else
G[G[i].parent].left = i;
}
leftb.push_back(-2000000000);
dfs(root);
dfs2(root);
an.push_back(*an.rbegin());
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d", &t);
int l = 0, r = leftb.size(), m;
while (r - l > 1) {
m = (r + l) / 2;
if (leftb[m] > t)
r = m;
else
l = m;
}
printf("%.9lf\n", answ[an[l]]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int main() {
double s;
while (cin >> s) {
double t;
scanf("%d%d%d", &a, &b, &c);
if (a == 0 && b == 0 && c == 0) {
printf("%.12f 0 0\n", s);
continue;
}
double sum = 1.0 * (a + b + c);
t = sum / s;
printf("%.12f %.12f %.12f\n", a / t, b / t, c / t);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x[100005] = {0}, y[100005] = {0}, xi[n + 5], yi[n + 5];
for (int i = 1; i <= n; i++) {
cin >> xi[i] >> yi[i];
x[xi[i]]++;
y[yi[i]]++;
}
for (int i = 1; i <= n; i++) {
int total = (n - 1) * 2;
int home = total / 2, away = total / 2, d;
d = x[yi[i]];
home += d;
away -= d;
cout << home << " " << away << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define INF 100000005
#define MAX 1004
int p[MAX], q[MAX*MAX];
int main(){
int n, m, ans, qq;
while(cin >> n >> m, n){
for(int i = 1;i <= n;i++)scanf("%d", &p[i]);
ans = 0;p[0] = 0;qq = 0;
for(int i = 0;i <= n;i++)
for(int j = 0;j <= n;j++)
if(p[i]+p[j] <= m)q[qq++] = p[i] + p[j];
sort(q, q+qq);
int t;
for(int i = 0;i < qq;i++){
t = upper_bound(q, q+qq, (m-q[i]))-q;
if(t-1 >= 0)ans = max(ans, q[i]+q[t-1]);
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, p, a;
cin >> n;
bool arr[n];
for (bool &b : arr) {
b = false;
}
cin >> p;
for (int i = 0; i < p; i++) {
cin >> a;
arr[a - 1] = true;
}
cin >> p;
for (int i = 0; i < p; i++) {
cin >> a;
arr[a - 1] = true;
}
for (const bool &b : arr) {
if (!b) {
cout << "Oh, my keyboard!";
return 0;
}
}
cout << "I become the guy.";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
char a[N], b[N];
int pre[N];
int main() {
scanf("%s %s", a + 1, b + 1);
int l1 = strlen(a + 1);
int l2 = strlen(b + 1);
int sum = 0;
for (int i = 1; i <= l2; i++) sum += (b[i] - '0');
sum %= 2;
for (int i = 1; i <= l1; i++) {
pre[i] = pre[i - 1] + (a[i] - '0');
}
int ans = 0;
for (int i = l2; i <= l1; i++) {
if ((pre[i] - pre[i - l2]) % 2 == sum) {
ans++;
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int>>> g;
int dp[101][101][27][2];
int dfsM(int M, int L, int c);
int dfsL(int M, int L, int c) {
if (dp[M][L][c][0] != -1) {
return dp[M][L][c][0];
}
for (auto i : g[L]) {
int u;
char w;
tie(u, w) = i;
if (w >= c && !dfsM(M, u, w)) {
dp[M][L][c][0] = 1;
return 1;
}
}
dp[M][L][c][0] = 0;
return 0;
}
int dfsM(int M, int L, int c) {
if (dp[M][L][c][1] != -1) {
return dp[M][L][c][1];
}
for (auto i : g[M]) {
int u;
char w;
tie(u, w) = i;
if (w >= c && !dfsL(u, L, w)) {
dp[M][L][c][1] = 1;
return 1;
}
}
dp[M][L][c][1] = 0;
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
g.resize(n);
for (int i = 0; i < m; i++) {
int x, y;
char w;
cin >> x >> y >> w;
x--;
y--;
g[x].push_back({y, w - 'a'});
}
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dfsM(i, j, 0)) {
cout << 'A';
} else {
cout << 'B';
}
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int du[100];
int val[100];
int main() {
int n;
cin >> n;
memset(du, 0, sizeof(du));
memset(val, 0, sizeof(val));
for (int i = 0; i < (n - 1) * n / 2 - 1; i++) {
int a, b;
scanf("%d%d", &a, &b);
du[a]++;
du[b]++;
val[a]++;
}
int s1, s2;
for (int i = 1; i <= n; i++) {
if (du[i] < n - 1) {
s1 = i;
break;
}
}
for (int i = s1 + 1; i <= n; i++) {
if (du[i] < n - 1) {
s2 = i;
break;
}
}
if (val[s2] > val[s1])
printf("%d %d\n", s2, s1);
else
printf("%d %d\n", s1, s2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
long long int max1 = 0;
string st = "";
int ct = 0;
string s = "";
for (int i = 0; i < str.length(); i++) {
if (str[i] == '0' && ct == 0) {
ct = 1;
continue;
} else
st.append(1u, str[i]);
}
if (ct == 0) {
cout << st.substr(0, st.length() - 1);
return 0;
}
for (int i = 0; i < st.length(); i++) {
if (st[i] == '0' && ct == 1)
continue;
else {
ct = 0;
s.append(1u, st[i]);
}
}
cout << s;
}
| 3 |
#include <iostream>
using namespace std;
int main() {
char g[8],c[6]={'R','Y','B','M','G','C'};
int n,m,i,j,k,s,h,ic[6],cd[6],d[30],a[6][4]={{2,3,5,4},{6,3,1,4},{2,6,5,1},{2,1,5,6},{1,3,6,4},{2,4,5,3}};
while(cin >> n) {
if (n==0) break;
for (i=0;i<30;i++) d[i]=0; h=0;
for (i=0;i<n;i++) {
for (j=0;j<6;j++) {
cin >> g;
for (k=0;k<6;k++) if (g[0]==c[k]) break;
ic[j]=k; if (k==0) s=j;
}
for (m=0,j=0;j<4;j++) { cd[j]=ic[a[s][j]-1]; if (cd[j]<cd[m]) m=j; }
d[i]=cd[m]*1000+cd[(m+1) % 4]*100+cd[(m+2) % 4]*10+cd[(m+3) % 4];
for (k=1,j=0;j<i;j++) if (d[i]==d[j]) { k=0; break;}
h+=k;
}
cout << n-h << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int num = 0;
stringstream ss;
void out(string &s) {
num++;
int l = s.size();
l = 4 - l;
for (int i = 1; i <= l; i++) ss << '0';
ss << s << ':';
}
void push0(string &s, string::iterator b) {
string::iterator k;
int nn = 0;
for (k = b + 1; k != s.end(); k++)
if (*k == ':') nn++;
nn = (8 - nn - num);
string ss;
for (int i = 1; i <= nn; i++) out(ss);
}
void work(string &s) {
string now;
string::iterator k;
for (k = s.begin(); k != s.end(); k++) {
if (*k == ':') {
if (now.empty())
push0(s, k);
else
out(now);
now.clear();
} else
now += *k;
}
}
int main() {
int N;
cin >> N;
string s, ans;
for (int i = 1; i <= N; i++) {
num = 0;
ss.clear();
cin >> s;
s += ':';
work(s);
ss >> ans;
ans.erase(ans.end() - 1);
cout << ans << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
puts("YES");
int t;
scanf("%d", &t);
while (t--) {
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%d\n", (((x1 & 1) << 1) | (y1 & 1)) + 1);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int foor[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int total;
cin >> total;
char hosp[total];
fill_n(hosp, total, 0);
cin >> hosp;
for (int k = 0; k < total; k++) {
if (hosp[k] == 'L') {
for (int d = 0; d < 10; d++) {
if (foor[d] != 1) {
foor[d] = 1;
d = 10;
}
}
}
if ((hosp[k] != 'L') && (hosp[k] != 'R')) {
foor[(hosp[k] - '0')] = 0;
}
if (hosp[k] == 'R') {
for (int p = 10; p > 0; p--) {
if (foor[p - 1] != 1) {
foor[p - 1] = 1;
p = 0;
}
}
}
}
for (int j = 0; j < 10; j++) {
cout << foor[j];
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> G[100000 + 2];
bool vis[100000 + 2];
void dfs(int u, int &cnt, vector<int> &v) {
vis[u] = 1;
v.push_back(u);
cnt++;
for (int i = 0; i < (int)G[u].size(); i++) {
if (!vis[G[u][i]]) dfs(G[u][i], cnt, v);
}
return;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
x--;
G[i].push_back(x);
}
vector<int> v;
bool enc = 0, enc2 = 0, imp = 0;
int nodo = -1, n1 = -1, n2 = -1;
for (int i = 0; i < n; i++) {
int cnt = 0;
if (vis[i]) continue;
dfs(i, cnt, v);
if (cnt % 2 == 1) imp = 1;
if (cnt == 1 && !enc) {
nodo = i;
enc = 1;
}
if ((cnt == 2) && !enc2) {
n1 = v[(int)v.size() - 1];
n2 = v[(int)v.size() - 2];
enc2 = 1;
}
}
if (enc) {
puts("YES");
for (int i = 0; i < (int)v.size(); i++)
if (v[i] != nodo) printf("%d %d\n", nodo + 1, v[i] + 1);
} else {
if (enc2) {
if (imp)
puts("NO");
else {
puts("YES");
for (int i = 0; i < (int)v.size(); i++) {
if (v[i] == n1 || v[i] == n2) continue;
if (i & 1)
printf("%d %d\n", n1 + 1, v[i] + 1);
else
printf("%d %d\n", n2 + 1, v[i] + 1);
}
printf("%d %d\n", n1 + 1, n2 + 1);
}
} else {
puts("NO");
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct matrix {
long long e[40][40];
static int sz;
matrix(bool iden = false) {
memset(e, 0, sizeof(e));
int i;
if (iden) {
for (i = 0; i < sz; i++) e[i][i] = 1;
}
}
matrix operator*(const matrix &rhs) const {
matrix r;
int i, j, k;
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
for (k = 0; k < sz; k++) {
r.e[i][j] += e[i][k] * rhs.e[k][j];
r.e[i][j] %= 1000000007;
}
}
}
return r;
}
vector<long long> operator*(const vector<long long> &v) const {
int i, j;
vector<long long> ret(sz, 0);
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
ret[i] += e[i][j] * v[j];
ret[i] %= 1000000007;
}
}
return ret;
}
matrix shift(int r) const {
matrix tmp;
int i, j;
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
tmp.e[i == sz - 1 ? sz - 1 : (i + r) % (sz - 1)]
[j == sz - 1 ? sz - 1 : (j + r) % (sz - 1)] = e[i][j];
}
}
return tmp;
}
void print() const {
int i, j;
for (i = 0; i < sz; i++) {
for (j = 0; j < sz; j++) {
printf("%d ", e[i][j]);
}
putchar('\n');
}
}
} A[100];
int matrix::sz;
int main() {
long long n, r = 1, sum = 0;
int k, i, j, w, up = 0, os;
matrix B;
scanf("%I64d%d", &n, &k);
matrix::sz = k + 1;
vector<long long> ans(k + 1, 0);
ans[k] = 1;
for (i = 0; i < k + 1; i++) {
A[0].e[0][i] = A[0].e[i][i] = 1;
}
while (n / r >= k) {
r *= k;
up++;
}
for (i = 1; i <= up; i++) {
B = A[i - 1];
A[i] = matrix(true);
j = k;
w = 0;
while (j) {
if (j & 1) {
A[i] = A[i] * B.shift((j ^ 1) << w);
}
B = B.shift(1 << w) * B;
w++;
j >>= 1;
}
}
os = 0;
while (n) {
for (i = 0; i < n / r; i++) {
ans = A[up].shift(os + i) * ans;
}
os = (os + i) % k;
n %= r;
r /= k;
up--;
}
for (i = 0; i < k + 1; i++) {
sum = (sum + ans[i]) % 1000000007;
}
printf("%I64d\n", sum);
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){int n,a; string s;a=0;cin>>n>>s;for(int i=0;i<n-2;i++)if(s.substr(i,3)=="ABC")a++;cout<<a;}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void adskiy_razgon() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int mxN = 1e+9 + 7;
const long long INF = 1e+18 + 7;
long long nod(long long a, long long b) {
if (b > a) {
swap(a, b);
}
while (b > 0) {
a %= b;
swap(a, b);
}
return a;
}
long long nok(long long a, long long b) { return a * b / nod(a, b); }
void sp(long long a, double b) { cout << fixed << setprecision(a) << b; }
long long binpow(long long a, long long n) {
if (n == 0) {
return 1;
}
if (n % 2 == 1) {
return binpow(a, n - 1) * a;
} else {
long long b = binpow(a, n / 2);
return b * b;
}
}
int x[1000000];
int y[1000000];
void solve() {
int n;
cin >> n;
int mn = mxN, mx = 0;
for (int i = 0; i < n; ++i) {
cin >> x[i] >> y[i];
mn = min(mn, x[i]);
mx = max(mx, y[i]);
}
for (int i = 0; i < n; ++i) {
if (x[i] == mn && y[i] == mx) {
cout << i + 1;
return;
}
}
cout << -1;
}
int main() {
adskiy_razgon();
long long t = 1;
for (int i = 1; i <= t; ++i) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 100010;
int n, m;
vector<int> adj[maxn];
bool vis[maxn];
int d[maxn];
int cnt[2];
void dfs(int u) {
cnt[d[u]]++;
vis[u] = 1;
for (int i = 0; i < int((adj[u]).size()); i++) {
int v = adj[u][i];
if (!vis[v]) {
d[v] = (d[u] + 1) % 2;
dfs(v);
} else if (d[u] == d[v]) {
printf("0 1");
exit(0);
}
}
}
void solve() {
memset(vis, 0, sizeof(vis));
memset(d, 0, sizeof(d));
scanf("%d%d", &n, &m);
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
long long ans = 0;
for (int i = 0; i < n; i++)
if (!vis[i]) {
cnt[0] = cnt[1] = 0;
dfs(i);
ans += 1LL * cnt[0] * (cnt[0] - 1) / 2 + 1LL * cnt[1] * (cnt[1] - 1) / 2;
}
if (ans) {
printf("1 %I64d", ans);
return;
}
for (int i = 0; i < n; i++)
if (int((adj[i]).size())) ans += n - 2;
if (ans) {
printf("2 %I64d", ans / 2);
return;
}
printf("3 %I64d", 1LL * n * (n - 1) * (n - 2) / 6);
}
int main() {
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > a;
int level[100009];
void dfs(int x, int p) {
int i;
for (i = (0); i < (a[x].size()); ++i) {
int y = a[x][i];
if (y == p) continue;
level[y] = level[x] + 1;
dfs(y, x);
}
}
int main() {
cin >> n;
int i;
a.resize(n);
for (i = (0); i < (n - 1); ++i) {
int x, y;
cin >> x >> y;
x--;
y--;
a[x].push_back(y);
a[y].push_back(x);
}
level[0] = 1;
dfs(0, -1);
double ans = 0.0;
for (i = (0); i < (n); ++i) ans += (1.0) / double(level[i]);
printf("%.9f\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int SZ = 1 << 18;
int tamt[2 * SZ];
long long tsum[2 * SZ];
void upd(int idx, int delta) {
idx += SZ;
while (idx > 0) {
if (delta > 0)
tamt[idx]++;
else
tamt[idx]--;
assert(tamt[idx] >= 0);
tsum[idx] += delta;
idx /= 2;
}
}
long long qry(int k) {
int idx = 1;
long long ret = 0;
while (idx < SZ) {
if (tamt[2 * idx + 1] <= k) {
ret += tsum[2 * idx + 1];
k -= tamt[2 * idx + 1];
idx = 2 * idx;
} else {
idx = 2 * idx + 1;
}
}
if (tamt[idx] <= k) ret += tsum[idx];
return ret;
}
pair<int, int> l[200000];
int n;
void upd(map<int, int>& m, int k, int v) {
m[k] += v;
if (m[k] == 0) m.erase(k);
}
void solve() {
cin >> n;
vector<int> allv;
for (int i = 0; i < n; i++) {
cin >> l[i].first >> l[i].second;
if (l[i].second > 0) allv.push_back(l[i].second);
}
sort(allv.begin(), allv.end());
allv.resize(unique(allv.begin(), allv.end()) - allv.begin());
map<int, int> fire, lightning;
fire[0]++;
int lamt = 0;
for (int i = 0; i < n; i++) {
int idx =
lower_bound(allv.begin(), allv.end(), abs(l[i].second)) - allv.begin();
upd(idx, l[i].second);
if (l[i].first == 0) {
if (l[i].second > 0)
upd(fire, l[i].second, 1);
else
upd(fire, -l[i].second, -1);
} else {
if (l[i].second > 0) {
upd(lightning, l[i].second, 1);
lamt++;
} else {
upd(lightning, -l[i].second, -1);
lamt--;
}
}
long long ret = tsum[1] + qry(lamt);
if (lamt == 0) {
cout << ret << "\n";
continue;
}
if (fire.rbegin()->first < lightning.begin()->first) {
ret -= lightning.begin()->first;
ret += fire.rbegin()->first;
}
cout << ret << "\n";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int readint() {
int t = 0;
char c, ch;
ch = getchar();
if (ch == '-')
c = getchar();
else
c = ch;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
t = (t << 3) + (t << 1) + c - '0';
c = getchar();
}
if (ch == '-')
return -t;
else
return t;
}
vector<vector<int> > pairs(2010);
int vis[2010], maxi = -1, f = 0;
int dfs(int x) {
int i, p = 0;
vis[x] = 1;
for (i = 0; i <= (int)pairs[x].size() - 1; i++) {
if (!vis[pairs[x][i]]) {
int a = dfs(pairs[x][i]);
p = (p) > (a) ? (p) : (a);
}
}
return 1 + p;
}
int main() {
int i, j, k, n, m, y;
cin >> n;
for (i = 1; i <= n; i++) {
int x;
cin >> x;
if (x != -1) pairs[x].push_back(i);
}
for (i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
;
y = dfs(i);
maxi = (maxi) > (y) ? (maxi) : (y);
}
cout << maxi << endl;
return 0;
}
| 3 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod
#define N 200010
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
//cout<<"paken"<<endl;
//cout<<"Paken"<<endl;
//cout<<"PAKEN"<<endl;
ll n,m;
cin>>n>>m;
cout<<n*m<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}
using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
const int MAX_N = 55;
vint graph[MAX_N];
int N, X, D;
vint m, p, l;
vint subw, subv;
void dfs(int u) {
subv[u] = 1;
subw[u] += m[u];
for(int v : graph[u]) {
dfs(v);
subv[u] += subv[v];
subw[u] += subw[v];
}
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> N >> X >> D;
resz(m, N);
resz(p, N);
resz(l, N, D);
l[0] = 1000000000;
cin >> m[0];
reps(i, 1, N) {
cin >> m[i] >> p[i];
--p[i];
graph[p[i]].push_back(i);
}
resz(subw, N);
resz(subv, N);
dfs(0);
vector<tuple<double, int, int, int> > vec;
rep(i, N) {
if(l[i] > 50) {
vec.emplace_back((double)subv[i]/subw[i], subv[i], subw[i], l[i]-50);
l[i] = 50;
}
}
sort(all(vec));
reverse(all(vec));
vint dp(50*50*50+1, LLONG_MAX/2);
dp[0] = 0;
rep(i, N) {
int lim = l[i];
for(int k = 0; lim > 0; ++k) {
int num = min(lim, 1LL<<k);
for(int j = 50*50*50-subv[i]*num; j >= 0; --j) {
if(dp[j] == LLONG_MAX/2) continue;
chmin(dp[j+subv[i]*num], dp[j]+subw[i]*num);
}
lim -= num;
}
}
/*
rep(i, 50*50+1) {
if(dp[i] < LLONG_MAX/2) cout << i << " " << dp[i] << endl;
}
*/
int ans = 0;
rep(i, 50*50*50+1) {
int tmp = i;
int x = X-dp[i];
if(x < 0) continue;
rep(j, vec.size()) {
double e;
int a, b, c;
tie(e, a, b, c) = vec[j];
if(x >= b) {
int num = min(c, x/b);
tmp += num*a;
x -= num*b;
}
}
if(tmp > ans) ans = tmp;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename type>
type beMin(type &a, type b) {
return a = (a < b ? a : b);
}
template <typename type>
type beMax(type &a, type b) {
return a = (a > b ? a : b);
}
long long read() {
long long a = 0, c = getchar(), s = 0;
while (!isdigit(c)) s |= c == '-', c = getchar();
while (isdigit(c)) a = a * 10 + c - 48, c = getchar();
return s ? -a : a;
}
const int N = 110005;
struct Matrix {
int a, b, c, d;
void echo() { printf("[%d %d]\n[%d %d]\n", a, b, c, d); }
};
const Matrix base = (Matrix){1, 0, 0, 1};
struct SegTree {
Matrix tree[N << 2];
void build(int l, int r, int t);
Matrix query(int x, int y, int l, int r, int t);
};
struct Extra {
long long pos, val;
};
long long k;
int n, m, p;
int s[N];
Matrix trans[N], totTrans;
SegTree tree;
Extra ext[N];
inline int _ad(int &a, int &b);
inline int _mu(int &a, int &b);
Matrix operator*(const Matrix &a, const Matrix &b);
Matrix operator*(const Matrix &t, const long long &k);
Matrix operator^(Matrix x, long long y);
Matrix getLine(long long l, long long r);
Matrix mkTran(int s1, int s2);
int main() {
k = read(), p = read(), n = read();
for (int i = 0; i < n; ++i) s[i] = read();
m = read();
s[n] = s[0];
for (int i = 1; i <= m; ++i) {
ext[i] = (Extra){read(), read()};
}
totTrans = base;
for (int i = 1; i <= n; ++i) {
trans[i] = mkTran(s[i], s[i - 1]);
totTrans = totTrans * trans[i];
}
tree.build(1, n, 1);
sort(ext + 1, ext + m + 1, [=](Extra a, Extra b) { return a.pos < b.pos; });
Matrix res = (Matrix){0, 1, 0, 0};
long long lastp = 0;
for (int i = 1; i <= m; ++i) {
if (ext[i].pos > k) break;
if (lastp < ext[i].pos) {
if (lastp < ext[i].pos - 1) res = res * getLine(lastp, ext[i].pos - 1);
if (ext[i - 1].pos == ext[i].pos - 1)
res = res * mkTran(ext[i].val, ext[i - 1].val);
else
res = res * mkTran(ext[i].val, s[(ext[i].pos - 1) % n]);
}
if (ext[i].pos < k) {
if (ext[i].pos + 1 == ext[i + 1].pos)
res = res * mkTran(ext[i + 1].val, ext[i].val);
else
res = res * mkTran(s[(ext[i].pos + 1) % n], ext[i].val);
}
lastp = ext[i].pos + 1;
}
printf("%d\n", (res * getLine(lastp, k)).a);
return 0;
}
inline int _ad(int a, int b) { return a + b >= p ? a + b - p : a + b; }
inline int _mu(int a, int b) { return (1ll * a * b) % p; }
Matrix operator*(const Matrix &x, const Matrix &y) {
return (Matrix){
_ad(_mu(x.a, y.a), _mu(x.b, y.c)), _ad(_mu(x.a, y.b), _mu(x.b, y.d)),
_ad(_mu(x.c, y.a), _mu(x.d, y.c)), _ad(_mu(x.c, y.b), _mu(x.d, y.d))};
}
Matrix operator*(const Matrix &t, const long long &k) {
return (Matrix){_mu(t.a, k), _mu(t.b, k), _mu(t.c, k), _mu(t.d, k)};
}
Matrix operator^(Matrix x, long long y) {
Matrix res = base;
while (y) {
if (y & 1) res = res * x;
x = x * x;
y >>= 1;
}
return res;
}
void SegTree::build(int l, int r, int t) {
int mid = (l + r) >> 1;
if (l == r) {
tree[t] = trans[mid];
return;
}
build(l, mid, t << 1), build(mid + 1, r, t << 1 | 1);
tree[t] = tree[t << 1] * tree[t << 1 | 1];
}
Matrix SegTree::query(int x, int y, int l, int r, int t) {
if (x > y || x > r || y < l) return base;
if (x <= l && r <= y) return tree[t];
int mid = (l + r) >> 1;
return query(x, y, l, mid, t << 1) * query(x, y, mid + 1, r, t << 1 | 1);
}
Matrix getLine(long long l, long long r) {
if (l > r) return base;
long long lin = l / n, rin = r / n;
if (lin == rin) return tree.query(l % n + 1, r % n, 1, n, 1);
return tree.query(l % n + 1, n, 1, n, 1) * (totTrans ^ (rin - lin - 1)) *
tree.query(1, r % n, 1, n, 1);
}
Matrix mkTran(int s1, int s2) { return (Matrix){0, s2, 1, s1}; }
| 1 |
#include<bits/stdc++.h>
#define LL long long
#define N 100005
using namespace std;
const int mo=1e9+7;
const LL e[3][3]={{2,2,1},{1,1,1},{1,0,1}};
int n,m,X[N];
LL A[3],B[3],C[3],G[3][3],H[3][3],z[3][3],f[N];
void mult(LL u[3][3],LL v[3][3])
{
int i,j,k;
for(i=0;i<3;i++)
for(j=0;j<3;j++){
z[i][j]=0;
for(k=0;k<3;k++)
z[i][j]+=u[i][k]*v[k][j];
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
u[i][j]=z[i][j]%mo;
}
int main()
{
int i,j,k,p;
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
scanf("%d",&X[i]);
X[0]=0,X[++m]=n,f[0]=B[0]=1;
for(i=1;i<=m;i++){
p=X[i]-X[i-1];
for(j=0;j<3;j++)
for(k=0;k<3;k++)
H[j][k]=e[j][k],G[j][k]=(j==k);
while(p){
if(p&1) mult(G,H);
p>>=1 , mult(H,H);
}
C[0]=(A[0]*G[0][0]+A[1]*G[1][0]+A[2]*G[2][0])%mo;
C[1]=(A[0]*G[0][1]+A[1]*G[1][1]+A[2]*G[2][1])%mo;
C[2]=(A[0]*G[0][2]+A[1]*G[1][2]+A[2]*G[2][2])%mo;
A[0]=C[0],A[1]=C[1],A[2]=C[2];
C[0]=(B[0]*G[0][0]+B[1]*G[1][0]+B[2]*G[2][0])%mo;
C[1]=(B[0]*G[0][1]+B[1]*G[1][1]+B[2]*G[2][1])%mo;
C[2]=(B[0]*G[0][2]+B[1]*G[1][2]+B[2]*G[2][2])%mo;
B[0]=C[0],B[1]=C[1],B[2]=C[2];
f[i]=(B[2]-A[2]+mo)%mo,(A[0]+=f[i])%=mo;
}
cout<<f[m];
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
const double e = 2.718281828459;
inline int sgn(double a);
inline long long gcd(long long a, long long b);
inline long long mod_pow(long long x, long long n, long long mod);
inline void Fill(int *a, int *b, int c);
inline int b_s1(int *a, int l, int r, int k);
vector<int> vv;
int main() {
double n;
double k;
cin >> n >> k;
int sum = 0;
for (int i = 0; i < (int)n; i++) {
int t1;
cin >> t1;
sum += t1;
}
double t = ceil(2 * (n * (k - 0.5) - sum));
t = max(t, 0.0);
cout << t << endl;
return 0;
}
inline int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; }
inline long long gcd(long long a, long long b) {
return a == 0 ? b : gcd(b % a, a);
}
long long mod_pow(long long x, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n = n >> 1;
}
return res;
}
int b_s1(int *a, int l, int r, int k) {
int m;
while (l < r) {
m = l + (r - l) / 2;
if (a[m] == k)
return m;
else if (a[m] < k)
l = m + 1;
else
r = m;
}
return -1;
}
void Fill(int *a, int *b, int c) {
for (int i = a - a; i < b - a; i++) {
a[i] = c;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int n;
cin>>n;
vector<int>dp(3);
for(int i=0;i<n;i++){
vector<int>new_dp(3,0);
vector<int>c(3);
for(int j=0;j<3;j++)cin>>c[j];
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(j!=k)new_dp[k]=max(new_dp[k],dp[j]+c[j]);
}
}dp=new_dp;
}cout<<max({dp[0],dp[1],dp[2]});
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, f, g, h, i, j, k;
struct Point {
int x, y;
Point() {}
Point(int x, int y) : x(x), y(y) {}
friend bool operator<(const Point &x, const Point &y) {
return x.x < y.x || (x.x == y.x && x.y < y.y);
}
friend Point operator+(const Point &x, const Point &y) {
return Point(x.x + y.x, x.y + y.y);
}
friend Point operator-(const Point &x, const Point &y) {
return Point(x.x - y.x, x.y - y.y);
}
};
Point G[200005], an[200005], M;
set<Point> GG, HH;
Point Mid(Point x, Point y) {
return Point((x.x + y.x) >> 1, (x.y + y.y) >> 1);
}
Point Mirror(Point x, Point y) {
Point kk = y - x;
return y + kk;
}
int main() {
scanf("%d%d", &a, &b);
for (i = 1; i <= a; i++) {
scanf("%d%d", &c, &d);
G[i] = Point(c << 1, d << 1);
GG.insert(G[i]);
}
sort(G + 1, G + a + 1);
if (b >= a) {
printf("-1\n");
return 0;
}
c = 0;
for (i = 1; i <= b + 1; i++)
for (j = a; j >= a - b && j >= i; j--) {
M = Mid(G[i], G[j]);
if (HH.find(M) != HH.end()) continue;
HH.insert(M);
e = 0;
for (k = i; k <= j; k++) {
e += GG.find(Mirror(G[k], M)) == GG.end();
if (e + i - 1 + a - j > b) break;
}
if (e + i - 1 + a - j <= b) {
an[++c] = M;
}
}
printf("%d\n", c);
for (i = 1; i <= c; i++)
printf("%.1lf %.1lf\n", (double)an[i].x / 2.0, (double)an[i].y / 2.0);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
G1 first;
G2 second;
G3 T;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pair<long long, long long>>> g(n);
for (int i = 0; i < m; i++) {
int x, y, t;
cin >> x >> y >> t;
--x;
--y;
g[y].push_back({t, x});
}
const int oo = 1e9;
vector<vector<bool>> s(2, vector<bool>(n));
s[0][n - 1] = s[1][n - 1] = true;
vector<vector<int>> p(2, vector<int>(n));
vector<vector<int>> d(2, vector<int>(n, oo));
d[0][n - 1] = d[1][n - 1] = 0;
vector<int> t(n), h(n, oo);
t[n - 1] = 2;
h[n - 1] = 0;
queue<pair<long long, long long>> q;
q.push({0, n - 1});
q.push({1, n - 1});
while (!q.empty()) {
pair<long long, long long> c = q.front();
q.pop();
for (auto v : g[c.second])
if (!s[v.first][v.second]) {
s[v.first][v.second] = true;
d[v.first][v.second] = h[c.second] + 1;
p[v.first][v.second] = c.second;
++t[v.second];
if (t[v.second] == 2) {
q.push({0, v.second});
q.push({1, v.second});
h[v.second] = max(d[0][v.second], d[1][v.second]);
}
}
}
vector<int> col(n, -1);
cout << (h[0] == oo ? -1 : h[0]) << '\n';
if (h[0] == oo) {
for (int i = 0; i < n; i++) col[i] = (d[1][i] == oo);
} else {
int b = 0;
while (b != n - 1) {
int a = (d[1][b] > d[0][b]);
col[b] = a;
b = p[a][b];
}
}
for (int i = 0; i < n; i++)
if (col[i] == -1) col[i] = (d[1][i] > d[0][i]);
for (int i = 0; i < n; i++) cout << col[i];
cout << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long x[n], a, b;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) {
if (i + 1 == n) {
cout << x[i] << endl;
} else
cout << x[i] + x[i + 1] << " ";
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[1005], b[1005];
cin >> a;
cin >> b;
for (int i = 0; i < strlen(a); i++) {
a[i] = tolower(a[i]);
}
for (int i = 0; i < strlen(b); i++) {
b[i] = tolower(b[i]);
}
if (strcmp(a, b) < 0) {
cout << "-1";
} else if (strcmp(a, b) > 0) {
cout << "1";
} else {
cout << "0";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long N = 2e5 + 10;
inline long long add(long long x, long long y) {
x += y;
if (x >= MOD) return x - MOD;
return x;
}
inline long long multiply(long long a, long long b) {
a %= MOD;
b %= MOD;
return (a * b) % MOD;
}
void test() {
long long n, k;
cin >> n >> k;
vector<long long> a(n), b(k);
vector<long long> maps(n + 10, 0), mark(n + 10, 0);
long long c = 0;
for (auto &e : a) {
cin >> e;
maps[e] = c;
c++;
}
for (auto &e : b) {
cin >> e;
mark[e] = 1;
}
long long ans = 1;
for (auto &e : b) {
long long id = maps[e];
c = 0;
if (id - 1 >= 0 and !mark[a[id - 1]]) {
c++;
}
if (id + 1 < n and !mark[a[id + 1]]) {
c++;
}
ans = multiply(ans, c);
mark[e] = false;
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long tt = 1;
cin >> tt;
while (tt--) {
test();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long mpow(long long a, int b) {
if (b == 0) return 1;
if (b % 2 == 1) return mpow(a, b - 1) * a % mod;
long long z = mpow(a, b / 2);
return z * z % mod;
}
int main() {
int X, Y;
scanf("%d%d", &X, &Y);
vector<long long> F(X + Y);
F[0] = 1;
for (int i = 1; i < (int) F.size(); ++i) F[i] = F[i - 1] * i % mod;
long long r = 0;
for (int j = 0; j <= X; ++j) {
int xa = j;
if ((X - xa) % 2 != 0) continue;
int ya = (X - xa) / 2;
if (xa + ya * 2 == X && xa * 2 + ya == Y) {
r += F[xa + ya] * mpow(F[xa], mod - 2) % mod * mpow(F[ya], mod - 2);
r %= mod;
}
}
printf("%lld\n", r);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct cmp {
bool operator()(int a, int b) { return a > b; }
};
priority_queue<int, vector<int>, cmp> q;
int main() {
int n, x;
long long ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
if (!q.empty() && q.top() < x) {
ans += x - q.top();
q.pop();
q.push(x);
}
q.push(x);
}
printf("%lld\n", ans);
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define ll long long
#define re register
#define ull unsigned int
using namespace std;
inline int read(){
int s=0,t=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*t;
}
const int N=105;
int n,m,K,P;
int C[N][N],p[N][N],f[2][N][N];
inline int Mod(int x){return x>=P?x-P:x;}
void Pre(int n){
for(int i=0;i<=n;i++){
C[i][0]=p[i][0]=1;
for(int j=1;j<=n;j++)p[i][j]=1ll*p[i][j-1]*i%P;
for(int j=1;j<=i;j++)C[i][j]=Mod(C[i-1][j-1]+C[i-1][j]);
}
}
void Clear(int t){
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)f[t][i][j]=0;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read(),m=read(),K=read(),P=read(),Pre(100);
int o=0;f[0][0][0]=1;
for(int t=1;t<=K;t++){
o^=1,Clear(o);
for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)if(f[o^1][i][j]){
int x=1ll*p[K-t+1][j]*p[t][m-j]%P,y=f[o^1][i][j];
for(int a=0;a+i<=n;a++,y=1ll*y*x%P)f[o][i+a][j]=Mod(f[o][i+a][j]+1ll*C[n-i][a]*y%P);
}o^=1,Clear(o);
for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)if(f[o^1][i][j]){
int x=1ll*p[K-t+1][i]*p[t][n-i]%P,y=f[o^1][i][j];
for(int b=0;b+j<=m;b++,y=1ll*y*x%P)f[o][i][j+b]=Mod(f[o][i][j+b]+1ll*C[m-j][b]*y%P);
}o^=1,Clear(o);
for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)if(f[o^1][i][j]){
int x=1ll*(P-p[K-t][j])*p[t][m-j]%P,y=f[o^1][i][j];
for(int a=0;a+i<=n;a++,y=1ll*y*x%P)f[o][i+a][j]=Mod(f[o][i+a][j]+1ll*C[n-i][a]*y%P);
}o^=1,Clear(o);
for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)if(f[o^1][i][j]){
int x=1ll*(P-p[K-t][i])*p[t][n-i]%P,y=f[o^1][i][j];
for(int b=0;b+j<=m;b++,y=1ll*y*x%P)f[o][i][j+b]=Mod(f[o][i][j+b]+1ll*C[m-j][b]*y%P);
}
}printf("%d",f[o][n][m]);
return 0;
}
/*2 2 2 998244353*/
| 0 |
#include<stdio.h>
int main(){
char s[4];
scanf("%s",s);
if(s[2]=='\0'){
printf("%s",s);
}else{
int i;
for(i=2;i>=0;i--){
printf("%c",s[i]);
}
}
return 0;
}
| 0 |
#include <cstdio>
#include <cstring>
int main()
{
char s[101];
scanf("%s", s);
const char c[2] = {'L', 'R'};
for (int i = 0; i < strlen(s); ++i)
{
if (s[i] == c[i & 1])
{
puts("No");
return 0;
}
}
puts("Yes");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n, m;
set<pair<int, int> > s;
int best[maxn];
vector<pair<int, int> > v[maxn];
vector<int> t[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int fi, se, c;
cin >> fi >> se >> c;
v[fi].push_back(pair<int, int>(se, c));
v[se].push_back(pair<int, int>(fi, c));
}
for (int i = 1; i <= n; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int mask;
cin >> mask;
t[i].push_back(mask);
}
}
for (int i = 1; i <= n; ++i) best[i] = 1000000000 + 1000000000;
best[1] = 0;
for (int i = 1; i <= n; ++i) s.insert(pair<int, int>(best[i], i));
while (s.size()) {
pair<int, int> Min = *s.begin();
s.erase(s.begin());
int ind = Min.second;
int w = Min.first;
for (int i = 0; i < t[ind].size(); ++i)
if (t[ind][i] == w) {
while (i < t[ind].size() && t[ind][i] == w) {
w++;
i++;
}
break;
}
for (int i = 0; i < v[ind].size(); ++i) {
int i1 = v[ind][i].first;
int c = v[ind][i].second;
if (best[i1] > w + c) {
s.erase(pair<int, int>(best[i1], i1));
best[i1] = w + c;
s.insert(pair<int, int>(best[i1], i1));
}
}
}
if (best[n] == 1000000000 + 1000000000)
cout << -1 << endl;
else
cout << best[n] << endl;
return 0;
}
| 2 |
#include <cstdio>
#include <cstdlib>
struct Node {
int key;
Node *p, *l, *r;
};
Node *root;
void insert(int key_) {
Node *x = root, *y = 0, *z;
z = (Node*)malloc(sizeof(Node));
z->key = key_, z->l = 0, z->r = 0;
while (x) y = x, x = (z->key < x->key) ? x->l : x->r;
z->p = y;
if (!y) root = z;
else if (z->key < y->key) y->l = z;
else y->r = z;
}
bool find(int key_) {
Node *x = root;
while (x) {
if (key_ < x->key) x = x->l;
else if (key_ > x->key) x = x->r;
else return true;
}
return false;
}
void walk1(Node *x) { if (!x) return; walk1(x->l); printf(" %d", x->key); walk1(x->r); }
void walk2(Node *x) { if (!x) return; printf(" %d", x->key); walk2(x->l); walk2(x->r); }
int n, v; char c[8];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", &c);
if (c[0] == 'i') scanf("%d", &v), insert(v);
else if (c[0] == 'f') scanf("%d", &v), printf("%s\n", find(v) ? "yes" : "no");
else walk1(root), printf("\n"), walk2(root), printf("\n");
}
}
| 0 |
#include <stdio.h>
#include <math.h>
int main(void)
{
double a,b,c;
double h,S,L;
scanf("%lf %lf %lf", &a,&b,&c);
c = (c*M_PI)/180;
h = b*sin(c);
S = h*a/2;
L = a + b + sqrt(a*a + b*b - 2*a*b*cos(c));
printf("%lf\n", S);
printf("%lf\n", L);
printf("%lf\n", h);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void no() {
cout << "No";
exit(0);
}
vector<vector<int64_t> > v;
vector<int64_t> bfs(int64_t u) {
queue<int64_t> q;
vector<int64_t> ch(v.size(), 0);
q.push(u);
ch[u] = 1;
while (q.size()) {
int64_t t = q.front();
q.pop();
for (int64_t j = 0; j < v[t].size(); ++j) {
int64_t p = v[t][j];
if (ch[p]) continue;
ch[p] = ch[t] + 1;
q.push(p);
}
}
return ch;
}
int32_t main() {
ios_base::sync_with_stdio(false);
int64_t n, k;
cin >> n >> k;
if (n < 4) no();
v.resize(n);
for (int64_t i = 0; i < n - 1; ++i) {
int64_t a, b;
cin >> a >> b;
--a;
--b;
v[a].push_back(b);
v[b].push_back(a);
}
int64_t o = -1;
for (int64_t i = 0; i < n; ++i)
if (v[i].size() == 1) {
vector<int64_t> ch = bfs(i);
int64_t mx = 0, mxi = 0;
for (int64_t i = 0; i < n; ++i)
if (ch[i] > mx) {
mx = ch[i];
mxi = i;
}
vector<int64_t> ch1 = bfs(mxi);
if (mx % 2 == 0) no();
mx = mx / 2 + 1;
for (int64_t i = 0; i < n; ++i)
if (ch[i] == mx && ch1[i] == mx) {
if (o == -1)
o = i;
else
no();
}
if (o == -1) no();
break;
}
vector<int64_t> ch = bfs(o);
for (int64_t i = 0; i < n; ++i) {
if (ch[i] - 1 > k) no();
if (v[i].size() == 1) {
if (ch[i] - 1 != k) no();
}
if (v[i].size() == 2) no();
if (v[i].size() == 3 && i != o) no();
}
cout << "Yes";
}
| 5 |
#include <stdio.h>
int main(void)
{
int n, num;
while (scanf("%d", &n) != EOF){
num = (n + 1) * n / 2 + 1;
printf("%d\n", num);
}
return (0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long R = 7 + 1e9, R1 = 19491001, R2 = 236, NUMTESTCASE;
const long long NN = 10 + 1e6;
const double pi = acos(-1.0);
int di[8] = {1, 0, -1, 0, 1, -1, 1, -1}, dj[8] = {0, 1, 0, -1, 1, -1, -1, 1};
bool Check(string &a, string &b) {
if (a.size() != b.size()) return false;
int a_ = 0, b_ = 0;
for (int i = (0); i <= (a.size() - 1); ++i)
a_ += (a[i] == '1'), b_ += (b[i] == '1');
if (min(a_, b_) == 0 && max(a_, b_) != 0) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string a, b;
cin >> a >> b;
if (Check(a, b))
cout << "YES";
else
cout << "NO";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char s[100], s1[100];
void sol() {
int i, j, l;
int vt, vt1, xp;
l = strlen(s);
memset(s1, 0, sizeof(s1));
if (s[0] == 'h') {
strcat(s1, "http://");
vt = 4;
xp = 6;
}
if (s[0] == 'f') {
strcat(s1, "ftp://");
vt = 3;
xp = 5;
}
for (i = vt; i < l - 1; i++) {
if (s[i] == 'r' && s[i + 1] == 'u') {
vt1 = i;
if (vt1 != vt) break;
}
}
for (i = vt; i < vt1; i++) {
xp++;
s1[xp] = s[i];
}
xp += 3;
strcat(s1, ".ru");
if (vt1 + 2 < l) {
xp++;
s1[xp] = '/';
for (i = vt1 + 2; i < l; i++) {
xp++;
s1[xp] = s[i];
}
}
printf("%s\n", s1);
}
int main() {
while (gets(s)) sol();
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int C[12],A[12][12];
int main() {
int N,M,X; cin>>N>>M>>X;
for (int i=0; i<N; i++) {
cin>>C[i];
for (int j=0; j<M; j++) cin>>A[i][j];
}
int ans=1e8;
for (int i=0; i<1<<N; i++) {
int now[12]={};
int sum=0;
for (int j=0; j<N; j++) {
if (i>>j&1) {
sum+=C[j];
for (int k=0; k<M; k++) now[k] += A[j][k];
}
}
bool flag=true;
for (int j=0; j<M; j++) if (now[j]<X) flag=false;
if (flag&&ans > sum) ans=sum;
}
cout<<(ans>1e7?-1:ans)<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000009;
long long pw(long long a, long long deg) {
if (deg == 0) return 1;
if (deg & 1) return (a * pw(a, deg - 1)) % MOD;
return pw((a * a) % MOD, deg / 2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, k;
cin >> n >> m >> k;
long long shit = n / k;
long long izi = n - shit;
long long kek = m - izi;
if (kek < 0) kek = 0;
long long ans = pw(2, kek + 1);
ans += MOD - 2;
ans %= MOD;
ans = (ans * k) % MOD;
ans += m - kek * k;
ans %= MOD;
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long i, j, k, T, n, m, u, v, p, q, r, s, d;
string a;
cin >> a;
long long ans = -1;
n = a.length();
p = -1;
q = -1;
r = -1;
s = -1;
for (i = 0; i < n; i++)
if (a[i] == '[') {
p = i;
break;
}
for (i = p; i < n; i++)
if (a[i] == ':') {
q = i;
break;
}
for (i = n - 1; i >= 0; i--)
if (a[i] == ']') {
s = i;
break;
}
for (i = s; i >= 0; i--)
if (a[i] == ':') {
r = i;
break;
}
if (p != -1 and q != -1 and r != -1 and s != -1 and p < q and q < r and
r < s) {
ans = 4;
for (i = q; i < r; i++)
if (a[i] == '|') ++ans;
}
cout << ans;
}
| 2 |
//PCK2018予選問題
#include "bits/stdc++.h"
using namespace std;
void math(int x){
cout << (x-30)/2 << endl;
}
int main(){
int f;
cin >> f;
math(f);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 1e5 + 1;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
string s;
cin >> s;
vector<long long> count;
long long cur = 0, i = 0, ans = 1, sum = 0, n = s.size();
while (i < n) {
while (i < n and s[i] != 'b') {
cur += (s[i] == 'a');
i++;
}
if (cur) count.push_back(cur);
cur = 0;
i++;
}
if (count.size()) {
for (long long i : count) ans = (ans * (i + 1)) % mod;
ans--;
} else
ans = 0;
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char neck[(int)1e6 + 128];
long long poww[(int)1e6 + 128];
long long Hash[(int)1e6 + 128];
int cf[(int)1e6 + 128];
const long long MOD = 1907258937;
const int bid = 31;
inline long long geth(int l, int r) {
l--;
return (Hash[r] - Hash[l] * poww[r - l] % MOD + MOD) % MOD;
}
int main() {
int len, T;
scanf("%d%d", &len, &T);
scanf("%s", neck + 1);
poww[0] = 1;
for (int i = 1; i <= len; ++i) {
poww[i] = poww[i - 1] * bid % MOD;
Hash[i] = Hash[i - 1] * bid % MOD + neck[i] - 'a';
}
for (int i = 1; i * T <= len; ++i) {
long long now = geth(1, i);
bool flag = true;
for (int j = 1; j < T; ++j) {
if (geth(1 + i * j, i + i * j) != now) {
flag = false;
break;
}
}
if (!flag) continue;
++cf[i * T];
int l = 1 + i * T, r = min(i + i * T, len) + 1;
int rep = i * T;
while (l < r) {
int mid = (l + r) >> 1;
if (geth(1, mid - i * T) == geth(1 + i * T, mid))
l = mid + 1, rep = mid;
else
r = mid;
}
--cf[rep + 1];
}
int val(0);
for (int i = 1; i <= len; ++i) {
val += cf[i];
putchar('0' + (val > 0));
}
putchar('\n');
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, a[100005], b[100005], rec[100005];
cin >> n;
string s1, s2;
cin >> s1 >> s2;
long long int n0 = 0, n1 = 0, n2 = 0, n3 = 0;
for (i = 0; i < n; i++) {
if (s1[i] == '0')
a[i] = 0;
else
a[i] = 1;
}
for (i = 0; i < n; i++) {
if (s2[i] == '0')
b[i] = 0;
else
b[i] = 1;
}
for (i = 0; i < n; i++) {
if (a[i] == b[i] && a[i] == 0)
n0++;
else if (a[i] == b[i] && a[i] == 1)
n3++;
else if (a[i] == 1 && b[i] == 0)
n2++;
else
n1++;
}
long long int ans = 0;
ans = (n0 * n2) + (n0 * n3);
ans += (n1 * n2);
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long BIT[2][555555];
vector<int> lev[555555], g[555555];
int chainParent[555555], chainHead[555555], ptr;
int chainNo[555555], pos[555555], sub[555555], nchain, dep[555555];
long long res[555555];
inline void update(int t, int x, int v) {
for (; x < 555555; x += x ^ (x & (x - 1))) BIT[t][x] += v * 1LL;
}
inline void update(int l, int r) {
update(0, l, 1);
update(0, r + 1, -1);
update(1, l, l - 1);
update(1, r + 1, -r);
}
inline long long query(int t, int x) {
long long ret = 0;
for (; x; x = (x & (x - 1))) ret += BIT[t][x] * 1LL;
return ret;
}
inline long long query(int x) { return query(0, x) * x - query(1, x); }
inline long long range_query(int l, int r) { return query(r) - query(l - 1); }
inline void dfs(int v, int p) {
dep[v] = dep[p] + 1;
lev[dep[v]].emplace_back(v);
sub[v] = 1;
for (auto u : g[v]) {
if (u == p) continue;
dfs(u, v);
sub[v] += sub[u];
}
}
inline void hld(int cur, int p) {
pos[cur] = ++ptr;
chainNo[cur] = nchain;
int heavy = -1, mx = 0;
for (auto u : g[cur]) {
if (u == p) continue;
if (heavy == -1 || sub[u] > mx) {
heavy = u;
mx = sub[u];
}
}
if (heavy == -1) return;
hld(heavy, cur);
for (auto u : g[cur]) {
if (u == p || u == heavy) continue;
nchain++;
chainParent[nchain] = cur;
chainHead[nchain] = u;
hld(u, cur);
}
}
inline long long query_up(int u, int v) {
long long ret = 0;
while (chainNo[u] != chainNo[v]) {
int no = chainNo[u];
ret += range_query(pos[chainHead[no]], pos[u]);
u = chainParent[no];
}
ret += range_query(pos[v], pos[u]);
return ret;
}
inline void update_up(int u, int p) {
while (chainNo[u] != chainNo[p]) {
int no = chainNo[u];
update(pos[chainHead[no]], pos[u]);
u = chainParent[no];
}
update(pos[p], pos[u]);
}
inline void init(int r) {
ptr = nchain = 0;
memset(BIT, 0, sizeof(BIT));
;
dep[r] = 0;
chainHead[nchain] = chainParent[nchain] = r;
dfs(r, r);
hld(r, r);
}
int main() {
int n;
scanf("%d", &n);
;
int root;
for (int x = 1; x <= n; x++) {
int u = x;
int v;
scanf("%d", &v);
;
if (v == 0)
root = x;
else {
g[u].emplace_back(v);
g[v].emplace_back(u);
}
}
init(root);
for (int x = 1; x <= n; x++) {
for (auto i : lev[x]) update_up(i, root);
for (auto i : lev[x]) res[i] = query_up(i, root) - x;
}
for (int x = 1; x <= n; x++) printf("%lld ", res[x]);
puts("");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
int main() {
while (cin >> n) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
if (n % 2) {
printf("%s\n", "Conan");
continue;
}
int ok = 1;
for (int i = 0; i <= n - 2; i += 2) {
if (a[i] != a[i + 1]) {
ok = 0;
break;
}
}
if (ok)
cout << "Agasa" << endl;
else
cout << "Conan" << endl;
}
return 0;
}
| 2 |
#include <iostream>
using namespace std;
int main()
{
int i,j,k,W,H;
while(1)
{
cin >> H >> W;
if(H==0||W==0) break;
for(j=0;j<H;j++)
{
for(k=0;k<W;k++)
{
cout << "#";
}
cout << endl;
}
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)
const int SZ = 2e5 + 5;
const int mod = 1e9 + 7;
int n,a[SZ];LL fac[SZ];
LL sum(vector<int> &v){return accumulate(v.begin(),v.end(),0LL);}
void dbg(){puts("");}
template<typename T, typename... R>void dbg(const T &f, const R &... r) {
cout << f << " ";
dbg(r...);
}
template<typename Type>inline void read(Type &xx){
Type f = 1;char ch;xx = 0;
for(ch = getchar();ch < '0' || ch > '9';ch = getchar()) if(ch == '-') f = -1;
for(;ch >= '0' && ch <= '9';ch = getchar()) xx = xx * 10 + ch - '0';
xx *= f;
}
bool jdg(int x){
rep(i,1,n) if((x & a[i]) != x) return false;
return true;
}
int main(int argc, char** argv) {
fac[0] = 1;rep(i,1,SZ-5) fac[i] = fac[i-1] * i % mod;
int T;read(T);
while(T--){
read(n);
rep(i,1,n) read(a[i]);
int x = *min_element(a+1,a+n+1);
if(!jdg(x)) puts("0");
else{
int u = 0;
rep(i,1,n) if(a[i] == x) ++u;
printf("%lld\n",fac[n-2]*(1LL*u*(u-1)%mod)%mod);
}
}
return 0;
}
| 2 |
#include "bits/stdc++.h"
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
using namespace std;
/* ??????????????¬ */
#include <complex>
typedef long double ld;
typedef complex<ld> Point;//????´???°
const ld eps = 1e-9, pi = acos(-1.0);
namespace std {
bool operator<(const Point &lhs, const Point &rhs) {
if (lhs.real() < rhs.real() - eps) return true;
if (lhs.real() > rhs.real() + eps) return false;
return lhs.imag() < rhs.imag();
}
}
// ????????\???
Point input_point() {
ld x, y;
cin >> x >> y;
return Point(x, y);
}
// ????????????????????????
bool eq(ld a, ld b) {
return (abs(a - b) < eps);
}
// ??????
ld dot(Point a, Point b) {
return real(conj(a) * b);//conj::??±???????´???°
}
// ??????
ld cross(Point a, Point b) {
return imag(conj(a) * b);
}
// ??´????????????
class Line {
public:
Point a, b;
Line() : a(Point(0, 0)), b(Point(0, 0)) {}
Line(Point a, Point b) : a(a), b(b) {}
};
/*int main(){
Point a(1,1),b(2,2);
Line l(a,b);
}*/
// ????????????
class Circle {
public:
Point p;
ld r;
Circle() : p(Point(0, 0)), r(0) {}
Circle(Point p, ld r) : p(p), r(r) {}
};
// CCW::counter clockwise
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b, c) > eps) return 1; // a,b,c??????????¨???¨?????????????????¶
if (cross(b, c) < -eps) return -1; // a,b,c???????¨???¨?????????????????¶
if (dot(b, c) < 0) return 2; // c,a,b???????????´???????????¶
if (norm(b) < norm(c)) return -2; // a,b,c???????????´???????????¶
return 0; // a,c,b???????????´???????????¶
}
/* ???????????? */
// ??´?????¨??´??????????????????
//l::??´??????s::??????
bool isis_ll(Line l, Line m) {
return !eq(cross(l.b - l.a, m.b - m.a), 0);
}
// ??´?????¨????????????????????? *slide??????
bool isis_ls(Line l, Line s) {
return isis_ll(l, s) &&
(cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps);
}
// ????????¨?????????????????????
bool isis_ss(Line s, Line t) {
return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 &&
ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}
// ????????´????????????
bool isis_lp(Line l, Point p) {
return (abs(cross(l.b - p, l.a - p)) < eps);
}
// ?????????????????????
bool isis_sp(Line s, Point p) {
return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps);
}
// ??????????¶?
Point proj(Line l, Point p) {
ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
//norm::??¶?????????2???
return l.a + t * (l.a - l.b);
}
// ??´?????¨??´????????????
Point is_ll(Line s, Line t) {
Point sv = s.b - s.a, tv = t.b - t.a;
assert(cross(sv, tv) != 0);
return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv);
}
// ??´?????¨???????????¢
ld dist_lp(Line l, Point p) {
return abs(p - proj(l, p));
}
// ??´?????¨??´???????????¢
ld dist_ll(Line l, Line m) {
return isis_ll(l, m) ? 0 : dist_lp(l, m.a);
}
// ??´?????¨??????????????¢
ld dist_ls(Line l, Line s) {
return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b));
}
// ????????¨???????????¢
ld dist_sp(Line s, Point p) {
Point r = proj(s, p);
return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p));
}
// ????????¨??????????????¢
ld dist_ss(Line s, Line t) {
if (isis_ss(s, t)) return 0;
return min({ dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b) });
}
/* ??? */
// ?????¨????????????
vector<Point> is_cc(Circle c1, Circle c2) {
vector<Point> res;
ld d = abs(c1.p - c2.p);
ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d);
ld dfr = c1.r * c1.r - rc * rc;
if (abs(dfr) < eps) dfr = 0.0;
else if (dfr < 0.0) return res; // no intersection
ld rs = sqrt(dfr);
Point diff = (c2.p - c1.p) / d;
res.push_back(c1.p + diff * Point(rc, rs));
if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs));
return res;
}
// ?????¨??´????????????
vector<Point> is_lc(Circle c, Line l) {
vector<Point> res;
ld d = dist_lp(l, c.p);
if (d < c.r + eps) {
ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); //safety;
Point nor = (l.a - l.b) / abs(l.a - l.b);
res.push_back(proj(l, c.p) + len * nor);
res.push_back(proj(l, c.p) - len * nor);
}
return res;
}
// ?????¨??????????????¢
vector<Point> is_sc(Circle c, Line l) {
vector<Point> v = is_lc(c, l), res;
for (Point p : v)
if (isis_sp(l, p)) res.push_back(p);
return res;
}
// ?????¨????????\???
vector<Line> tangent_cp(Circle c, Point p) {
vector<Line> ret;
Point v = c.p - p;
ld d = abs(v);
ld l = sqrt(norm(v) - c.r * c.r);
if (isnan(l)) { return ret; }
Point v1 = v * Point(l / d, c.r / d);
Point v2 = v * Point(l / d, -c.r / d);
ret.push_back(Line(p, p + v1));
if (l < eps) return ret;
ret.push_back(Line(p, p + v2));
return ret;
}
// ?????¨????????\???
vector<Line> tangent_cc(Circle c1, Circle c2) {
vector<Line> ret;
if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) {
Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r);
ret = tangent_cp(c1, center);
}
if (abs(c1.r - c2.r) > eps) {
Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r);
vector<Line> nret = tangent_cp(c1, out);
ret.insert(ret.end(), ALL(nret));
}
else {
Point v = c2.p - c1.p;
v /= abs(v);
Point q1 = c1.p + v * Point(0, 1) * c1.r;
Point q2 = c1.p + v * Point(0, -1) * c1.r;
ret.push_back(Line(q1, q1 + v));
ret.push_back(Line(q2, q2 + v));
}
return ret;
}
/* ????§???¢ */
typedef vector<Point> Polygon;
// ??¢???
ld area(const Polygon &p) {
ld res = 0;
int n = p.size();
REP(j, n) res += cross(p[j], p[(j + 1) % n]);
return res / 2;
}
// ????§???¢????????¢??????
bool is_counter_clockwise(const Polygon &poly) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n];
angle += arg((c - b) / (b - a));
}
return angle > eps;
}
// ??????????????????
// -1 => out
// 0 => on
// 1 => in
int is_in_polygon(const Polygon &poly, Point p) {
ld angle = 0;
int n = poly.size();
REP(i, n) {
Point a = poly[i], b = poly[(i + 1) % n];
if (isis_sp(Line(a, b), p)) return 1;
angle += arg((b - p) / (a - p));
}
return eq(angle, 0) ? 0 : 2;
}
// ??????
Polygon convex_hull(vector<Point> ps) {
int n = ps.size();
int k = 0;
sort(ps.begin(), ps.end());
Polygon ch(2 * n);
for (int i = 0; i < n; ch[k++] = ps[i++])
while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;
for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--])
while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k;
ch.resize(k - 1);
return ch;
}
// ????????????
Polygon convex_cut(const Polygon &ps, Line l) {
int n = ps.size();
Polygon Q;
REP(i, n) {
Point A = ps[i], B = ps[(i + 1) % n];
Line m = Line(A, B);
if (ccw(l.a, l.b, A) != -1) Q.push_back(A);
if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m))
Q.push_back(is_ll(l, m));
}
return Q;
}
typedef pair<ld, ld> P;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;cin>>n;
vector<P> v(n);
for(int i=0;i<n;i++){
ld x,y;cin>>x>>y;
v[i]=P(x,y);
}
ld ma=0;
P mid;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
ld len= (v[i].first-v[j].first)*(v[i].first-v[j].first)+(v[i].second-v[j].second)*(v[i].second-v[j].second);
if(len>ma){
ma=len;
mid= P((v[i].first+v[j].first)/2,(v[i].second+v[j].second)/2);
}
}
}
// cout<<mid.first<<" "<<mid.second<<endl;
// cout<<ma<<endl;
if(ma==0) cout<<"NA"<<endl;
else{
for(int i=0;i<n;i++){
bool flag=false;
for(int j=0;j<n;j++){
P mi=P((v[i].first+v[j].first)/2,(v[i].second+v[j].second)/2);
if(eq(mi.first,mid.first)&&eq(mi.second,mid.second)){
flag=true;break;
}
}
if(!flag){
cout<<"NA"<<endl;
return 0;
}
}
cout<<fixed<<setprecision(10)<<mid.first<<" "<<mid.second<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return (a.second < b.second);
}
bool check(string a, string cc) {
bool ok = 1;
for (long long int i = 0; i < 7; i++) {
if (a[i] == '?') continue;
if (a[i] != cc[i]) {
ok = 0;
break;
}
}
return ok;
}
void solve() {
long long int n;
cin >> n;
string s;
cin >> s;
string cc = "abacaba";
bool ok = 0;
long long int ans = 0;
for (long long int i = 0; i < n - 6; i++) {
if (s.substr(i, 7) == cc) {
ans++;
}
}
if (ans > 1) {
cout << "No" << endl;
return;
} else if (ans == 1) {
replace((s).begin(), (s).end(), '?', 'd');
cout << "Yes" << endl;
cout << s << endl;
return;
}
for (long long int i = 0; i < n - 6; i++) {
string ss = s;
string str = ss.substr(i, 7);
bool ok = 1;
if (check(str, cc)) {
for (long long int j = i; j < i + 7; j++) {
ss[j] = cc[j - i];
}
for (long long int j = i + 1; j < min(i + 7, n - 6); j++) {
string str = ss.substr(j, 7);
if (str == cc) {
ok = 0;
break;
}
}
if (ok) {
long long int ans = 0;
for (long long int i = 0; i < n - 6; i++) {
if (s.substr(i, 7) == cc) {
ans++;
}
}
if (ans != 1) continue;
replace((ss).begin(), (ss).end(), '?', 'd');
cout << "Yes" << endl;
cout << ss << endl;
return;
}
}
}
reverse((s).begin(), (s).end());
for (long long int i = 0; i < n - 6; i++) {
string ss = s;
string str = ss.substr(i, 7);
bool ok = 1;
if (check(str, cc)) {
for (long long int j = i; j < i + 7; j++) {
ss[j] = cc[j - i];
}
for (long long int j = i + 1; j < min(i + 7, n - 6); j++) {
string str = ss.substr(j, 7);
if (str == cc) {
ok = 0;
break;
}
}
if (ok) {
long long int ans = 0;
for (long long int i = 0; i < n - 6; i++) {
if (ss.substr(i, 7) == cc) {
ans++;
}
}
if (ans != 1) continue;
replace((ss).begin(), (ss).end(), '?', 'd');
cout << "Yes" << endl;
reverse((ss).begin(), (ss).end());
cout << ss << endl;
return;
}
}
}
cout << "No" << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
;
;
long long int t = 1;
cin >> t;
while (t--) {
solve();
}
}
| 1 |
#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int n,r[]={5,3,3,3,3,3,4,3,4};
char c[][5]={{},{'.',',','!','?',' '},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
string s;
int main(){
cin>>n;
while(n--){
cin>>s;
int cur1=0,cur2=0;
rep(i,s.size()){
int m=s[i]-'0';
if(!m){
if(cur1){cout<<c[cur1][cur2];cur1=0;}
}
else{
if(!cur1){cur1=m;cur2=0;}
else cur2=(cur2+1)%r[cur1-1];
}
}
cout<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[1000];
double findarea(pair<int, int> a, pair<int, int> b, pair<int, int> c) {
return ((double)a.first * (b.second - c.second) +
(double)b.first * (c.second - a.second) +
(double)c.first * (a.second - b.second)) /
2.0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d %d", &a[i].first, &a[i].second);
double ans = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
double maxright = -1, maxleft = -1;
for (int k = 0; k < n; k++) {
if (k != i && k != j) {
double area = findarea(a[i], a[j], a[k]);
if (area < 0)
maxleft = max(maxleft, -area);
else
maxright = max(maxright, area);
}
}
if (maxright >= 0 && maxleft >= 0) ans = max(ans, maxleft + maxright);
}
printf("%.6lf\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1.e18 + 2;
const long long mod = 1000006;
const int MAXN = 200005;
using namespace std;
int n, dif;
vector<int> seq;
bool ctrl[MAXN];
bool ver(int ind1, int ind2) {
set<int> myS;
if (ind1 % 2) {
if (seq[ind1] > seq[ind1 - 1]) {
if (ctrl[ind1]) myS.insert(ind1);
if (ctrl[ind1 - 1]) myS.insert(ind1 - 1);
} else
return false;
if (ind1 < seq.size() - 1 && seq[ind1] > seq[ind1 + 1]) {
if (ctrl[ind1]) myS.insert(ind1);
if (ctrl[ind1 + 1]) myS.insert(ind1 + 1);
} else if (ind1 < seq.size() - 1)
return false;
} else {
if (ind1 > 0 && seq[ind1] < seq[ind1 - 1]) {
if (ctrl[ind1]) myS.insert(ind1);
if (ctrl[ind1 - 1]) myS.insert(ind1 - 1);
} else if (ind1 > 0)
return false;
if (ind1 < seq.size() - 1 && seq[ind1] < seq[ind1 + 1]) {
if (ctrl[ind1]) myS.insert(ind1);
if (ctrl[ind1 + 1]) myS.insert(ind1 + 1);
} else if (ind1 < seq.size() - 1)
return false;
}
if (ind2 % 2) {
if (seq[ind2] > seq[ind2 - 1]) {
if (ctrl[ind2]) myS.insert(ind2);
if (ctrl[ind2 - 1]) myS.insert(ind2 - 1);
} else
return false;
if (ind2 < seq.size() - 1 && seq[ind2] > seq[ind2 + 1]) {
if (ctrl[ind2]) myS.insert(ind2);
if (ctrl[ind2 + 1]) myS.insert(ind2 + 1);
} else if (ind2 < seq.size() - 1)
return false;
} else {
if (ind2 > 0 && seq[ind2] < seq[ind2 - 1]) {
if (ctrl[ind2]) myS.insert(ind2);
if (ctrl[ind2 - 1]) myS.insert(ind2 - 1);
} else if (ind2 > 0)
return false;
if (ind2 < seq.size() - 1 && seq[ind2] < seq[ind2 + 1]) {
if (ctrl[ind2]) myS.insert(ind2);
if (ctrl[ind2 + 1]) myS.insert(ind2 + 1);
} else if (ind2 < seq.size() - 1)
return false;
}
return myS.size() == dif;
}
int f(int ind) {
int acm = 0;
for (int i = 0; i < seq.size(); i++) {
if (i == ind) continue;
int aux = seq[ind];
seq[ind] = seq[i];
seq[i] = aux;
if (ver(ind, i)) {
acm++;
if (!ctrl[i]) acm++;
}
aux = seq[ind];
seq[ind] = seq[i];
seq[i] = aux;
}
return acm;
}
int main() {
cin >> n;
seq = vector<int>(n);
for (int x, i = 0; i < n; i++) {
scanf("%d", &seq[i]);
}
memset(ctrl, false, sizeof(ctrl));
int acm = 0;
for (int i = 0; i < seq.size() - 1; i++) {
if (i % 2 && seq[i] <= seq[i + 1]) {
if (!ctrl[i]) {
ctrl[i] = true;
acm++;
}
if (!ctrl[i + 1]) {
ctrl[i + 1] = true;
acm++;
}
} else if (!(i % 2) && seq[i] >= seq[i + 1]) {
if (!ctrl[i]) {
ctrl[i] = true;
acm++;
}
if (!ctrl[i + 1]) {
ctrl[i + 1] = true;
acm++;
}
}
}
if (acm > 6) return cout << "0" << endl, 0;
int res = 0;
dif = acm;
for (int i = 0; i < seq.size(); i++) {
if (ctrl[i]) res += f(i);
}
cout << res / 2 << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
const long size = 2e+5;
vector<long long> v(size, 0), dp(size, 0);
long n;
cin >> n;
long a, _min = size;
for (long i = 0; i < n; ++i) {
cin >> a;
++v[a];
}
dp[1] = v[1];
for (long i = 2; i < size; ++i) {
dp[i] = max(dp[i - 2] + v[i] * i, dp[i - 1]);
}
cout << dp[size - 1] << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
long long mod(long long x) {
if (x < 0) return -x;
return x;
}
long long expo(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y % 2 == 1) res = (res * x);
y = y >> 1;
x = x * x;
}
return res;
}
using namespace std;
const int MAXN = 500005;
vector<long long> v;
long long a[1000000], b[1000000], c[MAXN], d[MAXN];
int main() {
ios_base::sync_with_stdio(false);
long long n, t, m, k, x = 0, y = 0;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> a[i] >> b[i];
if (a[i] > b[i]) {
x++;
} else {
y++;
}
}
set<pair<long long, long long>> s;
if (x > y) {
k = 0;
for (long long i = 0; i < n; i++) {
if (a[i] > b[i]) {
c[k] = a[i];
d[k] = b[i];
k++;
pair<long long, long long> p = make_pair(d[k - 1], i);
s.insert(p);
}
}
} else {
k = 0;
for (long long i = 0; i < n; i++) {
if (a[i] < b[i]) {
c[k] = a[i];
d[k] = b[i];
k++;
pair<long long, long long> p = make_pair(d[k - 1], i);
s.insert(p);
}
}
}
for (set<pair<long long, long long>>::iterator it = s.begin(); it != s.end();
it++) {
pair<long long, long long> p = *it;
v.push_back(p.second);
}
cout << int(v.size()) << endl;
long long i;
if (x > y) {
for (long long i = 0; i < v.size(); i++) {
cout << v[i] + 1 << " ";
}
return 0;
}
for (long long i = v.size() - 1; i >= 0; i--) {
cout << v[i] + 1 << " ";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 22;
int dp[1 << N];
void print_binary(int n) {
if (!n) return;
print_binary(n >> 1);
putchar('0' + (n & 1));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
memset(dp, inf, sizeof dp);
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
dp[a[i]] = min(dp[a[i]], a[i]);
}
for (int i = 0; i < N; ++i) {
for (int mask = 0; mask < (1 << N); ++mask) {
if (mask & (1 << i)) {
dp[mask] = min(dp[mask], dp[mask ^ (1 << i)]);
}
}
}
int tot = (1 << N) - 1;
for (int i = 0; i < n; ++i) {
int other = (((1 << N) - 1) ^ a[i]);
cout << (dp[other] == inf ? -1 : dp[other]) << " ";
}
cout << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int main()
{
ll n , m ;
cin >> n >> m ;
vector<vector<ll>> g(n);
rep(i, m)
{
ll a, b;
cin >> a >> b;
g[a - 1].push_back(b - 1);
g[b - 1].push_back(a - 1); //無向辺
}
rep(i,n){
if(g[i].size()%2!=0){
cout << "No" << endl;
return 0;
}
}
ll f=0;
rep(i,n){
if(g[i].size()>=6){
cout << "Yes" << endl;
return 0;
}
if(g[i].size()==4)f++;
}
if(f<2){
cout << "No" << endl;
return 0;
}
if(f>2){
cout << "Yes" << endl;
return 0;
}
vector<ll> t;
ll v;
rep(i,n){
if(g[i].size()==4)v=i;
}
vector<ll> see(n);
see[v]=1;
queue<ll> q;
rep(i,4){
q.push(g[v][i]);
}
while(!q.empty()){
ll p=q.front();
q.pop();
if(g[p].size()==4){
t.push_back(p);
continue;
}
see[p]=1;
rep(i,2){
if(see[g[p][i]]==0)q.push(g[p][i]);
}
}
if(t.size()==2){
cout << "Yes" << endl;
return 0;
}
else{
cout << "No" << endl;
return 0;
}
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.