solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 2e5 + 9;
void fixit(string &s) {
string tmp = s;
while (tmp.size() > 0 && tmp.back() == ' ') tmp.pop_back();
reverse(tmp.begin(), tmp.end());
while (tmp.size() > 0 && tmp.back() == ' ') tmp.pop_back();
reverse(tmp.begin(), tmp.end());
s = tmp;
}
pair<string, string> SolveCatch(string s) {
int idx = 0;
string ret, ret2;
while (s[idx++] != '(')
;
idx += (s[idx] == ' ');
while (s[idx] != ',') ret.push_back(s[idx++]);
idx++;
int cnt = 0;
while (cnt < 2) {
cnt += (s[idx] == '"');
if (s[idx] != '"' && cnt) ret2.push_back(s[idx]);
idx++;
}
fixit(ret), fixit(ret2);
return {ret, ret2};
}
string solvethrow(string s) {
string ret;
int idx = 0;
while (s[idx++] != '(')
;
idx += (s[idx] == ' ');
while (s[idx] != ')') ret.push_back(s[idx++]);
if (ret.back() == ' ') ret.pop_back();
fixit(ret);
return ret;
}
stack<int> tries;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
string s;
cin.ignore();
string excep = "$";
int idx = -1;
for (int i = 1; i <= n; i++) {
getline(cin, s);
fixit(s);
if (s[0] == 'c') {
auto it = SolveCatch(s);
int thetop = tries.top();
tries.pop();
if (thetop < idx && it.first == excep) cout << it.second, exit(0);
} else if (s.substr(0, 2) == "th")
excep = solvethrow(s), idx = i;
else if (s[0] == 't')
tries.push(i);
}
cout << "Unhandled Exception";
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
long long e[100002];
int main()
{
long long a,b,c,d=0,i;
cin>>a>>b>>c;
for(i=1;i<=a;i++)
{
cin>>e[i];
if(e[i]<b||e[i]>=c)
d++;
}
cout<<d;
cout<<endl;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int m,n,i,j,ans;
int main()
{
scanf("%d%d",&m,&n);
for(j=1;j<=m;j++)
for(i=1;i<=n;i++)
if(i%10>=2&&i/10>=2&&j==(i%10)*(i/10))
ans++;
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
const int x = 1;
using namespace std;
long long qpow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res *= a, res %= mod;
a *= a;
a %= mod;
b >>= 1;
}
return res;
}
long long isprime(long long n) {
int i;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
for (i = 3; i <= sqrt(n); i += 2)
if (n % i == 0) return 0;
return 1;
}
int main() {
char a[40][100] = {
"111111101010101111100101001111111", "100000100000000001010110001000001",
"101110100110110000011010001011101", "101110101011001001111101001011101",
"101110101100011000111100101011101", "100000101010101011010000101000001",
"111111101010101010101010101111111", "000000001111101111100111100000000",
"100010111100100001011110111111001", "110111001111111100100001000101100",
"011100111010000101000111010001010", "011110000110001111110101100000011",
"111111111111111000111001001011000", "111000010111010011010011010100100",
"101010100010110010110101010000010", "101100000101010001111101000000000",
"000010100011001101000111101011010", "101001001111101111000101010001110",
"101101111111000100100001110001000", "000010011000100110000011010000010",
"001101101001101110010010011011000", "011101011010001000111101010100110",
"111010100110011101001101000001110", "110001010010101111000101111111000",
"001000111011100001010110111110000", "000000001110010110100010100010110",
"111111101000101111000110101011010", "100000100111010101111100100011011",
"101110101001010000101000111111000", "101110100011010010010111111011010",
"101110100100011011110110101110000", "100000100110011001111100111100000",
"111111101101000101001101110010001"};
int c, b;
cin >> c >> b;
cout << a[c][b];
}
| 2 |
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll num_digit(ll n){
ll nd = 0;
for(nd = 0; n > 0; ++nd){
n /= 10;
}
return nd;
}
ll sn(ll n){
ll ret = 0;
while(n > 0){
ret += n % 10;
n /= 10;
}
return ret;
}
ll f(ll x){
ll nd = num_digit(x);
ll min_n = x;
ll min_sn = sn(x);
ll d10 = 1;
for(ll d = 0; d <= nd + 1; ++d, d10 *= 10){
ll x2 = d10 * (x / d10 + 1) - 1;
if(x2 <= x){ continue; }
ll sx2 = sn(x2);
if(min_n * sx2 > x2 * min_sn){
min_n = x2;
min_sn = sx2;
}
}
return min_n;
}
int main(){
ll k;
cin >> k;
ll x = 1;
for(int i = 0; i < k; ++i, x = f(x + 1)){
cout << x << endl;
}
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int B[102][102];
bool maze = false;
void DFS(int Y, int X, int YG, int XG, int C)
{
if(B[Y][X]!=C || C==0)
return;
if(Y==YG && X==XG)
maze = true;
B[Y][X] = 0;
DFS(Y-1, X , YG, XG, C);
DFS(Y , X+1, YG, XG, C);
DFS(Y+1, X , YG, XG, C);
DFS(Y , X-1, YG, XG, C);
}
int main()
{
while(true){
int w, h;
cin >> w >> h;
if(w==0 && h==0)
break;
int xs, ys, xg, yg, n;
cin >> xs >> ys;
cin >> xg >> yg;
cin >> n;
for(int y=0; y<=h; y++){
for(int x=0; x<=w; x++){
B[y][x] = 0;
}
}
for(int i=0; i<n; i++){
int c, d, x, y;
cin >> c >> d >> x >> y;
if(d==0){
B[y ][x ] = c;
B[y ][x+1] = c;
B[y ][x+2] = c;
B[y ][x+3] = c;
B[y+1][x ] = c;
B[y+1][x+1] = c;
B[y+1][x+2] = c;
B[y+1][x+3] = c;
}
else{
B[y ][x ] = c;
B[y+1][x ] = c;
B[y+2][x ] = c;
B[y+3][x ] = c;
B[y ][x+1] = c;
B[y+1][x+1] = c;
B[y+2][x+1] = c;
B[y+3][x+1] = c;
}
}
maze = false;
DFS(ys, xs, yg, xg, B[ys][xs]);
if(maze){
cout << "OK" << endl;
}
else{
cout << "NG" << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200005;
int n, m, a[N];
ll b[N], r;
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%d", a + i);
if(i > 1){
int x = a[i - 1], y = a[i];
if(x > y) y += m;
r += (y - x);
b[x + 2]++;
b[y + 1] -= (y - x);
b[y + 2] += (y - x - 1);
}
}
for(int i = 1; i <= 2 * m; i++) b[i] += b[i - 1];
for(int i = 1; i <= 2 * m; i++) b[i] += b[i - 1];
for(int i = 1; i <= m; i++) b[i] += b[m + i];
printf("%lld\n", r - *max_element(b + 1, b + m + 1));
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
double eps = 1e-12;
long long hcf(long long p, long long q) {
if (p > q) return hcf(q, p);
if (!p) return q;
return hcf(q % p, p);
}
long long lcm(long long p, long long q) { return p * q / hcf(p, q); }
long long power(long long a, long long n, long long mod) {
long long ans = 1;
while (n) {
if (n % 2) ans *= a;
a *= a;
n /= 2;
a %= mod;
ans %= mod;
}
return ans;
}
long long fac(long long k, long long mod) {
long long ans = 1;
for (long long i = 1; i < k + 1; i++) {
ans *= i;
ans %= mod;
}
return ans;
}
void solve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) cin >> a[i];
string s = to_string(a[0]);
long long l = ((long long)(s).size());
long long ans = 0;
long long mod = 998244353;
for (long long i = 0; i < 2 * l; i += 2) {
for (long long j = 0; j < n; j++) {
long long d = a[j] % 10;
a[j] /= 10;
long long k1 = power(10, i, 998244353), k2 = power(10, i + 1, 998244353);
long long k = k1 + k2;
k %= mod;
k *= d;
k %= mod;
ans += k;
ans %= mod;
}
}
ans *= n;
ans %= mod;
cout << ans << "\n";
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
int n, i, sum, s, tab[105];
vector<int> c;
int main() {
scanf("%d", &n);
sum = 0;
s = 0;
c.clear();
for (i = 0; i < n; i++) {
scanf("%d", &tab[i]);
sum += tab[i];
if (i == 0 || tab[i] * 2 <= tab[0]) {
s += tab[i];
c.push_back(i + 1);
}
}
if (s <= sum - s)
printf("0\n");
else {
printf("%d\n", c.size());
for (auto& w : c) {
printf("%d ", w);
}
printf("\n");
}
return 0;
}
| 1 |
#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<long long int, long long int> P;
long long int INF = 1e18;
long long int MOD = 1e9 + 7;
long long int GCD(long long int a, long long int b){
long long int dummy1, dummy2, dummy;
dummy1 = max(a, b);
dummy2 = min(a, b);
while(true){
dummy = dummy1 % dummy2;
dummy1 = dummy2;
dummy2 = dummy;
if(dummy == 0){
break;
}
}
return dummy1;
}
int main(){
int T;
cin >> T;
for(int loop = 0; loop < T; loop++){
long long int h, w;
cin >> h >> w;
long long int G = GCD(h, w);
h /= G;
w /= G;
if(h == w){
cout << 1 << " " << 0 << endl;
continue;
}
if(h % 2 == 0 || w % 2 == 0){
cout << 1 << " " << 1 << endl;
continue;
}
cout << h * w / 2 + 1 << " " << h * w / 2 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[3005], b[3005];
long long dp[3005][3005];
int main() {
int n;
while (~scanf("%d", &n)) {
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
a[i] -= i;
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
int len = unique(b + 1, b + 1 + n) - b - 1;
long long mine;
for (int i = 1; i <= n; i++) {
mine = 9223372036854775807;
for (int j = 1; j <= len; j++) {
mine = min(dp[i - 1][j], mine);
dp[i][j] = abs(a[i] - b[j]) + mine;
}
}
mine = 9223372036854775807;
for (int i = 1; i <= len; i++) mine = min(dp[n][i], mine);
printf("%lld\n", mine);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void Fast_Read_Out() {
ios_base::sync_with_stdio(0);
cin.tie(), cout.tie();
}
void Random() {
unsigned int seed;
asm("rdtsc" : "=A"(seed));
srand(seed);
}
unsigned int Time() {
unsigned int time = clock() / 1000.00;
return time;
}
const int inf = int(1e9) + 123;
const int N = int(2e5) + 123;
long long a[N], pref[N];
vector<long long> v;
int main() {
Random();
Fast_Read_Out();
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
v.push_back(pref[i]);
}
for (int j = 1; j <= m; j++) {
long long q;
cin >> q;
long long pos = lower_bound(v.begin(), v.end(), q) - v.begin();
pos++;
q -= pref[pos - 1];
cout << pos << ' ' << q << endl;
}
}
| 3 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)
int N, A[1 << 18];
int minx = (1 << 30), minid = 0;
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
if (minx > A[i]) { minx = A[i]; minid = i; }
}
cout << minid << endl;
return 0;
}
| 0 |
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int S[100000];
int numS;
cin >> numS;
for (int i = 0; i < numS; i++) {
cin >> S[i];
}
int numT;
int sum = 0;
cin >> numT;
for (int i = 0; i < numT; i++) {
int key;
cin >> key;
if (*lower_bound(S, S + numS, key) == key) sum++;
}
cout << sum << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int min(int a, int b) {
if (a > b)
return b;
else
return a;
}
int main() {
int n, m, a[101], b[101][101], i, j, r, sum, x, y;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
memset(b, 0, sizeof(b));
for (i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
b[x - 1][y - 1] = 1;
b[y - 1][x - 1] = 1;
}
sum = 999999999;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (b[i][j] == 1) {
for (r = 0; r < n; r++) {
if (b[j][r] == 1) {
if (b[r][i] == 1) sum = min(sum, a[i] + a[j] + a[r]);
}
}
}
}
}
if (sum == 999999999)
printf("-1\n");
else
printf("%d\n", sum);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<long long> P;
long long abs1(long long x) {
if (x < 0) return -x;
return x;
}
long long xx[1002], yy[1002];
int main() {
int n;
long long x, y;
scanf("%d%I64d%I64d", &n, &x, &y);
int T = x + y;
for (int i = 1; i <= n; i++) scanf("%I64d", &xx[i]);
for (int i = 1; i <= n; i++) scanf("%I64d", &yy[i]);
for (int i = 1; i <= n; i++) P.push(abs1(xx[i] - yy[i]));
while (T--) {
long long v = P.top();
P.pop();
v--;
P.push(abs1(v));
}
long long ans = 0;
while (!P.empty()) ans += P.top() * P.top(), P.pop();
printf("%I64d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + 5;
long long n, a[maxN], k;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
priority_queue<long long> pq;
long long Sum = 0;
for (int i = n; i >= 2; --i) {
Sum += a[i];
pq.push(Sum);
}
long long res = Sum + a[1], cnt = 1;
while (cnt < k) {
res += pq.top();
pq.pop();
cnt++;
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int m;
int get (const string& s) {
int n = (int) s.size();
vector <int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n and s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
vector <bool> vis(n, false);
int ans = 0;
for (int i = m; i < n; i++) {
if (vis[i]) continue;
vis[i] = true;
if (z[i] >= m) {
int cnt = 1;
int j = i;
while (j + m < n and z[j + m] >= m) {
j += m;
vis[j] = true;
cnt++;
}
ans = max(ans, cnt);
}
}
return ans;
}
int main () {
ios::sync_with_stdio(false); cin.tie(0);
string s, t;
cin >> s >> t;
m = sz(t);
string S = s;
while (sz(S) < sz(t)) S = S + S;
string X = S + S;
string Y = X + S;
string Z = Y + S;
// int ans1 = get(t + X);
double t1 = clock();
int ans2 = get(t + Y);
double t2 = clock();
if ((t2 - t1) / CLOCKS_PER_SEC > 1.2) {
cout << -1 << endl;
return (0);
}
int ans3 = get(t + Z);
if (ans2 != ans3) {
cout << -1 << endl;
return (0);
} else {
cout << ans2 << endl;
}
return (0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
struct node {
int id;
};
node a, b, c;
int xr[N], d[N];
int mark[N];
int vis[N];
vector<int> mp[N];
set<pair<int, int> > st;
int main() {
int n, s, m = 0, k = 0;
queue<node> q;
;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &d[i], &xr[i]);
m += d[i];
a.id = i;
if (d[i] == 1) {
q.push(a);
}
}
while (q.size()) {
b = q.front();
q.pop();
int id = b.id;
int bd = d[xr[id]];
d[id]--;
d[xr[id]]--;
if (d[id] < 0) continue;
if (d[xr[id]] == 1) {
c.id = xr[id];
q.push(c);
}
xr[xr[id]] ^= id;
int xa = id;
int xb = xr[id];
if (xb < xa) swap(xa, xb);
st.insert(make_pair(xa, xb));
}
cout << m / 2 << endl;
set<pair<int, int> >::iterator it;
for (it = st.begin(); it != st.end(); it++) {
printf("%d %d\n", it->first, it->second);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
set<int> v[k + 1];
vector<int> in(k + 1, 0);
vector<int> out(k + 1, 0);
auto add_edge = [&](int a, int b) {
if (v[a].find(b) != v[a].end()) return;
v[a].insert(b);
in[b]++;
out[a]++;
};
for (int i = 1; i <= n; i++) {
int c;
cin >> c;
int s;
cin >> s;
for (int j = 2; j <= c; j++) {
int a;
cin >> a;
add_edge(s, a);
s = a;
}
}
vector<bool> vis(k + 1, 0);
vector<int> cnt(m + 1, 0);
for (int i = 1; i <= k; i++) {
if (vis[i] || in[i] >= 1 || out[i] > 1) continue;
bool ok = 1;
int a = i;
int len = 0;
while (1) {
ok &= (in[a] <= 1 && out[a] <= 1 && !vis[a]);
if (!ok) break;
vis[a] = 1;
len++;
if (out[a] == 0) break;
a = (*v[a].begin());
}
if (ok && len <= m) cnt[len]++;
}
vector<array<int, 2>> opt;
for (int i = 1; i <= m; i++)
if (cnt[i] > 0) opt.push_back({i, cnt[i]});
const long long mod = 998244353;
auto add = [&](long long a, long long b) -> long long {
return (a + b) % mod;
};
auto mul = [&](long long a, long long b) -> long long {
return (a * b) % mod;
};
vector<long long> dp(m + 1, 0);
dp[0] = 1;
for (int i = 0; i <= m; i++) {
for (auto [c, o] : opt)
if (i + c <= m) dp[i + c] = add(dp[i + c], mul(o, dp[i]));
}
cout << dp[m] << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int n, m, i;
scanf("%d %d", &n, &m);
char s[5];
int flag = 0;
m = m * n;
for (i = 0; i < m; i++) {
scanf("%s", &s);
if (s[0] == 'C' || s[0] == 'M' || s[0] == 'Y') {
flag = 1;
}
}
if (flag == 0)
printf("#Black&White\n");
else
printf("#Color\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
char ch[100][100];
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
cin >> ch[i][j];
}
}
int flag = 0;
int sum = 0;
for (int i = 0; i != n; ++i) {
int rflag = 0;
for (int j = 0; j != n; ++j) {
if ('.' == ch[i][j]) {
rflag = 1;
break;
}
}
if (rflag) ++sum;
}
if (n == sum) flag ^= 1;
sum = 0;
for (int j = 0; j != n; ++j) {
int rflag = 0;
for (int i = 0; i != n; ++i) {
if ('.' == ch[i][j]) {
rflag = 1;
break;
}
}
if (rflag) ++sum;
}
if (n == sum) flag ^= 2;
if (flag & 1) {
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if ('.' == ch[i][j]) {
cout << i + 1 << ' ' << j + 1 << endl;
break;
}
}
}
} else if (flag & 2) {
for (int j = 0; j != n; ++j) {
for (int i = 0; i != n; ++i) {
if ('.' == ch[i][j]) {
cout << i + 1 << ' ' << j + 1 << endl;
break;
}
}
}
} else
cout << "-1" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 100100, mod = 1e9 + 7, maxs = 321, maxa = 1e6 + 100,
maxb = 24;
const long long inf = 2e18 + 4;
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }
long long seg[4 * maxn];
long long lazy[4 * maxn];
set<pair<pair<long long, long long>, long long> > st;
set<pair<pair<long long, long long>, long long> >::iterator it;
void shift(long long x, long long l, long long r) {
long long mid = (l + r) / 2;
lazy[2 * x] += lazy[x];
seg[2 * x] += (mid - l) * lazy[x];
lazy[2 * x + 1] += lazy[x];
seg[2 * x + 1] += (r - mid) * lazy[x];
lazy[x] = 0;
}
void update(long long x, long long l, long long r, long long s, long long t,
long long val) {
if (l >= s && r <= t) {
lazy[x] += val;
seg[x] += (r - l) * val;
return;
}
shift(x, l, r);
long long mid = (l + r) / 2;
if (s < mid) {
update(2 * x, l, mid, s, min(mid, t), val);
}
if (t > mid) {
update(2 * x + 1, mid, r, max(s, mid), t, val);
}
seg[x] = seg[2 * x] + seg[2 * x + 1];
}
long long get(long long x, long long l, long long r, long long s, long long t) {
if (l >= s && r <= t) {
return seg[x];
}
shift(x, l, r);
long long mid = (l + r) / 2;
long long res = 0;
if (s < mid) {
res += get(2 * x, l, mid, s, min(mid, t));
}
if (t > mid) {
res += get(2 * x + 1, mid, r, max(s, mid), t);
}
return res;
}
int32_t main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, q;
cin >> n >> q;
for (long long i = 0; i < n; i++) {
st.insert(make_pair(make_pair(i, i + 1), i + 1));
}
while (q--) {
long long t, l, r, x;
cin >> t >> l >> r;
l--;
if (t == 1) {
cin >> x;
it = st.lower_bound(make_pair(make_pair(r, -inf), -inf));
while (it != st.begin()) {
it--;
long long cl = it->first.first;
long long cr = it->first.second;
long long cx = it->second;
long long val = abs(x - cx);
if (cr <= l) {
break;
}
st.erase(it);
if (cr > r && cl >= l) {
update(1, 0, maxn, cl, r, val);
st.insert(make_pair(make_pair(r, cr), cx));
} else if (cr <= r && cl < l) {
update(1, 0, maxn, l, cr, val);
st.insert(make_pair(make_pair(cl, l), cx));
} else if (cr <= r && cl >= l) {
update(1, 0, maxn, cl, cr, val);
} else {
update(1, 0, maxn, l, r, val);
st.insert(make_pair(make_pair(cl, l), cx));
st.insert(make_pair(make_pair(r, cr), cx));
}
it = st.lower_bound(make_pair(make_pair(r, -inf), -inf));
}
st.insert(make_pair(make_pair(l, r), x));
continue;
}
cout << get(1, 0, maxn, l, r) << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool f1, f6, f8, f9;
int main() {
string s;
cin >> s;
int n = s.size();
string ns = "";
int p = 0;
int st = 1;
for (int i = 0; i < n; i++) {
if (s[i] == '1' && !f1) {
f1 = true;
} else if (s[i] == '6' && !f6) {
f6 = true;
} else if (s[i] == '8' && !f8) {
f8 = true;
} else if (s[i] == '9' && !f9) {
f9 = true;
} else {
p *= 10;
p %= 7;
p += (int)(s[i] - '0');
p %= 7;
ns += s[i];
st *= 10;
st %= 7;
}
}
int v = (7 - p) % 7;
string u = "1689";
do {
int k = atoi(u.c_str());
if (((k * st) % 7) == v) {
cout << k << ns << endl;
return 0;
}
} while (next_permutation(u.begin(), u.end()));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> mas(n);
for (int i = 0; i < n; ++i) {
cin >> mas[i];
}
for (int i = 0; i < m; ++i) {
string last = "";
for (int j = 0; j < n; ++j) {
if (mas[j][i] == '#') {
if (last == "") {
last = mas[j];
} else {
if (last != mas[j]) {
cout << "No" << endl;
return 0;
}
}
}
}
}
cout << "Yes" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a, h, x, y;
bool flag = false;
for (int i = 0; i < n; i++) {
cin >> x >> y;
if (flag) continue;
if (i == 0)
h = max(x, y);
else {
a = max(x, y);
if (a <= h)
h = a;
else {
a = min(x, y);
if (a <= h)
h = a;
else
flag = true;
}
}
}
if (flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N=505;
int A[N][N], D[31][31];
int dp[3][31];
int main() {
int n,c;
cin >> n >> c;
for (int i=0; i<c; i++) {
for (int j=0; j<c; j++) {
cin >> D[i][j];
}
}
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> A[i][j];
A[i][j]--;
}
}
for (int i=0; i<c; i++) {
for (int x=0; x<n; x++) {
for (int y=0; y<n; y++) {
int t=(x+y)%3;
dp[t][i]+=D[A[x][y]][i];
}
}
}
int ans=1e9;
for (int i=0; i<c; i++) {
for (int j=0; j<c; j++) {
if ( i==j ) continue;
for (int k=0; k<c; k++) {
if ( i==k || j==k ) continue;
int res=dp[0][i]+dp[1][j]+dp[2][k];
ans=min(res,ans);
}
}
}
cout << ans << '\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, k;
std::cin >> n >> k;
int b = std::sqrt(n), bl = (n + b - 1) / b;
std::vector<std::vector<int>> a(n);
std::vector<int64_t> sum(n);
std::vector<std::vector<int64_t>> pre(bl + 1, std::vector<int64_t>(k + 1)),
suf(pre);
for (int i = 0; i < n; ++i) {
int m;
std::cin >> m;
a[i].resize(m);
for (int j = 0; j < m; ++j) std::cin >> a[i][j];
if (m > k) a[i].resize(m = k);
sum[i] = std::accumulate(a[i].begin(), a[i].end(), 0ll);
}
for (int i = 0; i < bl; ++i) {
pre[i + 1] = pre[i];
for (int j = i * b; j < std::min((i + 1) * b, n); ++j)
for (int x = k; x >= int(a[j].size()); --x)
pre[i + 1][x] =
std::max(pre[i + 1][x], pre[i + 1][x - a[j].size()] + sum[j]);
}
for (int i = bl - 1; i >= 0; --i) {
suf[i] = suf[i + 1];
for (int j = i * b; j < std::min((i + 1) * b, n); ++j)
for (int x = k; x >= int(a[j].size()); --x)
suf[i][x] = std::max(suf[i][x], suf[i][x - a[j].size()] + sum[j]);
}
int64_t ans = pre[bl][k];
for (int i = 0; i < bl; ++i) {
std::vector<int64_t> dp(k + 1);
for (int x = 0; x <= k; ++x)
for (int y = 0; x + y <= k; ++y)
dp[x + y] = std::max(dp[x + y], pre[i][x] + suf[i + 1][y]);
for (int j = i * b; j < std::min((i + 1) * b, n); ++j) {
auto f(dp);
for (int x = i * b; x < std::min((i + 1) * b, n); ++x)
if (x != j)
for (int y = k; y >= int(a[x].size()); --y)
f[y] = std::max(f[y], f[y - a[x].size()] + sum[x]);
int64_t s = 0;
for (int x = 0; x < int(a[j].size()); ++x) {
s += a[j][x];
ans = std::max(ans, s + f[k - 1 - x]);
}
}
}
std::cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long maxn = 150;
const long long alpha = 29;
const long long bsz = 400;
const long long maxv = 200;
const long long maxlog = 30;
void init() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
int dp[maxn][maxn][maxn][2];
int main() {
init();
for (int i = 0; i < maxn; i++) {
for (int j = 0; j < maxn; j++) {
for (int k = 0; k < maxn; k++) {
dp[i][j][k][0] = dp[i][j][k][1] = inf;
}
}
}
int n;
cin >> n;
vector<int> p(n);
for (auto &ev : p) cin >> ev;
set<int> foo;
for (int i = 1; i <= n; i++) {
foo.insert(i);
}
for (auto ev : p) foo.erase(ev);
int cnt1 = 0;
int cnt0 = 0;
for (auto ev : foo) {
if (ev & 1)
cnt1++;
else
cnt0++;
}
if (p[0] != 0) {
dp[0][0][0][p[0] & 1] = 0;
} else {
dp[0][1][0][0] = 0;
dp[0][0][1][1] = 0;
}
for (int i = 1; i < n; i++) {
for (int ch = 0; ch < maxn; ch++) {
for (int nech = 0; nech < maxn; nech++) {
if (p[i] != 0) {
if (p[i] & 1)
dp[i][ch][nech][p[i] & 1] =
min(dp[i - 1][ch][nech][0] + 1, dp[i - 1][ch][nech][1]);
else
dp[i][ch][nech][p[i] & 1] =
min(dp[i - 1][ch][nech][0], dp[i - 1][ch][nech][1] + 1);
continue;
}
if (ch > 0) {
dp[i][ch][nech][0] =
min(dp[i - 1][ch - 1][nech][1] + 1, dp[i - 1][ch - 1][nech][0]);
}
if (nech > 0) {
dp[i][ch][nech][1] =
min(dp[i - 1][ch][nech - 1][0] + 1, dp[i - 1][ch][nech - 1][1]);
}
}
}
}
cout << min(dp[n - 1][cnt0][cnt1][0], dp[n - 1][cnt0][cnt1][1]) << endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define maxn 8000
using namespace std;
long long mod=1000000007;
long long a[maxn];
long long b[maxn];
int n;
int p[maxn];
bool vis[maxn];
int inc[maxn];
vector<int> c[maxn];
long long step[maxn];
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
c[i].push_back(i);
inc[i]=i;
}
for(int i=1;i<=n;i++) {
scanf("%d",&p[i]);
if(p[i]!=-1) {
int x=inc[i];
int y=inc[p[i]];
if(x!=y) {
for(auto t:c[x]) {
c[y].push_back(t);
inc[t]=y;
}
c[x].clear();
}
}
}
long long ck=0;
for(int i=1;i<=n;i++) {
if(inc[i]==i && p[i]!=-1) {
ck++;
}
}
long long k=0;
a[0]=1;
step[0]=1;
long long ans=1;
for(int i=1;i<=n;i++) {
if(p[i]==-1) {
k++;
step[k]=(step[k-1]*(n-1))%mod;
for(int j=0;j<=n;j++) {
b[j]=(b[j]+a[j])%mod;
b[j+1]=(b[j+1]+1ll*c[i].size()*a[j])%mod;
}
for(int j=0;j<=n;j++) {
a[j]=b[j];
b[j]=0;
}
}
}
ans=(step[k]*(n-ck))%mod;
for(int i=1;i<=n;i++) {
if(p[i]==-1) {
long long tp=c[i].size()-1;
ans=(ans-tp*step[k-1])%mod;
ans+=mod;
ans%=mod;
}
}
long long fac=1;
for(int i=2;i<=k;i++) {
fac=fac*(i-1)%mod;
long long tp=(a[i]*fac)%mod;
ans=(ans-step[k-i]*tp)%mod;
ans+=mod;
ans%=mod;
}
printf("%lld",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, s1;
map<string, int> mp;
mp["monday"] = 1;
mp["tuesday"] = 2;
mp["wednesday"] = 3;
mp["thursday"] = 4;
mp["friday"] = 5;
mp["saturday"] = 6;
mp["sunday"] = 7;
cin >> s >> s1;
int temp = mp[s1] - mp[s];
if (temp >= 0) {
if (temp == 0 || temp == 2 || temp == 3)
cout << "YES";
else
cout << "NO";
} else {
if (temp == -4 || temp == -5)
cout << "YES";
else
cout << "NO";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
int f[N][N];
int a[N];
int n, mod, l, r;
int get(int x) { return (x % mod + mod) % mod; }
int check(int x) {
if (x >= l && x <= r) return 1;
return 0;
}
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
cin >> n >> mod >> l >> r;
for (int i = 1; i <= n; i++) cin >> a[i];
memset(f, -0x3f, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < mod; j++) {
int t1 = get(j + a[i]);
int c1 = check(t1);
int t2 = get(j + a[i] - 1);
int c2 = check(t2);
f[i][t1] = max(f[i][t1], f[i - 1][j] + c1);
f[i][t2] = max(f[i][t2], f[i - 1][j] + c2);
}
}
int res = 0;
for (int i = 0; i < mod; i++) res = max(res, f[n][i]);
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int father[1000001];
int degree[1000001];
int setCount;
int findset(int x) {
if (x != father[x]) father[x] = findset(father[x]);
return father[x];
}
int unionset(int a, int b) {
int x = findset(a);
int y = findset(b);
if (x != y) {
father[y] = x;
setCount--;
}
return 0;
}
int main() {
int n, m;
cin >> n >> m;
int x, y;
for (int i = 1; i <= n; i++) {
father[i] = i;
}
setCount = n;
for (int i = 0; i < m; i++) {
cin >> x >> y;
degree[x]++;
degree[y]++;
unionset(x, y);
}
int num = 0;
vector<int> vv;
for (int i = 1; i <= n; i++) {
if (degree[i] % 2 == 1) {
num++;
vv.push_back(i);
}
if (i != 1 && degree[i] == 0) setCount--;
}
if (setCount == 1) {
cout << num / 2 << endl;
} else if (num == 0) {
cout << setCount << endl;
} else {
set<int> temp;
for (int i = 0; i < vv.size(); i++) temp.insert(findset(vv[i]));
int t = temp.size();
cout << (num) / 2 + (setCount - t) << endl;
}
}
| 3 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define lint long long
using namespace std;
int q,m=1000000007;
lint x,y,f[99]={0,1,1};
int main(){
scanf("%d",&q);
rep(i,96)f[i+3]=f[i+1]+f[i+2];
rep(i,q){
scanf("%lld %lld",&x,&y);
if(x>y)swap(x,y);
if(y<=2){
printf("1 %d ",y==1?1:x==1?2:4);
continue;
}
rep(j,99){
if(x<f[j]||y<f[j+1]){
lint a=x==1?2:1;
rep(k,j-2){
lint n=f[j-1]+f[j-3-k]*f[k+2],m=f[j]+f[j-2-k]*f[k+2];
if(n<=x&&m<=y)a+=(y-m)/n+1;
}
if(f[j]<=x){
a++;
rep(k,j-2)a+=(x-f[j])/(f[j-2-k]*f[k+2]);
}
printf("%d %d ",j-2,a%m);
break;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[300005], b[300005], n, name[300005];
bool done[300005];
int main() {
cin >> n;
for (int i = 0, _e(n); i < _e; i++) cin >> a[i];
for (int i = 0, _e(n); i < _e; i++) cin >> b[i];
for (int i = 0, _e(n); i < _e; i++) name[b[i]] = i + 1;
for (int i = 0, _e(n); i < _e; i++) a[i] = name[a[i]];
int ok = 1;
for (int i(1), _e(n - 1); i <= _e; i++)
if (a[i] > a[i - 1])
ok++;
else
break;
cout << n - ok << endl;
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int main(){
while(true){
int floor = 1;
double vc;
cin >> vc;
if(cin.eof()) break;
for(floor;vc*vc>19.6*(5*(double)floor-5);floor++) ;
cout << floor << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
int pfreq[128], curfreq[128];
int match() {
for (int i = 'a'; i < 'z' + 1; ++i) {
if (curfreq[i] > pfreq[i]) return 0;
}
return 1;
}
int main() {
string s, p;
cin >> s >> p;
if ((int)s.size() < (int)p.size()) {
cout << 0 << endl;
return 0;
}
memset(pfreq, 0, sizeof pfreq);
memset(curfreq, 0, sizeof curfreq);
for (int i = 0; i < (int)p.size(); ++i)
pfreq[(int)p[i]]++, curfreq[(int)s[i]]++;
int ans = 0;
for (int en = (int)p.size() - 1, st = 0; en < (int)s.size(); ++en, ++st) {
ans += match();
if (en + 1 >= (int)s.size()) break;
if (s[st] != '?') curfreq[(int)s[st]]--;
if (s[en + 1] != '?') curfreq[(int)s[en + 1]]++;
}
cout << ans << endl;
return 0;
}
| 3 |
#include<iostream>
#include<stack>
using namespace std;
char mat[500][500];
int dp[500][500];
int h, w;
int solve(){
int ret = -1;
for(int i = 0; i < h; i++){
// 長方形の左端、高さを積む
stack<pair<int,int> > s;
for(int j = 0; j < w; j++){
if(s.empty()){
s.push({j, dp[i][j]});
}else if(s.top().second < dp[i][j]){
s.push({j, dp[i][j]});
}else if(s.top().second > dp[i][j]){
int lastl;
while(!s.empty() && s.top().second > dp[i][j]){
pair<int,int> out = s.top(); s.pop();
ret = max(ret, (j-out.first)*(out.second));
lastl = out.first;
}
s.push({lastl, dp[i][j]});
}else if(s.top().second == dp[i][j]){
// do nothing.
}
}
while(!s.empty()){
pair<int,int> out = s.top(); s.pop();
ret = max(ret, (w-out.first)*out.second);
}
}
return ret;
}
int main(){
while(cin >> h >> w){
if(w + h == 0) break;
// input
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cin >> mat[i][j];
}
}
// preparation
for(int j = 0; j < w; j++){
if(mat[0][j]=='.') dp[0][j] = 1;
else dp[0][j] = 0;
}
for(int i = 1; i < h; i++){
for(int j = 0; j < w; j++){
if(mat[i][j]=='.') dp[i][j] = dp[i-1][j]+1;
else dp[i][j] = 0;
}
}
cout << solve() << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
vector<int>::iterator it;
int main() {
long long int n, r, i, k = 2, m, p = 1, a, b, flag = 0, j, temp;
cin >> n;
m = n;
if (n == 2) {
cout << 1 << endl << 1;
return 0;
}
if (n == 3) {
cout << 2 << endl << "1 3";
return 0;
}
if (n == 1) {
cout << 1 << endl << 1;
return 0;
}
if (n == 4) {
cout << 4 << endl << "3 1 4 2";
return 0;
}
if (n > 4) {
cout << n << endl;
for (i = 0; i <= n / 2; i++) {
if (p <= n) {
cout << p << " ";
flag++;
}
p = p + 2;
}
for (i; i < n; i++) {
cout << k << " ";
k = k + 2;
flag++;
}
if (flag != n) cout << k;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long INF = 1e18;
bool comp1(pair<long long, pair<long long, long long> > a,
pair<long long, pair<long long, long long> > b) {
return a.second.first > b.second.first;
}
bool comp2(pair<long long, pair<long long, long long> > a,
pair<long long, pair<long long, long long> > b) {
return a.second.first < b.second.first;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
vector<pair<long long, pair<long long, long long> > > asc, desc;
for (long long i = 0; i < n; i++) {
long long a, b;
cin >> a >> b;
if (a < b)
asc.push_back({a, {b, i + 1}});
else
desc.push_back({a, {b, i + 1}});
}
sort(asc.begin(), asc.end(), comp1);
sort(desc.begin(), desc.end(), comp2);
if (asc.size() < desc.size()) asc = desc;
cout << asc.size() << '\n';
for (long long i = 0; i < asc.size(); i++)
cout << asc[i].second.second << ' ';
cout << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long val[100005][105];
int n, m;
long long modpow(long long x, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * x % 1000000007;
x = x * x % 1000000007;
n >>= 1;
}
return res;
}
long long F[100005], R[100005];
void make() {
F[0] = 1;
for (int i = 1; i < 100005; i++) F[i] = F[i - 1] * i % 1000000007;
for (int i = 0; i < 100005; i++) R[i] = modpow(F[i], 1000000007 - 2);
}
long long C(int a, int b) {
return F[a] * R[b] % 1000000007 * R[a - b] % 1000000007;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
make();
for (int i = 1; i <= n; i++) cin >> val[i][0];
for (int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
int a = r - l;
if (k <= a) {
for (int i = 1; i <= k; i++) {
val[l + i - 1][i] = (val[l + i - 1][i] + C(k, k + 1 - i)) % 1000000007;
val[l + a + 1][i] =
(val[l + a + 1][i] - C(k + a + 1 - i, k + 1 - i) + 1000000007) %
1000000007;
}
val[k + l][k + 1]++;
val[l + a + 1][k + 1]--;
} else {
for (int i = 1; i <= a; i++) {
val[l + i - 1][i] = (val[l + i - 1][i] + C(k, k + 1 - i)) % 1000000007;
val[l + a + 1][i] =
(val[l + a + 1][i] - C(k + a + 1 - i, k + 1 - i) + 1000000007) %
1000000007;
}
val[l + a][a] += C(k, a);
}
}
for (int j = 101; j >= 1; j--) {
for (int i = 2; i <= n; i++)
val[i][j] = (val[i][j] + val[i - 1][j]) % 1000000007;
for (int i = 1; i <= n; i++)
val[i][j - 1] = (val[i][j - 1] + val[i][j]) % 1000000007;
}
for (int i = 1; i <= n; i++)
printf("%lld\n", (val[i][0] + 1000000007) % 1000000007);
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int n, a[500100], f[500100];
char s[500100];
int main() {
scanf("%d%s", &n, s + 1);
for(int i = 1; i <= n; i++) {
a[i] = s[i] - '0';
}
for(int i = 3, lp = 0; i <= n; i++) {
f[i] = f[i - 1];
if(a[i] && !a[i - 1] && a[i - 2]) {
lp = i;
for(int j = i - 2; a[j] && j >= 1; j--) {
f[i] = max(f[i], f[j - 1] + i - j - 1);
}
}
if(!a[i]) lp = 0;
if(a[i] && lp) f[i] = max(f[i], f[lp - 3] + i - lp + 1);
//printf("%d %d\n", i, f[i]);
}
printf("%d\n", f[n]);
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
long long a,b;
int main()
{
cin>>a>>b;
cout<<a-(b<a)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin>>T;
for(int i=0;i<T;i++){
int N;
int64_t Z=0;
cin>>N;
vector<pair<int,int64_t>> l;
vector<pair<int,int64_t>> r;
for(int i=0;i<N;i++){
int a;
int64_t b,c;
cin>>a>>b>>c;
if(b>c){
Z+=c;
l.push_back(make_pair(a,b-c));
}
else if(c>b&&a!=N){
Z+=b;
r.push_back(make_pair(N-a,c-b));
}
else{
Z+=b;
}
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
priority_queue<int64_t, vector<int64_t>, greater<int64_t>> pq;
for(auto x:l){
int a;
int64_t b;
tie(a,b)=x;
pq.push(b);
if(a<pq.size()){
pq.pop();
}
}
while(!pq.empty()){
Z+=pq.top();
pq.pop();
}
for(auto x:r){
int a;
int64_t b;
tie(a,b)=x;
pq.push(b);
if(a<pq.size()){
pq.pop();
}
}
while(!pq.empty()){
Z+=pq.top();
pq.pop();
}
cout<<Z<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:32000000")
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
const int maxn = 2010;
int ti[maxn];
int hi[maxn];
int mi[maxn];
int u[maxn];
int main() {
int n, first;
scanf("%d%d", &n, &first);
for (int i = 0; i < n; ++i) scanf("%d%d%d", &ti[i], &hi[i], &mi[i]);
int bestans = -1;
for (int iter = 0; iter < 2; ++iter) {
memset(u, 0, sizeof(u));
int curt = iter;
int curx = first;
int ans = 0;
while (true) {
int v = -1;
for (int i = 0; i < n; ++i) {
if (!u[i] && curx >= hi[i] && ti[i] == curt &&
(v == -1 || mi[i] > mi[v]))
v = i;
}
if (v != -1 && !u[v] && curx >= hi[v] && ti[v] == curt) {
u[v] = 1;
curx += mi[v];
ans++;
curt ^= 1;
} else
break;
}
bestans = max(ans, bestans);
}
printf("%d\n", bestans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const bool debug = true;
const long long INFL = LLONG_MAX;
const int output_precision = 15;
stringstream ss;
int X[11000], Y[10100], x, y;
int main() {
ios_base::sync_with_stdio(0);
cout.precision(output_precision);
cout << fixed;
ss.precision(output_precision);
ss << fixed;
for (int(i) = 1; (i) <= (10000); (i)++) {
int d = (i + 1) >> 1;
X[i] = X[i - 1];
Y[i] = Y[i - 1];
switch ((i - 1) % 4) {
case 0:
X[i] += d;
break;
case 1:
Y[i] += d;
break;
case 2:
X[i] -= d;
break;
case 3:
Y[i] -= d;
break;
}
}
cin >> x >> y;
for (int(i) = 0; (i) < (10000); (i)++) {
int x1 = X[i - 1];
int x2 = X[i];
int y1 = Y[i - 1];
int y2 = Y[i];
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
if (x1 <= x && x <= x2 && y1 <= y && y <= y2) {
cout << max(0, i - 1) << '\n';
return 0;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
A convert(B x) {
stringstream ss;
ss << x;
A ret;
ss >> ret;
return ret;
}
const int oo = ~0u >> 2;
const double eps = 1e-10;
const int mn = 1000, mo = 100000007;
const int fx[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
struct po {
double x, y, z;
po() {}
po(double X, double Y) { x = X, y = Y, z = 0; }
po(double X, double Y, double Z) { x = X, y = Y, z = Z; }
po operator/(double a) { return po(x / a, y / a, z / a); }
po operator+(po p) { return po(x + p.x, y + p.y, z + p.z); }
po operator-(po p) { return po(x - p.x, y - p.y, z - p.z); }
} a[mn], p[mn];
int n, m;
int sgn(double x) {
if (fabs(x) <= eps) return 0;
if (x > -eps) return 1;
return -1;
}
double dis(po a, po b) {
return sqrt(((double)(a.x - b.x) * (a.x - b.x)) +
((double)(a.y - b.y) * (a.y - b.y)) +
((double)(a.z - b.z) * (a.z - b.z)));
}
po Project(po p, int A, int B, int C) {
if (!A && !B) return po(p.x, p.y);
if (!B && !C) return po(p.y, p.z);
if (!A && !C) return po(p.z, p.x);
double alpha =
(double)(A * p.x + B * p.y + C * p.z) /
(((double)(A) * (A)) + ((double)(B) * (B)) + ((double)(C) * (C)));
po pA = po(p.x - alpha * A, p.y - alpha * B, p.z - alpha * C);
po pO = po(0, 0), pB = po(0, -C, B);
double a = dis(pO, pA), b = dis(pO, pB), c = dis(pA, pB);
if (!sgn(a)) return po(0, 0);
double t = (((double)(a) * (a)) + ((double)(b) * (b)) - ((double)(c) * (c))) /
(2 * a * b),
Ang;
if (!sgn(t - 1))
Ang = acos(1);
else if (!sgn(t + 1))
Ang = acos(-1);
else
Ang = acos(t);
if (sgn(pA.x) < 0) Ang *= -1;
return po(a * cos(Ang), a * sin(Ang));
}
po calc(po A, po B, po C) {
double a, b, c, d, e, f;
a = B.x - A.x, b = B.y - A.y,
c = (((double)(B.x) * (B.x)) + ((double)(B.y) * (B.y)) -
((double)(A.x) * (A.x)) - ((double)(A.y) * (A.y))) /
2.0;
d = C.x - A.x, e = C.y - A.y,
f = (((double)(C.x) * (C.x)) + ((double)(C.y) * (C.y)) -
((double)(A.x) * (A.x)) - ((double)(A.y) * (A.y))) /
2.0;
return po((c * e - f * b) / (a * e - b * d),
(c * d - f * a) / (b * d - e * a));
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y >> a[i].z;
random_shuffle(a + 1, a + n + 1);
while (m--) {
int A, B, C;
cin >> A >> B >> C;
for (int i = 1; i <= n; ++i) p[i] = Project(a[i], A, B, C);
po o = po(0, 0, 0);
double R = 0;
for (int i = 1; i <= n; ++i)
if (sgn(dis(o, p[i]) - R) > 0) {
o = p[i], R = 0;
for (int j = 1; j <= i - 1; ++j)
if (sgn(dis(o, p[j]) - R) > 0) {
o = (p[i] + p[j]) / 2, R = dis(o, p[i]);
for (int k = 1; k <= j - 1; ++k)
if (sgn(dis(o, p[k]) - R) > 0)
o = calc(p[i], p[j], p[k]), R = dis(o, p[i]);
}
}
printf("%.10f\n", R);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100007];
int val[100007], n, k;
map<int, int> id;
set<int> S;
vector<int> adj[100007];
bool d[100007];
void dfs(int u) {
cout << val[u] << " ";
d[u] = 1;
for (auto v : adj[u])
if (!d[v]) dfs(v);
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i].first >> a[i].second, S.insert(a[i].first),
S.insert(a[i].second);
int t = 1;
while (S.size()) {
id[*S.begin()] = t;
val[t++] = *S.begin();
S.erase(S.begin());
}
for (int i = 1; i <= n; ++i)
adj[id[a[i].first]].push_back(id[a[i].second]),
adj[id[a[i].second]].push_back(id[a[i].first]);
for (int i = 1; i <= t; ++i)
if (adj[i].size() == 1) {
k = i;
break;
}
dfs(k);
return 0;
}
| 3 |
#include <iostream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;
int left_to_right(string s) {
int n = s[0]-'0';
for (int i=0; i<(int)s.length()/2; i++) {
int right = s[2*i+2]-'0';
if (s[2*i+1] == '+') {
n += right;
}
else {
n *= right;
}
}
return n;
}
int multiplication_first(string s) {
vector<int> terms;
terms.push_back(s[0]-'0');
for (int i=0; i<(int)s.length()/2; i++) {
int right = s[2*i+2]-'0';
if (s[2*i+1] == '+') {
terms.push_back(right);
}
else {
terms[terms.size()-1] *= right;
}
}
return accumulate(terms.begin(), terms.end(), 0);
}
int main() {
string s;
int n;
cin >> s >> n;
if (left_to_right(s) == n) {
if (multiplication_first(s) == n) {
cout << "U" << endl;
}
else {
cout << "L" << endl;
}
}
else {
if (multiplication_first(s) == n) {
cout << "M" << endl;
}
else {
cout << "I" << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
bool e[maxn][maxn];
map<string, int> id;
int n, ans, tot, num, cnt[maxn], vis[maxn], a[maxn];
string t;
bool dfs(int u, int dep) {
for (int v = u + 1; v <= n; ++v) {
if (cnt[v] + dep <= ans) return 0;
if (e[u][v]) {
int i = 0;
while (i < dep && e[v][vis[i]]) ++i;
if (i == dep) {
vis[dep] = v;
if (dfs(v, dep + 1)) return 1;
}
}
}
if (dep > ans) {
ans = dep;
return 1;
}
return 0;
}
void maxclique() {
ans = -1;
for (int i = n; i; i--) {
vis[0] = i;
dfs(i, 1);
cnt[i] = ans;
}
}
int main() {
int m;
cin >> m >> n;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) e[i][j] = 1;
for (int i = 0; i < m; ++i) {
int ty;
scanf("%d", &ty);
if (ty == 1)
tot = 0;
else {
cin >> t;
if (id.find(t) == id.end()) id[t] = ++num;
int cur = id[t];
for (int i = 1; i <= tot; ++i) e[a[i]][cur] = e[cur][a[i]] = 0;
a[++tot] = cur;
}
}
maxclique();
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5;
bool viz[1 + N];
int p[1 + N];
vector<int> lev[1 + N];
vector<int> gr[1 + N];
void dfs(int node, int par, int level) {
viz[node] = true;
p[node] = par;
lev[level].push_back(node);
for (int vec : gr[node])
if (not viz[vec]) dfs(vec, node, level + 1);
}
void solveTest() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) gr[i].clear(), viz[i] = false, lev[i].clear();
while (m--) {
int x, y;
cin >> x >> y;
gr[x].push_back(y);
gr[y].push_back(x);
}
dfs(1, 0, 1);
if (lev[(n + 1) / 2].size()) {
int node = lev[(n + 1) / 2].back();
vector<int> ans;
while (node) {
ans.push_back(node);
node = p[node];
}
reverse(ans.begin(), ans.end());
cout << "PATH\n";
cout << ans.size() << "\n";
for (int node : ans) cout << node << " ";
cout << "\n";
} else {
vector<pair<int, int>> pairs;
for (int level = 1; level < (n + 1) / 2; level++)
for (int i = 0; i + 1 < lev[level].size(); i += 2)
pairs.push_back({lev[level][i], lev[level][i + 1]});
cout << "PAIRING\n";
cout << pairs.size() << "\n";
for (pair<int, int> p : pairs) cout << p.first << " " << p.second << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solveTest();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int f = 1;
int ans = 0;
while (c > '9' || c < '0') {
if (c == '-') f = -f;
c = getchar();
}
while (c <= '9' && c >= '0') {
ans = ans * 10 + c - '0';
c = getchar();
}
return ans * f;
}
int a[105], b[105], n, m;
double dp[105][105 * 105], p[105];
inline bool solve(double E) {
for (int i = n; i; i--)
for (int j = 0; j <= m; j++)
dp[i][j] =
min(E, p[i] * (a[i] + (j + a[i] <= m ? dp[i + 1][j + a[i]] : E)) +
(1.0 - p[i]) *
(b[i] + (j + b[i] <= m ? dp[i + 1][j + b[i]] : E)));
return dp[1][0] + 1e-11 > E;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
b[i] = read();
p[i] = (double)read() / 100;
}
double l = 0, r = 1e9;
while (r - l > 1e-11) {
double mid = (l + r) / 2;
if (solve(mid))
l = mid;
else
r = mid;
}
printf("%.9lf\n", l);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int nowx[1010], nowy[1010], tarx[1010], tary[1010], n;
int ansx[20 * 1010], ansy[20 * 1010], cnt = 0;
bool ok[1010][1010], tarok[1010][1010];
vector<int> t1, t2;
void sol(vector<int> &t) {
int d = t.size();
if (d <= 3) return;
int nbest = 0, dx = -1, dy = -1;
for (int i = 0; i < d; i++)
for (int j = i + 1; j < d; j++)
if (j != ((i + 1) % t.size()) && j != ((i - 1 + t.size()) % t.size()))
if (tarok[t[i]][t[j]] || tarok[t[j]][t[i]]) {
int tmp = (i - j + d) % d;
if (tmp > (d >> 1)) tmp = d - tmp;
if (tmp > nbest) {
dx = i;
dy = j;
nbest = tmp;
}
}
if (!ok[t[dx]][t[dy]]) {
int a = dy, b = dy;
while (!ok[t[dx]][t[a]] && ((a + 1) % t.size()) != dx)
a = ((a + 1) % t.size());
while (!ok[t[dx]][t[b]] && ((b - 1 + t.size()) % t.size()) != dx)
b = ((b - 1 + t.size()) % t.size());
int c = ((a - 1 + t.size()) % t.size());
bool dir = false;
if (!ok[t[b]][t[c]])
while ((!ok[t[b]][t[c]] || !ok[t[a]][t[c]]) &&
((c - 1 + t.size()) % t.size()) != b) {
if (c == dy) dir = true;
c = ((c - 1 + t.size()) % t.size());
}
if (!(ok[t[a]][t[b]] == true && ok[t[b]][t[a]] == true))
while (1)
;
if (!(dx != ((c - 1 + t.size()) % t.size()) &&
c != ((dx - 1 + t.size()) % t.size())))
while (1)
;
cnt++;
ansx[cnt] = t[a];
ansy[cnt] = t[b];
ok[t[a]][t[b]] = ok[t[b]][t[a]] = false;
ok[t[dx]][t[c]] = ok[t[c]][t[dx]] = true;
while (c != dy && b != dy) {
if (!dir)
a = c;
else
b = c;
c = ((a - 1 + t.size()) % t.size());
dir = false;
if (!ok[t[b]][t[c]])
while ((!ok[t[b]][t[c]] || !ok[t[a]][t[c]]) &&
((c - 1 + t.size()) % t.size()) != b) {
if (c == dy) dir = true;
c = ((c - 1 + t.size()) % t.size());
}
if (!(dx != ((c - 1 + t.size()) % t.size()) &&
c != ((dx - 1 + t.size()) % t.size())))
while (1)
;
if (!(ok[t[a]][t[b]] == true && ok[t[b]][t[a]] == true))
while (1)
;
cnt++;
ansx[cnt] = t[a];
ansy[cnt] = t[b];
ok[t[a]][t[b]] = ok[t[b]][t[a]] = false;
ok[t[dx]][t[c]] = ok[t[c]][t[dx]] = true;
}
}
vector<int> t1, t2;
bool ins = false;
for (int i = 0; i < t.size(); i++) {
if (!ins)
t1.push_back(t[i]);
else
t2.push_back(t[i]);
if (i == dx || i == dy) {
ins ^= 1;
if (!ins)
t1.push_back(t[i]);
else
t2.push_back(t[i]);
}
}
sol(t1);
sol(t2);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 3; i++) {
scanf("%d%d", &nowx[i], &nowy[i]);
ok[nowx[i]][nowy[i]] = ok[nowy[i]][nowx[i]] = true;
}
for (int i = 1; i <= n - 3; i++) {
scanf("%d%d", &tarx[i], &tary[i]);
tarok[tarx[i]][tary[i]] = tarok[tary[i]][tarx[i]] = true;
if (tarx[i] > tary[i]) swap(tarx[i], tary[i]);
}
vector<int> a;
for (int i = 1; i <= n; i++) a.push_back(i);
sol(a);
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d %d\n", ansx[i], ansy[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 998244353;
ll mod_pow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) {
res = res * x % mod;
}
x = x * x % mod;
n >>= 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, x;
cin >> n >> x;
vector<vector<ll>> f(n, vector<ll>(n + 1, 1));
for (ll i = 1; i < n; i++) {
for (ll j = 1; j <= n; j++) {
f[i][j] = f[i][j - 1] * i;
f[i][j] %= mod;
}
}
vector<ll> up(n + 1);
vector<ll> down(n + 1);
up[0] = 1;
down[0] = 1;
for (ll i = 1; i <= n; i++) {
up[i] = up[i - 1] * i;
up[i] %= mod;
down[i] = down[i - 1] * mod_pow(i, mod - 2, mod);
down[i] %= mod;
}
vector<vector<ll>> dp(n, vector<ll>(max(n, x), 0));
dp[n - 1][n - 1] = 1;
for (ll i = n - 1; i >= 1; i--) {
for (ll j = i + 1; j < x; j++) {
for (ll h = i; h < n; h++) {
dp[i][j] += dp[h][j - i] * up[h + 1] % mod * down[i + 1] % mod *
down[h - i] % mod * f[h][h - i];
dp[i][j] %= mod;
}
}
}
ll ans = 0;
ll k = min(x, n - 1);
ans += f[k][n];
for (ll i = n - 1; i >= 1; i--) {
for (ll j = 0; j < x; j++) {
if (dp[i][j] == 0) continue;
for (ll h = i; h >= 1; h--) {
ll a = min(h, x - j);
ll c = up[i + 1] * down[i - h] % mod * down[h + 1] % mod;
c %= mod;
ans += dp[i][j] % mod * c % mod * f[i][i - h] % mod * f[a][h + 1] % mod;
ans %= mod;
}
}
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
int f = 0;
x = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) f |= (ch == '-');
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if (f) x = -x;
}
const int N = 100005;
int T[N << 2], ans[N], n, m, Q, x, y;
vector<pair<int, int> > q[N];
vector<int> v[N];
void down(int o) {
T[(o << 1)] = min(T[(o << 1)], T[o]);
T[(o << 1 | 1)] = min(T[(o << 1 | 1)], T[o]);
}
void up(int o) { T[o] = max(T[(o << 1)], T[(o << 1 | 1)]); }
void build(int o, int l, int r) {
T[o] = 1e9;
if (l == r) return;
build((o << 1), l, ((l + r) >> 1));
build((o << 1 | 1), ((l + r) >> 1) + 1, r);
}
void upd(int o, int l, int r, int x, int y, int z) {
if (l == x && y == r) {
T[o] = min(T[o], z);
return;
}
down(o);
if (x <= ((l + r) >> 1))
upd((o << 1), l, ((l + r) >> 1), x, min(y, ((l + r) >> 1)), z);
if (((l + r) >> 1) < y)
upd((o << 1 | 1), ((l + r) >> 1) + 1, r, max(((l + r) >> 1) + 1, x), y, z);
up(o);
}
int ask(int o, int l, int r, int p, int lim) {
if (T[o] <= lim) return -1;
if (p <= l) {
for (; l < r;) {
if (T[(o << 1)] <= lim)
l = ((l + r) >> 1) + 1, o = (o << 1 | 1);
else
r = ((l + r) >> 1), o = (o << 1);
}
return l;
}
int tmp = -1;
if (p <= ((l + r) >> 1)) tmp = ask((o << 1), l, ((l + r) >> 1), p, lim);
if (tmp != -1) return tmp;
return ask((o << 1 | 1), ((l + r) >> 1) + 1, r, p, lim);
}
int main() {
read(n), n++, read(m);
for (int i = (1); i <= (m); i++) {
read(x), read(y);
if (x < y) v[x].push_back(y);
}
read(Q);
for (int i = (1); i <= (Q); i++) {
read(x), read(y);
q[x].push_back(make_pair(y, i));
}
build(1, 1, n);
for (int i = (n - 1); i >= (1); i--) {
for (auto x : v[i]) upd(1, 1, n, i, x - 1, x);
for (auto x : q[i]) ans[x.second] = ask(1, 1, n, i, x.first);
}
for (int i = (1); i <= (Q); i++) printf("%d\n", ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int R, S;
char grid[155][155];
bool nispe[155];
pair<int, int> niz[160];
int main(void) {
scanf("%d%d", &R, &S);
int last = 0;
for (int i = 0; i < R; ++i) {
scanf("%s", grid[i]);
bool pwn = false;
for (int j = 0; j < S; ++j) {
if (grid[i][j] == 'W' && !pwn) {
pwn = true;
niz[i].first = j;
}
if (grid[i][j] == 'W') niz[i].second = j;
}
if (pwn) {
nispe[i] = true;
last = i;
}
}
int sol = 0, pos = 0;
for (int i = 0; i < R; ++i) {
if (i % 2 == 0) {
int tmp = niz[i].second - pos, tmp_pos = niz[i].second;
if (!nispe[i]) {
tmp = 0;
tmp_pos = pos;
}
if (i + 1 < R && nispe[i + 1]) {
tmp = max(tmp, niz[i + 1].second - pos);
tmp_pos = max(tmp_pos, niz[i + 1].second);
}
pos = tmp_pos;
sol += tmp;
} else {
int tmp = pos - niz[i].first, tmp_pos = niz[i].first;
if (!nispe[i]) {
tmp = 0;
tmp_pos = pos;
}
if (i + 1 < R && nispe[i + 1]) {
tmp = max(tmp, pos - niz[i + 1].first);
tmp_pos = min(tmp_pos, niz[i + 1].first);
}
sol += tmp;
pos = tmp_pos;
}
}
printf("%d\n", sol + last);
return 42 - 42;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool subset[1001][1001];
bool isSubsetSum(int set[], int n, int sum) {
for (int i = 0; i <= n; i++) subset[0][i] = true;
for (int i = 1; i <= sum; i++) subset[i][0] = false;
for (int i = 1; i <= sum; i++) {
for (int j = 1; j <= n; j++) {
subset[i][j] = subset[i][j - 1];
if (i >= set[j - 1])
subset[i][j] = subset[i][j] || subset[i - set[j - 1]][j - 1];
}
}
return subset[sum][n];
}
int main() {
int k, n, j, b[1001], a[1001], i, d[1001];
scanf("%d%d", &n, &j);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
int l = 0, cnt, c[1001], sum = 0, loc = 0;
for (i = 0; i < n; i++) {
if (a[i] == -1) {
c[i] = i;
} else {
k = a[i];
while (k != -1) {
c[i] = k;
k = a[k];
if (i == j - 1) loc++;
}
}
}
for (i = 0; i < n; i++) b[c[i]]++;
l = 0;
for (i = 0; i < 1001; i++)
if (b[i] > 0 && i != c[j - 1]) {
d[l++] = b[i];
sum += b[i];
}
sum -= n;
if (sum > 0)
for (i = 0; i < sum; i++) d[l++] = 1;
isSubsetSum(d, l, n);
for (i = 0; i < n; i++)
if (subset[i][l]) {
printf("%d\n", i + loc + 1);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long l[200005], r[200005];
vector<long long> p[30], suml[30], sumr[30];
int main() {
long long n, sum = 0;
cin >> n;
string a, b;
cin >> a >> b;
a = " " + a;
b = " " + b;
for (long long i = n; i >= 1; i--) {
sum += i * i;
}
for (long long i = 1; i <= n; i++) {
l[i] = i;
r[i] = n + 1 - i;
}
for (long long i = 0; i < 26; i++) {
p[i].push_back(0);
}
for (long long i = 1; i <= n; i++) {
p[b[i] - 'A'].push_back(i);
}
for (long long i = 0; i < 26; i++) {
long long pre = 0, sx = 0;
for (auto v : p[i]) {
pre += l[v];
sx += r[v];
suml[i].push_back(pre);
sumr[i].push_back(sx);
}
}
long double ans = 0.0;
for (long long i = 1; i <= n; i++) {
long long v = a[i] - 'A', sz = p[v].size() - 1;
long long rx = lower_bound(p[v].begin(), p[v].end(), i) - p[v].begin(),
lx = rx - 1;
ans += (long double)l[i] * (long double)(sumr[v][sz] - sumr[v][lx]);
ans += (long double)r[i] * (long double)suml[v][lx];
}
printf("%.15Lf\n", (long double)ans / (long double)sum);
}
| 3 |
/* HARDWORK IS THE KEY TO SUCCESS.
RATING IS TEMPORARY , KNOWLEDGE IS PERMANENT.!!!*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int maxs = 103;
const int MAXS = 1500;
int a[maxs];
int fact[maxs];
int dp[maxs][maxs][MAXS];
int n,k;
void pre()
{
fact[0] = fact[1] = 1;
for(int i = 2;i < maxs;++i)
fact[i] = (i * fact[i - 1]) % mod;
}
int go(int pos,int start,int XOR)
{
if(pos == n + 1)
{
if(XOR == k && start == 0)
return 1LL;
else
return 0LL;
}
if(start == 0)
{
if(XOR == k)
return 1LL;
else
return 0LL;
}
if(dp[pos][start][XOR] != -1)
return dp[pos][start][XOR];
int ans = 0;
ans = (ans % mod + go(pos + 1,start - 1,XOR ^ a[pos]) % mod) % mod;
ans = (ans % mod + go(pos + 1,start,XOR) % mod) % mod;
return dp[pos][start][XOR] = ans;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
pre();
memset(dp,-1,sizeof dp);
cin >> n >> k;
for(int i = 1;i <= n;++i)
cin >> a[i];
int ans = 0;
for(int i = 1;i <= n;++i)
{
int res = (go(1,i,0) % mod * fact[i] % mod) % mod;
ans = (ans % mod + res % mod) % mod;
}
cout << ans << '\n';
}
| 0 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string S;
int main(){
cin>>S;
reverse(S.begin(),S.end());
cout<<S<<endl;
}
| 0 |
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::max;
using std::min;
long long W(long long x1, long long y1, long long x2, long long y2, bool v) {
long long ans = 0;
if (((x2 - x1 + 1) & 1) == 0 || ((y2 - y1 + 1) & 1) == 0 || ((x1 + y1) & 1))
ans = (x2 - x1 + 1) * (y2 - y1 + 1) / 2;
else
ans = (x2 - x1 + 1) * (y2 - y1 + 1) / 2 + 1;
if (v) return (x2 - x1 + 1) * (y2 - y1 + 1) - ans;
return ans;
}
int main() {
int t;
long long n, m;
long long x1, x2, y1, y2, x3, x4, y3, y4;
long long w = 0;
cin >> t;
while (t--) {
cin >> n >> m;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
if (((n & 1) == 0) || (m & 1) == 0)
w = n * m / 2;
else
w = n * m / 2 + 1;
w = w + W(x1, y1, x2, y2, 1) - W(x3, y3, x4, y4, 0);
if ((max(x1, x3) <= min(x2, x4)) && (max(y1, y3) <= min(y2, y4)))
w -= W(max(x1, x3), max(y1, y3), min(x2, x4), min(y2, y4), 1);
cout << w << ' ' << n * m - w << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int c[2002];
vector<int> vec;
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
c[1]++;
c[1 + n]--;
vec.push_back(1);
}
for (int i = 2; i <= n + m + 1; i++) {
c[i] += c[i - 1];
if (c[i] == 0) {
vec.push_back(i - 1);
c[i - 1]++;
c[i]++;
c[i - 1 + n]--;
}
while (i <= n + m && c[i] < k) {
vec.push_back(i);
c[i]++;
}
}
printf("%d\n", vec.size());
for (auto x : vec) printf("%d ", x);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int N;
cin >> N;
int R = 1;
vector<int> ct;
for (int p = 2; p * p <= N; p++) {
if (N % p == 0) {
int rep = 0;
while (N % p == 0) {
rep++;
N /= p;
}
R *= p;
ct.push_back(rep);
}
}
if (N != 1) {
R *= N;
ct.push_back(1);
}
bool same = true;
bool pow2 = true;
int mpow = 0;
for (int i = 0; i < ct.size(); i++) {
if (i > 0 && ct[i] != ct[i - 1]) same = false;
int rep = 0;
if (__builtin_popcount(ct[i]) > 1) {
pow2 = false;
rep = 1;
}
for (int tmp = ct[i]; tmp > 1; tmp /= 2) rep++;
mpow = max(mpow, rep);
}
if (!same || !pow2) mpow++;
cout << R << " " << mpow << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 0x3f3f3f3f;
int solve(const vector<int>& v) {
int n = v.size();
vector<int> dp(n << 8 | 1, -inf);
dp[0] = 0;
for (int a : v) {
auto ndp = dp;
ndp[a] = 1;
for (int i = 1; i < (int)dp.size(); ++i) {
if (dp[i] == -inf) {
continue;
}
if ((i & -i) < a) {
continue;
}
ndp[i + a] = max(ndp[i + a], dp[i] + 1);
}
swap(dp, ndp);
}
int res = 0;
for (int i = 1; i < (int)dp.size(); i *= 2) {
res = max(res, dp[i]);
}
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
while (cin >> n, n) {
vector<vector<int>> v(500);
while (n--) {
int a;
cin >> a;
int tz = __builtin_ctz(a);
v[a >> tz].push_back(1 << tz);
}
int res = 0;
for (const auto& e : v) {
res = max(res, solve(e));
}
cout << res << '\n';
}
}
| 0 |
//kruskal tree
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define M 9999
int parent[M],a[M],b[M];
pair<double,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
int x=root(a),y=root(b);
if(x==y)return 0;
parent[x]=y;
return 1;
}
int main(){
int i,j,k,m,n;
double x,y,z,r;
for(;cin>>n,n;){
vector<vector<double> >v;
for(i=0;i<n;i++)cin>>x>>y>>z>>r,v.push_back({x,y,z,r});
for(i=0;i<n;i++)parent[i]=i;
for(m=i=0;i<n;i++)for(j=i+1;j<n;j++){
for(x=k=0;k<3;k++)x+=(v[i][k]-v[j][k])*(v[i][k]-v[j][k]);
x=sqrt(x)-v[i][3]-v[j][3];
if(x<=0)unite(i,j);
else a[m]=i,b[m]=j,node[m].first=x,node[m++].second=m;
}
sort(node,node+m);
for(x=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))x+=node[i].first;
cout<<fixed<<setprecision(3)<<x<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void read(int& x) {
bool fu = 0;
char c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') fu = 1, c = getchar();
for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
if (fu) x = -x;
};
char getc() {
char c;
for (c = getchar(); c <= 32; c = getchar())
;
return c;
}
const int mod = 1000000007;
int n, m, i, j, k, l, p;
char a[3010][3010];
int f[2][3010][3010];
void dp(int t, int x, int y) {
int i, j;
f[t][x][y] = (a[x][y] == '.');
for (i = x; i <= n; i++)
for (j = y; j <= m; j++) {
if (a[i + 1][j] == '.')
f[t][i + 1][j] = ((f[t][i + 1][j]) + (f[t][i][j])) % mod;
if (a[i][j + 1] == '.')
f[t][i][j + 1] = ((f[t][i][j + 1]) + (f[t][i][j])) % mod;
}
}
int main() {
read(n);
read(m);
for (i = 1; i <= n; i++) scanf("%s", a[i] + 1);
dp(0, 1, 2);
dp(1, 2, 1);
printf("%d\n", int(((1LL * f[0][n - 1][m] * f[1][n][m - 1] -
1LL * f[0][n][m - 1] * f[1][n - 1][m]) %
mod +
mod) %
mod));
scanf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
bool checkSurvive(char f[], char s[], int pos, int flooded) {
if (pos < 0) {
return false;
}
if (flooded >= pos) {
return false;
}
if (pos > n - 1) {
return true;
}
if (f[pos] != '-') {
return false;
}
f[pos] = 'a';
bool jump = checkSurvive(s, f, pos + k, flooded + 1);
if (jump) {
return true;
}
bool next = checkSurvive(f, s, pos + 1, flooded + 1);
if (next) {
return true;
}
bool prev = checkSurvive(f, s, pos - 1, flooded + 1);
if (prev) {
return true;
}
return false;
}
int main() {
cin >> n >> k;
char first[n + 5];
char second[n + 5];
cin >> first;
cin >> second;
bool f = checkSurvive(first, second, 0, -1);
if (f) {
cout << "YES" << '\n';
} else {
cout << "NO" << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
long long gcd(long long a, long long b) {
if (!a) return b;
return gcd(b % a, a);
}
int main() {
long long n, a, b, c;
scanf("%lld", &n);
for (long long i = 1; i * i <= n; i++)
if (n % i == 0 && gcd(i, n / i) == 1) {
a = i;
b = n / i;
}
printf("%lld %lld\n", a, b);
}
| 3 |
#include <iostream>
#include <cstring>
using namespace std;
int f[7][7][7],N,M;
int dx[] ={0,0,-1,1,0,-1,1,-1,1};
int dy[] ={0,-1,0,0,1,-1,-1,1,1};
int dfs(int x, int y, int z, int c,int m,int mz, bool d)
{
int s=1;
int tx=x, ty=y,tz=z;
if(d) tx+=dx[m], ty+=dy[m], tz+=mz;
else tx-=dx[m], ty-=dy[m], tz-=mz;
if(tx<0||ty<0||tz<0||tx>=N||ty>=N||tz>=N) return s;
if(f[tx][ty][tz]!=c) return s;
s+=dfs(tx,ty,tz,c,m,mz,d);
return s;
}
int main()
{
int P;
while(cin >> N >> M >> P, (N||M||P))
{
memset(f,0,sizeof(f));
int c=0, e=0;
for(int j=1; j<=P; j++)
{
c=(c==1?2:1);
int x,y,z=0;
cin >> x >> y;
x--,y--;
while(1)
{
if(f[x][y][z]==0)
{
f[x][y][z]=c;
break;
}
z++;
}
if(e!=0) continue;
for(int mz=-1; mz<=1; mz++)
for(int i=0; i<9; i++)
{
int t=1;
if(i==0&&mz==0) continue;
for(int d=0; d<2; d++)
{
int tx=x, ty=y,tz=z;
if(d) tx+=dx[i], ty+=dy[i], tz+=mz;
else tx-=dx[i], ty-=dy[i], tz-=mz;
if(tx<0||ty<0||tz<0||tx>=N||ty>=N||tz>=N) continue;
if(f[tx][ty][tz]!=c) continue;
t+=dfs(tx,ty,tz,c,i,mz,d);
}
if(t>=M) e=j;
}
}
if(e==0) cout << "Draw" << endl;
else
{
cout << (e%2==0?"White ":"Black ") << e << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> get(int n, int m) {
if (n == 1) {
return vector<int>(1, 1);
}
vector<int> res;
if (m >= (1 << (n - 2))) {
res = get(n - 1, m - (1 << (n - 2)));
res.push_back(0);
} else {
res = get(n - 1, m);
res.insert(res.begin(), 0);
}
for (int i = (0); i <= (int((int)(res.size())) - 1); i++) res[i]++;
return res;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> res = get(n, m - 1);
for (int i = (0); i <= (int((int)(res.size())) - 1); i++) {
if (i) printf(" ");
printf("%d", res[i]);
}
puts("");
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
char a;
cin>>a;
if(a=='A') cout<<'T';
if(a=='T') cout<<'A';
if(a=='G') cout<<'C';
if(a=='C') cout<<'G';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
T ans = 1;
while (b > 0) {
if (b % 2 == 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans % m;
}
int a[3 * 100005];
int n, k;
long long dp[5001][5001];
long long solve(int i, int j) {
if (i == (k - (n % k)) && j == (n % k)) {
return 0;
}
long long ans = (long long)1e18;
if (dp[i][j] != -1) return dp[i][j];
if (i < (k - (n % k))) {
long long sz1 = (n / k);
long long sz2 = ((n / k) + 1);
long long id = (i * sz1 + j * sz2);
ans = min(ans, abs(a[id + sz1 - 1] - a[id]) + solve(i + 1, j));
}
if (j < (n % k)) {
long long sz1 = (n / k);
long long sz2 = ((n / k) + 1);
long long id = (i * sz1 + j * sz2);
ans = min(ans, abs(a[id + sz2 - 1] - a[id]) + solve(i, j + 1));
}
return dp[i][j] = ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
memset(dp, -1, sizeof dp);
cout << solve(0, 0);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void Read(T &x) {
char c = getchar();
bool f = false;
for (x = 0; !isdigit(c); c = getchar()) {
if (c == '-') {
f = true;
}
}
for (; isdigit(c); c = getchar()) {
x = x * 10 + c - '0';
}
if (f) {
x = -x;
}
}
template <typename T>
inline bool CheckMax(T &a, const T &b) {
return a < b ? a = b, true : false;
}
template <typename T>
inline bool CheckMin(T &a, const T &b) {
return a > b ? a = b, true : false;
}
const int N = 100005;
struct Info {
int x, y;
Info(int x = 0, int y = 0) : x(x), y(y) {}
bool operator<(const Info &b) const { return 1LL * x * b.y < 1LL * y * b.x; }
bool operator==(const Info &b) const {
return 1LL * x * b.y == 1LL * y * b.x;
}
} c[N];
pair<Info, Info> a[N];
int n, m, b[N];
long long ans;
inline void Modify(int x) {
for (; x <= m; ++b[x], x += x & -x)
;
}
inline int Query(int x) {
int r = 0;
for (; x; r += b[x], x -= x & -x)
;
return r;
}
int main() {
Read(n), Read(m);
for (int i = 1, x, y; i <= n; ++i) {
Read(x), Read(y);
if (y < 0) {
a[i] = make_pair(Info(x, -y + m), Info(-x, -y - m));
} else {
a[i] = make_pair(Info(-x, y - m), Info(x, y + m));
}
c[i] = a[i].second;
}
sort(a + 1, a + n + 1), sort(c + 1, c + n + 1),
m = unique(c + 1, c + n + 1) - c - 1;
for (int i = 1, p; i <= n; ++i) {
p = lower_bound(c + 1, c + m + 1, a[i].second) - c, ans += Query(p),
Modify(p);
}
printf("%lld\n", ans);
return 0;
}
| 4 |
// Problem Statement: https://codeforces.com/contest/1474/problem/E/
/**
* Author: Ravi Kanth Gojur
* Code Forces: ravireddy07
* Code Chef: ravireddy115
* Github: ravireddy07
**/
#include <bits/stdc++.h>
#define ll long long int
#define ravireddy07 return
#define ii(a) scanf("%d", &a)
#define ii2(a, b) scanf("%d%d", &a, &b)
#define ii3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define ill(a) scanf("%lld", &a)
#define ill2(a, b) scanf("%lld%lld", &a, &b)
#define ill3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define for1(i, a, b) for (int i = a; i < b; ++i)
#define for2(i, a, b) for (int i = b; i >= a; --i)
#define vi vector<int>
#define vii vector<vector<int>>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define pii pair<int,int>
#define unmp unordered_map
#define pqi priority_queue<int>
#define pb push_back
#define sorta(a) sort(a.begin(), a.end())
#define sortd(a) sort(a.begin(), a.end(), greater<>())
#define sortr(a) sort(a.rbegin(), a.rend())
#define yes printf("YES\n")
#define no printf("NO\n")
using namespace std;
template <typename T, typename T1>
T amax(T &a, T1 b)
{
if (b > a)
a = b;
return a;
}
const int N = 100100;
int n, m;
int p[N];
int ans[N][2];
ll sqr(ll x) {
return x * x;
}
void harry() {
ii(n);
m = 0;
for (int i = 1; i <= n; i++)
p[i] = i;
ll w = 0;
int l = 1;
int r = n - 1;
int t = 0;
while (m < n - 1) {
if (t == 0) {
w += sqr(n - l);
ans[m][0] = l;
ans[m][1] = n;
m++;
swap(p[l], p[n]);
l++;
} else {
w += sqr(r - 1);
ans[m][0] = r;
ans[m][1] = 1;
m++;
swap(p[r], p[1]);
r--;
}
t ^= 1;
}
printf("%lld\n", w);
for (int i = 1; i <= n; i++)
printf("%d ", p[i]);
printf("\n");
printf("%d\n", m);
for (int i = m - 1; i >= 0; i--)
printf("%d %d\n", ans[i][0], ans[i][1]);
ravireddy07;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
ii(t);
while (t--)
harry();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 666;
int n, p[maxn][6], len[maxn];
vector<int> rt;
int main(void) {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 5; ++j) {
scanf("%d", &p[i][j]);
len[i] += p[i][j] * p[i][j];
}
}
if (n > 12)
cout << 0 << endl;
else {
for (int i = 0; i < n; ++i) {
bool flag = true;
for (int j = 0; j < n; ++j) {
if (!flag) break;
if (i == j) continue;
for (int q = 0; q < min(16, n); ++q) {
if (q == i || q == j) continue;
int pt = 0;
for (int k = 0; k < 5; ++k)
pt += (p[q][k] - p[i][k]) * (p[j][k] - p[i][k]);
if (pt > 0) {
flag = false;
break;
}
}
}
if (flag) rt.push_back(i + 1);
}
cout << rt.size() << endl;
sort(rt.begin(), rt.end());
for (auto t : rt) cout << t << endl;
}
}
| 3 |
#include<iostream>
#include<cstring>
typedef long long ll;
typedef long double ld;
using namespace std;
ll dx[] = {1, -1, 0, 0};
ll dy[] = { 0,0,1,-1 };
ll W, H;
ll cnt;
char field[100][100];
void dfs(ll y, ll x) {
field[y][x] = '#';
for (ll k = 0; k < 4; k++) {
ll ny = y + dy[k];
ll nx = x + dx[k];
if (ny >= 0 && ny <H && nx >= 0 && nx < W) {
if (field[ny][nx] == '.')
{
cnt++;
dfs(ny, nx);
}
}
}
return;
}
int main() {
while (cin >> W >> H) {
if (W == 0 && H == 0) { break; }
cnt = 1;
for (ll i = 0; i < H; i++) {
cin >> field[i];
}
for (ll i = 0; i < H; i++) {
for (ll j = 0; j < W; j++) {
if (field[i][j] == '@') {
dfs(i, j);
}
}
}
cout << cnt << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int> > T(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
T[a].push_back(b);
T[b].push_back(a);
}
vector<bool> odw(n, false);
stack<int> S;
vector<double> odl(n);
odw[0] = true;
S.push(0);
odl[0] = 1;
double odp = 0.0;
while (!S.empty()) {
int akt = S.top();
S.pop();
odp += 1.0 / odl[akt];
for (int i = 0; i < T[akt].size(); ++i) {
if (!odw[T[akt][i]]) {
odw[T[akt][i]] = true;
S.push(T[akt][i]);
odl[T[akt][i]] = odl[akt] + 1;
}
}
}
cout.precision(10);
cout << odp;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s, v1, v2, t1, t2;
int first, second;
cin >> s >> v1 >> v2 >> t1 >> t2;
first = s * v1 + 2 * t1;
second = s * v2 + 2 * t2;
if (first < second) {
cout << "First";
} else {
if (first == second)
cout << "Friendship";
else
cout << "Second";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename L>
bool smax(T& x, L y) {
return x < y ? (x = y, true) : false;
}
template <typename T, typename L>
bool smin(T& x, L y) {
return y < x ? (x = y, true) : false;
}
struct node {
node *l, *r;
int len, pref, suff, val;
node() {
l = NULL;
r = NULL;
len = pref = suff = val = 0;
}
};
struct nnode {
int len, pref, suff, val;
nnode() { len = pref = suff = val = 0; }
inline void modify(node* id) {
len = id->len;
pref = id->pref;
suff = id->suff;
val = id->val;
}
};
inline void build_seg();
inline void build(node* id, int be, int en);
inline void add_to_seg(node* prev, node* cur, int be, int en, int p);
inline nnode query(node* id, int be, int en, int l, int r);
const int MAXN = 1e5 + 15;
node* version[MAXN];
int n, q;
bool b[MAXN];
pair<int, int> h[MAXN];
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i].first;
h[i].second = i;
}
sort(h, h + n, [](pair<int, int> a, pair<int, int> b) { return b <= a; });
build_seg();
cin >> q;
for (int i = 0, l, r, w; i < q; i++) {
cin >> l >> r >> w;
int lo, hi;
lo = -1, hi = n - 1;
while (lo + 1 < hi) {
int mid = (lo + hi) >> 1;
if (query(version[mid], 0, n, l - 1, r).val >= w) {
hi = mid;
} else {
lo = mid;
}
}
cout << h[hi].first << '\n';
}
return false;
}
inline void build_seg() {
node* tree = new node();
b[h[0].second] = true;
build(tree, 0, n);
version[0] = tree;
for (int i = 1; i < n; i++) {
b[h[i].second] = true;
version[i] = new node();
add_to_seg(version[i - 1], version[i], 0, n, h[i].second);
}
}
inline void build(node* id, int be, int en) {
if (be == en - 1) {
id->len = 1;
id->val = b[be];
id->pref = b[be];
id->suff = b[be];
return;
}
int mid = (be + en) >> 1;
id->l = new node();
id->r = new node();
build(id->l, be, mid);
build(id->r, mid, en);
id->val = max(id->l->val, max(id->r->val, id->l->suff + id->r->pref));
id->pref = id->l->pref + (id->l->pref == id->l->len ? id->r->pref : 0);
id->suff = id->r->suff + (id->r->suff == id->r->len ? id->l->suff : 0);
id->len = id->l->len + id->r->len;
}
inline void add_to_seg(node* prev, node* cur, int be, int en, int p) {
if (be == en - 1) {
cur->val = b[be];
cur->pref = cur->suff = b[be];
cur->len = 1;
return;
}
if (be > p || en <= p) return;
int mid = (be + en) >> 1;
if (p < mid) {
cur->l = new node();
cur->r = prev->r;
add_to_seg(prev->l, cur->l, be, mid, p);
} else {
cur->r = new node();
cur->l = prev->l;
add_to_seg(prev->r, cur->r, mid, en, p);
}
cur->val = max(cur->l->val, max(cur->r->val, cur->l->suff + cur->r->pref));
cur->pref = cur->l->pref + (cur->l->pref == cur->l->len ? cur->r->pref : 0);
cur->suff = cur->r->suff + (cur->r->suff == cur->r->len ? cur->l->suff : 0);
cur->len = cur->l->len + cur->r->len;
}
inline nnode query(node* id, int be, int en, int l, int r) {
if (be >= r || en <= l) {
nnode ret;
ret.val = 0;
ret.pref = 0;
ret.suff = 0;
ret.len = id->len;
return ret;
}
if (be >= l && en <= r) {
nnode ret;
ret.modify(id);
return ret;
}
int mid = (be + en) >> 1;
nnode ret, rel;
ret = query(id->l, be, mid, l, r);
rel = query(id->r, mid, en, l, r);
ret.val = max(ret.val, max(rel.val, ret.suff + rel.pref));
ret.pref = ret.pref + (ret.pref == ret.len ? rel.pref : 0);
ret.suff = rel.suff + (rel.suff == rel.len ? ret.suff : 0);
ret.len = ret.len + rel.len;
return ret;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long modulo(long long base, long long exponent, long long modulus);
long long choose(long long n, long long k);
long long inverse(long long a, long long m);
void build();
void fileio();
long long ncr(long long n, long long r);
const int nax = 1e6 + 10;
const int LG = log2(nax) + 1;
vector<long long> fact(nax);
vector<long long> adj[nax];
vector<long long> sub[nax];
long long sz[nax];
long long n;
void dfs(long long x, long long p) {
for (int i = 0; i < adj[x].size(); i++) {
long long y = adj[x][i];
if (y == p) continue;
dfs(y, x);
sz[x] += sz[y];
sub[x].push_back(sz[y]);
}
sz[x]++;
sub[x].push_back(n - sz[x]);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < (n - 1); i++) {
long long x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0);
long long ans = 0;
for (int i = 1; i <= n; i++) {
long long tot = 0;
for (int j = 0; j < sub[i].size(); j++) {
tot += (n - sub[i][j] - 1) * (sub[i][j]);
}
tot /= 2;
for (int j = 0; j < sub[i].size(); j++) {
long long x = sub[i][j];
ans += ((x * (x - 1)) / 2) * (n - x - 1 + tot - ((n - x - 1) * (x)));
}
}
cout << ans << '\n';
return 0;
}
long long ncr(long long n, long long r) {
if (r > n || r < 0 || n < 0) return 0;
long long ans = fact[n];
long long temp = (fact[n - r] * fact[r]) % 1000000007;
ans = (ans * inverse(temp, 1000000007)) % 1000000007;
return ans;
}
void fileio() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
void build() {
fact[0] = 1;
for (long long i = 1; i < nax; i++) fact[i] = (fact[i - 1] * i) % 1000000007;
}
long long modulo(long long base, long long exponent, long long modulus) {
if (modulus == 1) return 0;
long long result = 1;
base = base % modulus;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % modulus;
}
exponent = exponent >> 1;
base = (base * base) % modulus;
}
return result;
}
long long choose(long long n, long long k) {
if (k == 0) return 1;
return (n * choose(n - 1, k - 1)) / k;
}
void EE(long long a, long long b, long long &co1, long long &co2) {
if (a % b == 0) {
co1 = 0;
co2 = 1;
return;
}
EE(b, a % b, co1, co2);
long long temp = co1;
co1 = co2;
co2 = temp - co2 * (a / b);
}
long long inverse(long long a, long long m) {
long long x, y;
EE(a, m, x, y);
if (x < 0) x += m;
return x;
}
| 4 |
#include<cstdio>
#include<vector>
#define MAXN 2005
using namespace std;
vector<int> G[MAXN];
int n,k,tot;
void dfs(int u,int f,int dep)
{
if(dep>k)
return ;
tot++;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==f) continue;
dfs(v,u,dep+1);
}
}
int main()
{
scanf("%d %d",&n,&k);
for(int i=1;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
int ans=n;
int t=k;
k/=2;
if(t%2==0)
{
for(int i=1;i<=n;i++)
{
tot=0;
dfs(i,-1,0);
ans=min(ans,n-tot);
}
}
else if(t%2)
{
for(int i=1;i<=n;i++)
for(int j=0;j<G[i].size();j++)
{
int v=G[i][j];
tot=0;
dfs(v,i,0);
dfs(i,v,0);
ans=min(ans,n-tot);
}
}
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 110, M = N * N, MOD = 1000 * 1000 * 1000 + 7;
int n, a, s, ans, cnt[N], c[N][N], dp[2][N][M];
int sum(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
fill(c[0], c[0] + N, 1);
for (int i = 1; i < N; i++)
for (int j = 1; j < N; j++) c[i][j] = sum(c[i - 1][j - 1], c[i][j - 1]);
dp[0][0][0] = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a;
s += a;
cnt[a]++;
auto last = dp[!(i & 1)], now = dp[i & 1];
for (int j = 0; j < N; j++)
for (int k = 0; k < M; k++) now[j][k] = 0;
for (int j = 0; j <= i; j++)
for (int k = 0; k <= s; k++)
now[j][k] = sum(last[j][k], (a <= k && j ? last[j - 1][k - a] : 0));
}
int type_cnt = 0;
for (int i = 1; i < N; i++) type_cnt += (cnt[i] > 0);
for (int want = 1; want <= n; want++)
for (int sum = want; sum <= s; sum += want)
if (dp[n & 1][want][sum] &&
dp[n & 1][want][sum] == c[want][cnt[sum / want]]) {
ans = want;
if (type_cnt == 2 && want == cnt[sum / want]) ans = n;
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Dx[] = {0, 1, 0, -1};
const int Dy[] = {1, 0, -1, 0};
const int Ex[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int Ey[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int maxn = 2100;
int n, A[maxn][maxn];
struct Tpair {
int x, y;
Tpair(int _x = 0, int _y = 0) { x = _x, y = _y; }
inline bool operator<(const Tpair &b) const {
return x != b.x ? x < b.x : y < b.y;
}
inline bool operator!=(const Tpair &b) const { return x != b.x || y != b.y; }
};
inline double sqr(double x) { return x * x; }
inline int dist(const Tpair &a, const Tpair &b) {
return sqr(a.x - b.x) + sqr(a.y - b.y);
}
namespace Ninit {
void init() {
int i, j;
scanf("%d", &n);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) scanf("%d", A[i] + j);
}
} // namespace Ninit
namespace Nsolve {
int N1, N2;
int T1, T2, T3, T4;
namespace Nsmooth {
void solve() {
int i, j, k, x;
Tpair u, v;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) {
u = Tpair(i, j);
for (x = 0, k = 0; k < 8; ++k) {
v = Tpair(u.x + Ex[k], u.y + Ey[k]);
if (v.x < 0 || v.y < 0 || v.x >= n || v.y >= n) continue;
if (A[u.x][u.y] == A[v.x][v.y]) ++x;
}
if (x < 4) A[i][j] = 1 - A[i][j];
}
}
} // namespace Nsmooth
namespace Nfloor {
Tpair H[maxn * maxn];
bool U[maxn][maxn];
void bfs(int _x, int _y) {
int i, j, l, r, k;
Tpair u, v;
H[1] = Tpair(_x, _y), U[_x][_y] = true;
for (l = 0, r = 1; l++ != r;) {
u = H[l];
for (k = 0; k < 4; ++k) {
v = Tpair(u.x + Dx[k], u.y + Dy[k]);
if (v.x < 0 || v.y < 0 || v.x >= n || v.y >= n || !A[v.x][v.y] ||
U[v.x][v.y])
continue;
H[++r] = v, U[v.x][v.y] = true;
}
}
if (r < 70) return;
double Si = r, Cx = 0, Cy = 0, Ri = 0;
for (i = 1; i <= r; ++i) Cx += H[i].x, Cy += H[i].y;
Cx /= r, Cy /= r;
for (i = 1; i <= r; ++i) Ri = max(Ri, sqr(H[i].x - Cx) + sqr(H[i].y - Cy));
Si / Ri > 2.54 ? ++N1 : ++N2;
}
void solve() {
int i, j;
memset(U, 0, sizeof U);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
if (A[i][j] && !U[i][j]) bfs(i, j);
}
} // namespace Nfloor
void solve() {
int i;
for (i = 0; i < 5; ++i) Nsmooth::solve();
Nfloor::solve();
cout << N1 << ' ' << N2 << endl;
}
} // namespace Nsolve
int main() {
Ninit::init();
Nsolve::solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) ret = (ret << 1) + (ret << 3) + ch - '0', ch = getchar();
return ret;
}
const int N = 1e5 + 5;
pair<int, int> num[N * 6];
int n, m, ans;
int a[8], b[N], cnt[N];
int main() {
register int i, j;
for (i = 1; i <= 6; i++) a[i] = read();
for (n = read(), i = 1; i <= n; i++)
for (b[i] = read(), j = 1; j <= 6; j++)
num[++m] = make_pair(b[i] - a[j], i);
sort(num + 1, num + m + 1);
int tot = 0;
for (i = j = 1; j <= m; j++) {
if (!cnt[num[j].second]) tot++;
cnt[num[j].second]++;
if (tot == n) break;
}
i = 1;
ans = num[j].first - num[i].first;
while (j <= m) {
cnt[num[i].second]--;
while (j <= m && !cnt[num[i].second]) j++, cnt[num[j].second]++;
i++;
if (j <= m) ans = min(ans, num[j].first - num[i].first);
}
return printf("%d\n", ans), 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100;
const int mod = (int)1e9 + 7;
const int maxlog = (int)20;
const int P = mod;
int n, q, timer;
vector<int> g[maxn];
set<int> ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> q;
while (q--) {
int t, x;
cin >> t >> x;
if (t == 1) {
g[x].push_back(++timer);
ans.insert(timer);
} else if (t == 2) {
for (auto it : g[x]) {
ans.erase(it);
}
g[x].clear();
} else {
while (1) {
if (ans.empty()) break;
int cur = *ans.begin();
if (cur <= x)
ans.erase(cur);
else
break;
}
}
cout << ans.size() << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXH = 1010;
int n, k;
vector<int> arr;
vector<int> ans;
int best = -1, bestS0 = 0;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
for (int s0 = 1; s0 < MAXH; s0++) {
int res = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == s0 + k * i) {
res++;
}
}
if (res > best) {
best = res;
bestS0 = s0;
}
}
cout << (n - best) << endl;
for (int i = 0; i < n; i++) {
int d = (bestS0 + i * k) - arr[i];
if (d != 0) {
if (d > 0)
cout << "+ ";
else
cout << "- ";
cout << (i + 1) << " ";
cout << abs(d) << endl;
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
# define int long long
# define pb push_back
# define in insert
# define endl '\n'
int SubArraySum( int arr[] , int n )
{
int result = 0;
// computing sum of subarray using formula
for (int i=0; i<n; i++)
result += (arr[i] * (i+1) * (n-i));
// return all subarray sum
return result ;
}
void TC()
{
int n;
cin >> n;
int a[n];
int t[n - 1];
for(int i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a + n);
for(int i = 0; i < n - 1; i++){
t[i] = a[i + 1] - a[i];
}
int minus = SubArraySum(t, n - 1);
int plus = 0;
if(n > 1){
plus = a[1] - a[0];
}
for(int i = 1; i < n - 1; i++){
plus += a[i + 1] - a[i];
}
cout << plus - minus << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int tc = 1;
cin >> tc;
for(int tt=1;tt<=tc;tt++){
// cout << "Case #" << tt << ": ";
TC();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int fun(int x) { return (x > 0) ? x : -x; }
int main() {
int a[21][21], n, m, tt = 1, t = 0;
cin >> n >> m;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) a[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] != j) {
a[i][j] = -a[i][j];
a[i][0]++;
}
}
for (int i = 1; i <= n; i++)
if (a[i][0] > 4) {
cout << "NO";
return 0;
}
for (int i = 1; i <= n; i++)
if (a[i][0] == 4) {
for (int j = 1; j <= m; j++) {
if (a[i][j] < 0 && a[i][fun(a[i][j])] == -j) {
for (int k = 1; k <= n; k++) {
if (a[k][0] != 0 && (a[k][j] > 0 || a[k][fun(a[i][j])] > 0)) {
tt = 0;
break;
}
if (a[k][0] == 4 && (a[k][j] != a[i][j] ||
a[k][fun(a[i][j])] != a[i][fun(a[i][j])])) {
tt = 0;
break;
}
}
if (tt == 1) {
t = 1;
break;
} else
tt = 1;
}
}
if (t == 0) {
cout << "NO";
return 0;
} else {
cout << "YES";
return 0;
}
}
tt = 1;
t = 0;
for (int i = 1; i <= n; i++) {
if (a[i][0] == 3) {
for (int j = 1; j <= m; j++) {
if (a[i][j] < 0) {
for (int k = 1; k <= n; k++)
if (a[k][0] != 0 && (a[k][j] > 0 || a[k][fun(a[i][j])] > 0)) {
tt = 0;
break;
}
if (tt == 1) {
t = 1;
break;
} else
tt = 1;
}
}
if (t == 0) {
cout << "NO";
return 0;
} else {
cout << "YES";
return 0;
}
}
t = 0;
}
cout << "YES";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j = 0, k = 0, len1, len2;
string str1, str2;
cin >> str1 >> str2;
len1 = str1.size();
len2 = str2.size();
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
if (str1[i + j] != str2[j]) break;
if (j == len2 - 1) {
k++;
i += len2 - 1;
}
}
}
cout << k << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 5;
void fun(string s, int n, int &first, int &second) {
for (int i = n - 1; i >= 0; i--) {
if (s[i] == '1') {
second = (i + 1) * 2;
break;
}
}
for (int i = 0; i < n; i++) {
if (s[i] == '0')
first++;
else
first += 2;
}
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int first1 = 0, second1 = 0, first2 = 0, second2 = 0;
fun(s, n, first1, second1);
reverse(s.begin(), s.end());
fun(s, n, first2, second2);
cout << max({second1, first1, first2, second2, n}) << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
const int Mo = (int)1e9;
struct Node {
int mat[2][2];
int lenx, leny;
} B[200005], C[200005], tree[200005 * 4], Ans;
int n, m, i, a[200005], len[200005 * 4], add[200005 * 4];
int type, u, l, r, x;
inline Node operator+(Node a, Node b) {
Node c = a;
int i, j;
for (i = 0; i < a.lenx; ++i)
for (j = 0; j < a.leny; ++j) c.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % Mo;
return c;
}
inline Node operator*(Node a, Node b) {
Node c = {0};
c.lenx = a.lenx;
c.leny = b.leny;
int i, j, k;
for (k = 0; k < a.leny; ++k)
for (i = 0; i < a.lenx; ++i)
for (j = 0; j < b.leny; ++j)
(c.mat[i][j] += 1ll * a.mat[i][k] * b.mat[k][j] % Mo) %= Mo;
return c;
}
inline Node operator*(Node a, int b) {
int i, j;
for (i = 0; i < a.lenx; ++i)
for (j = 0; j < a.leny; ++j) a.mat[i][j] = 1ll * a.mat[i][j] * b % Mo;
return a;
}
void change(int t, int c) {
Node A = {0};
A.lenx = 1;
A.leny = 2;
A.mat[0][1] = c;
tree[t] = tree[t] + (A * C[len[t]]);
(add[t] += c) %= Mo;
}
void down(int t) {
if (!add[t]) return;
change((t << 1), add[t]);
change(((t << 1) | 1), add[t]);
add[t] = 0;
}
void build_tree(int l, int r, int t) {
len[t] = r - l + 1;
if (l == r) {
tree[t].lenx = 1;
tree[t].leny = 2;
tree[t].mat[0][1] = a[l];
tree[t] = tree[t] * B[1];
} else {
build_tree(l, ((l + r) >> 1), (t << 1));
build_tree(((l + r) >> 1) + 1, r, ((t << 1) | 1));
tree[t] = tree[(t << 1)] + (tree[((t << 1) | 1)] * B[len[(t << 1)]]);
}
}
void update(int ll, int c, int l, int r, int t) {
if (l == r) {
tree[t].mat[0][0] = 0;
tree[t].mat[0][1] = c;
tree[t] = tree[t] * B[1];
} else {
down(t);
if (ll <= ((l + r) >> 1))
update(ll, c, l, ((l + r) >> 1), (t << 1));
else
update(ll, c, ((l + r) >> 1) + 1, r, ((t << 1) | 1));
tree[t] = tree[(t << 1)] + (tree[((t << 1) | 1)] * B[len[(t << 1)]]);
}
}
void ask(int ll, int rr, int l, int r, int t) {
if (ll <= l && r <= rr)
Ans = Ans + (tree[t] * B[l - ll]);
else {
down(t);
if (ll <= ((l + r) >> 1)) ask(ll, rr, l, ((l + r) >> 1), (t << 1));
if (rr > ((l + r) >> 1)) ask(ll, rr, ((l + r) >> 1) + 1, r, ((t << 1) | 1));
}
}
void modify(int ll, int rr, int c, int l, int r, int t) {
if (ll <= l && r <= rr)
change(t, c);
else {
down(t);
if (ll <= ((l + r) >> 1)) modify(ll, rr, c, l, ((l + r) >> 1), (t << 1));
if (rr > ((l + r) >> 1))
modify(ll, rr, c, ((l + r) >> 1) + 1, r, ((t << 1) | 1));
tree[t] = tree[(t << 1)] + (tree[((t << 1) | 1)] * B[len[(t << 1)]]);
}
}
int main() {
scanf("%d%d", &n, &m);
B[0].lenx = B[0].leny = 2;
B[0].mat[0][0] = 1;
B[0].mat[1][1] = 1;
B[1].mat[0][1] = 1;
B[1].mat[1][1] = 1;
B[1].mat[1][0] = 1;
B[1].lenx = B[1].leny = 2;
for (i = 2; i <= n; ++i) B[i] = B[i - 1] * B[1];
C[1] = B[1];
for (i = 2; i <= n; ++i) C[i] = B[i] + C[i - 1];
for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
build_tree(1, n, 1);
Ans.lenx = 1;
Ans.leny = 2;
for (; m--;) {
scanf("%d", &type);
if (type == 1) {
scanf("%d%d", &u, &x);
update(u, x, 1, n, 1);
} else if (type == 2) {
scanf("%d%d", &l, &r);
memset(Ans.mat, 0, sizeof(Ans.mat));
ask(l, r, 1, n, 1);
printf("%d\n", Ans.mat[0][0]);
} else if (type == 3) {
scanf("%d%d%d", &l, &r, &x);
modify(l, r, x, 1, n, 1);
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define INF INT64_MAX
int main()
{
int n, m;
cin >> n >> m;
vector<vector<pair<int, long long>>> g( n );
for( int i = 0; i < m; i++ ) {
int u, v;
long long s;
cin >> u >> v >> s;
u--;
v--;
g[u].push_back( make_pair( v, s ) );
g[v].push_back( make_pair( u, s ) );
}
vector<bool> plus( n );
plus[0] = true;
vector<long long> value( n, INF );
value[0] = 0;
long long offset = INF;
stack<int> st;
st.push( 0 );
while( !st.empty() ) {
int u = st.top();
st.pop();
for( auto e : g[u] ) {
int v = e.first;
long long s = e.second;
if( u == v ) continue;
if( value[v] == INF ) {
plus[v] = !plus[u];
value[v] = s - value[u];
st.push( v );
continue;
}
if( plus[u] != plus[v] ) {
if( value[u] + value[v] != s ) {
cout << 0 << endl;
return 0;
}
continue;
}
long long diff = s - ( value[u] + value[v] );
if( plus[v] ) diff = ( value[u] + value[v] ) - s;
if( diff % 2 ) {
cout << 0 << endl;
return 0;
}
if( offset != INF && offset != diff / 2 ) {
cout << 0 << endl;
return 0;
}
offset = diff / 2;
}
}
long long posmin = INF;
long long negmin = INF;
for( int i = 0; i < n; i++ ) {
if( plus[i] ) {
posmin = min( posmin, value[i] );
}
else {
negmin = min( negmin, value[i] );
}
}
int ans;
if( offset != INF ) {
if( posmin - offset > 0 && negmin + offset > 0 ) {
ans = 1;
}
else {
ans = 0;
}
}
else {
ans = max( 0LL, posmin + negmin - 1 );
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int M = 100005;
int read() {
int x = 0, flag = 1;
char c;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = -1;
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * flag;
}
int n, tot, rt, Min = 0x3f3f3f3f, f[M], siz[M], bel[M];
long long ans, dis[M];
set<int> in[M], minn;
set<pair<int, int> > Set;
struct edge {
int v, c, next;
edge(int V = 0, int C = 0, int N = 0) : v(V), c(C), next(N) {}
} e[2 * M];
void dfs1(int u, int fa) {
int Max = 0;
siz[u] = 1;
for (int i = f[u]; i; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs1(v, u);
siz[u] += siz[v];
Max = max(Max, siz[v]);
}
Max = max(Max, n - siz[u]);
if (Max < Min) {
Min = Max;
rt = u;
}
}
void dfs2(int u, int fa) {
ans += dis[u];
for (int i = f[u]; i; i = e[i].next) {
int v = e[i].v, c = e[i].c;
if (v == fa) continue;
dis[v] = dis[u] + c;
dfs2(v, u);
}
}
void dfs3(int u, int fa, int tp) {
bel[u] = tp;
in[tp].insert(u);
for (int i = f[u]; i; i = e[i].next) {
int v = e[i].v;
if (v == fa) continue;
dfs3(v, u, tp);
}
}
void link(int u, int v) {
int x = bel[u], y = bel[v];
minn.erase(v);
if (x) {
Set.erase(make_pair(siz[x], x));
Set.insert(make_pair(--siz[x], x));
}
if (y) {
in[y].erase(v);
if (!in[y].empty()) minn.insert(*in[y].begin());
Set.erase(make_pair(siz[y], y));
Set.insert(make_pair(--siz[y], y));
}
}
int solve(int i) {
int ret = 0;
if (Set.rbegin()->first == n - i + 1 && Set.rbegin()->second != bel[i])
ret = *in[Set.rbegin()->second].begin();
else {
set<int>::iterator it = minn.begin();
it++;
ret = (bel[i] != bel[*minn.begin()] || i == rt) ? (*minn.begin()) : (*it);
}
link(i, ret);
return ret;
}
int main() {
n = read();
for (int i = 1; i <= n - 1; i++) {
int u = read(), v = read(), c = read();
e[++tot] = edge(v, c, f[u]), f[u] = tot;
e[++tot] = edge(u, c, f[v]), f[v] = tot;
}
dfs1(1, 0);
dfs2(rt, 0);
printf("%lld\n", ans * 2);
if (n == 1) {
puts("1");
return 0;
}
minn.insert(rt);
for (int i = f[rt]; i; i = e[i].next) {
int v = e[i].v;
dfs3(v, rt, v);
siz[v] = in[v].size() * 2;
minn.insert(*in[v].begin());
Set.insert(make_pair(siz[v], v));
}
for (int i = 1; i <= n; i++) printf("%d ", solve(i));
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int64_t x,k,d;
cin >> x >> k >> d;
x=llabs(x);
int64_t steps=min(k,x/d);
k-=steps;
x-=d*steps;
if(k!=0){
if(k%2==0)cout << x << endl;
else cout << d-x << endl;
}else cout << x << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long n, m;
cin >> n >> m;
vector<long long> a(n + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 1; i <= n; i++) {
if (m <= i) a[i] += a[i - m];
cout << a[i] << ' ';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 1e4 + 5, OO = 0x3f3f3f3f;
int n, m, sz;
vector<int> types;
int arr[105];
int vis[110];
int main() {
cin >> n >> m;
memset(vis, 0, sizeof vis);
for (int i = 0; i < m; i++) {
cin >> arr[i];
vis[arr[i]]++;
}
for (int i = 0; i <= 105; i++) {
if (vis[i] > 0) types.push_back(vis[i]);
}
for (int i = 0; i < types.size(); i++) sort(types.rbegin(), types.rend());
int ctr = 0, sum = 0, mx = 0;
for (int j = 1; j <= 100; j++) {
for (int i = 0; i < types.size(); i++) {
{ ctr = types[i] / j; }
sum += ctr;
}
if (sum < n) {
sum = 0;
ctr = 0;
break;
}
mx = j;
sum = 0;
}
cout << mx << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 100001;
const int M = 20;
const int MOD = 1000000007;
const int inf = 1234567890;
const long long INF = 1122334455667788990;
bool ispalin(string &s) {
for (int i = 0, j = s.size() - 1; i <= j; i++, j--)
if (s[i] != s[j]) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int tc = 1, cs = 0;
while (tc--) {
string s;
cin >> s;
if (ispalin(s)) return cout << 0 << '\n', 0;
int n = s.size();
cout << 7 << '\n';
cout << 'L' << ' ' << 2 << '\n';
cout << 'L' << ' ' << 2 << '\n';
cout << 'L' << ' ' << 2 << '\n';
n += 3;
cout << 'R' << ' ' << 2 << '\n';
n += (n - 2);
cout << 'L' << ' ' << n - 1 << '\n';
n += (n - 2);
cout << 'R' << ' ' << 2 << '\n';
n += (n - 2);
cout << 'R' << ' ' << n - 1 << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i % 2 == 0) {
if (i % 4 == 0 && j == 1) {
cout << '#';
continue;
} else if (i % 4 > 0 && j == m) {
cout << '#';
continue;
}
cout << '.';
continue;
}
cout << '#';
}
cout << endl;
}
}
| 1 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 300002
using namespace std;
struct shop{
int l,r;
}a[N];
int n,m,i,j=1,d,c[N];
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
int my_comp(const shop &x,const shop &y)
{
return x.r-x.l<y.r-y.l;
}
int lowbit(int x)
{
return x&(-x);
}
int ask(int x)
{
int ans=0;
for(int i=x;i>=1;i-=lowbit(i)) ans+=c[i];
return ans;
}
void add(int x,int y)
{
for(int i=x;i<=m+1;i+=lowbit(i)) c[i]+=y;
}
int main()
{
n=read();m=read();
for(i=1;i<=n;i++) a[i].l=read()+1,a[i].r=read()+1;
sort(a+1,a+n+1,my_comp);
for(d=1;d<=m;d++){
while(j<=n&&a[j].r-a[j].l<d){
add(a[j].l,1);
add(a[j].r+1,-1);
j++;
}
int ans=n-j+1;
for(i=1;i<=m+1;i+=d) ans+=ask(i);
printf("%d\n",ans);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long(n);
cin >> (n);
;
if (n == 1)
return puts("1");
else {
long long val = 0, ans = 1;
for (long long i = 2; i <= n; i++) {
val += 4;
ans += val;
}
cout << ans << "\n";
}
return 0;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.