solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
int n;
cin >> n;
double a[103];
for (int i = 0; i < n; i++) scanf("%lf", &a[i]);
sort(a, a + n);
double ans = 0.0, curr = 1.0;
for (int i = n - 1; i >= 0; i--) {
if (ans < ((ans * (1.00 - a[i])) + (curr * (a[i])))) {
ans = (double)ans * (1.00 - a[i]) + curr * a[i];
}
curr = curr * (1.00 - a[i]);
}
printf("%.10lf", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int x[300005];
int y[300005];
int z[300005];
long long FT[4][300005];
vector<int> g[300005];
void add(int t, int idx, long long amt) {
while (idx < 300005) {
FT[t][idx] += amt;
idx += idx & -idx;
}
}
long long sum(int t, int idx) {
long long ret = 0ll;
while (idx > 0) {
ret += FT[t][idx];
idx -= idx & -idx;
}
return ret;
}
vector<int> c;
inline int d(int x) {
return lower_bound(c.begin(), c.end(), x) - c.begin() + 1;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d%d", x + i, y + i);
z[i] = x[i] - y[i];
c.push_back(z[i]);
}
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a].push_back(b);
g[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
add(0, d(z[i]), x[i]);
add(1, d(z[i]), y[i]);
add(2, d(z[i]), 1);
add(3, d(z[i]), 1);
g[i].push_back(i);
}
for (int i = 1; i <= n; i++) {
long long ans = (long long)x[i] * (sum(3, 300004) - sum(3, d(z[i]))) +
sum(1, 300004) - sum(1, d(z[i]));
ans += (long long)y[i] * sum(2, d(z[i])) + sum(0, d(z[i]));
for (int p : g[i]) {
if (z[p] > z[i]) {
ans -= x[i];
ans -= y[p];
} else {
ans -= x[p];
ans -= y[i];
}
}
printf("%lld ", ans);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, u;
cin >> n >> k;
int i, j;
set<string> set;
vector<string> v(n);
for (i = 0; i < n; i++) {
cin >> v[i];
set.insert(v[i]);
}
int cnt = 0;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
string g = "";
for (u = 0; u < k; u++) {
if (v[i][u] == v[j][u]) {
g.push_back(v[i][u]);
} else {
if ((v[i][u] == 'S' && v[j][u] == 'E') ||
(v[i][u] == 'E' && v[j][u] == 'S'))
g = g + "T";
if ((v[i][u] == 'S' && v[j][u] == 'T') ||
(v[i][u] == 'T' && v[j][u] == 'S'))
g = g + "E";
if ((v[i][u] == 'T' && v[j][u] == 'E') ||
(v[i][u] == 'E' && v[j][u] == 'T'))
g = g + "S";
}
}
if (set.find(g) != set.end()) {
cnt++;
}
}
}
cout << cnt / 3;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int getVal(int a, int b, int c, int d) {
int e = d - '0';
e += (c - '0') * 10;
e += (b - '0') * 100;
e += (a - '0') * 1000;
return e;
}
int main() {
int n;
cin >> n;
vector<int> v(n);
set<int> st;
char a, b, c, d;
int me, e;
for (int i = 0; i < n; ++i) {
cin >> a >> b >> c >> d;
e = getVal(a, b, d, c);
me = e;
e = getVal(b, d, c, a);
me = min(e, me);
e = getVal(d, c, a, b);
me = min(e, me);
e = getVal(c, a, b, d);
me = min(e, me);
st.insert(me);
if (i != n - 1) cin >> a >> b;
}
cout << st.size() << endl;
}
| 1 |
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
cin >> n;
while(n-- > 0){
int x = 0, y = 0, dx, dy, ma = 0, ansx, ansy;
while(cin >> dx >> dy, dx+dy){
x += dx, y += dy;
if(x*x+y*y > ma || (x*x+y*y == ma && x > ansx)){
ma = x*x + y*y;
ansx = x, ansy = y;
}
}
cout << ansx << " " << ansy << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int N;
cin >> N;
if (N % 2) {
cout << "black" << endl;
} else {
cout << "white" << endl;
cout << 1 << ' ' << 2 << endl;
}
}
int main() {
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string getStr(string s1) {
string p = "";
for (int i = 0; i < s1.size(); i++) {
if ((s1[i] >= 'a' && s1[i] <= 'z') || (s1[i] >= 'A' && s1[i] <= 'Z'))
p += tolower(s1[i]);
}
return p;
}
int main() {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
int n;
cin >> n;
string arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
string p = getStr(s1), q = getStr(s2), r = getStr(s3);
for (int i = 0; i < n; i++) {
string k = getStr(arr[i]);
if (k == (p + q + r) || k == (p + r + q) || k == (q + p + r) ||
k == (q + r + p) || k == (r + p + q) || k == (r + q + p)) {
cout << "ACC" << endl;
} else
cout << "WA" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long f[100][3];
long long s, x;
vector<int> sm, xr;
long long dfs(int j, int g) {
if (j == sm.size()) {
return (g == 0);
}
long long &res = f[j][g];
if (res != -1) {
return res;
}
res = 0;
if (g == 0 && xr[j] == 0 && sm[j] == 0) {
res = dfs(j + 1, 0) + dfs(j + 1, 1);
}
if (g == 0 && xr[j] == 1 && sm[j] == 1) {
res = dfs(j + 1, 0) + dfs(j + 1, 0);
}
if (g == 1 && xr[j] == 1 && sm[j] == 0) {
res = dfs(j + 1, 1) + dfs(j + 1, 1);
}
if (g == 1 && xr[j] == 0 && sm[j] == 1) {
res = dfs(j + 1, 0) + dfs(j + 1, 1);
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> s >> x;
if (s < x) {
cout << 0 << "\n";
return 0;
}
int mins = ((s == x) ? -2 : 0);
while (s) {
sm.push_back(s & 1);
s >>= 1;
}
while (x) {
xr.push_back(x & 1);
x >>= 1;
}
for (int i = xr.size(); i < sm.size(); i++) {
xr.push_back(0);
}
memset(f, -1, sizeof f);
cout << dfs(0, 0) + mins;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 200222, inf = 1000111222;
int n, a[max_n], b[max_n];
map<int, vector<int>> m;
set<int> all;
long long ans;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
m[a[i]].push_back(b[i]);
all.insert(a[i]);
}
multiset<int> q;
long long sum = 0;
for (int x = 0;;) {
auto it = m.find(x);
if (it != m.end()) {
for (int t : it->second) {
q.insert(t);
sum += t;
}
}
if (!q.empty()) {
sum -= *q.rbegin();
q.erase(--q.end());
}
ans += sum;
if (q.empty()) {
auto it = all.upper_bound(x);
if (it == all.end()) {
break;
}
x = *it;
} else {
++x;
}
}
printf("%I64d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200003;
string s, t;
int n, m, lft[N], rht[N];
bool solve(int L) {
bool c = 0;
for (int i = 1; i < n - L; i++) {
int lf = lft[i - 1], rt = rht[i + L];
if (lf >= rt || rt - lf < 2) {
c = 1;
break;
}
}
if (rht[L] == 0) c = 1;
if (lft[n - 1 - L] == m - 1) c = 1;
return c;
}
int main() {
cin >> s >> t;
n = s.length();
m = t.length();
int k = 0;
for (int i = 0; i < n; i++) {
if (k == m) {
lft[i] = k - 1;
continue;
}
if (s[i] == t[k]) k++;
lft[i] = k - 1;
}
k = m - 1;
for (int i = n - 1; i >= 0; i--) {
if (k < 0) {
rht[i] = k + 1;
continue;
}
if (s[i] == t[k]) k--;
rht[i] = k + 1;
}
int l = 0, r = n, mid = l;
while (r - l > 1) {
mid = (l + r) / 2;
if (solve(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(a+b==15) cout<<'+';
else if(a*b==15) cout<<'*';
else cout<<'x';
cout<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long w1, h1, w2, h2;
cin >> w1 >> h1 >> w2 >> h2;
long long ans = h1 + h2 + 2;
ans += h1 + h2 + 2 + (w1 - w2);
ans += w1 + w2;
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2005;
const int dx[10] = {0, 0, 0, 1, -1, 1, 1, -1, -1};
const int dy[10] = {0, 1, -1, 0, 0, -1, 1, -1, 1};
int n, area, sx, sy;
bool p[maxN][maxN], vis[maxN][maxN];
void init() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &p[i][j]);
}
void fixNoise() {
int cnt;
for (int tme = 1; tme <= 5; ++tme)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cnt = 0;
for (int k = 0; k <= 8; ++k) cnt += p[i + dx[k]][j + dy[k]];
if (cnt < 5)
p[i][j] = 0;
else
p[i][j] = 1;
}
}
void dfs(int x, int y) {
int xx, yy;
vis[x][y] = 1;
++area;
sx += x;
sy += y;
for (int i = 1; i <= 4; ++i) {
xx = x + dx[i];
yy = y + dy[i];
if (xx < 1 || yy < 1 || xx > n || yy > n || !p[xx][yy] || vis[xx][yy])
continue;
dfs(xx, yy);
}
}
void work() {
double ox, oy, r;
int circle = 0, rectangle = 0;
fixNoise();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (p[i][j] && !vis[i][j]) {
area = 0;
sx = 0;
sy = 0;
dfs(i, j);
if (area >= 158) {
ox = double(sx) / area;
oy = double(sy) / area;
r = ((i - ox) * (i - ox)) + ((j - oy) * (j - oy));
if (area / r > 2.9)
++circle;
else
++rectangle;
}
}
printf("%d %d\n", circle, rectangle);
}
int main() {
init();
work();
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
const double pi=acos(-1.0);
int main(){
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
if(a*a*b>=(x<<1)){
printf("%.8f\n",atan((double)a*b*b/(x*2))*180/pi);
}else{
printf("%.8f\n",atan((double)(2*b-2.0*x/(a*a))/a)*180/pi);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> A, B;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
A.push_back(x);
}
for (int i = 0; i < n; i++) {
int x;
cin >> x;
B.push_back(x);
}
sort(A.begin(), A.end());
reverse(A.begin(), A.end());
sort(B.begin(), B.end());
reverse(B.begin(), B.end());
long long int sum = 0;
int p1 = 0, p2 = 0;
for (int i = 0; i < n; i++) {
if ((p2 >= n) || ((p1 < n) && (A[p1] >= B[p2]))) {
sum += A[p1];
p1++;
} else {
p2++;
}
if (p1 >= n || ((p2 < n) && (B[p2] >= A[p1]))) {
sum -= B[p2];
p2++;
} else {
p1++;
}
}
cout << sum << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int n, m;
char s[1010][1010];
bool vis[1010][1010];
bool fail(int x, int y) {
return x < 1 || y < 1 || x > n || y > m || s[x][y] == '.';
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
bool tag = 0;
for (int k = 0; k < 8; k++) {
int nx = i + dx[k], ny = j + dy[k];
if (fail(nx, ny)) {
tag = 1;
break;
}
}
if (!tag) {
for (int k = 0; k < 8; k++) {
int nx = i + dx[k], ny = j + dy[k];
vis[nx][ny] = 1;
}
}
}
}
bool tag(1);
for (int i = 1; i <= n && tag; i++)
for (int j = 1; j <= m && tag; j++)
if (s[i][j] == '#' && !vis[i][j]) tag = 0;
puts(tag ? "YES" : "NO");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct paire {
int pos;
int val;
};
bool sortbyval(const paire &a, const paire &b) { return (a.val < b.val); }
bool sortbypos(const paire &a, const paire &b) { return (a.pos < b.pos); }
int main() {
int n;
cin >> n;
paire a[n];
for (int i = 0; i < n; i++) {
cin >> a[i].val;
a[i].pos = i;
}
sort(a, a + n, sortbyval);
int max = -1;
for (int i = 0; i < n; i++)
if (a[i].val > max)
max = a[i].val;
else {
a[i].val = max + 1;
max += 1;
}
sort(a, a + n, sortbypos);
for (int i = 0; i < n; i++) cout << a[i].val << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, k, d1, d2, a, b, c;
bool func(long long x, long long y) {
long long sum = 2 * x + y;
if (k - sum >= 0 && (k - sum) % 3 == 0) {
a = (k - sum) / 3;
b = a + x;
c = b + y;
if (a < 0 || b < 0 || c < 0) return 0;
long long maxx = max(a, max(b, c));
if (3 * maxx - a - b - c <= (n - k) && ((n - k) + a + b + c) % 3 == 0 &&
(n % 3 == 0) && (a <= n / 3 && b <= n / 3 && c <= n / 3))
return 1;
}
return 0;
}
int main() {
if (fopen("nigga.txt", "r")) {
freopen("nigga.txt", "r", stdin);
freopen("pls.txt", "w", stdout);
}
cin >> t;
while (t--) {
cin >> n >> k >> d1 >> d2;
if (func(-d1, -d2)) {
cout << "yes" << endl;
continue;
}
if (func(d1, -d2)) {
cout << "yes" << endl;
continue;
}
if (func(-d1, d2)) {
cout << "yes" << endl;
continue;
}
if (func(d1, d2)) {
cout << "yes" << endl;
continue;
}
cout << "no" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios_base::sync_with_stdio(0);
cin.tie();
cout.tie();
int i;
int n;
cin>>n;
int a[n],b[n];
for (i = 0 ; i < n ; i++) cin>>a[i];
for (i = 0 ; i < n ; i++) cin>>b[i];
int s[3*n+2];
s[0] = -2;
for (i = 1; i < n+1 ; i++) s[i] = b[i-1]^b[i%n];
s[n+1] = -1;
for (i = n+2; i < 2*n+2 ; i++) s[i] = a[i-n-2]^a[(i-n-1)%n];
for (i = 2*n+2; i < 3*n+2 ; i++) s[i] = s[i-n];
//for (i = 0; i < 3*n+2; i++) cout<<s[i]<<" ";
int P[3*n+2];
P[1] = 0;
for (i = 2; i < 3*n+1; i++){
int ps = P[i-1];
while (s[i] != s[ps+1] && ps) ps = P[ps];
if (s[i] == s[ps+1]) P[i] = ps+1;
else P[i] = 0;
if (P[i] == n) cout<<i-2*n-1<<" "<<(b[0]^a[i-2*n-1])<<"\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 60;
int n;
int a[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int x;
scanf("%d", &x);
a[i] = max(a[i], x);
}
}
for (int i = 0; i < n; i++)
if (a[i] == n - 1) {
a[i] = n;
break;
}
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define pii pair<long long,long long>
#define ppi pair<long long, pair<long long, long long>>
#define pq priority_queue
#define v vector<long long>
#define pb push_back
#define mp make_pair
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define ps(x,y) fixed << setprecision(y) << x
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define Ceil(a , b) (((a) / b) + ((a)%b && 1))
#define mod 1000000007
#define inf 1e18
#define ll long long
ll gcd(ll a, ll b)
{
if (a == 0) return b;
return gcd(b % a, a);
}
ll lcm(ll a, ll b) {return a * (b / gcd(a, b));}
ll power(ll n, ll p, ll m = mod)
{
ll res = 1ll;
n %= m;
while (p)
{
if (p & 1ll) res = (res * n) % m;
p = p >> 1ll;
n = (n * n) % m;
}
return res % m;
}
const int x8[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int y8[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int x4[4] = {1, -1, 0, 0};
const int y4[4] = {0, 0, -1, 1};
inline ll Mul(ll a, ll b, ll m = mod)
{return ((a % m) * (b % m)) % m;}
inline ll Add(ll a, ll b, ll m = mod)
{return ((a % m) + (b % m)) % m;}
inline ll Sub(ll a, ll b, ll m = mod)
{return ((a % m) - (b % m) + m) % m;}
inline ll Div(ll a, ll b, ll m = mod)
{return ((a % m) * power(b , m - 2ll, m)) % m; }
// bitset<10000005> b;
// vector<ll> prime;
// void primeseive()
// {
// b.set();
// b[0] = b[1] = 0;
// for (ll i = 2; i < 10000005; i++)
// {
// if (b[i])
// {
// prime.push_back(i);
// for (ll j = i * i; j < 10000005; j += i)
// b[j] = 0;
// }
// }
// }
// bool isPalindrome(string s, ll i, ll j)
// {
// while (i < j)
// {
// if (s[i] != s[j])
// return false;
// i++; j--;
// }
// return true;
// }
// class Comp {
// public:
// bool operator()(ppi &a, ppi&b) {
// return a.first > b.first;
// }
// };
void file_input_output()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main()
{
file_input_output();
ll t; cin >> t;
while (t--)
{
ll n; cin >> n;
vector<ll> neg, pos;
for (ll i = 0; i < n; i++)
{
ll x; cin >> x;
if (x <= 0)
neg.push_back(-x);
else pos.push_back(x);
}
sort(pos.begin(), pos.end());
sort(neg.begin(), neg.end());
if (neg.size() == 0)
{
cout << 1 << endl;
continue;
}
ll ans = max(1ll , (ll)neg.size());
if (pos.size() > 0)
{
ll Max = pos[0];
ll curr = 2ll;
ll ele = 0;
for (ll i = 1; i < neg.size(); i++)
{
if (neg[i] - neg[ele] >= Max)
{
curr++;
ele = i;
}
}
ans = max(ans, curr);
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
signed main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<unordered_set<int>> v;
for (int i = 0; i < n - 1; i++) {
int k;
cin >> k;
unordered_set<int> vv;
for (int j = 0; j < k; j++) {
int t;
cin >> t;
vv.insert(t);
}
v.push_back(vv);
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
vector<unordered_set<int>> vv = v;
vector<int> p({i});
for (int j = 0; j < n - 1; j++) vv[j].erase(i);
bool yes = true;
for (int j = 0; j < n - 1; j++) {
vector<int> ok;
for (int k = 0; k < n - 1; k++) {
if (vv[k].size() == 1) {
ok.push_back(k);
}
}
if (ok.size() != 1) {
yes = false;
break;
}
unordered_set<int> tt = v[ok[0]];
int nw = *vv[ok[0]].begin();
tt.erase(nw);
if (tt.empty()) {
yes = false;
break;
}
bool nok = 0;
for (int i = p.size() - 1; tt.size() && i >= 0; i--) {
if (tt.count(p[i]) == 0) {
nok = 1;
break;
}
tt.erase(p[i]);
}
if (nok) {
yes = false;
break;
}
p.push_back(nw);
for (int k = 0; k < n - 1; k++) vv[k].erase(nw);
}
if (yes) {
ans = p;
break;
}
}
for (int i : ans) cout << i << " ";
cout << endl;
}
}
| 6 |
#include <bits/stdc++.h>
int main() {
char str[200000] = {0};
int n, i = 0, value = 0, arr[27] = {0}, key = 0;
scanf("%d", &n);
scanf("%s", str);
while (str[i * 2 + 1] && i <= 100000) {
arr[str[i * 2] - 'a']++;
arr[str[i * 2 + 1] - 'A']--;
if (arr[str[i * 2 + 1] - 'A'] < 0) {
arr[str[i * 2 + 1] - 'A'] = 0;
key++;
}
i++;
}
printf("%d", key);
}
| 1 |
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
map<string,int>dp;
int dx[4]={1,-1,4,-4};
void bfs()
{
queue<string>que;
string p;
p="01234567";
dp[p]=0;
que.push(p);
while(que.size())
{
string q;
q=que.front(); que.pop();
int k;
for(int i=0;i<8;i++)
{
if(q[i]=='0')
{
k=i;
break;
}
}
for(int i=0;i<4;i++)
{
int x;
x=k+dx[i];
if(x>=0&&x<=7&&!(k==3&&i==0)&&!(k==4&&i==1))
{
string pp;
pp=q;
swap(pp[x],pp[k]);
if(dp.find(pp)==dp.end())
{
dp[pp]=dp[q]+1;
que.push(pp);
}
}
}
}
}
int main()
{
bfs();
string s;
while(getline(cin,s))
{
s.erase(remove(s.begin(),s.end(),' '),s.end());
cout<<dp[s]<<endl;
}
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int n;
double xa,xb,xc,xd,ya,yb,yc,yd,ta,tb,tc,td;
int main(){
int i;
cin >> n;
for(i=0;i<n;i++){
cin >> xa >> ya >> xb >> yb >> xc >> yc >> xd >> yd;
ta = xa - xb;
tb = ya - yb;
tc = xc - xd;
td = yc - yd;
if(tc/td == ta/tb) cout << "YES" << endl;
else if(ta == -tc && tb == -td) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
const int N = 2005;
int n, a[N][N], md = 2e9;
long long d[N];
bool used[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
scanf("%d", &a[i][j]), a[j][i] = a[i][j], md = std::min(md, a[i][j]);
for (int i = 1; i <= n; ++i) {
d[i] = 2e9;
for (int j = 1; j <= n; ++j)
if (i != j) d[i] = std::min(d[i], 2ll * (a[i][j] -= md));
}
for (int i = 1; i <= n; ++i) {
int u = -1;
for (int v = 1; v <= n; ++v)
if (!used[v] && (u == -1 || d[v] < d[u])) u = v;
used[u] = 1;
for (int v = 1; v <= n; ++v) d[v] = std::min(d[v], d[u] + a[u][v]);
}
for (int i = 1; i <= n; ++i) printf("%lld\n", d[i] + 1ll * md * (n - 1));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int q, n, x;
int main() {
ios_base::sync_with_stdio(false);
cin >> q;
for (int i = 0; i < q; ++i) {
cin >> n >> x;
int best = -1;
int best_d = 0;
for (int i = 0; i < n; ++i) {
int d = 0;
int h = 0;
cin >> d >> h;
best = max(best, d - h);
best_d = max(best_d, d);
}
x -= best_d;
if (x <= 0) {
cout << 1 << endl;
continue;
}
if (best <= 0) {
cout << -1 << endl;
} else {
cout << ((x % best == 0 ? x / best : x / best + 1) + 1) << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
char ch[30], x, y;
int n, m;
cin >> n >> m;
cin >> s;
for (int i = 0; i < 26; i++) ch[i] = i + 'a';
for (int i = 0; i < m; i++) {
cin >> x >> y;
for (int l = 0; l < 26; l++) {
if (ch[l] == x) {
ch[l] = y;
} else if (ch[l] == y) {
ch[l] = x;
}
}
}
for (int i = 0; i < n; i++) {
cout << ch[s[i] - 'a'];
}
}
| 2 |
#include <iostream>
using namespace std;
int main(void) {
int n;
cin >> n;
cout << 111 * ((n + (110)) / 111) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[200005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
cin >> TESTS;
long long int sum = 0, n = 1;
stack<int> s;
s.push(0);
while (TESTS--) {
int t;
cin >> t;
if (t == 1) {
int a, x;
cin >> a >> x;
sum += (a * x);
arr[a - 1] += x;
} else if (t == 2) {
int k;
cin >> k;
s.push(k);
sum += k;
n++;
} else {
sum -= s.top();
s.pop();
sum -= arr[n - 1];
arr[n - 2] += arr[n - 1];
arr[n - 1] = 0;
n--;
}
cout << setprecision(7) << (1.0) * (sum) / n << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int len, op, i, j;
char ch1, ch2;
cin >> len >> op;
string line;
cin >> line;
while (op--) {
cin >> i >> j >> ch1 >> ch2;
for (; i <= j; i++) {
if (line[i - 1] == ch1) line[i - 1] = ch2;
}
}
cout << line << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int K;
cin >> K;
cout << ((K + 1) / 2) * (K / 2) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
string S;
cin >> n;
while (n > 0) {
if (n % 7 == 0) {
S += '7';
n -= 7;
} else {
S += '4';
n -= 4;
}
}
if (n == 0)
cout << S << endl;
else
cout << "-1" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC push_options
#pragma GCC optimize("unroll-loops")
const int N = int(1e6 + 20);
int fen[N];
void update(int i, int add) {
while (i > 0 && i < N) {
fen[i] += add;
i += (i & (-i));
}
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += fen[i];
i = i - (i & (-i));
}
return s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
update(num, 1);
}
while (q--) {
int x;
cin >> x;
if (x > 0) {
update(x, 1);
} else {
x = -x;
int low = 0, high = N;
while (low < high) {
int mid = (low + high) / 2;
int val = sum(mid);
if (x <= val)
high = mid;
else
low = mid + 1;
}
update(low, -1);
}
}
int ans = sum(N);
if (ans == 0)
cout << 0 << "\n";
else {
int low = 0, high = N;
while (low < high) {
int mid = (low + high) / 2;
int val = sum(mid);
if (ans <= val)
high = mid;
else
low = mid + 1;
}
cout << low << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b) {
while (true) {
if (a == 0) return b;
b %= a;
if (b == 0) return a;
a %= b;
}
}
int main() {
int n, m;
cin >> n >> m;
int a, gcd = 0;
for (; n > 0; --n) {
cin >> a;
gcd = GCD(gcd, a - 1);
}
while (gcd % 2 == 0) gcd /= 2;
int sr = sqrt(gcd);
int exp;
vector<int> factors;
factors.push_back(1);
for (int i = 2; i <= sr; ++i) {
if (gcd % i) continue;
exp = 0;
do {
gcd /= i;
++exp;
} while (gcd % i == 0);
sr = sqrt(gcd);
for (int j = factors.size() - 1; j >= 0; --j) {
a = factors[j];
for (int k = 0; k < exp; ++k) {
factors.push_back(a *= i);
}
}
}
if (gcd != 1)
for (int i = factors.size() - 1; i >= 0; --i)
factors.push_back(factors[i] * gcd);
long long ans = 0;
for (int i = factors.size() - 1; i >= 0; --i) {
for (a = factors[i]; a < m; a *= 2) ans += m - a;
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<int> g[N];
int was[N];
bool Q[N];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0, u, v; i < m; i++) {
scanf("%d %d", &u, &v);
g[u].push_back(v);
}
for (int u = 1; u <= n; u++) {
if (!was[u]) {
was[u] = 1;
for (int v : g[u]) {
if (!was[v]) was[v] = 2;
}
}
}
int k = 0;
for (int u = n; u >= 1; u--) {
if (was[u] == 1) {
k++;
Q[u] = true;
for (int v : g[u]) {
was[v] = 2;
}
}
}
printf("%d\n", k);
for (int u = 1; u <= n; u++) {
if (Q[u]) printf("%d ", u);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int h[n];
int hprev = 0;
long long tot = 0;
for (int i = 0; i < n; i++) {
cin >> h[i];
tot += fabs(hprev - h[i]) + 2;
hprev = h[i];
}
tot--;
cout << tot << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
long long ret = 1;
for (; b; b >>= 1, a = (a * a) % MMM)
if (b & 1) ret = (ret * a) % MMM;
return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
if (a == 0 || b == 0) return a + b;
return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int ddx[] = {0, 0, 1, 1, 1, -1, -1, -1}, ddy[] = {1, -1, 1, 0, -1, 1, 0, -1};
int a[100001], sum[100001];
int main() {
int n, l, r, ql, qr;
scanf("%d%d%d%d%d", &n, &l, &r, &ql, &qr);
for (int(i) = (1); (i) <= (n); (i) += (1)) {
scanf("%d", a + i);
sum[i] = sum[i - 1] + a[i];
}
int ans = min(sum[n] * l + (n - 1) * ql, sum[n] * r + (n - 1) * qr);
for (int(i) = (1); (i) <= (n); (i) += (1)) {
int L = i, R = n - i;
if (L == R) {
ans = min(ans, sum[i] * l + (sum[n] - sum[i]) * r);
} else if (L > R) {
ans = min(ans, sum[i] * l + (sum[n] - sum[i]) * r + (L - R - 1) * ql);
} else {
ans = min(ans, sum[i] * l + (sum[n] - sum[i]) * r + (R - L - 1) * qr);
}
}
printf("%d", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> pos[110];
int n;
int main() {
int m;
cin >> n >> m;
int i, j;
char c;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> c;
if (c == '1') pos[i].push_back(j);
}
if (pos[i].empty()) {
cout << -1 << endl;
return 0;
}
}
int mm = 100000000;
int k;
for (k = 0; k < m; k++) {
int sum = 0;
for (i = 0; i < n; i++) {
vector<int>::iterator it = lower_bound(pos[i].begin(), pos[i].end(), k);
if (it != pos[i].end() && *it == k)
continue;
else {
int x, y;
if (it == pos[i].end()) {
x = k - pos[i].back();
y = m - k + pos[i].front();
} else if (it == pos[i].begin()) {
x = *it - k;
y = m - pos[i].back() + k;
} else {
x = *it - k;
y = k - *(it - 1);
}
sum += min(x, y);
}
}
if (sum < mm) mm = sum;
}
cout << mm << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e5 + 10;
char s[maxn];
int main() {
gets(s);
int n = strlen(s);
int i = 0;
while (i < n) {
if (s[i] == ' ') {
i++;
continue;
}
if (s[i] == '\"') {
i++;
putchar('<');
while (s[i] != '\"') {
putchar(s[i]);
i++;
}
putchar('>');
i++;
puts("");
} else {
putchar('<');
while (i < n && s[i] != '\"' && s[i] != ' ') {
putchar(s[i]);
i++;
}
putchar('>');
puts("");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios ::sync_with_stdio(0);
int n;
cin >> n;
if (n < 4) {
cout << "NO\n";
} else if (n == 4) {
cout << "YES\n";
cout << 1 << " * " << 2 << " = " << 2 << "\n";
cout << 2 << " * " << 3 << " = " << 6 << "\n";
cout << 4 << " * " << 6 << " = " << 24 << "\n";
} else if (n == 5) {
cout << "YES\n";
cout << 5 << " - " << 3 << " = " << 2 << "\n";
cout << 1 << " + " << 2 << " = " << 3 << "\n";
cout << 2 << " * " << 3 << " = " << 6 << "\n";
cout << 4 << " * " << 6 << " = " << 24 << "\n";
} else {
cout << "YES\n";
cout << 1 << " + " << 5 << " = " << 6 << "\n";
cout << 6 << " - " << 6 << " = " << 0 << "\n";
for (int i = 7; i <= n; i++) {
cout << 0 << " * " << i << " = " << 0 << "\n";
}
cout << 0 << " + " << 2 << " = " << 2 << "\n";
cout << 2 << " * " << 3 << " = " << 6 << "\n";
cout << 4 << " * " << 6 << " = " << 24 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
const int nl = 20;
const int nm = 10;
vint depth;
vvint adj_list;
vvint node_min;
vvvint log_min;
vvint log_parent;
vint vmerge(const vint& left, const vint& right) {
vint ans(left);
ans.insert(ans.end(), right.begin(), right.end());
sort(ans.begin(), ans.end());
int ans_size = ans.size();
if (ans_size > nm) ans.resize(nm);
return ans;
}
void dfs(int node, vint& visited, int d) {
visited[node] = 1;
depth[node] = d;
for (int i = 0; i < nl - 1; ++i) {
int lp = log_parent[node][i];
if (lp != -1) {
log_parent[node][i + 1] = log_parent[lp][i];
log_min[node][i + 1] = vmerge(log_min[node][i], log_min[lp][i]);
}
}
for (int i : adj_list[node]) {
if (!visited[i]) {
log_parent[i][0] = node;
log_min[i][0] = node_min[i];
dfs(i, visited, d + 1);
}
}
}
int find_lca(int u, int v) {
if (u == v) return u;
if (depth[u] != depth[v]) {
if (depth[v] > depth[u]) swap(u, v);
int d = depth[u] - depth[v];
for (int i = nl - 1; i >= 0; --i)
if (1 << i <= d) {
u = log_parent[u][i];
d -= (1 << i);
}
if (u == v) return u;
}
for (int i = nl - 1; i >= 0; --i)
if (log_parent[u][i] != log_parent[v][i]) {
u = log_parent[u][i];
v = log_parent[v][i];
}
assert(log_parent[u][0] == log_parent[v][0]);
return log_parent[u][0];
}
vint solve(int u, int v) {
vector<int> ans;
int lca = find_lca(u, v);
int du = depth[u] - depth[lca];
int dv = depth[v] - depth[lca];
for (int i = nl - 1; i >= 0; --i) {
if (du & (1 << i)) {
ans = vmerge(ans, log_min[u][i]);
u = log_parent[u][i];
}
if (dv & (1 << i)) {
ans = vmerge(ans, log_min[v][i]);
v = log_parent[v][i];
}
}
ans = vmerge(ans, node_min[lca]);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cout.precision(10);
cout << fixed;
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
depth.assign(n, -1);
adj_list.assign(n, vint());
node_min.assign(n, vint());
log_min.assign(n, vvint(nl, vint()));
log_parent.assign(n, vint(nl, -1));
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
--u;
--v;
adj_list[u].push_back(v);
adj_list[v].push_back(u);
}
for (int i = 0; i < m; ++i) {
int c;
scanf("%d", &c);
--c;
node_min[c].push_back(i + 1);
}
for (int i = 0; i < n; ++i) {
vint& c = node_min[i];
sort(c.begin(), c.end());
int c_size = c.size();
if (c_size > nm) c.resize(nm);
}
vint visited(n, 0);
int root = 0;
dfs(root, visited, 0);
while (q--) {
int u, v, a;
scanf("%d %d %d", &u, &v, &a);
--u;
--v;
vint ans = solve(u, v);
int ans_size = ans.size();
if (ans_size > a) ans.resize(a);
printf("%d ", (int)ans.size());
for (int i : ans) printf("%d ", i);
printf("\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int M = 1e6;
const long long INF = (long long)(1e18);
const int inf = 1e9;
const long long MOD = 1000000007LL;
void solve() {}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int a1, a2, k1, k2, n;
cin >> a1 >> a2 >> k1 >> k2 >> n;
int mini, maxi, teamMin, teamMax;
if (k1 < k2) {
mini = k1;
maxi = k2;
teamMin = a1;
teamMax = a2;
} else {
mini = k2;
maxi = k1;
teamMin = a2;
teamMax = a1;
}
int ans1 = 0, ans2 = 0;
int tmp = (mini - 1) * teamMin + (maxi - 1) * teamMax;
ans1 = max(0, n - tmp);
tmp = n;
while (tmp >= mini && teamMin > 0) {
ans2++;
tmp -= mini;
teamMin--;
}
while (tmp >= maxi && teamMax > 0) {
ans2++;
tmp -= maxi;
teamMax--;
}
cout << ans1 << " " << ans2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline namespace Infinity {
inline namespace IO {
const char CR = '\n';
const char SP = ' ';
inline void write(const int n) { printf("%d", n); }
inline void write(const unsigned n) { printf("%u", n); }
inline void write(const long long n) { cout << n; }
inline void write(const unsigned long long n) { cout << n; }
inline void writef(const double a, const int n = 15) { printf("%.*f", n, a); }
inline void writef(const long double a, const int n = 18) {
cout << setprecision(n) << fixed << a;
}
inline void write(const char c) { printf("%c", c); }
inline void write(const char s[]) { printf("%s", s); }
inline void write(const string &s) { cout << s; }
inline void write(const pair<int, int> &p) {
printf("%d %d", p.first, p.second);
}
template <class T>
inline void write(const T a) {
for (auto i : a) write(i), write(SP);
}
inline void writeln() { write(CR); }
template <typename T>
inline void writeln(const T &a) {
write(a);
write(CR);
}
inline void writefln(const double a, int n) {
printf("%.*f", n, a);
write(CR);
}
inline void writefln(const long double a, int n = 18) {
cout << setprecision(n) << fixed << a << endl;
}
inline void writesln(const int *a, const int l, const int r) {
for (int i = l; i <= r; i++) printf("%d ", a[i]);
writeln(CR);
}
template <class T>
inline void writelns(const T a) {
for (__typeof a.begin() i = a.begin(); i != a.end(); i++) writeln(*i);
}
template <typename T, typename... types>
inline void write(const T &a, const types &...args) {
write(a);
write(args...);
}
template <typename... types>
inline void writeln(const types &...args) {
write(args...);
write(CR);
}
template <typename... types>
inline void writeSP(const types &...args) {
write(args...);
write(SP);
}
inline void writelnYN(bool b) { writeln(b ? "YES" : "NO"); }
inline void writelnyn(bool b) { writeln(b ? "Yes" : "No"); }
string caseSharpSpace(int n) { return "Case #" + to_string(n) + ": "; }
string caseNoSharpSpace(int n) { return "Case " + to_string(n) + ": "; }
string caseSharpNoSpace(int n) { return "Case #" + to_string(n) + ":"; }
string caseNoSharpNoSpace(int n) { return "Case " + to_string(n) + ":"; }
inline int read(int &n) { return scanf("%d", &n); }
inline int read(long long &n) { return cin >> n ? 1 : -1; }
template <typename T, typename... types>
inline int read(T &n, types &...args) {
read(n);
return read(args...);
}
inline char getcc() {
char c;
do c = getchar();
while (c == ' ' || c == '\n');
return c;
}
inline int getint() {
int n;
read(n);
return n;
}
inline long long getll() {
long long n;
read(n);
return n;
}
inline double getdouble() {
double n;
scanf("%lf", &n);
return n;
}
inline vector<int> getints(int n) {
vector<int> v(n);
for (int i = 0; i < n; i++) v[i] = getint();
return v;
}
inline vector<pair<int, int>> getpairs(int n) {
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int a = getint(), b = getint();
v[i] = pair<int, int>(a, b);
}
return v;
}
inline void read(string &str, const unsigned size) {
char s[size];
scanf("%s", s);
str = string(s);
}
inline string getstr(const unsigned size = 1048576) {
string s;
read(s, size + 1);
return s;
}
inline string getln(const unsigned size = 1048576) {
char s[size + 1];
scanf("%[^\n]", s);
getchar();
return string(s);
}
} // namespace IO
inline namespace Functions {
inline constexpr int ctoi(const char c) { return c - '0'; }
inline constexpr char itoc(const int n) { return n + '0'; }
inline int dtoi(const double d) { return round(d); }
template <typename T>
inline bool in(T x, T l, T r) {
return l <= x && x <= r;
}
template <class T>
inline int size(const T &a) {
return a.size();
}
template <typename T1, typename T2>
inline pair<T1, T2> mkp(const T1 &a, const T2 &b) {
return make_pair(a, b);
}
template <class T>
inline void sort(T &a) {
std::sort(a.begin(), a.end());
}
template <class T1, class T2>
inline void sort(T1 &a, T2 comp) {
std::sort(a.begin(), a.end(), comp);
}
template <class T1, typename T2>
inline int lbound(const T1 &a, const T2 k) {
return lower_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, typename T2>
inline int ubound(const T1 &a, const T2 k) {
return upper_bound(a.begin(), a.end(), k) - a.begin();
}
template <class T1, class T2>
int count(T1 &a, T2 k) {
return ubound(a, k) - lbound(a, k);
}
template <class T1, class T2>
int find(T1 &a, T2 k) {
return count(a, k) ? lbound(a, k) : -1;
}
template <typename T>
inline void clear(T &a) {
memset(a, 0, sizeof a);
}
template <typename T>
T gcd(T a, T b) {
while (b) {
T t = a % b;
a = b;
b = t;
}
return a;
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
long long qpow(long long a, long long b, long long c) {
return b ? qpow(a * a % c, b >> 1, c) * (b & 1 ? a : 1) % c : 1;
}
template <typename T>
T exGcd(T a, T b, T &x, T &y) {
T d = a;
if (b) {
d = exGcd(b, a % b, y, x);
y -= a / b * x;
} else {
x = 1;
y = 0;
}
return d;
}
template <typename T>
T mps(T l, T r, T k) {
return ((r - (r % k + k) % k) - (l + (k - l % k) % k)) / k + 1;
}
template <typename T>
T sgn(T a) {
return a == 0 ? 0 : a > 0 ? 1 : -1;
}
template <typename T>
constexpr T sq(T a) {
return a * a;
}
} // namespace Functions
inline namespace TypeDefine {}
inline namespace Constant {
const int maxint = INT_MAX;
const long long maxll = LLONG_MAX;
} // namespace Constant
} // namespace Infinity
class DisjointSetUnion {
public:
DisjointSetUnion(int n) : a(n + 1), r(n + 1) { iota(a.begin(), a.end(), 0); }
int find(int x) { return a[x] != x ? a[x] = find(a[x]) : x; }
void join(int x, int y) {
if ((x = find(x)) == (y = find(y))) return;
if (r[x] < r[y])
a[x] = y;
else
a[y] = x;
if (r[x] == r[y]) r[x]++;
}
bool same(int x, int y) { return find(x) == find(y); }
protected:
vector<int> a;
vector<int> r;
};
class DimensionConvert {
public:
DimensionConvert(int, int m) : m(m) {}
int ptoi(int x, int y) { return x * m + y; }
int ptoi(const pair<int, int> &p) { return ptoi(p.first, p.second); }
pair<int, int> itop(int n) { return pair<int, int>(n / m, n % m); }
protected:
int m;
};
int main(int, char *[]) {
int n = getint();
int m = getint();
DimensionConvert dc(n, m);
vector<pair<int, pair<int, int>>> v;
v.reserve(n * m);
DisjointSetUnion dsu(n * m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
v.push_back(mkp(getint(), mkp(i, j)));
if (i && v[dc.ptoi(i - 1, j)].first == v.back().first)
dsu.join(dc.ptoi(i - 1, j), dc.ptoi(i, j));
if (j && v[dc.ptoi(i, j - 1)].first == v.back().first)
dsu.join(dc.ptoi(i, j - 1), dc.ptoi(i, j));
}
for (int i = 0; i < n; i++) {
auto begin = v.begin() + dc.ptoi(i, 0);
auto end = v.begin() + dc.ptoi(i + 1, 0);
vector<pair<int, pair<int, int>>> t(end - begin);
copy(begin, end, t.begin());
sort(t);
for (int j = 1; j < size(t); j++)
if (t[j].first == t[j - 1].first)
dsu.join(dc.ptoi(t[j].second), dc.ptoi(t[j - 1].second));
}
for (int j = 0; j < m; j++) {
vector<pair<int, pair<int, int>>> t;
for (int i = 0; i < n; i++) t.push_back(v[dc.ptoi(i, j)]);
sort(t);
for (int i = 1; i < n; i++)
if (t[i].first == t[i - 1].first)
dsu.join(dc.ptoi(t[i].second), dc.ptoi(t[i - 1].second));
}
sort(v, [&dsu, &dc](const pair<int, pair<int, int>> &p1,
const pair<int, pair<int, int>> &p2) {
return p1.first != p2.first
? p1.first < p2.first
: dsu.find(dc.ptoi(p1.second)) < dsu.find(dc.ptoi(p2.second));
});
vector<int> xm(n), ym(m), mm(n * m);
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < size(v); i++) {
int j = i;
while (j < size(v) && v[j].first == v[i].first &&
dsu.find(dc.ptoi(v[j].second)) == dsu.find(dc.ptoi(v[i].second)))
j++;
int m = 0;
for (int k = i; k < j; k++)
m = max(m, max(xm[v[k].second.first], ym[v[k].second.second]) + 1);
for (int k = i; k < j; k++) {
a[v[k].second.first][v[k].second.second] = m;
xm[v[k].second.first] = ym[v[k].second.second] = m;
}
i = j - 1;
}
for (int i = 0; i < n; i++, writeln())
for (int j = 0; j < m; j++) write(a[i][j], SP);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;
constexpr double PI = 3.1415926535897932384626433832795;
double ans;
Int n;
using P = pair<double, double>;
vector<P> v;
double dot(P a, P b){
return a.first * b.first + a.second * b.second;
}
double norm(P a){
return sqrt(a.first * a.first + a.second * a.second);
}
P operator+(P a, P b){
return P(a.first + b.first, a.second + b.second);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
v.resize(n);
for(Int i=0; i<n; ++i){
double x, y;
cin >> x >> y;
v[i] = P(x, y);
}
for(Int i=0; i<5e6; ++i){
P x = P(cos(2*PI*5e-6*i), sin(2*PI*5e-6*i));
P w = P(0, 0);
for(Int j=0; j<n; ++j){
if(dot(x, v[j]) >= 0.0){
w = w + v[j];
}
}
ans = max(ans, norm(w));
}
cout << fixed << setprecision(15) << ans << "\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long n, s;
cin >> n >> s;
long long cs = 0;
vector<int> v;
for (int i = 0; i < n - 1; ++i) v.push_back(1);
v.push_back(s - (n - 1));
if (s < 2 * n)
cout << "NO"
<< "\n";
else {
cout << "YES"
<< "\n";
for (int i = 0; i < v.size(); ++i) cout << v[i] << " ";
cout << "\n";
cout << v[n - 1] - 1 << "\n";
}
return 0;
}
| 4 |
/*
Nintai-ryoku ga ari, jikan ga kakaru purosesu o shinrai suru
~ 死神
*/
#include <bits/stdc++.h>
/*
#include<boost/multiprecision/cpp_int.hpp>
namespace mp=boost::multiprecision;
*/
using namespace std;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!shortcuts!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
#define ll long long
// #define ll int
#define lo mp::cpp_int
#define pb push_back
#define pob pop_back
#define mk make_pair
#define endl "\n"
#define ce(x) cout<<x<<"\n";
#define fill(s,v) memset(s,v,sizeof(s))
#define speed {ios_base::sync_with_stdio(false); cin.tie(0); }
#define deb(x) cout<<#x<<" "<<x<<endl;
#define ull unsigned long long int
#define in(a) ll a; cin>>a;
#define in2(a,b) ll a,b; cin>>a>>b;
#define in3(a,b,c) ll a,b,c; cin>>a>>b>>c;
#define in4(a,b,c,d) ll a,b,c,d; cin>>a>>b>>c>>d;
#define in5(a,b,c,d,e) ll a,b,c,d,e; cin>>a>>b>>c>>d>>e;
#define YES cout<<"YES"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define No cout<<"No"<<endl;
#define yes cout<<"yes"<<endl;
#define no cout<<"no"<<endl;
#define take(a,n) for(ll pp=0;pp<n;pp++) { cin>>a[pp]; }
#define take2(a,n) for(ll p=0;p<n;p++) { ll temp;cin>>temp;a.pb(temp); }
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,a,n) for(ll i=a;i<n;i++)
#define show(x) for(auto t:x)cout<<t<<" ";
// #define show(a,n) for(ll p=0;p<n;p++) cout<<a[p]<<" ";
#define _ <<" "<<
#define nl cout<<endl;
#define sq(x) ((x)*(x))
#define call(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ff first
#define ss second
#define ld long double
#define dekh_lo cout<<"dekh_lo"<<endl;
#define de2(x,y) cout<<#x<<" = "<<(x) <<' '<< #y<<" = "<<y<<endl;typedef pair<ll,ll> pii;
#define copy(b,a,n) for(ll i=0;i<n;i++) b[i]=a[i];
#define lelo(b) for(auto &i:b)cin>>i;
#define tc(tt) \
ll tt; \
cin>>tt; \
for(ll _tt=0;_tt<tt; _tt++)
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!data-structures!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
typedef vector<vector<ll>> matrix ;
typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!helping-arrays!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
const ll dx[4] = {-1,0,1,0};
const ll dy[4] = {0,-1,0,1};
const ll dxx[] = {1,1,0,-1,-1,-1,0,1};
const ll dyy[] = {0,1,1,1,0,-1,-1,-1};
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!constants!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
const ll mod=1000000007;
const ll mod2=1000000009;
#define pi 3.1415926535897932
const int N=(int)(1e6+10);
const int FULL_MASK=(1<<10)-1;
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Helping-Algos!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
//---------------Kadanes-algo-------------------//
ll maxSubArraySum(ll a[],ll size){
ll max_so_far=a[0];
ll curr_max=a[0];
for (ll i=1;i<size;i++) {
curr_max=max(a[i],curr_max+a[i]);
max_so_far=max(max_so_far,curr_max);
}
return max_so_far;
}
//---------------Fendwick-------------------//
int fendwick[N];
void update(int i,int add) {
while(i>0 && i<N){
fendwick[i]+=add;
i+=(i&(-i));
}
}
int sum(int i){
int s=0;
while(i>0){
s+=fendwick[i];
i=i-(i&(-i));
}
return s;
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!helping-functions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
ll gcd(ll a,ll b){if(!b)return a;return gcd(b,a%b);}
ll power_mod(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll modexpo(ll x,ll n,ll M){if(n==0)return 1;else if(n%2 == 0)return modexpo((x*x)%M,n/2,M);else return(x*modexpo((x*x)%M,(n-1)/2,M))%M;}
ll lcm(ll a, ll b){return (a*b)/gcd(a, b);}
ll sumofdigits(ll n){ll out=0;while(n){out+=(n%10);n/=10;}return out;}
bool isPrime(ll n){if (n <= 1)
return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false;
for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false;return true;}
// ll power(ll a,ll b){if(b==0)return 1;if(b&1)return a*power(a,b-1);ll res=power(a,b/2);return res*res;}
ll bexpo(ll x,ll n){if(n==0)return 1;else if(n%2==0)return bexpo(x*x,n/2);else return x*bexpo(x*x,(n-1)/2);}
ll log(ll d){return log2(d);}
bool isPower(ll x, ll y){if(x==1) return (y==1); ll pow=1; while(pow<y) pow*=x; return(pow == y);}
bool isPerfectSquare(long double x){long double sr = sqrt(x);return ((sr - floor(sr)) == 0);}
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b) { return (a.second < b.second);}
ll isPalindrome(string s){ll l=0;ll h=s.size()-1;while(h>l){if(s[l++]!=s[h--])return 0;}return 1;}
ll setbits(ll n) { if(n==0)return 0; else return 1+setbits(n&(n-1)); }
ll modInverse(ll A,ll M){return modexpo(A,M-2,M);}
ll suma(ll *a,ll n){ll sum=0;for(ll i=0;i<n;i++)sum+=a[i];return sum;}
void cer(ll x){cout<<x<<"\n";return;}
//------modular inverse by extended euclid-------//
// ll d, x, y;
// void extendedEuclid(ll A, ll B) {
// if(B==0){
// d=A;
// x=1;
// y=0;
// }
// else{
// extendedEuclid(B,A%B);
// ll temp=x;
// x=y;
// y=temp-(A/B)*y;
// }
// }
// ll modInverse2(ll A, ll M){
// extendedEuclid(A,M);
// return (x%M+M)%M;
// }
//---------------matrix exponentiation-------------------//
// matrix mul(matrix a,matrix b){
// matrix res(n+1,vector<ll>(n+1));
// f(i,n)f(j,n)f(k,n)res[i][j]=(res[i][j]%mod+((a[i][k]%mod)*(b[k][j]%mod))%mod)%mod;
// return res;
// }
// matrix pow(matrix A,ll p) {
// if(p==1) return A;
// matrix res=pow(A,p/2) ;
// res=mul(res,res);
// if(p&1) res=mul(res,A) ;
// return res;
// }
vector<ll> factorize(ll n) {
vector<ll> res;
for (ll i = 2; i * i <= n; ++i) {
while (n % i == 0) {
res.push_back(i);
n /= i;
}
}
if (n != 1) {
res.push_back(n);
}
return res;
}
vector<ll> divisors(ll n){
vector<ll> res;
for (ll i=1; i<=sqrt(n); i++){
if (n%i == 0){ if (n/i == i){ res.pb(i); } else{ res.pb(n/i); res.pb(i); } }
}
return res;
}
vector<ll> primeFactors(ll n){
if(n==1) return vector<ll>(1,1);
bool idx[1]={false};
vector<ll> res;
while (n%2 == 0){
if(!idx[1]){idx[1]=true;res.pb(2);}
n = n/2;
}
for (ll i=3;i*i<=n; i+=2){
bool once=false;
while (n%i==0){ if(!once){res.pb(i);once=true;} n=n/i; }
}
if (n > 2)res.pb(n);
return res;
}
//----check if a point is in range-----///
bool check(ll x,ll y,ll n,ll m){return(x>=0&&x<n&&y>=0&&y<m);}
//__builtin_ctz(x) --> get power of 2 !!!!
//---------------BFS-------------------//
vector<vector<ll>> ed;
void bfs(ll s,vector<ll> &d){
d[s]=0;
queue<ll>q;
q.push(s);
while(!q.empty()){
ll v=q.front();
q.pop();
for(auto to:ed[v]){
if (d[to]==1e9){
d[to]=d[v]+1;
q.push(to);
}
}
}
}
//---------------DFS-------------------//
// bool vis[N];
// vi adj[N];
// void dfs(ll u){
// if(vis[u])return;
// sz++;
// vis[u]=1;
// for(auto it:adj[u]){
// dfs(it);
// }
// }
// void dfs(ll u){
// if(vis[u]) return;
// deg += g[u].size();
// ver++;
// vis[u] = 1;
// for(auto v: g[u])dfs(v);
// }
// const ll NN=100006;
// std::vector<ll> tree[NN];
// double dfs(ll node,ll parent){
// double sum=0;
// ll child=0;
// for(auto i:tree[node]){
// if(i!=parent){
// sum+=dfs(i,node);
// child++;
// }
// }
// if(child){
// sum/=child;
// sum+=1;
// }
// return sum;
// }
//bs-ready-implementation//
// ll l=1,r=n,ans=0;
// while(l<=r){
// ll mid=(l+r)>>1;
// if(ch(mid,n)){
// ans=mid;
// r=mid-1;
// }
// else{
// l=mid+1;
// }
//
// }
// ce(ans);
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!extra-data-structures!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
struct node{
ll a,b,c,d;
};
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!logic!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
const ll limit=1e6+5;
ll n,b[limit],dp[limit];
void solve(){
in(n);ll a;
f(i,n){
cin>>a;
cin>>b[a];
}
if(b[0]>0){
dp[0]=1;
}
ll maxi=-1e9;
// sort(call(v));
// vi dp(n+1,0);
// for(ll i=1;i<=n;i++){
// dp[i]=dp[i-1]+v[i-1].ff;
// }
for(ll i=1;i<limit;i++){
if(b[i]==0){
dp[i]=dp[i-1];
}
else{
if(b[i]>=i){
dp[i]=1;
}
else{
dp[i]=dp[i-b[i]-1]+1;
}
}
maxi=max(maxi,dp[i]);
}
ce(n-maxi);
}
int main(){
// speed
// tc(tt){
solve();
// }
}
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!test-cases!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
/*
*/
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 105;
vector<pair<long long int, long long int>> adj[N];
vector<bool> vis(N);
bool dfs(long long int u, long long int v, long long int col) {
vis[u] = true;
if (u == v) {
return true;
}
for (auto i : adj[u]) {
long long int vert = i.first;
long long int color = i.second;
if (color == col && !vis[vert]) {
if (dfs(vert, v, col)) {
return true;
}
}
}
return false;
}
void assignFalse() {
for (long long int i = 0; i < N; i++) {
vis[i] = false;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, m;
cin >> n >> m;
for (long long int i = 1; i <= m; i++) {
long long int u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
long long int Q;
cin >> Q;
while (Q--) {
long long int cnt = 0;
long long int u, v;
cin >> u >> v;
for (long long int i = 1; i <= 100; i++) {
assignFalse();
if (dfs(u, v, i)) {
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
| 2 |
#include <iostream>
#include <algorithm>
using namespace std;
int Q;
int N;
int solve(int n) {
if (0 <= n && n <= 9) return 0;
int max_n = 0;
for (int i = 10; i <= n; i *= 10) {
int a = n / i;
int b = n % i;
max_n = max(max_n, a*b);
}
return solve(max_n) + 1;
}
int main() {
cin >> Q;
for (int i = 0; i < Q; i++) {
cin >> N;
cout << solve(N) << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int main() {
memset(a, 1, sizeof(a));
int m, n, k;
cin >> n >> m >> k;
int nn, t;
for (int i = 0; i < n; i++) {
cin >> nn >> t;
a[nn] = min(a[nn], t);
}
int res = 0;
for (int i = 1; i <= m; i++) res += a[i];
cout << min(res, k) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s,t; cin>>s>>t;
if(s == t.substr(0, s.size()))
cout<<"Yes\n";
else
cout<<"No\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (cin >> n) {
vector<int> v;
for (int i = (1); (i) <= (n); ++(i)) {
int tmp;
cin >> tmp;
v.push_back(tmp);
}
bool *flag = new bool[n + 1];
int q, p, x, y;
cin >> q;
while (q > 0) {
cin >> p >> x >> y;
if (p == 2) {
swap(v[x - 1], v[y - 1]);
} else {
int num = 0;
int total = y - x + 1;
memset(flag, 0, sizeof(bool) * (n + 1));
for (int i = (0); (i) < (v.size()); ++(i)) {
if (v[i] < x || v[i] > y || flag[v[i]]) continue;
total--;
int cur = v[i];
for (int j = (i + 1); (j) < (v.size()); ++(j)) {
if (v[j] == cur + 1) {
cur++;
flag[cur] = true;
if (cur <= y) total--;
}
}
num++;
if (total == 0) break;
}
cout << num << endl;
}
q--;
}
delete[] flag;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long int n, m;
long long int k;
cin >> n >> m >> k;
cout << (k - 1) / (2 * m) + 1 << ' ';
cout << ((k - 1) % (2 * m)) / 2 + 1 << ' ';
if ((k - 1) % 2 == 0)
cout << 'L';
else
cout << 'R';
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
int main() {
int n, m;
cin >> n >> m;
string s[maxn];
int sx = -1, sy = -1;
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m; j++) {
if (s[i][j] == 'S') {
sx = i, sy = j;
}
}
}
string w;
cin >> w;
vector<int> p{0, 1, 2, 3};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int res = 0;
do {
int x = sx, y = sy;
bool fail = false;
for (auto c : w) {
x += dx[p[c - '0']];
y += dy[p[c - '0']];
if (x >= n || x < 0 || y >= m || y < 0 || s[x][y] == '#') {
fail = true;
break;
}
if (s[x][y] == 'E') break;
}
if (!fail && s[x][y] == 'E') {
res++;
}
} while (next_permutation(p.begin(), p.end()));
cout << res << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
char s[N];
int a[N], sta[N], tp;
int main() {
scanf("%s", s + 1);
int n = strlen(s + 1);
for (int i = 1; i <= n; ++i)
if (s[i] == '0') {
if (tp) a[sta[tp--]] = 1;
} else
sta[++tp] = i;
for (int i = 1; i <= n; ++i) printf("%d", a[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int r[9];
int n;
int a[9];
int b[3][3];
int x[3], y[3], z, k;
set<int> s;
int main() {
cin >> n;
for (int i = 0; i < n * n; i++) {
cin >> a[i];
r[i] = i;
}
do {
for (int i = 0; i < 9; i++) b[i / n][i % n] = a[r[i]];
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
z = k = 0;
s.clear();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
x[i] += b[i][j], y[j] += b[i][j];
if (i == j) z += b[i][j];
if (n - 1 - i == j) k += b[i][j];
}
s.insert(x[i]);
}
s.insert(z);
s.insert(k);
for (int i = 0; i < n; i++) {
s.insert(y[i]);
}
if (s.size() == 1) break;
} while (next_permutation(r, r + n * n));
cout << *s.begin() << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j) cout << " ";
cout << b[i][j];
}
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, t1;
int v[200005], tot[4], a[200005], pos[4];
long long b[4][200005], s[4][200005], ans;
int getcnt(int t, int x, int y) {
for (int i = 0; i < 4; i++) {
pos[i] = upper_bound(b[i], b[i] + tot[i], t) - b[i];
if (i == 3)
pos[i] = max(pos[i], x);
else if (i)
pos[i] = max(pos[i], y);
}
return pos[0] + pos[1] + pos[2] + pos[3];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
scanf("%d", &t1);
for (int i = 1; i <= t1; i++) scanf("%d", &x), a[x] |= 1;
scanf("%d", &t1);
for (int i = 1; i <= t1; i++) scanf("%d", &x), a[x] |= 2;
for (int i = 1; i <= n; i++) b[a[i]][tot[a[i]]++] = v[i];
for (int i = 0; i < 4; i++) sort(b[i], b[i] + tot[i]);
for (int i = 0; i < 4; i++)
for (int j = 1; j <= tot[i]; j++) s[i][j] = s[i][j - 1] + b[i][j - 1];
ans = 1e18;
for (int i = 0; i <= k && i <= tot[3]; i++) {
if (k - i > min(tot[1], tot[2])) continue;
if (k * 2 - i > m) continue;
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) / 2;
if (getcnt(mid, i, k - i) >= m)
r = mid;
else
l = mid + 1;
}
int cnt = getcnt(l, i, k - i);
long long tmp = 0;
for (int j = 0; j < 4; j++) tmp += s[j][pos[j]];
tmp -= 1ll * (cnt - m) * l;
ans = min(ans, tmp);
}
printf("%lld", ans > 1e17 ? -1 : ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = (long long)1e18 + 5;
const int MAXN = 100005;
struct BIT {
int c[MAXN];
void add(int p, int v) {
while (p < MAXN) {
c[p] += v;
p += p & -p;
}
}
int get(int p) {
int res = 0;
while (p) {
res += c[p];
p -= p & -p;
}
return res;
}
} tree;
int N;
long long P;
set<pair<long long, long long>> seat, done;
priority_queue<int, vector<int>, greater<int>> hung;
long long ans[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> P;
for (int i = 1; i <= N; i++) {
long long t;
cin >> t;
seat.insert({t, i});
}
long long nxt = 0;
while (!seat.empty() || !done.empty() || !hung.empty()) {
long long ev1 = INF, ev2 = INF, ev = INF;
if (!seat.empty()) ev1 = seat.begin()->first;
if (!done.empty()) ev2 = done.begin()->first;
ev = min(ev1, ev2);
if (ev1 != INF && ev1 <= ev2) {
while (!seat.empty() && seat.begin()->first == ev1) {
hung.push(seat.begin()->second);
seat.erase(seat.begin());
}
}
if (ev2 != INF && ev2 <= ev1) {
tree.add(done.begin()->second, -1);
done.erase(done.begin());
}
if (!hung.empty()) {
int busy = tree.get(hung.top());
if (busy > 0) continue;
nxt = max(ev, nxt) + P;
ans[hung.top()] = nxt;
done.insert({nxt, hung.top()});
tree.add(hung.top(), 1);
hung.pop();
}
}
for (int i = 1; i <= N; i++) {
cout << ans[i] << " \n"[i == N];
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000 * 1000 * 1000;
const long long mod = 1000 * 1000 * 1000 + 7;
int main() {
ios_base::sync_with_stdio(false);
bool arr[17];
arr[1] = true;
arr[2] = false;
arr[3] = false;
arr[4] = true;
arr[5] = false;
arr[6] = true;
arr[7] = false;
arr[8] = true;
arr[9] = true;
arr[10] = true;
arr[11] = false;
arr[12] = false;
arr[13] = true;
arr[14] = false;
arr[15] = true;
arr[16] = false;
int n;
cin >> n;
int ans = arr[n];
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INFTY = 1 << 29;
const pair<int, int> EMPTY = make_pair(-1, -1);
ostream& operator<<(ostream& os, const pair<int, int>& p) {
return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& a) {
os << '[';
for (int i = int(0); i < int(a.size()); i++) os << (i ? " " : "") << a[i];
return os << ']';
}
int main() {
for (int n, m; cin >> n >> m;) {
vector<pair<int, int> > cs(n, EMPTY);
vector<int> ss(m);
for (int i = int(0); i < int(m); i++) {
cin >> ss[i];
for (int j = int(0); j < int(ss[i]); j++) {
int x;
cin >> x;
cs[x - 1] = make_pair(i, j);
}
}
vector<pair<int, int> > res;
for (int i = 0, j = 0, k = 0; j < m; i++) {
if (cs[i] != make_pair(j, k)) {
int a = find(i + begin(cs), end(cs), make_pair(j, k)) - cs.begin();
if (cs[i] != EMPTY) {
int b = find(i + begin(cs), end(cs), EMPTY) - cs.begin();
res.emplace_back(i, b);
swap(cs[i], cs[b]);
}
res.emplace_back(a, i);
swap(cs[i], cs[a]);
}
k++;
if (k == ss[j]) {
j++;
k = 0;
}
}
cout << res.size() << endl;
for (int i = int(0); i < int(res.size()); i++)
cout << res[i].first + 1 << ' ' << res[i].second + 1 << endl;
}
return 0;
}
| 1 |
#include <iostream>
#include <fstream>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef __int128 lll;
constexpr ll mod = 1000000007;
int n, k[2], a[200000][2], p, q;
queue<int> v;
int root(int node, int tr) {
int c = node;
while (a[c][tr] != -1) {
v.push(c);
c = a[c][tr];
}
while (!v.empty()) {
a[v.front()][tr] = c;
v.pop();
}
return c;
}
int main() {
cin >> n >> k[0] >> k[1];
for (int i = 0; i < n; i++) a[i][0] = a[i][1] = -1;
for (int j = 0; j < 2; j++) {
for (int i = 0; i < k[j]; i++) {
cin >> p >> q;
if (root(p - 1, j) != root(q - 1, j)) a[root(p - 1, j)][j] = root(q - 1, j);
}
}
map<pair<int, int>, int> w;
for (int i = 0; i < n; i++) w[make_pair(root(i, 0), root(i, 1))]++;
for (int i = 0; i < n; i++) cout << w[make_pair(root(i, 0), root(i, 1))] << endl;
} | 0 |
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
while(1){
int n, s[100]={0};
scanf(" %d", &n);
if(n==0) break;
for(int i=0; i<n; ++i){
for(int j=0; j<5; ++j){
int a;
scanf(" %d", &a);
s[i]+=a;
}
}
sort(s, s+n);
printf("%d %d\n", s[n-1], s[0]);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1\n";
return 0;
}
for (int i = (0); i < (n); ++i) cout << i << " ";
cout << "\n";
for (int i = (0); i < (n); ++i) cout << (n - i - 1 - i + 3 * n) % n << " ";
cout << "\n";
for (int i = (0); i < (n); ++i) cout << (n - i - 1) << " ";
cout << "\n";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 201000;
int n, m, k, x, y, dx, dy, bx[N], by[N];
char dir[10];
set<int> bk[N], pnt[N];
vector<pair<int, int> > tv1[N], tv2[N];
long long ret;
void ff() {
int px = x, py = y;
if (dx == -1) px += 1;
if (dy == -1) py += 1;
if (!bk[px + dx].count(py + dy)) return;
if ((bk[px + dx].count(py) ^ bk[px].count(py + dy)) == 0)
dx *= -1, dy *= -1;
else if (bk[px + dx].count(py))
dx *= -1;
else
dy *= -1;
}
int gg() {
if (dx == 1 && dy == 1)
return *pnt[x + m - y].upper_bound(x) - x;
else if (dx == 1 && dy == -1)
return *pnt[x + y].upper_bound(x) - x;
else if (dx == -1 && dy == 1)
return x - *(--pnt[x + y].lower_bound(x));
else
return x - *(--pnt[x + m - y].lower_bound(x));
}
void addpnt(int px, int py) {
if ((px + py + x + y) % 2 == 0) {
pnt[px + py].insert(px);
pnt[px + m - py].insert(px);
}
}
void addrec(int d) {
if (dx == 1 && dy == 1)
tv1[x + m - y].push_back(make_pair(x + 1, x + d + 1));
else if (dx == -1 && dy == -1)
tv1[x + m - y].push_back(make_pair(x - d + 1, x + 1));
else if (dx == 1 && dy == -1)
tv2[x + y].push_back(make_pair(x + 1, x + d + 1));
else
tv2[x + y].push_back(make_pair(x - d + 1, x + 1));
}
void gao(vector<pair<int, int> > &a) {
if (!((int)(a).size())) return;
sort((a).begin(), (a).end());
ret += a[0].second - a[0].first;
for (int i = 1; i < ((int)(a).size()); i++) {
a[i].first = max(a[i - 1].second, a[i].first);
a[i].second = max(a[i].second, a[i].first);
ret += a[i].second - a[i].first;
}
}
inline void judge() {
freopen("ray.in", "r", stdin);
freopen("ray.out", "w", stdout);
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; i++) scanf("%d%d", bx + i, by + i);
scanf("%d%d%s", &x, &y, dir);
if (dir[0] == 'S')
dx = 1;
else
dx = -1;
if (dir[1] == 'E')
dy = 1;
else
dy = -1;
if (dx == 1) --x;
if (dy == 1) --y;
addpnt(x, y);
for (int i = 0; i < n + 1; i++) addpnt(i, 0), addpnt(i, m);
for (int i = 0; i < m + 1; i++) addpnt(0, i), addpnt(n, i);
for (int i = 1; i < n + 1; i++) bk[i].insert(0), bk[i].insert(m + 1);
for (int i = 0; i < m + 2; i++) bk[0].insert(i), bk[n + 1].insert(i);
for (int i = 0; i < k; i++) {
int cx = bx[i], cy = by[i];
addpnt(cx - 1, cy - 1);
addpnt(cx, cy - 1);
addpnt(cx - 1, cy);
addpnt(cx, cy);
bk[cx].insert(cy);
}
int sx0 = x, sy0 = y, sx1 = dx, sy1 = dy;
while (1) {
int d = gg();
addrec(d);
x = x + d * dx;
y = y + d * dy;
ff();
if (x == sx0 && y == sy0 && dx == sx1 && dy == sy1) break;
}
for (int i = 0; i < n + m + 1; i++) gao(tv1[i]), gao(tv2[i]);
printf("%lld\n", ret);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100001], b[100001];
long long int k, SS;
int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); }
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
SS = SS + a[i];
}
if (SS < k) {
cout << -1;
return 0;
}
if (SS == k) {
return 0;
}
qsort(b, n, sizeof(int), compare);
int s = 0;
long long int K = k;
for (int i = 0; i < n; i++)
if (b[i] > s) {
if (K >= (long long int)(n - i) * (b[i] - s)) {
K = K - (long long int)(n - i) * (b[i] - s);
s = b[i];
} else {
break;
}
}
int conlai = n;
for (int i = 0; i < n; i++) {
if (a[i] <= s) {
a[i] = -1;
conlai--;
} else
a[i] = a[i] - s;
}
for (int i = 0; i < n; i++)
if (a[i] != -1) {
a[i] = a[i] - K / conlai;
if (a[i] == 0) a[i] == -1;
}
K = K % conlai;
if (K > 0)
for (int i = 0; i < n; i++) {
if (a[i] >= 1) {
K--;
if (a[i] == 1) a[i] = -1;
if (K == 0) {
for (int j = i + 1; j < n; j++)
if (a[j] != -1) cout << j + 1 << " ";
for (int j = 0; j <= i; j++)
if (a[j] != -1) cout << j + 1 << " ";
}
}
}
else
for (int i = 0; i < n; i++)
if (a[i] != -1) cout << i + 1 << " ";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long beuty(string s) {
long long l = s.size(), b = 0;
int ch[130];
memset(ch, 0, sizeof(ch));
for (int i = 0; i < l; i++) {
int c = s[i] - 'A';
ch[c]++;
b = max(b, (long long)ch[c]);
}
if (b < s.size())
b = min((long long)s.size(), b + n);
else {
if (n == 1) b--;
}
return b;
}
int main() {
ios_base ::sync_with_stdio(false);
cin >> n;
string kur, shir, kati;
cin >> kur >> shir >> kati;
long long a = beuty(kur);
long long b = beuty(shir);
long long c = beuty(kati);
if ((a == b && a >= c) || (b == c && b >= a) || (a == c && a >= b))
cout << "Draw\n";
else if (a > b && a > c)
cout << "Kuro\n";
else if (b > a && b > c)
cout << "Shiro\n";
else
cout << "Katie\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n;
cin >> n >> k;
int even = 0;
int odd = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (a % 2 == 0)
++even;
else
++odd;
}
int left = n - k;
int dan = left / 2;
int stan = left / 2 + left % 2;
if (left == 0) {
if (odd % 2 == 1) {
cout << "Stannis\n";
} else {
cout << "Daenerys\n";
}
return 0;
}
if (k == 0) {
cout << "Daenerys\n";
return 0;
}
if (left % 2 == 0) {
if (k % 2 == 0) {
cout << "Daenerys\n";
return 0;
}
if (odd - even < k) {
cout << "Daenerys\n";
} else {
cout << "Stannis\n";
}
return 0;
}
if (k % 2 == 1) {
if (even - odd < k) {
cout << "Stannis\n";
} else
cout << "Daenerys\n";
return 0;
}
if (even - odd < k && odd - even < k) {
cout << "Stannis\n";
} else
cout << "Daenerys\n";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
long long n;
cin >> n;
vector<long long> v(n);
for (auto &ele : v) cin >> ele;
long long answer = 1e18L;
long long total = accumulate(v.begin(), v.end(), 0LL);
long long end = 1e5 + 5;
sort(v.begin(), v.end());
long long diff = total;
end = exp((18 * log(10)) / n);
for (long long i = 1; i <= end; i++) {
bool flag = false;
long long cnt = 0;
for (long long p = 0; p < n; p++) {
long long x = pow(i, p);
cnt += abs(x - v[p]);
}
if (flag) break;
answer = min(answer, cnt);
}
cout << answer;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long bp(long long x, long long y) {
long long ans = 1;
x %= mod;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long mod_inverse(long long x) { return bp(x, mod - 2); }
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
long long max(long long a, long long b) {
if (a < b) swap(a, b);
return a;
}
long long min(long long a, long long b) {
if (a > b) swap(a, b);
return a;
}
long long countSetBits(long long n) {
long long count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
void solve() {
long long t;
cin >> t;
while (t--) {
long long h, n;
cin >> h >> n;
long long a[n];
for (long long i = 0; i < n; i++) cin >> a[i];
long long dp[n];
if (n == 1) {
cout << "0" << '\n';
continue;
}
dp[0] = 0;
for (long long i = 1; i < n; i++) {
if (a[i] + 1 == a[i - 1]) {
dp[i] = dp[i - 1] + 1;
if (i - 2 >= 0) dp[i] = min(dp[i], dp[i - 2]);
} else
dp[i] = dp[i - 1] + 1;
}
if (a[n - 1] == 1)
cout << min(dp[n - 2], dp[n - 1]) << '\n';
else
cout << dp[n - 1] << '\n';
}
}
signed main() { solve(); }
| 3 |
#include <bits/stdc++.h>
#define N 100100
using namespace std;
int n, ind[N], ld[N], cnt, num[N];
vector<int> G[N];
void dfs(int u, int p, int now, int &dep, int &pnt) {
if (now > dep) dep = now, pnt = u;
for (int v : G[u])
if (v != p) dfs(v, u, now + 1, dep, pnt);
}
inline void getans(int *p) {
int now = 1;
for (int i = 1; i <= cnt; ++i, ++now) {
int nxt = now + num[i];
p[nxt] = now;
for (int j = now; j < nxt; ++j)
p[j] = ++now;
}
}
int stk[N], top;
void dfs2(int u, int p, int d) {
stk[++top] = u;
if (u == d) {
for (int i = top; i; --i)
ind[stk[i]] = 1;
memcpy(ld, stk, sizeof(stk));
cnt = top;
}
for (int v : G[u])
if (v != p) dfs2(v, u, d);
stk[top--] = 0;
}
inline bool small(int *pa, int *pb) {
for (int i = 1; i <= n; ++i)
if (pa[i] != pb[i]) return pa[i] < pb[i];
return false;
}
inline void write(int *p) {
for (int i = 1; i <= n; ++i)
printf("%d%c", p[i], " \n"[i == n]);
}
int pa[N], pb[N];
int main() {
#ifdef isLOCAL
freopen("work.in", "r", stdin);
freopen("work.out", "w", stdout);
#endif
cin >> n;
for (int i = 1, u, v; i < n; ++i)
scanf("%d%d", &u, &v),
G[u].push_back(v),
G[v].push_back(u);
int p1, p2, d;
d = 0, dfs(1, 0, 0, d, p1);
d = 0, dfs(p1, 0, 0, d, p2);
dfs2(p1, 0, p2);
for (int i = 1, u; i <= cnt; ++i) {
u = ld[i];
for (int v : G[u])
if (!ind[v]) ++num[i], ind[v] = -1;
}
for (int i = 1; i <= n; ++i)
if (!ind[i]) puts("-1"), exit(0);
getans(pa);
reverse(num + 1, num + cnt + 1);
getans(pb);
write(small(pa, pb) ? pa : pb);
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
unsigned char memo[1<<20];
int mask[5][5][6][6];
void printS(int s){
int tmp[5][5];
fill(tmp[0], tmp[5], 0);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
tmp[i][j] = s >> (i*5+j) & 1;
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout << tmp[i][j];
}
cout << endl;
}
cout << endl;
}
void init(){
fill(memo, memo + (1<<20), -1);
fill(mask[0][0][0], mask[5][0][0], 0);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
for(int h=1;i+h<=5;h++){
for(int w=1;j+w<=5;w++){
int tmp = (1 << w) - 1 << j;
for(int k=0;k<h;k++){
mask[i][j][h][w] |= tmp << (i+k) * 5;
}
}
}
}
}
queue<int> q;
memo[0] = 0;
q.push(0);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
for(int h=1;i+h<=4;h++){
for(int w=1;j+w<=5;w++){
int v = u ^ mask[i][j][h][w];
if(memo[v] > memo[u] + 1){
memo[v] = memo[u] + 1;
/*if((int)memo[v] == 2 && 0){
cout << "cost = " << (int)memo[v] << endl;
printS(v);
}*/
q.push(v);
}
}
}
}
}
}
}
const int INF = 1 << 28;
void rec(int t, int x, int cnt, int &res){
if((t >> 20) == 0){
//printS(t);
//cout << (int)memo[t&(1<<20)-1] << endl;
res = min(res, memo[t] + cnt);
}
if(x >= 5) return;
for(int i=0;i<5;i++){
for(int j=x;j<5;j++){
int nt = t ^ mask[i][x][5-i][j-x+1];
if(!(nt&1<<20+x)) rec(nt, x+1, cnt+1, res);
}
}
if(!(t&1<<20+x)) rec(t, x+1, cnt, res);
}
int solve(int s){
int cnt = 0;
for(int i=0;i<5;i++){
if(s >> (20 + i) & 1) cnt++;
}
int res = memo[s&(1<<20)-1] + min(cnt, 6-cnt);
//cout << res << ' ';
rec(s, 0, 0, res);
//cout << res << endl;
return res;
}
int main(){
init();
int n;
while(cin >> n && n){
int m = 0;
for(int i=0;i<n;i++){
int t = 0, c;
for(int j=0;j<n;j++){
cin >> c;
t |= c << j;
}
m |= t << i * 5;
}
int ans = n < 5 ? memo[m] : solve(m);
for(int i=0;i<ans;i++){
cout << "myon";
}
cout << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int m;
cin >> m;
int discounts[100005];
for (int i = 0; i < m; i++) cin >> discounts[i];
sort(discounts, discounts + m);
int n;
cin >> n;
int arr[100005];
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
long long sum = 0;
int cnt = 0;
for (int i = n - 1; i >= 0; i--) {
sum += arr[i];
cnt++;
if (cnt == discounts[0]) {
i -= 2;
cnt = 0;
}
}
cout << sum << endl;
return 0;
}
| 3 |
#include <iostream>
#include <string>
using namespace std;
string t[26] =
{
"100101",
"10011010",
"0101",
"0001",
"110",
"01001",
"10011011",
"010000",
"0111",
"10011000",
"0110",
"00100",
"10011001",
"10011110",
"00101",
"111",
"10011111",
"1000",
"00110",
"00111",
"10011100",
"10011101",
"000010",
"10010010",
"10010011",
"10010000"
};
int main()
{
string line;
while(getline(cin,line))
{
string buf;
for(int i=0; i<line.size(); i++)
{
switch(line[i])
{
case ' ' : buf+="101"; break;
case '\'': buf+="000000"; break;
case ',' : buf+="000011"; break;
case '-' : buf+="10010001"; break;
case '.' : buf+="010001"; break;
case '?' : buf+="000001"; break;
default : buf+=t[line[i]-'A'];break;
}
}
buf+="0000";
for(int i=0; i<buf.size()-4; i+=5)
{
int x = 0;
for(int j=0; j<5; j++)
{
x+= (buf[i+4-j]=='1' ? 1<<j : 0);
}
char c;
switch(x)
{
case 26: c=' '; break;
case 27: c='.'; break;
case 28: c=','; break;
case 29: c='-'; break;
case 30: c='\''; break;
case 31: c='?'; break;
default: c=x+'A';break;
}
cout << c;
}
cout << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int TAM = 3e5 + 3;
int k, n, s;
void prt() {
cout << n << endl << -1 << " ";
s++;
for (int i = 0; i < n - 1; i++) {
if (i)
cout << s / (n - 1) << " ";
else
cout << (s / (n - 1)) + s % (n - 1) << " ";
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k;
for (int i = 2; i <= 2000; i++) {
n = i;
s = k + n - 1;
if (s <= n * 1e6 and (s + 1) / (n - 1) + (s + 1) % (n - 1) <= 1e6) {
prt();
return 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int Mat[1003][1003];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int sum = 0;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cin >> Mat[i][j];
if (i == j) sum = sum ^ Mat[i][j];
}
int q;
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
int u, v;
scanf("%d", &u);
if (u == 1) {
scanf("%d", &v);
sum = 1 - sum;
continue;
}
if (u == 2) {
scanf("%d", &v);
sum = 1 - sum;
continue;
}
if (u == 3) {
printf("%d", sum);
}
}
printf("\n");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
char mp[maxn][maxn];
int cc[maxn][maxn];
int siz[maxn * maxn];
int vis[maxn * maxn];
int inside[maxn * maxn];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int n, k;
bool legal(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= n; }
void dfs(int x, int y, int c) {
cc[x][y] = c;
siz[c]++;
for (int i = 0; i < 4; i++) {
int p = x + dx[i];
int q = y + dy[i];
if (legal(p, q) && mp[p][q] == '.' && cc[p][q] == 0) dfs(p, q, c);
}
}
void add(int x, int y, int &tans) {
int u = cc[x][y];
if (u != 0) {
if (vis[u] == 0) tans += siz[u] - inside[u];
vis[u]++;
}
}
void minu(int x, int y, int &tans) {
int u = cc[x][y];
if (u != 0) {
vis[u]--;
if (vis[u] == 0) tans -= siz[u] - inside[u];
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%s", mp[i] + 1);
}
int color = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (mp[i][j] == '.' && cc[i][j] == 0) dfs(i, j, ++color);
}
}
int ans = 0;
for (int i = 1; i <= n - k + 1; i++) {
int tans = k * k;
for (int j = 1; j <= color; j++) {
vis[j] = 0;
inside[j] = 0;
}
for (int r = i; r < i + k; r++) {
for (int c = 1; c < 1 + k; c++) {
if (cc[r][c] != 0) inside[cc[r][c]]++;
}
}
for (int j = i; j < i + k; j++) {
add(j, k + 1, tans);
}
for (int j = 1; j <= k; j++) {
add(i + k, j, tans);
add(i - 1, j, tans);
}
ans = max(ans, tans);
for (int z = 2; z <= n - k + 1; z++) {
for (int j = i; j < i + k; j++) {
minu(j, z - 2, tans);
minu(j, z + k - 1, tans);
}
for (int j = z - 1; j <= z + k - 2; j++) {
minu(i - 1, j, tans);
minu(i + k, j, tans);
}
for (int j = i; j < i + k; j++) {
inside[cc[j][z - 1]]--;
inside[cc[j][z + k - 1]]++;
}
for (int j = z; j <= z + k - 1; j++) {
add(i - 1, j, tans);
add(i + k, j, tans);
}
for (int j = i; j < i + k; j++) {
add(j, z - 1, tans);
add(j, z + k, tans);
}
ans = max(ans, tans);
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100 * 1000 + 5, LG = 19, mod = 1000 * 1000 * 1000 + 7;
long long n, q;
long long sz[N], h[N];
long long sumd[N], sumu[N], dpd[N], dpu[N];
pair<long long, long long> par[N][LG];
vector<pair<long long, long long> > adj[N];
void dfs1(long long v) {
sz[v] = 1;
for (auto p : adj[v]) {
long long u = p.first;
long long w = p.second;
if (u != par[v][0].first) {
h[u] = h[v] + 1;
par[u][0] = {v, w};
dfs1(u);
sz[v] += sz[u];
sumd[v] += sumd[u] + 1ll * w * sz[u];
sumd[v] %= mod;
long long t = 1ll * w * w;
t %= mod;
t *= sz[u];
t %= mod;
dpd[v] += 2ll * sumd[u] * w;
dpd[v] %= mod;
dpd[v] += t;
dpd[v] += dpd[u];
dpd[v] %= mod;
}
}
}
void dfs2(long long v) {
for (auto p : adj[v]) {
long long u = p.first;
long long w = p.second;
if (u != par[v][0].first) {
long long q = dpd[v] + dpu[v];
long long t = 1ll * w * w;
t %= mod;
t *= sz[u];
t %= mod;
q -= t - mod;
q -= (2ll * sumd[u] * w) % mod - mod;
q -= dpd[u] - mod;
q %= mod;
dpu[u] = q;
long long t2 = 1ll * w * w;
t2 %= mod;
t2 *= (n - sz[u]);
t2 %= mod;
dpu[u] += t2;
dpu[u] %= mod;
dpu[u] += 1ll * (mod + 1) * mod;
dpu[u] %= mod;
long long kft =
sumd[v] + sumu[v] - sumd[u] - 1ll * w * sz[u] + 1ll * mod * mod;
kft %= mod;
kft *= 2 * w;
kft %= mod;
dpu[u] += kft;
dpu[u] %= mod;
sumu[u] =
sumd[v] + sumu[v] - 1ll * sz[u] * w - sumd[u] + 1ll * (n - sz[u]) * w;
sumu[u] += 1ll * mod * mod;
sumu[u] %= mod;
dfs2(u);
}
}
}
pair<long long, long long> lcad(long long u, long long v) {
long long ans = 0;
if (h[u] > h[v]) {
swap(u, v);
}
for (long long i = LG - 1; i >= 0; i--) {
if (h[u] + (1 << i) <= h[v]) {
ans += par[v][i].second;
v = par[v][i].first;
}
}
if (u == v) {
return {ans, u};
}
for (long long i = LG - 1; i >= 0; i--) {
if (par[v][i].first != par[u][i].first) {
ans += par[v][i].second;
ans += par[u][i].second;
v = par[v][i].first;
u = par[u][i].first;
}
}
ans += par[v][0].second + par[u][0].second;
return {ans, par[v][0].first};
}
int32_t main() {
cin >> n;
for (long long i = 1; i < n; i++) {
long long u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
par[1][0].first = 1;
dfs1(1);
dfs2(1);
cin >> q;
for (long long j = 1; j < LG; j++) {
for (long long i = 1; i <= n; i++) {
long long u = par[i][j - 1].first;
par[i][j].first = par[u][j - 1].first;
par[i][j].second = (par[i][j - 1].second + par[u][j - 1].second) % mod;
}
}
for (long long i = 0; i < q; i++) {
long long u, v;
cin >> u >> v;
pair<long long, long long> p = lcad(u, v);
long long x = p.first;
long long r = p.second;
x %= mod;
if (r == v) {
long long ans = dpd[u] + dpu[u];
long long e = 2ll * sumu[v];
e %= mod;
e *= x;
e %= mod;
ans -= 2ll * (e + dpu[v]) - 4ll * mod;
ans %= mod;
long long kft = 1ll * ((2ll * x * x) % mod) * (n - sz[v]);
kft %= mod;
ans -= kft - mod;
ans += mod;
ans %= mod;
cout << ans << endl;
} else {
long long ans = 2ll * mod - dpd[u] - dpu[u];
long long e = 2ll * sumd[v];
e %= mod;
e *= x;
e %= mod;
ans += 2ll * (e + dpd[v]);
ans %= mod;
ans += ((2ll * x * x) % mod) * sz[v];
ans %= mod;
cout << ans << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
string S;
map<string,int> card;
for(int i=0; i<N; i++){
cin >> S;
card[S]++;
}
int M;
cin >> M;
for(int i=0; i<M; i++){
cin >> S;
card[S]--;
}
int max_card = 0;
for(auto x:card){
if(max_card < x.second){
max_card = x.second;
}
}
cout << max_card << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, n, k, a, b, count = 0, max = 0;
cin >> n >> k;
for (i = 0; i < n; i++) {
cin >> a >> b;
if (b < k) {
count = a;
if (count > max) max = count;
} else {
count = a - (b - k);
if (count > max || max == 0) max = count;
}
}
cout << max;
}
| 1 |
#ifdef debug
#include <chrono>
#endif
#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstring> //memcpy
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <map>
//#include <unordered_map> //hash func.
#include <fstream> //ifstream, ofstream
//#define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert
using namespace std;
#define dout cout
//If u wanna output to a text file instead of standard output, plz define OUTPUTFILE.
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif
#define din cin
//If u wanna input from a text file instead of standard input, plz define INPUTFROMTEXTFILE???.
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif
#define scan(A) scanf("%d", &(A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl
//#define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) << A[j] << " ";} cout << endl
#define sign(x) ((x)>0)-((x)<0) //x<0: -1, x=0: 0, x>0: +1
#define p(i) (i)/2
#define l(i) (i)*2
#define r(i) (i)*2+1
int dx[] = {1,-1, 0, 0, 1, 1,-1,-1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0,-1, 1,-1, 1, 1,-1};
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;
const int INF = (int)2e9+10;
const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow.
const ull INF_ULL = (ull)1e19-1ULL;
const int NONE = -1;
const ll MOD = (ll)1e9+7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????
const int N_MAX = 1001; //num of vertex or element
const int M_MAX = 100000; //num of edge
const int DATA_MAX = 1000000010;
int N;
char T[N_MAX][N_MAX];
char P[N_MAX][N_MAX];
int H, W, R, C;
int Hash[N_MAX][N_MAX];
int Bpow[N_MAX][N_MAX];
int main() {
//cin, cout???????????? ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
cin.tie(0); //cin??¨cout??????????????????
ios::sync_with_stdio(false); //iostream??¨stdio??????????????????
//read input data
scanf("%d %d", &H, &W);
rep(i,0,H) {
rep(j,0,W) {
scanf(" %c", &T[i][j]);
}
}
scanf("%d %d", &R, &C);
rep(i,0,R) {
rep(j,0,C) {
scanf(" %c", &P[i][j]);
}
}
#ifdef debug
rep(i,0,H) {
rep(j,0,W) {
dout << T[i][j] << " ";
}
dout << endl;
}
dout << endl;
din >> R >> C;
rep(i,0,R) {
rep(j,0,C) {
dout << P[i][j] << " ";
}
dout << endl;
}
dout << endl;
#endif
//------------------------------------------------------------------------------------------
#ifdef debug
//start timer
auto startTime = chrono::system_clock::now();
#endif
//------------------------------------------------------------------------------------------
#ifdef debug
//calc.
bool found;
rep(i,0,H-R+1) {
rep(j,0,W-C+1) {
found = true;
rep(ii,0,R) {
rep(jj,0,C) {
if(T[i+ii][j+jj]!=P[ii][jj]) {
found = false;
ii = R;
jj = C;
}
}
}
if(found) {
printf("%d %d\n", i, j);
}
}
}
dout << "============================================\n";
#endif
//calc.
const int B = 10007;
int t = 1;
for(int i=R-1; i>=0; i--) {
for(int j=C-1; j>=0; j--) {
Bpow[i][j] = t;
t *= B;
}
}
#ifdef debug
dout << "Bpow[][] = \n";
rep(i,0,R) {
rep(j,0,C) {
dout << Bpow[i][j] << " ";
}
dout << endl;
}
#endif
int HashP = 0;
rep(i,0,R) {
rep(j,0,C) {
HashP += P[i][j]*Bpow[i][j];
}
}
#ifdef debug
disp(HashP);
#endif
int HashT = 0;
rep(i,0,R) {
rep(j,0,C) {
HashT += T[i][j]*Bpow[i][j];
}
}
#ifdef debug
disp(HashT);
#endif
//search
rep(i,0,H-R+1) {
rep(j,0,W-C+1) {
Hash[i][j] = HashT;
#ifdef debug
dout << "-------------------------\n";
dout << i << " " << j << endl;
disp(HashT);
disp(HashP);
dout << "Hash[][] = \n";
rep(i,0,H) {
rep(j,0,W) {
dout << Hash[i][j] << " ";
}
dout << endl;
}
#endif
if(HashT == HashP) {
printf("%d %d\n", i, j);
#ifdef debug
dout << "//////////////////////////////////////////////////////////////\n";
#endif
}
if(j+C<W) {
HashT *= B;
rep(k,i,i+R) {
HashT -= T[k][j]*Bpow[k-i][0]*B;
HashT += T[k][j+C]*Bpow[k-i][C-1];
}
}
else if(i+R<H) { //j+C = W ????????? j=W-C ???????????????j??????????????°???????????´???
HashT = Hash[i][0] * Bpow[R-1][0] * B;
rep(k,0,C) {
HashT -= T[i][k]*Bpow[0][k]*Bpow[R-1][0]*B;
HashT += T[i+R][k]*Bpow[R-1][k];
}
}
}
}
//------------------------------------------------------------------------------------------
#ifdef debug
//stop timer
auto endTime = chrono::system_clock::now();
auto dur = endTime - startTime;
auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
dout << fixed << setprecision(4) << (double)msec/1000 << " sec \n";
#endif
//------------------------------------------------------------------------------------------
#ifdef INPUTFILE
inputfile.close();
#endif
#ifdef OUTPUTFILE
outputfile.close();
cout << "\"" << OutputFilePath << "\"" << endl;
#endif
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char mirro[] = {'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y'};
bool ismirror(char code[], int len) {
int i;
for (i = 0; i < len; i++) {
bool tt = false;
for (int j = 0; j < 11; j++) {
if (code[i] == mirro[j]) {
tt = true;
break;
}
}
if (tt)
continue;
else
break;
}
if (i == len)
return true;
else
return false;
}
int main() {
char code[100001];
scanf("%s", &code);
int n = strlen(code);
code[n] = '\0';
if (ismirror(code, n)) {
for (int i = 0; i <= n / 2; i++) {
if (code[n - i - 1] != code[i]) {
puts("NO \n");
return 0;
}
}
puts("YES \n");
} else
puts("NO \n");
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int d,s,t;
cin>>d>>t>>s;
if(d*1.0/s>t) cout<<"No";
else cout<<"Yes";
} | 0 |
#include <iostream>
#include <algorithm>
using namespace std;
int arr[100005];
int main()
{
int n,ans;
scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%d",&arr[i]);
sort(arr,arr+n);
long long sum=0;
for (int i=0;i<n;i++)
{
if (arr[i]>2*sum)
ans=1;
else
ans++;
sum+=arr[i];
}
printf("%d",ans);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline istream& operator>>(istream& str, vector<T>& a) {
for (auto& i : a) str >> i;
return str;
}
long long M = 1e9 + 7;
void solve() {
long long int k;
cin >> k;
vector<char> v = {'c', 'o', 'd', 'e', 'f', 'o', 'r', 'c', 'e', 's'};
vector<long long int> v2(10, 1);
long long int x = 1;
while (x < k) {
for (long long int j = 0; x < k && j < 10; j++) {
x *= (v2[j] + 1);
x /= v2[j]++;
}
}
for (long long int i = 0; i < 10; i++) {
long long int x = v2[i];
for (long long int j = 0; j < x; j++) {
cout << v[i];
}
}
cout << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long int t;
t = 1;
while (t--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int vis[109];
int len[109];
int main() {
int n;
cin >> n;
string w;
for (int i = 0; i < n; ++i) {
cin >> w;
for (int j = 0; j < w.length(); ++j) vis[i] |= 1 << (w[j] - 'a');
len[i] = w.length();
}
int mx = 0, sel;
for (int i = 0; i < 26; ++i)
for (int j = 0; j < 26; ++j) {
sel = 0;
int k = (1 << i) | (1 << j);
for (int u = 0; u < n; ++u)
if ((k & vis[u]) == vis[u]) sel += len[u];
mx = max(mx, sel);
}
cout << mx << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
long long int ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p);
p = (p * p);
}
return (T)ret;
}
int toInt(string s) {
int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
int toLlint(string s) {
long long int sm;
stringstream ss(s);
ss >> sm;
return sm;
}
bool al[22][22];
int rt, k, n;
long long int p[25][(1 << 19) + 9];
long long int dp(int u, int msk) {
if (u == rt && msk != k) return 1LL;
long long int &pr = p[u][msk];
if (pr != -1) return pr;
pr = 0LL;
for (int i = 0; i < n; i++) {
if (al[u][i] == 0) continue;
if (msk & (1 << i)) continue;
al[u][i] = 0;
al[i][u] = 0;
pr += dp(i, (msk | (1 << i)));
al[u][i] = 1;
al[i][u] = 1;
}
return pr;
}
int main() {
int cs = 1, t, i, j;
long long int rs;
scanf("%d%d", &n, &k);
while (k--) {
scanf("%d%d", &i, &j);
i--;
j--;
al[i][j] = 1;
al[j][i] = 1;
}
k = 0;
rs = 0;
for (i = 0; i < n; i++) {
rt = i;
memset(p, -1, sizeof(p));
rs += dp(rt, k);
k |= (1 << i);
}
cout << rs / 2LL << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, h[300005], ans;
long long stk1, stk2;
signed main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
scanf("%d", &h[i]);
ans += h[i] / 2;
if (h[i] % 2) {
if (i % 2) {
stk1++;
} else {
stk2++;
}
}
}
cout << ans + min(stk1, stk2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 88;
const int maxn = 2e5 + 88;
long long a[maxn], cur[10], cura[10];
long long curbase[10][10], base[10][10];
long long k;
int n;
int readint() {
int cur;
scanf("%d", &cur);
return cur;
}
long long readll() {
long long cur;
scanf("%I64d", &cur);
return cur;
}
void writell(long long x) { printf("%I64d", x); }
long long brut(long long res) {
for (int i = 0; i < n; i++)
if (a[i] >= k)
return res;
else
a[i] = a[i] + (i ? a[i - 1] : 0);
return brut(res + 1);
}
long long umn(long long a, long long b) {
long long cur = a * b;
if (b != 0 && cur / b != a) cur = k;
return min(cur, k);
}
void summ(long long &a, long long b) { a = min(a + b, k); }
void umncurbase() {
int i, j, k;
for (i = 0; i < n; i++) cura[i] = 0;
for (j = 0; j < n; j++)
for (k = 0; k < n; k++) summ(cura[j], umn(cur[k], base[k][j]));
for (i = 0; i < n; i++) cur[i] = cura[i];
}
void umnbasebase() {
int i, j, k;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) curbase[i][j] = 0;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++) summ(curbase[i][j], umn(base[i][k], base[k][j]));
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) base[i][j] = curbase[i][j];
}
void binpow(long long pow) {
if (pow == 1) {
int i;
i = 1;
}
int i, j;
long long curpow = pow;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) base[i][j] = (i <= j) ? 1 : 0;
for (i = 0; i < n; i++) cur[i] = a[i];
while (curpow) {
if (curpow % 2) {
curpow--;
umncurbase();
} else {
curpow /= 2;
umnbasebase();
}
}
}
bool check(long long pow) {
binpow(pow);
for (int i = 0; i < n; i++)
if (cur[i] == k) return true;
return false;
}
long long bin() {
long long l = 0, r = inf;
while (l < r) {
long long mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid + 1;
}
return l;
}
int main() {
int i;
n = readint();
k = readll();
for (i = 0; i < n; i++) a[i] = readint();
int cnt0 = 0;
while (a[cnt0] == 0) cnt0++;
for (i = 0; i < n - cnt0; i++) a[i] = a[i + cnt0];
n -= cnt0;
if (n > 10)
writell(brut(0));
else
writell(bin());
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
int cNum, sum = 0;
int middle = (n - 1) / 2, lDiag = 0, rDiag = n - 1;
for (int i = 0; i < n; ++i, rDiag--, lDiag++) {
for (int j = 0; j < n; ++j) {
scanf("%d", &cNum);
if (i == middle) {
sum += cNum;
} else if (j == middle) {
sum += cNum;
} else if (j == lDiag && j == rDiag) {
sum += cNum;
} else if (j == lDiag || j == rDiag) {
sum += cNum;
}
}
}
printf("%d\n", sum);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int n;
vector<long long int> v;
long long int l, r;
void solve() {
vector<long long int> bits(31, 0);
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) {
cin >> v[i];
long long int temp = v[i];
long long int x = 0;
while (temp > 0) {
if ((temp & 1)) bits[x]++;
x++;
temp = temp >> 1;
}
}
long long int b = -1;
for (long long int i = 0; i < 31; i++) {
if (bits[i] == 1) b = i;
}
long long int ind = -1;
for (long long int i = 0; i < n; i++) {
if (((v[i] >> b) & 1)) ind = i;
}
if (ind != -1) cout << v[ind] << " ";
for (long long int i = 0; i < n; i++) {
if (i != ind) cout << v[i] << " ";
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
for (long long int i = 0; i < t; i++) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
const int INF = 1e9;
vector<int> g[maxn * 2][2];
int cost[maxn][maxn][2], L[maxn * 2][2], R[maxn * 2][2], DP[maxn],
sz[maxn * 2][2];
int N, A, B;
void DFS(int v, int f) {
sz[v][f] = v != 1;
for (int u : g[v][f]) {
DFS(u, f);
sz[v][f] += sz[u][f];
L[v][f] = min(L[v][f], L[u][f]);
R[v][f] = max(R[v][f], R[u][f]);
}
cost[L[v][f]][R[v][f]][f] = max(cost[L[v][f]][R[v][f]][f], sz[v][f]);
if (0)
printf("cost[%d][%d][%d] = %d\n", L[v][f], R[v][f], f,
cost[L[v][f]][R[v][f]][f]);
}
int main() {
int i, j;
scanf("%d", &N);
scanf("%d", &A);
for (i = 1; i < maxn * 2; ++i) {
for (j = 0; j < 2; ++j) {
L[i][j] = INF;
R[i][j] = -INF;
}
}
for (i = 2; i <= A; ++i) {
int p;
scanf("%d", &p);
g[p][0].push_back(i);
}
for (i = 1; i <= N; ++i) {
int x;
scanf("%d", &x);
L[x][0] = R[x][0] = i;
}
scanf("%d", &B);
for (i = 2; i <= B; ++i) {
int p;
scanf("%d", &p);
g[p][1].push_back(i);
}
for (i = 1; i <= N; ++i) {
int x;
scanf("%d", &x);
L[x][1] = R[x][1] = i;
}
DFS(1, 0);
DFS(1, 1);
for (i = 1; i <= N; ++i) {
for (j = 0; j < i; ++j) {
DP[i] = max(DP[i], DP[j] + max(cost[j + 1][i][0], cost[j + 1][i][1]));
}
if (0) printf("DP[%d] = %d\n", i, DP[i]);
}
printf("%d\n", DP[N]);
}
| 6 |
#include <iostream>
int main()
{
char A;
std::cin >> A;
A++;
std::cout << A;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101;
const int MAXNODES = 5;
int N;
bool visit[MAXNODES];
int from[MAXN], to[MAXN], value[MAXN], degree[MAXNODES];
vector<int> G[MAXNODES][MAXNODES];
void input() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> from[i] >> value[i] >> to[i];
}
}
void init(int u, int v) {
for (int i = 1; i <= 4; i++) {
degree[i] = 0;
visit[i] = false;
for (int j = 1; j <= 4; j++) {
G[i][j].clear();
}
}
for (int i = 1; i <= N; i++) {
if (i == u || i == v) continue;
G[from[i]][to[i]].push_back(value[i]);
G[to[i]][from[i]].push_back(value[i]);
degree[from[i]]++;
degree[to[i]]++;
}
}
void DFS(int node, int &oddDegree, long long &cur) {
visit[node] = true;
if (degree[node] % 2 == 1) oddDegree++;
for (int nxt = 1; nxt <= 4; nxt++) {
for (int add : G[node][nxt]) cur += add;
if (visit[nxt] || G[node][nxt].empty()) continue;
DFS(nxt, oddDegree, cur);
}
}
long long solve(int u, int v) {
init(u, v);
long long ret = 0;
for (int i = 1; i <= 4; i++) {
if (!visit[i]) {
int oddDegree = 0;
long long cur = 0;
DFS(i, oddDegree, cur);
if (oddDegree <= 2) ret = max(ret, cur / 2);
}
}
return ret;
}
void solve() {
long long ans = 0;
for (int i = 0; i <= N; i++) {
for (int j = i; j <= N; j++) {
ans = max(ans, solve(i, j));
}
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
input();
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n;
bool check(long long x) {
long long temp = n, sum = 0;
while (temp) {
if (temp > x)
temp -= x, sum += x;
else
sum += temp, temp = 0;
temp -= temp / 10;
}
if (sum >= (n + 1) / 2) return true;
return false;
}
long long erfen(long long l, long long r) {
while (l < r) {
long long mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
return l;
}
int main() {
scanf("%lld", &n);
if (n < 10)
printf("1\n");
else {
long long l = 1, r = n;
long long ans = erfen(l, r);
printf("%lld\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long mod(long long a) { return a > 0 ? a : -a; }
const int N = 333333;
long long dp[N], best[N], t[N];
int main() {
int n, m, dd;
cin >> n >> m >> dd;
for (int i = 1; i <= m; i++) {
long long a, b;
cin >> a >> b >> t[i];
long long d = (t[i] - t[i - 1]) * dd;
if (d > n) d = n;
deque<pair<long long, long long> > window;
for (int j = 1; j <= n; j++) {
while (!window.empty() && window.back().first <= dp[j]) window.pop_back();
window.push_back(pair<long long, long long>(dp[j], j));
while (window.front().second < j - 2 * d) window.pop_front();
best[j] = window.front().first;
}
for (int j = n + 1; j <= n + d; j++) {
while (window.front().second < j - 2 * d) window.pop_front();
best[j] = window.front().first;
}
for (int i = 1; i <= n; i++) dp[i] = best[i + d] + b - mod(a - i);
}
cout << *max_element(dp + 1, dp + n + 1) << endl;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int X,A,B;
cin>>X>>A>>B;
if(B<=A)cout<<"delicious"<<endl;
else if(B>A+X)cout<<"dangerous"<<endl;
else cout<<"safe"<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
string a, b, tmp;
int main() {
ios_base::sync_with_stdio(0);
cin >> a >> b;
n = (int)a.length();
for (int i = 0; i < n; i++) {
if (a[i] < 'z') {
tmp = a;
tmp[i]++;
for (int h = i + 1; h < n; h++) tmp[h] = 'a';
if (tmp < b) {
cout << tmp;
return 0;
}
}
}
cout << "No such string";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkmax(T& x, T y) {
if (x < y) x = y;
}
template <typename T>
void chkmin(T& x, T y) {
if (x > y) x = y;
}
inline int read() {
int x = 0;
char c = getchar();
bool f = 0;
while (c < 48) f |= c == '-', c = getchar();
while (c > 47) x = x * 10 + (c ^ 48), c = getchar();
return f ? -x : x;
}
const int maxn = 1e5 + 10;
int n;
struct point {
long long x, y;
} A[maxn];
bool check(long long mid) {
static long long pmn[maxn], pmx[maxn], smn[maxn], smx[maxn];
for (int i = (1), iend = (n); i <= iend; ++i) {
pmn[i] = smn[i] = smx[i] = pmx[i] = A[i].y;
}
for (int i = (2), iend = (n); i <= iend; ++i)
chkmin(pmn[i], pmn[i - 1]), chkmax(pmx[i], pmx[i - 1]);
for (int i = (n - 1), iend = (1); i >= iend; --i)
chkmin(smn[i], smn[i + 1]), chkmax(smx[i], smx[i + 1]);
for (int i = 1, j = 1; i <= n; i++) {
while (A[i].x - A[j].x > 2 * mid) j++;
long long mx = -1e10, mn = 1e10;
if (j > 1) chkmax(mx, pmx[j - 1]), chkmin(mn, pmn[j - 1]);
if (i < n) chkmax(mx, smx[i + 1]), chkmin(mn, smn[i + 1]);
if (mx - mn <= 2 * mid) return 1;
}
return 0;
}
void solve() {
cin >> n;
for (int i = (1), iend = (n); i <= iend; ++i) {
long long x = read(), y = read();
A[i].x = (x + y) * 2, A[i].y = (x - y) * 2;
}
sort(A + 1, A + n + 1, [](point X, point Y) { return X.x < Y.x; });
long long l = 0, r = 2e9;
while (l < r) {
long long mid = (l + r) / 2;
check(mid) ? r = mid : l = mid + 1;
}
printf("%.10lf\n", l / 2.0);
}
signed main() {
solve();
return 0;
}
| 4 |
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
int n;
int len[100];
char word[100][100];
int memo[14][1 << 14];
int memo2[20][20];
int calc2(int next, int prev) {
if (memo2[next][prev] != -1) { return memo2[next][prev]; }
int ret = len[next];
REP(i, len[prev]) {
bool ok = true;
FOR(j, i, len[prev]) {
if (word[prev][j] != word[next][j - i]) { ok = false; break; }
}
if (ok) { ret = len[next] - (len[prev] - i); break; }
}
return memo2[next][prev] = ret;
}
int calc(int prev, int use) {
if (use == (1 << n) - 1) { return 0; }
if (memo[prev][use] != -1) { return memo[prev][use]; }
int ret = 1 << 30;
REP(i, n) {
if (use & (1 << i)) { continue; }
int nuse = use | (1 << i);
int d = calc2(i, prev);
ret = min(ret, calc(i, nuse) + d);
}
return memo[prev][use] = ret;
}
int main() {
while (scanf("%d", &n), n) {
MEMSET(memo, -1);
MEMSET(memo2, -1);
REP(i, n) {
scanf("%s", word[i]);
len[i] = strlen(word[i]);
}
int use = 0;
REP(i, n) {
REP(j, n) {
if (i == j) { continue; }
if (strstr(word[j], word[i]) == NULL) { continue; }
use |= 1 << i;
}
}
int ans = 1 << 30;
REP(i, n) {
if (use & (1 << i)) { continue; }
ans = min(ans, calc(i, use | (1 << i)) + len[i]);
}
cout << ans << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string foo;
getline(cin, foo);
for (int i = 0; i < n; i++) {
string s;
getline(cin, s);
string st = s.substr(0, min(5, (int)s.length())),
b = s.substr(max((int)s.length() - 5, 0), 5);
cerr << st << " " << b << "\n";
if (st == "miao." && b == "lala.")
cout << "OMG>.< I don't know!";
else if (st == "miao.")
cout << "Rainbow's";
else if (b == "lala.")
cout << "Freda's";
else
cout << "OMG>.< I don't know!";
cout << "\n";
}
return 0;
}
| 1 |
Subsets and Splits