solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int prime = 999983;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-8;
const long long mod = (long long)1e9 + 7;
const unsigned long long mx = 133333331;
const int ma = 1e9;
inline void RI(int &x) {
char c;
while ((c = getchar()) < '0' || c > '9')
;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0';
}
int a[1000][10];
int main() {
int n;
cin >> n;
for (int i = 0; i <= n; i++) {
cin >> a[i][0] >> a[i][1];
}
int ans = 0;
for (int i = 2; i <= n; i++) {
if (a[i - 2][0] == a[i - 1][0] && a[i - 2][1] > a[i - 1][1]) {
if (a[i - 1][0] < a[i][0]) ans++;
}
if (a[i - 2][0] == a[i - 1][0] && a[i - 2][1] < a[i - 1][1]) {
if (a[i - 1][0] > a[i][0]) ans++;
}
if (a[i - 2][1] == a[i - 1][1] && a[i - 2][0] > a[i - 1][0]) {
if (a[i - 1][1] > a[i][1]) ans++;
}
if (a[i - 2][1] == a[i - 1][1] && a[i - 2][0] < a[i - 1][0]) {
if (a[i - 1][1] < a[i][1]) ans++;
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,x,y;
cin>>a>>b;
x=b-a+1;
y=b+a-1;
while(x<=y)
{
cout<<x<<" ";
x++;
}
}
| 0 |
#include<iostream>
using namespace std;
int n;
long long f[87];
int main()
{
cin>>n;
f[0]=2;
f[1]=1;
for(int i=2;i<=n;i++)
f[i]=f[i-1]+f[i-2];
cout<<f[n];
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
namespace gbd_ns {
template <typename C>
struct is_iterable {
template <class T>
static long check(...);
template <class T>
static char check(int, typename T::const_iterator = C().end());
enum {
value = sizeof(check<C>(0)) == sizeof(char),
neg_value = sizeof(check<C>(0)) != sizeof(char)
};
};
template <class T>
ostream &_out_str(ostream &os, const T &x) {
return os << '"' << x << '"';
}
template <bool B, typename T = void>
using eit = typename enable_if<B, T>::type;
template <class T>
struct _gbd3C;
template <class T>
ostream &_gbd3(ostream &os, const T &x) {
return _gbd3C<T>::call(os, x);
}
template <class T>
struct _gbd3C {
template <class U = T>
static ostream &call(ostream &os, eit<is_iterable<U>::value, const T> &V) {
os << "{";
bool ff = 0;
for (const auto &E : V) _gbd3<decltype(E)>(ff ? os << "," : os, E), ff = 1;
return os << "}";
}
template <class U = T>
static ostream &call(ostream &os,
eit<is_iterable<U>::neg_value, const T> &x) {
return os << x;
}
};
template <>
struct _gbd3C<string> {
static ostream &call(ostream &os, const string &s) {
return os << '"' << s << '"';
}
};
template <>
struct _gbd3C<char *> {
static ostream &call(ostream &os, char *const &s) {
return os << '"' << s << '"';
}
};
template <class S, class T>
struct _gbd3C<pair<S, T> > {
static ostream &call(ostream &os, const pair<S, T> &p) {
_gbd3(os << '(', p.first);
_gbd3(os << ',', p.second);
return os << ')';
}
};
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args);
ostream &_gbd2(ostream &os, vector<string>::iterator) { return os; }
template <typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const char *x,
Args &&...args) {
return _gbd2(os << " " << x, nm + 1, args...);
}
template <class T, typename... Args>
ostream &_gbd2(ostream &os, vector<string>::iterator nm, const T &x,
Args &&...args) {
return _gbd2(_gbd3<T>(os << " " << *nm << "=", x), nm + 1, args...);
}
vector<string> split(string s) {
vector<string> Z;
string z = "";
s += ',';
int dep = 0;
for (char c : s) {
if (c == ',' && !dep)
Z.push_back(z), z = "";
else
z += c;
if (c == '(') ++dep;
if (c == ')') --dep;
}
return Z;
}
template <typename... Args>
ostream &_gbd1(int ln, const string &nm, Args &&...args) {
auto nms = split(nm);
_gbd2(cerr << "L" << ln << ":", nms.begin(), args...);
return cerr << endl;
}
} // namespace gbd_ns
inline void nop() {}
const int e3 = 1000;
const int e6 = e3 * e3;
const int e9 = e6 * e3;
const long double tau = 2 * acosl(-1);
typedef long double ld;
const ld eps = (ld)1e-8;
const int inf = e9 + 99;
const long long linf = 1LL * e9 * e9 + 99;
const int P = e9 + 7;
const int N = 100 << 10;
int n, m, k, s, a[N];
vector<int> adj[N];
int d[128][N];
int32_t main() {
scanf("%d%d%d%d", &n, &m, &k, &s);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int t = 1; t <= k; t++) {
static bool seen[N];
for (int i = 1; i <= n; i++) seen[i] = 0;
static vector<int> Q;
Q.clear();
for (int i = 1; i <= n; i++)
if (a[i] == t) seen[i] = 1, Q.push_back(i);
int ii = 0;
for (; ii < (int((Q).size()));) {
int u = Q[ii++];
for (int v : adj[u])
if (!seen[v]) {
seen[v] = 1;
Q.push_back(v);
d[t][v] = d[t][u] + 1;
}
}
assert(ii == n);
}
for (int i = 1; i <= n; i++) {
static vector<int> v;
v.clear();
for (int t = 1; t <= k; t++) v.push_back(d[t][i]);
sort(v.begin(), v.end());
printf("%d%c", accumulate(v.begin(), v.begin() + s, 0), " \n"[i == n]);
}
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M, T;
cin >> N >> M >> T;
vector<int>a (N+1);
a[N] = T;
for( int i = 0; i < N; i++)
cin >> a[i];
int ans = a[0] - M;
for( int i = 0; i < a.size()-1; i++) {
if( i == a.size() - 2 )
ans += max(0, a[i+1]-a[i]-M);
else
ans += max(0, a[i+1]-a[i]-2*M);
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[50][7], b[50] = {0}, d = 0;
string x[50];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i];
for (int j = 0; j < 7; j++) cin >> a[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 2; j++) {
if (j == 0)
a[i][j] *= 100;
else
a[i][j] *= -50;
}
for (int j = 0; j < 7; j++) b[i] += a[i][j];
}
int max = b[0];
for (int i = 1; i < n; i++) {
if (b[i] > max) {
max = b[i];
d = i;
}
}
cout << x[d];
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, i, j, k, x, y, z, l, r, mid, ans, mod = 1e9 + 7, g, m;
long long a[400005], b[400005], c[400005];
char ch;
string s, st;
vector<long long> v[300005];
vector<pair<long long, long long> > u;
pair<long long, long long> d[2005];
long long dp(int x, int d) {
b[x] = 1;
for (int i = 0; i < v[x].size(); i++) b[x] += dp(v[x][i], d + 1);
u.push_back({d, x});
return b[x];
}
void dfs(int x) {
if (x != u[i].second) d[j++] = {c[x], x};
for (auto i : v[x]) dfs(i);
}
int main() {
ios::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
cin >> n;
for (i = 0; i < n; i++) {
cin >> x >> y;
v[x].push_back(i + 1);
a[i + 1] = y;
}
x = v[0][0];
y = dp(x, 0);
sort(u.rbegin(), u.rend());
for (i = 0; i < n; i++) {
x = u[i].second;
if (b[x] - 1 < a[x]) return cout << "NO", 0;
c[x] = a[x] + 1;
j = 0;
dfs(x);
y = b[x];
sort(d, d + j);
for (j = j - 1; j >= 0; j--)
if (y == c[x]) {
y--;
j++;
continue;
} else
c[d[j].second] = y--;
}
cout << "YES\n";
for (i = 1; i <= n; i++) cout << c[i] << " ";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char g[100100];
int mark[505][505];
int main() {
int x, y, x0, y0;
cin >> x >> y >> x0 >> y0;
scanf("%s", g);
int len = strlen(g);
printf("1 ");
mark[x0][y0] = 1;
for (int i = 0; i < len; i++) {
if (g[i] == 'U') {
x0 = max(1, x0 - 1);
}
if (g[i] == 'D') {
x0 = min(x, x0 + 1);
}
if (g[i] == 'L') {
y0 = max(1, y0 - 1);
}
if (g[i] == 'R') {
y0 = min(y, y0 + 1);
}
if (i == len - 1) {
int flag = 0;
if (mark[x0][y0] == 0) {
mark[x0][y0] = 1;
flag = 1;
}
int tmp = 0;
for (int i = 1; i <= x; i++)
for (int j = 1; j <= y; j++)
if (mark[i][j] == 0) tmp++;
cout << tmp + flag;
} else {
if (mark[x0][y0] == 0) {
printf("1 ");
mark[x0][y0] = 1;
} else
printf("0 ");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int z[5001];
int main() {
int n;
cin >> n;
int x[n], maxi;
cin >> x[0];
maxi = x[0];
for (int i = 1; i < n; i++) {
cin >> x[i];
if (x[i] > maxi) {
maxi = x[i];
}
}
vector<int> f, l;
for (int i = 0; i < n; i++) {
z[x[i]]++;
}
for (int i = maxi - 1; i >= 0; i--) {
if (z[i] >= 2) {
f.push_back(i);
l.push_back(i);
} else if (z[i] == 1) {
f.push_back(i);
}
}
cout << f.size() + l.size() + 1 << endl;
for (int i = f.size() - 1; i >= 0; i--) {
cout << f[i] << " ";
}
cout << maxi << " ";
for (int i = 0; i < l.size(); i++) {
cout << l[i] << " ";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
long long power(long long x, long long n) {
if (n == 0) return 1;
if (n == 1) return x % mod;
if (n % 2 == 0) {
long long y = power(x, n / 2) % mod;
return (y * y) % mod;
}
if (n & 1) {
long long y = power(x, n - 1);
return (x % mod * y % mod) % mod;
}
return 0;
}
long long dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
long long dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const long long maxn = 100005;
void solve() {
long long n, i, j, k, m;
cin >> n >> m;
vector<long long> ar(n);
for (i = 0; i < n; i++) cin >> ar[i];
while (m--) {
long long l, r, x;
cin >> l >> r >> x;
l--, r--, x--;
long long cnt = 0;
for (long long i = l; i <= r; i++) {
if (ar[i] < ar[x]) cnt++;
}
if (l + cnt == x)
cout << "Yes\n";
else
cout << "No\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t = 1;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
long long fix_mod(long long x, long long y) { return (y + x % y) % y; }
void fast() {
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
long long mem[5006][5006];
int main() {
fast();
int n;
cin >> n;
vector<long long> v(n + 1, 0);
for (int i = 1; i <= n; i++) cin >> v[i], mem[1][i] = v[i];
for (int j = 2; j <= n; j++) {
for (int i = 1; i + j - 1 <= n; i++) {
v[i] ^= v[i + 1];
mem[j][i] = v[i];
}
}
for (int j = 1; j <= n; j++) {
for (int i = 1; i + j - 1 <= n; i++) {
mem[j][i] = max(mem[j - 1][i], mem[j][i]);
mem[j][i] = max(mem[j - 1][i + 1], mem[j][i]);
}
}
int q, l, r;
cin >> q;
while (q--) {
cin >> l >> r;
cout << mem[r - l + 1][l] << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng((uint64_t)chrono::duration_cast<chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count());
inline int rand(int l, int r) {
uniform_int_distribution<int> RNG(l, r);
return RNG(rng);
}
const int N = 1e5 + 5;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
char s[55][55];
int lvl[55][55];
int n, m;
bool f(int i, int j) {
bool ret = 0;
for (int k = 0; k < 4; k++) {
int newi = i + dx[k];
int newj = j + dy[k];
if (newi >= 0 && newi < n && newj >= 0 && newj < m) {
if (s[newi][newj] != s[i][j]) continue;
if (lvl[newi][newj] != -1) {
if (abs(lvl[i][j] - lvl[newi][newj]) >= 3) return ret = 1;
} else {
lvl[newi][newj] = lvl[i][j] + 1;
ret |= f(newi, newj);
}
}
}
return ret;
}
void solve(int casenum) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) lvl[i][j] = -1;
for (int i = 0; i < n; i++) scanf(" %s", &s[i]);
bool b = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (lvl[i][j] == -1) {
lvl[i][j] = 0;
b |= f(i, j);
}
if (b)
printf("Yes\n");
else
printf("No\n");
}
int main() {
int T = 1;
for (int i = 1; i <= T; i++) solve(i);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);
struct Node {
Node *lc, *rc;
int leftmost, upmost;
Node() {
lc = rc = NULL;
leftmost = INF, upmost = -INF;
}
} * root;
void push(Node*& now) {
if (!now->lc) now->lc = new Node();
if (!now->rc) now->rc = new Node();
now->lc->leftmost = min(now->lc->leftmost, now->leftmost);
now->lc->upmost = max(now->lc->upmost, now->upmost);
now->rc->leftmost = min(now->rc->leftmost, now->leftmost);
now->rc->upmost = max(now->rc->upmost, now->upmost);
now->leftmost = INF, now->upmost = -INF;
}
int query(Node*& now, int L, int R, int qx, bool up) {
if (!now) now = new Node();
if (L == R) return (up ? now->upmost : now->leftmost);
int M = L + (R - L) / 2;
push(now);
if (qx <= M) return query(now->lc, L, M, qx, up);
return query(now->rc, M + 1, R, qx, up);
}
void modify(Node*& now, int L, int R, int mL, int mR, int mv, bool up) {
if (!now) now = new Node();
if (mL <= L && R <= mR) {
if (up)
now->upmost = max(now->upmost, mv);
else
now->leftmost = min(now->leftmost, mv);
return;
}
int M = L + (R - L) / 2;
push(now);
if (mL <= M) modify(now->lc, L, M, mL, mR, mv, up);
if (mR > M) modify(now->rc, M + 1, R, mL, mR, mv, up);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
while (q--) {
int c, r;
char dir[3];
scanf("%d%d%s", &c, &r, dir);
if (dir[0] == 'U') {
int U = query(root, 1, n, r, 1);
if (query(root, 1, n, r, 0) == r) {
printf("0\n");
continue;
}
U = max(U, 0);
printf("%d\n", r - U);
modify(root, 1, n, U, r, r, 0);
} else {
int L = query(root, 1, n, r, 0);
if (query(root, 1, n, r, 1) == r) {
printf("0\n");
continue;
}
L = min(L, n + 1);
printf("%d\n", L - r);
modify(root, 1, n, r, L, r, 1);
}
}
}
| 5 |
#include <bits/stdc++.h>
const double EPS = 0.00000001;
const long long mod = 1000000000 + 7;
using namespace std;
int a[100100];
int pref[100100], suf[100100];
int main() {
ios::sync_with_stdio(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
char x;
cin >> x;
if (x == '0') a[i]++;
}
k++;
pref[0] = a[0];
for (int i = 1; i < n; i++) pref[i] = pref[i - 1] + a[i];
long long ans = n + 1;
for (int i = 0; i < n; i++) {
if (a[i]) {
long long lo = 0, hi = n;
while (hi - lo > 1) {
long long mid = (hi + lo) / 2;
int m = mid;
int j = min(i + m, n - 1);
int w = max(0, i - m);
if (pref[j] - pref[w] + a[w] >= k)
hi = m;
else
lo = m;
}
ans = min(ans, hi);
}
}
cout << ans << endl;
int ____________;
cin >> ____________;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 70, MAXM = 100, MOD = 1e9 + 7, INF = 1e9;
char c[] = {'c', 'o', 'd', 'e', 'f', 'o', 'r', 'c', 'e', 's'};
void solve() {
long long n;
cin >> n;
long long x = 0, x1 = 0;
while (pow(x + 1, 10) <= n) x++;
while (pow(x, 10 - x1) * pow(x + 1, x1) < n) x1++;
for (int i = 0; i < 10; i++) {
if (i < x1)
for (int j = 0; j <= x; j++) cout << c[i];
else
for (int j = 0; j < x; j++) cout << c[i];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int arr[30] = {0};
unsigned long long sum = 0;
long long int a, b, n;
cin >> n;
for (int i = 0; i < n; i++) {
int j = 0;
cin >> a;
while (a != 0) {
b = a % 10;
arr[j] += b;
arr[j + 1] += b;
a /= 10;
j += 2;
}
}
for (unsigned long long int i = 0, ten = 1; i < 30; i++, ten = ten * 10) {
if (!arr[i]) continue;
arr[i] *= n;
unsigned long long x =
((arr[i] % 998244353) * (ten % 998244353)) % 998244353;
sum = (sum + x) % 998244353;
}
cout << sum << endl;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2")
#pragma GCC optimize("unroll-loops")
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define req(i,a,b) for(int i=(a);i>=(b);--i)
#define rep_(i,a,b) for(int i=(a);i<(b).size();++i)
#define F(a) rep(a,1,n)
#define M(a,b) memset(a,b,sizeof a)
#define DC int T;cin>>T;while(T--)
#define ll long long
#define Z(a,b) sort(a+1,a+b+1)
using namespace std;
const unsigned _mod=998244353;
const unsigned mod=1e9+7;
const ll infi=0x3f3f3f3f3f3f3fll;
const int inf=0x3f3f3f3f;
void rd(ll &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;}
ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;}
ll qpow(ll x,ll y=2){ll ret=1;while(y>0){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;}
/*
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
*/
ll fac[1000010],inv[1000010];
ll C(ll n,ll m){if(m>n||m<0)return 0;return fac[n]*inv[m]%mod*inv[n-m]%mod;}
int n,l,r,ans;
int chk(int x)
{
if (x<=0) return 0;
int a=max(x+l-1,0),b=max(n-r+x,0);
if (a+b>n) return 0;
if (n&1) return C(n-a-b,n/2-a)+C(n-a-b,n/2-a+1);
return C(n-a-b,n/2-a);
}
int chk2()
{
if (n&1) return C(n,n/2)+C(n,n/2+1);
return C(n,n/2);
}
int main()
{
*fac=*inv=1;
rep(i,1,1000000) fac[i]=fac[i-1]*i%mod;
rep(i,1,1000000) inv[i]=ksm(fac[i]);
DC
{
ans=0;
cin>>n>>l>>r;
rep(i,min(-l,r-n),min(n-l,r)) (ans+=chk(i))%=mod;
(ans+=1ll*max(min(-l,r-n)-1,0)*chk2()%mod)%=mod;
cout<<ans<<'\n';
}
return 0;
}
| 4 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <tuple>
#include <math.h>
#include <iomanip>
using namespace std;
class Sover {
};
int main()
{
int n;
cin >> n;
vector<double> x(n), r(n);
vector<double> L, R;
for (int i = 0; i < n; i++)
{
cin >> x[i] >> r[i];
L.emplace_back(x[i] - r[i]);
R.emplace_back(x[i] + r[i]);
}
sort(L.begin(), L.end(), greater<double>());
sort(R.begin(), R.end());
double left = L[0];
double right = R[0];
double bottom = left;
double top = right;
double thres = 0.0001;
while (bottom + thres < top) {
double mid = (bottom + top) / 2;
double mid2 = (bottom + mid) / 2;
double bottom_height = 1e9;
double mid_height = 1e9;
double mid2_height = 1e9;
for (int i = 0; i < n; i++) {
double dx = abs(bottom - x[i]);
double tmp_bottom_height = 0;
if (dx < thres)tmp_bottom_height = r[i];
else tmp_bottom_height = sqrt(r[i] * r[i] - dx * dx);
dx = abs(mid - x[i]);
double tmp_mid_height = 0;
if (dx < thres)tmp_mid_height = r[i];
else tmp_mid_height = sqrt(r[i] * r[i] - dx * dx);
dx = abs(mid2 - x[i]);
double tmp_mid2_height = 0;
if (dx < thres)tmp_mid2_height = r[i];
else tmp_mid2_height = sqrt(r[i] * r[i] - dx * dx);
bottom_height = min(bottom_height, tmp_bottom_height);
mid_height = min(mid_height, tmp_mid_height);
mid2_height = min(mid2_height, tmp_mid2_height);
}
if (bottom_height <= mid2_height + thres
&& mid2_height < mid_height + thres) {
bottom = mid2;
}
else {
top = mid;
}
}
double res = 1e9;
for (int i = 0; i < n; i++) {
double dx = abs(bottom - x[i]);
if (dx < thres)res = min(res, r[i]);
else res = min(res, sqrt(r[i] * r[i] - dx * dx));
}
cout << fixed << setprecision(10) << res;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
class CAlyonaAndSpreadsheet {
public:
static void solve(istream& in, ostream& out) {
int n, m;
in >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
in >> a[i][j];
}
}
vector<int> mx(n);
for (int j = 0; j < m; ++j) {
int pre = 0;
for (int i = 0; i + 1 < n; ++i) {
if (a[i][j] > a[i + 1][j]) {
mx[pre] = max(mx[pre], i);
pre = i + 1;
}
}
mx[pre] = max(mx[pre], n - 1);
}
for (int i = 1; i < n; ++i) {
mx[i] = max(mx[i], mx[i - 1]);
}
int k;
in >> k;
for (int i = 0; i < k; ++i) {
int l, r;
in >> l >> r;
--l;
--r;
out << (mx[l] >= r ? "Yes" : "No") << '\n';
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
CAlyonaAndSpreadsheet::solve(in, out);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long p[300005];
priority_queue<long long> L, R, U;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &p[i]);
if (!U.empty() && -U.top() < p[i]) {
if (!R.empty() && -R.top() < p[i] && p[i] + R.top() > p[i] + U.top()) {
U.push(R.top());
R.pop();
R.push(-p[i]);
} else {
L.push(U.top());
R.push(-p[i]);
U.pop();
}
} else {
if (!R.empty() && -R.top() < p[i]) {
U.push(R.top());
R.pop();
R.push(-p[i]);
} else {
U.push(-p[i]);
}
}
}
long long ans = 0;
while (!R.empty()) {
ans -= R.top();
R.pop();
}
while (!L.empty()) {
ans += L.top();
L.pop();
}
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
class unionfind {
vector<int> par;
public:
unionfind(int n) {
for (int i = 0; i < n; i++) {
par.push_back(i);
}
}
int find(int n) {
if (n == par.at(n)) return n;
return par.at(n) = find(par.at(n));
}
void merge(int m, int n) {
m = find(m); n = find(n);
par.at(m) = n;
}
};
int main() {
int n, m;
cin >> n >> m;
vector<int> p(n), x(m), y(m);
for (int i = 0; i < n; i++) {
cin >> p.at(i);
p.at(i)--;
}
for (int i = 0; i < m; i++) {
cin >> x.at(i) >> y.at(i);
x.at(i)--;
y.at(i)--;
}
unionfind t(n);
for (int i = 0; i < m; i++) {
t.merge(x.at(i), y.at(i));
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (t.find(i) == t.find(p.at(i))) {
ans++;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string a, b, c;
long long ca[26];
long long cb[26];
long long cc[26];
int main() {
ios::sync_with_stdio(0);
getline(cin, a);
getline(cin, b);
getline(cin, c);
for (int i = 0; i < (int)a.size(); ++i) ca[a[i] - 'a']++;
for (int i = 0; i < (int)b.size(); ++i) cb[b[i] - 'a']++;
for (int i = 0; i < (int)c.size(); ++i) cc[c[i] - 'a']++;
int ansb = 0;
int ansc = 0;
for (int i = 0; i < (int)a.size(); ++i) {
bool can = 1;
for (int j = 0; j < 26; ++j) {
ca[j] -= i * cb[j];
if (ca[j] < 0) can = 0;
}
if (can) {
long long cnt = 1e9;
for (int j = 0; j < 26; ++j)
if (cc[j]) cnt = min(cnt, ca[j] / cc[j]);
if (i + cnt > ansc + ansb) {
ansb = i;
ansc = cnt;
}
}
for (int j = 0; j < 26; ++j) ca[j] += i * cb[j];
}
for (int j = 0; j < 26; ++j) {
ca[j] -= ansb * cb[j];
ca[j] -= ansc * cc[j];
}
for (int i = 0; i < ansc; ++i) printf("%s", c.c_str());
for (int i = 0; i < ansb; ++i) printf("%s", b.c_str());
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < ca[i]; ++j) printf("%c", (char)('a' + i));
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, a, b, c, cnt = 0;
cin >> n >> a >> b >> c;
long long ans = n / a;
long long y = n % a;
if (y >= b) ans += ((y - b) / (b - c)) + 1;
if (n >= b) {
cnt += (n - b) / (b - c) + 1;
n = n - (cnt) * (b - c);
}
cout << max(ans, cnt + n / a) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int x,y,m=0;
cin >> x >> y;
if(x<=3) m+=(4-x)*100000;
if(y<=3) m+=(4-y)*100000;
if(x==1&&y==1) m+=400000;
cout << m << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct state {
int u, c;
state(int u, int c) : u(u), c(c) {}
bool operator<(const state& e) const { return e.c < c; }
};
const int N = 1e5;
vector<state> g[N];
vector<int> in[N];
int n, m, cost[N];
bool vis[N];
int Dijkstra(int src, int tar) {
for (int i = 0; i < n; ++i) cost[i] = 2e9;
priority_queue<state> qu;
qu.push(state(src, 0));
cost[src] = 0;
while (!qu.empty()) {
state e = qu.top();
qu.pop();
if (e.u == tar) return e.c;
if (vis[e.u]) continue;
vis[e.u] = true;
int k = lower_bound(in[e.u].begin(), in[e.u].end(), e.c) - in[e.u].begin(),
add = 0;
for (size_t i = k; i < in[e.u].size(); ++i) {
if (e.c + add != in[e.u][i]) break;
++add;
}
for (size_t i = 0; i < g[e.u].size(); ++i) {
int u = g[e.u][i].u;
int c = e.c + add + g[e.u][i].c;
if (vis[u] || c >= cost[u]) continue;
cost[u] = c;
qu.push(state(u, c));
}
}
return -1;
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
--u;
--v;
g[u].push_back(state(v, c));
g[v].push_back(state(u, c));
}
for (int i = 0, x; i < n; ++i) {
scanf("%d", &x);
in[i].resize(x);
for (int j = 0; j < x; ++j) scanf("%d", &in[i][j]);
}
printf("%d\n", Dijkstra(0, n - 1));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int w, h, n;
struct seg {
int l, r;
bool operator<(const seg &p) const { return r - l < p.r - p.l; }
};
struct BIT {
int n, a[200001];
void init(int size) { n = size; }
void insert(int x) {
for (int i = x; i <= n; i += i & -i) a[i]++;
}
int query(seg s) {
int ret = 0;
for (int i = s.l; i; i -= i & -i) ret -= a[i];
for (int i = s.r - 1; i; i -= i & -i) ret += a[i];
return ret;
}
} hB, wB;
set<int> W, H;
priority_queue<seg> hQ, wQ;
int wmax() {
while (wB.query(wQ.top())) wQ.pop();
seg top = wQ.top();
return top.r - top.l;
}
int hmax() {
while (hB.query(hQ.top())) hQ.pop();
seg top = hQ.top();
return top.r - top.l;
}
int main() {
scanf("%d%d%d", &w, &h, &n);
W.insert(0);
W.insert(w);
H.insert(0);
H.insert(h);
wQ.push((seg){0, w});
hQ.push((seg){0, h});
wB.init(w);
hB.init(h);
for (int i = 1; i <= n; i++) {
char c[21];
int d;
scanf("%s %d", c, &d);
if (c[0] == 'H') {
d = h - d;
H.insert(d);
set<int>::iterator it = H.find(d);
it--;
int pre = *it;
it++;
it++;
int suf = *it;
hB.insert(d);
hQ.push((seg){pre, d});
hQ.push((seg){d, suf});
} else {
d = w - d;
W.insert(d);
set<int>::iterator it = W.find(d);
it--;
int pre = *it;
it++;
it++;
int suf = *it;
wB.insert(d);
wQ.push((seg){pre, d});
wQ.push((seg){d, suf});
}
printf("%I64d\n", (long long)wmax() * hmax());
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t;
scanf("%d %d\n", &n, &t);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
x = 86400 - x;
t -= x;
if (t <= 0) {
printf("%d", i);
return 0;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
enum faces {
Tetrahedron = 4,
Cube = 6,
Octahedron = 8,
Dodecahedron = 12,
Icosahedron = 20
};
int main() {
int n;
cin >> n;
long long total = 0;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
if (s == "Tetrahedron")
total += Tetrahedron;
else if (s == "Cube")
total += Cube;
else if (s == "Octahedron")
total += Octahedron;
else if (s == "Dodecahedron")
total += Dodecahedron;
else if (s == "Icosahedron")
total += Icosahedron;
}
cout << total;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct D {
vector<int> p, sz;
int cc;
D(int n) {
cc = n;
p.resize(n);
sz.resize(n, 1);
iota(p.begin(), p.end(), 0);
}
int find(int x) { return p[x] = p[x] == x ? x : find(p[x]); }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) {
return false;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
p[y] = x;
--cc;
return true;
}
};
void check(bool x) {
if (!x) {
cout << "No\n";
exit(0);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> edges(m);
for (auto& e : edges) {
cin >> e.first >> e.second;
--e.first, --e.second;
}
vector<int> sp(2), deg(2);
for (int i = 0; i < int(2); i++) cin >> sp[i], --sp[i];
for (int i = 0; i < int(2); i++) cin >> deg[i];
vector<vector<int>> adj(2);
bool between = 0;
for (int i = 0; i < int(2); i++) {
for (auto& e : edges) {
for (int rot = 0; rot < int(2); rot++) {
if (e.first == sp[i] && e.second != sp[i ^ 1]) {
adj[i].push_back(e.second);
}
if (e.first == sp[i] && e.second == sp[i ^ 1]) {
between = 1;
}
swap(e.first, e.second);
}
}
}
D d(n);
vector<pair<int, int>> ans;
for (auto [u, v] : edges) {
if (u != sp[0] && u != sp[1] && v != sp[0] && v != sp[1]) {
if (d.unite(u, v)) {
ans.emplace_back(u, v);
}
}
}
vector<set<int>> has(2);
vector<map<int, int>> who(2);
for (int i = 0; i < int(2); i++) {
for (int v : adj[i]) {
has[i].insert(d.find(v));
who[i][d.find(v)] = v;
}
}
set<int> both;
for (int x : has[0]) {
if (has[1].count(x)) {
both.insert(x);
}
}
for (int x : both) {
has[0].erase(x);
has[1].erase(x);
}
for (int i = 0; i < int(2); i++) {
for (int x : has[i]) {
check(deg[i]--);
assert(d.unite(sp[i], x));
ans.emplace_back(who[i][x], sp[i]);
}
}
bool connected = 0;
for (int x : both) {
bool done = 0;
for (int i = 0; i < int(2); i++) {
if (!done && deg[i] > 0) {
--deg[i];
assert(d.unite(sp[i], x));
ans.emplace_back(who[i][x], sp[i]);
done = 1;
} else if (!connected) {
check(deg[i]--);
assert(d.unite(sp[i], x));
ans.emplace_back(who[i][x], sp[i]);
connected = 1;
}
}
check(done);
}
if (d.cc > 1) {
check(between);
check(deg[0]--);
check(deg[1]--);
assert(d.unite(sp[0], sp[1]));
ans.emplace_back(sp[0], sp[1]);
}
assert(d.cc == 1);
cout << "Yes\n";
for (auto [u, v] : ans) {
cout << u + 1 << " " << v + 1 << "\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n, m; cin >> n >> m;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
mp[l]++;
mp[r + 1]--;
}
int ma = -1, bef = 0;
for (auto& p : mp) {
p.second += bef;
bef = p.second;
ma = max(ma, p.second);
}
int ans = 0;
if (ma % 2) {
int r = -1;
for (auto it = mp.begin(); it != mp.end(); ++it) {
if (ma == it->second) {
r = next(it)->first - 1;
}
}
ans = (ma - 1) * m + r;
} else {
int l = -1;
for (auto& p : mp) {
if (ma == p.second) {
l = p.first;
break;
}
}
ans = (ma - 1) * m + m - l;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool comparePairs(const std::pair<int, int>& lhs,
const std::pair<int, int>& rhs) {
if (lhs.second < rhs.second) return 1;
return 0;
}
void solve() {
int n, x;
int arr[100000];
std::vector<pair<int, int> > v;
int i;
cin >> n >> x;
for (i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
for (i = 0; i < n; i++) {
int a = (x / arr[i]) + ((x % arr[i] == 0) ? 0 : 1);
if (a <= n - i) {
v.push_back(make_pair(i, i + a - 1));
}
}
if (v.size() == 0) {
cout << 0 << endl;
return;
}
sort(v.begin(), v.end(), comparePairs);
int end = v[0].second, cnt = 1;
for (i = 1; i < v.size(); i++) {
if (v[i].first > end) {
cnt++;
end = v[i].second;
}
}
cout << cnt << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 200005;
const int BL = 300;
long long ans;
char s[N];
int n, p[N], cnt[N * BL + N], rem[N];
int main() {
scanf("%s", s + 1);
n = (int)strlen(s + 1);
for (int i = 1; i <= n; i++) p[i] = p[i - 1] + s[i] - '0';
int shift = n * BL;
for (int k = 1; k <= BL; k++) {
for (int i = 0; i <= n; i++) {
rem[i] = i - k * p[i] + shift;
ans += cnt[rem[i]];
cnt[rem[i]]++;
}
for (int i = 0; i <= n; i++) cnt[rem[i]] = 0;
}
rem[0] = 0;
for (int num = 1; num <= n / BL; num++) {
for (int i = 1; i <= n; i++) {
rem[i] = rem[i - 1] + 1;
if (rem[i] == num) rem[i] = 0;
}
int L = 1, R = 0;
int cur = BL * num;
for (int i = 1; i <= n && p[i - 1] + num <= p[n]; i++) {
while (L <= n && (L < cur + i || p[i - 1] + num > p[L])) cnt[rem[L++]]--;
while (R < n && p[R + 1] <= p[i - 1] + num) cnt[rem[++R]]++;
if (L > R) continue;
ans += cnt[rem[i - 1]];
}
for (int i = 1; i <= n; i++) cnt[rem[i]] = 0;
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int k;
cin >> k;
string str, s;
cin >> str;
s = str;
for (long long int i = 1; i < k; i++) str = str + s;
map<char, vector<long long int>> pos;
long long int i = 0;
for (auto c : str) {
pos[c].push_back(i);
i++;
}
long long int n;
cin >> n;
char c;
long long int p;
while (n--) {
cin >> p >> c;
str[pos[c][p - 1]] = 0;
pos[c].erase(pos[c].begin() + (p - 1));
}
for (auto c : str)
if (c) cout << c;
return 0;
}
| 3 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
long long ni(long long ta, vector<long long> & v) {
}
long long rui(long long n, long long k, long long p) {
if (k == 0) {
return 1;
}
else {
long long a = rui(n, k / 2, p);
if (k % 2 == 0) {
return (a*a) % p;
}
return (((a*a) % p)*n) % p;
}
}
int main(){
string s;
cin >> s;
if (s[0] == 'a') {
cout << "square1001" << endl;
}
else {
cout << "e869120" << endl;
}
return 0;
}
| 0 |
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int x[n];
for(int i=0;i<n;i++){
cin>>x[i];
}
for(int i=0;i<n-1;i++){
cout<<x[n-1-i]<<' ';
}
cout<<x[0]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int main() {
long long n;
scanf("%lld", &n);
if (n == 1)
printf("1\n");
else if (n == 2)
printf("2\n");
else if (n % 2)
printf("%lld\n", n * (n - 1) * (n - 2));
else if (n % 3 == 0)
printf("%lld\n", (n - 1) * (n - 2) * (n - 3));
else
printf("%lld\n", n * (n - 1) * (n - 3));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct phrase {
int author;
bool guilty;
phrase(int a, bool g) : author(a), guilty(g) {}
};
int main() {
int n, m;
cin >> n >> m;
std::vector<int> guilty(n, 0);
std::vector<int> innocent(n, 0);
vector<bool> can_kill(n, false);
vector<int> say(n, 0);
int t;
int gn = 0, in = 0;
for (int i = 0; i < n; ++i) {
cin >> t;
say[i] = t;
if (t > 0) {
++guilty[t - 1];
++gn;
} else {
++innocent[-t - 1];
++in;
}
}
int pos = 0;
for (int i = 0; i < n; ++i) {
if (guilty[i] + in - innocent[i] == m) {
can_kill[i] = true;
++pos;
}
}
for (int i = 0; i < n; ++i) {
if (say[i] > 0) {
if (!can_kill[say[i] - 1])
cout << "Lie" << endl;
else {
if (pos > 1)
cout << "Not defined" << endl;
else
cout << "Truth" << endl;
}
} else {
if (can_kill[-say[i] - 1] && (pos == 1))
cout << "Lie" << endl;
else {
if (!can_kill[-say[i] - 1])
cout << "Truth" << endl;
else
cout << "Not defined" << endl;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, temp;
const int max_n = 300;
int cnt[max_n];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int mini = -1, ind;
for (int j = 0; j < n; j++) {
cin >> temp;
if (mini == -1 || mini < temp) {
mini = temp;
ind = j;
}
}
cnt[ind] += 1;
}
int mini = -1, ind;
for (int i = 0; i < 200; i++) {
if (mini == -1 || mini < cnt[i]) {
ind = i;
mini = cnt[i];
}
}
cout << ind + 1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s;
int i, j, n, m, k, p, c[10005], b[100005];
vector<int> g[10005];
vector<string> f[10005];
void dfs(int i, int d) {
b[i] = g[i].size();
for (int j = 0; j < g[i].size(); j++) {
dfs(g[i][j], d + 1);
b[i] += b[g[i][j]];
c[i] += c[g[i][j]];
}
if (d > 1) {
k = max(b[i], k);
p = max(c[i], p);
}
}
int main() {
while (cin >> s) {
n = s.size();
j = 0;
for (i = 0; i < n; i = k + 1) {
for (k = i; k < n; k++)
if (s[k] == '\\') break;
if (k < n) {
string t = s.substr(i, k - i);
for (p = 0; p < f[j].size(); p++)
if (t == f[j][p]) break;
if (p == f[j].size()) {
g[j].push_back(++m);
f[j].push_back(t);
j = m;
} else
j = g[j][p];
} else
c[j]++;
}
}
k = p = 0;
dfs(0, 0);
cout << k << ' ' << p;
return 0;
}
| 3 |
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define MP(x,y) make_pair(x,y)
#define ARRAY 65536
#define X first
#define Y second
#define DBG 0
using namespace std;
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
std::fill( (T*)array, (T*)(array+N), val );
}
class Dice{
// 1 / rX 4 /
// ___/ -> _____/
// 2 | 3 2 | 1
// |
// v rY
//
// 0
// 1 2
public:
int value[6];
int id[6];
Dice(){
rep(i,6) id[i] = i;
rep(i,6) value[i] = i+1;
}
int& operator[](int x){return value[id[x]];}
void swp(int a, int b, int c, int d){
int tmp = id[a];
id[a] = id[b];
id[b] = id[c];
id[c] = id[d];
id[d] = tmp;
}
void rX(){swp(0, 3, 5, 2);}
void revX(){rX();rX();rX();}
void rY(){swp(0, 4, 5, 1);}
void revY(){rY();rY();rY();}
void print(){
rep(i,6)cout << value[id[i]] << ",";
cout << endl;
}
};
class Stump{
public:
int x, y, value;
Stump(int _x, int _y, int _value):x(_x),y(_y),value(_value){};
void print(){
cout << "( " << x<< " , " << y<< " ) " << value << endl;
}
};
class iStump{
public:
int id, val;
iStump(int _id, int _val):id(_id),val(_val){};
};
//int mpl[2200][2200] ={0};
// ??§?¨????id 31*15?????§
map<pair<int,int>, int> memo;
int main(void){
int n;
int ca = 0;
while(cin >> n, n){
vector<char> paths[ARRAY]; //[state]?????¨???????????????????????????
int dp[ARRAY] = {0}; //[state]?????¨???????????§???
int xy[1024][16] = {0}; //[??§?¨?id]???[????????????id] ??? ??????????????????
int x, y, v;
int ans = 0;
int used_xy = 0;
memo.clear();
string r;
vector<Stump> ts[32];
vector<iStump> s[32];
rep(i,n){
Dice dice;
cin >> x >> y;
x+=1050;
y+=1050;
cin >> dice[3] >> dice[2] >> dice[1] >> dice[4] >> dice[5] >> dice[0];
ts[i].push_back(Stump(x, y, dice[5]));
if(DBG) ts[i].back().print();
cin >> r;
rep(j,r.size()){
if( r[j] == 'L'){
dice.revX();x--;
}else if(r[j] == 'R'){
dice.rX();x++;
}else if(r[j] == 'F'){
dice.rY();y--;
}else{
dice.revY();y++;
}
ts[i].push_back(Stump(x, y, dice[5]));
if(DBG) ts[i].back().print();
}
map<pair<int,int>, int> used;
for(int k = ts[i].size()-1; k >= 0 ; --k){
pair<int,int> xyz=MP(ts[i][k].x,+ts[i][k].y);
if(used[xyz] != 0) ts[i][k].value = 0;
if(memo[xyz] == 0){
//cout << xyz.X << " " << xyz.Y << " " << used_xy << endl;
memo[xyz] = ++used_xy;
}
used[xyz] = 1;
}
int sum = 0;
//cout << i << endl;
rep(k,ts[i].size()){
if(ts[i][k].value > 0){
int cell_id = memo[MP(ts[i][k].x,ts[i][k].y)];
s[i].push_back( iStump(cell_id, ts[i][k].value));
xy[cell_id][i] = ts[i][k].value;
// cout << cell_id << " -> " << xy[cell_id][i] << endl;
sum += xy[cell_id][i];
}
}
// cout << "sum " << sum << endl;
}
// rep(i, used_xy){
// rep(j, n){
// cout << xy[i][j] << "\t";
// }cout << endl;
// }
//DP
rep(i, (1<<n)-1){
int state = i;
if(DBG) cout << "baselp " << dp[state] << endl;
rep(k,n){
int mask = 1<<k;
if((state | mask) != state){
int lp = 0;
if(DBG)cout << "stump(rev) " << k;
rep(l, s[k].size()){
iStump ss = s[k][l];
int current = 0;
for(int j = paths[state].size()-1; j >= 0; --j){
if(xy[ss.id][paths[state][j]] > 0){
current = xy[ss.id][paths[state][j]];
break;
}
}
if(current==0) lp += ss.val;
}
if(DBG)cout << " res = " << lp+dp[state] << endl;
if(dp[state|mask] < lp+dp[state]){
dp[state|mask] = lp+dp[state];
//paths[state|mask] = paths[state];
paths[state|mask].clear();
paths[state|mask].push_back(k);
rep(j,paths[state].size()) paths[state|mask].push_back(paths[state][j]);
}
}
}
}
cout << dp[(1<<n)-1] << endl;
// cout << "case" << endl;
// rep(i, paths[(1<<n)-1].size()){
// cout << paths[(1<<n)-1][i] << endl;
// }
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long x, y, k;
scanf("%lld %lld %lld", &x, &y, &k);
x = abs(x);
y = abs(y);
if (x > k || y > k) {
printf("-1\n");
return;
}
long long ret = k;
if (x % 2 != k % 2) ret--;
if (y % 2 != k % 2) ret--;
printf("%lld\n", ret);
}
int main() {
int qq;
scanf("%d", &qq);
while (qq--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int N = 5e5 + 5;
int n, m, k, x, y;
int a[N];
int main() {
scanf("%d%d%d", &n, &x, &y);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
int now = i;
int flag = 0;
for (int j = now - 1; j >= now - x && j >= 1; --j)
if (a[j] < a[now]) {
flag = 1;
break;
}
if (flag) continue;
for (int j = now + 1; j <= now + y && j <= n; ++j)
if (a[j] < a[now]) {
flag = 1;
break;
}
if (!flag) {
printf("%d\n", now);
break;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t = 1;
while (t--) {
long int n, ans = 0;
cin >> n;
string a, b;
cin >> a >> b;
for (int i = 0; i < n; i++) {
if (i + 1 < n && a[i] != b[i] && a[i] == b[i + 1] && a[i + 1] == b[i]) {
ans++;
a[i] = b[i];
a[i + 1] = b[i + 1];
i++;
}
if (i < n) {
if (a[i] != b[i]) {
ans++;
a[i] = b[i];
}
}
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char& s) {
string res = "'";
res += s;
res += "'";
return res;
}
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (i) res += ", ";
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void upd(pair<int, long long>& x, pair<int, long long> y, long long z) {
if (x.first == -1) {
x = {y.first + 1, y.second + z};
} else {
x = min(x, {y.first + 1, y.second + z});
}
}
pair<int, long long> pr[1000 + 1][7000];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
long long k;
cin >> n >> k;
if (k == 1) {
cout << 1 << '\n';
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
cout << min_element((a).begin(), (a).end()) - a.begin() + 1;
return 0;
}
vector<long long> d;
for (long long i = 1; i * i <= k; i++) {
if (k % i == 0) {
d.push_back(i);
if (k / i != i) d.push_back(k / i);
}
}
sort((d).begin(), (d).end());
vector<long long> z;
for (int i = 0; i < (int)(d).size(); i++) {
int f = 1;
for (int j = 0; j < i; j++) {
if (d[i] % d[j] == 0 && d[j] != 1) f = 0;
}
if (f) z.push_back(d[i]);
}
unordered_map<long long, int> uk;
for (int i = 0; i < (int)(d).size(); i++) uk[d[i]] = i;
int m = (int)(d).size();
vector<pair<pair<int, long long>, int>> tmp(m, {{-1, -1}, -1});
vector<vector<int>> go(m, vector<int>((int)(z).size()));
for (int i = 0; i < m; i++) {
for (int j = 0; j < (int)(z).size(); j++) {
if (d[i] % z[j] != 0) {
go[i][j] = i;
} else
go[i][j] = uk[d[i] / z[j]];
}
}
tmp[m - 1] = {{0, 0}, 0};
vector<pair<int, long long>> dp(m);
vector<long long> cnt(m);
for (int i = 1; i <= n; i++) {
long long value;
cin >> value;
fill((dp).begin(), (dp).end(), make_pair(-1, -1));
iota((cnt).begin(), (cnt).end(), 0);
for (int v = 0; v < (int)(z).size(); v++) {
int p = z[v];
if (p == 1) continue;
int c = 0;
long long tmp = value;
while (tmp % p == 0) c++, tmp /= p;
for (int i = 0; i < m; i++) {
for (int V = 0; V < c; V++) {
if (go[cnt[i]][v] == cnt[i]) break;
cnt[i] = go[cnt[i]][v];
}
}
}
for (int j = 0; j < m; j++) {
if (tmp[j].second == -1) continue;
int x = cnt[j];
pair<int, long long> res = dp[x];
upd(dp[x], tmp[j].first, value);
if (dp[x] != res) {
pr[i][x] = {tmp[j].second, j};
}
}
for (int j = 0; j < m; j++) {
if (dp[j].first == -1) continue;
if (tmp[j].second == -1 || dp[j] < tmp[j].first) {
tmp[j] = {dp[j], i};
}
}
}
if (tmp[0].second == -1) {
cout << -1;
return 0;
}
vector<int> ans;
int id = tmp[0].second, j = 0;
while (id > 0) {
ans.push_back(id);
tie(id, j) = pr[id][j];
}
cout << (int)(ans).size() << '\n';
for (auto x : ans) cout << x << " ";
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
T gcd(T a, T b) {
return ((b == 0) ? a : gcd(b, a % b));
}
void solve() {
int r, g, b;
cin >> r >> g >> b;
vector<long long> R(r), G(g), B(b);
for (int i = 0; i < (int)(r); i++) cin >> R[i];
for (int i = 0; i < (int)(g); i++) cin >> G[i];
for (int i = 0; i < (int)(b); i++) cin >> B[i];
sort(R.begin(), R.end());
sort(G.begin(), G.end());
sort(B.begin(), B.end());
long long ans = 2e18;
for (int i = 0; i < (int)(r); i++) {
vector<long long> G1, B1;
int cl = lower_bound(G.begin(), G.end(), R[i]) - G.begin();
if (cl < (int)G.size()) G1.push_back(G[cl]);
if (cl > 0) G1.push_back(G[cl - 1]);
int cl1 = lower_bound(B.begin(), B.end(), R[i]) - B.begin();
if (cl1 < (int)B.size()) B1.push_back(B[cl1]);
if (cl1 > 0) B1.push_back(B[cl1 - 1]);
for (int j = 0; j < (int)(G1.size()); j++) {
int cl2 = lower_bound(B.begin(), B.end(), G1[j]) - B.begin();
if (cl2 < (int)B.size()) B1.push_back(B[cl2]);
if (cl2 > 0) B1.push_back(B[cl2 - 1]);
}
for (int j = 0; j < (int)(B1.size()); j++) {
int cl2 = lower_bound(G.begin(), G.end(), B1[j]) - G.begin();
if (cl2 < (int)G.size()) G1.push_back(G[cl2]);
if (cl2 > 0) G1.push_back(G[cl2 - 1]);
}
for (int j = 0; j < (int)(G1.size()); j++)
for (int k = 0; k < (int)(B1.size()); k++) {
ans = min(ans, ((R[i] - G1[j]) * (R[i] - G1[j])) +
((R[i] - B1[k]) * (R[i] - B1[k])) +
((G1[j] - B1[k]) * (G1[j] - B1[k])));
}
}
for (int i = 0; i < (int)(g); i++) {
vector<long long> R1, B1;
int cl = lower_bound(R.begin(), R.end(), G[i]) - R.begin();
if (cl < (int)R.size()) R1.push_back(R[cl]);
if (cl > 0) R1.push_back(R[cl - 1]);
int cl1 = lower_bound(B.begin(), B.end(), G[i]) - B.begin();
if (cl1 < (int)B.size()) B1.push_back(B[cl1]);
if (cl1 > 0) B1.push_back(B[cl1 - 1]);
for (int j = 0; j < (int)(R1.size()); j++) {
int cl2 = lower_bound(B.begin(), B.end(), R1[j]) - B.begin();
if (cl2 < (int)B.size()) B1.push_back(B[cl2]);
if (cl2 > 0) B1.push_back(B[cl2 - 1]);
}
for (int j = 0; j < (int)(B1.size()); j++) {
int cl2 = lower_bound(R.begin(), R.end(), B1[j]) - R.begin();
if (cl2 < (int)R.size()) R1.push_back(R[cl2]);
if (cl2 > 0) R1.push_back(R[cl2 - 1]);
}
for (int j = 0; j < (int)(R1.size()); j++)
for (int k = 0; k < (int)(B1.size()); k++) {
ans = min(ans, ((G[i] - R1[j]) * (G[i] - R1[j])) +
((G[i] - B1[k]) * (G[i] - B1[k])) +
((R1[j] - B1[k]) * (R1[j] - B1[k])));
}
}
for (int i = 0; i < (int)(b); i++) {
vector<long long> R1, G1;
int cl = lower_bound(G.begin(), G.end(), B[i]) - G.begin();
if (cl < (int)G.size()) G1.push_back(G[cl]);
if (cl > 0) G1.push_back(G[cl - 1]);
int cl1 = lower_bound(R.begin(), R.end(), B[i]) - R.begin();
if (cl1 < (int)R.size()) R1.push_back(R[cl1]);
if (cl1 > 0) R1.push_back(R[cl1 - 1]);
for (int j = 0; j < (int)(G1.size()); j++) {
int cl2 = lower_bound(R.begin(), R.end(), G1[j]) - R.begin();
if (cl2 < (int)R.size()) R1.push_back(R[cl2]);
if (cl2 > 0) R1.push_back(R[cl2 - 1]);
}
for (int j = 0; j < (int)(R1.size()); j++) {
int cl2 = lower_bound(G.begin(), G.end(), R1[j]) - G.begin();
if (cl2 < (int)G.size()) G1.push_back(G[cl2]);
if (cl2 > 0) G1.push_back(G[cl2 - 1]);
}
for (int j = 0; j < (int)(G1.size()); j++)
for (int k = 0; k < (int)(R1.size()); k++) {
ans = min(ans, ((B[i] - G1[j]) * (B[i] - G1[j])) +
((B[i] - R1[k]) * (B[i] - R1[k])) +
((G1[j] - R1[k]) * (G1[j] - R1[k])));
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[111];
int vv(int k) {
int i, j;
j = 0;
for (i = 1; i < k; i++)
if (a[i] == 0) j++;
for (i = k; i <= n; i++)
if (a[i] == 1) j++;
return j;
}
int main() {
cin >> n;
int i, j, k;
for (i = 1; i <= n; i++) cin >> a[i];
a[n + 1] = 1;
int d = 1;
for (i = 0; i <= n + 1; i++) {
d = max(vv(i), d);
}
cout << d;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, b;
cin >> a >> b;
int N;
cin >> N;
double best_time = 1e40;
while (N--) {
double x, y, v;
cin >> x >> y >> v;
double dis = sqrt((x - a) * (x - a) + (y - b) * (y - b));
double time = dis / v;
best_time = min(best_time, time);
}
printf("%.20f", best_time);
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
const int N = 2005, P = 924844033;
int f[N][N][2], n, S, k, g[N], M[N], fac[N];
int main () {
scanf("%d%d", &n, &k);
S = (n + k - 1) / k;
f[0][0][1] = 1;
fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % P;
for (int i = 1; i <= S; ++i) {
for (int j = 0; j <= n; ++j) {
f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % P;
if (j) f[i][j][1] = f[i - 1][j - 1][0];
}
}
M[0] = 1;
for (int i = min(k << 1, n), j, v, w, l; i; --i) {
l = (n - i + k) / k + (i > k);
memset(g, 0, sizeof g);
for (v = l >> 1; ~v; --v) {
w = f[l][v][0] + f[l][v][1];
for (j = n; j >= v; --j) {
g[j] = (1ll * M[j - v] * w + g[j]) % P;
}
}
memcpy(M, g, sizeof g);
}
int ret = 0;
for (int i = 0; i <= n; ++i) {
if (i & 1) ret = (-1ll * fac[n - i] * M[i] + ret) % P;
else ret = (1ll * fac[n - i] * M[i] + ret) % P;
}
printf("%d\n", (ret + P) % P);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long adj[4002][4002], ar[4003];
void preprocess(void) { return; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(20);
preprocess();
int n, m, a, b;
cin >> n >> m;
vector<pair<int, int> > arr;
for (int i = 0; i < 4002; i++) {
for (int j = 0; j < 4002; j++) {
adj[i][j] = 0;
}
ar[i] = 0;
}
for (int i = 0; i < m; i++) {
cin >> a >> b;
adj[a][b] = 1;
adj[b][a] = 1;
arr.push_back(make_pair(a, b));
ar[a]++;
ar[b]++;
}
long long ans = 1000000007;
for (int i = 0; i < m; i++) {
for (int j = 1; j < n + 1; j++) {
if (adj[arr[i].first][j] == 1 && adj[arr[i].second][j] == 1) {
ans = min(ans, ar[arr[i].first] + ar[arr[i].second] + ar[j] - 6);
}
}
}
if (ans != 1000000007)
cout << ans << endl;
else
cout << "-1" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n = 0, d = 0, m = 0, x = 0, y = 0;
cin >> n >> d >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
if (x - y <= d && y - x <= d && x + y <= n + n - d && x + y >= d) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int arr[26] = {};
int n = s.length();
int count = 0;
for (int i = 0; i < n; i++) {
arr[s[i] - 'a']++;
if (arr[s[i] - 'a'] == 2) {
arr[s[i] - 'a'] = 0;
count--;
} else
count++;
}
if (count == 0 || count == 1) {
cout << "First"
<< "\n";
} else {
if (count % 2 == 0) {
cout << "Second"
<< "\n";
} else
cout << "First"
<< "\n";
}
}
int main() {
long long t = 1;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, -1, 0, 1};
const int drr[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dcc[8] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
for (int i = 0; i < ((int)((t).size())); i++) s << t[i] << " ";
return s;
}
class UnionFind {
private:
vector<int> rank, p;
public:
UnionFind(int n) {
for (int i = 0; i < n; i++) p.push_back(i);
rank.assign(n, 0);
}
int findSet(int s) { return (p[s] == s) ? s : (p[s] = findSet(p[s])); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (isSameSet(i, j)) return;
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y])
p[y] = x;
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
};
bool fcomp(double A, double B) {
double EPSILON = numeric_limits<double>::epsilon();
double diff = A - B;
return (diff < EPSILON) && (-diff < EPSILON);
}
double x_y(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
string s;
cin >> s;
int n = s.size();
vector<pair<int, int> > segs;
for (int i = 0; i < n; i++) {
int j = i, ct = 0;
while (j < n && s[j] == s[i]) {
ct++;
j++;
}
i = j - 1;
segs.push_back(make_pair(ct, s[i] - 'A'));
}
int sz = segs.size();
if (sz % 2 == 0) {
printf("0");
return 0;
}
int id = (sz - 1) / 2;
int na = 0, diff = 1;
if (segs[id].first + 1 < 3) na = 1;
while (id - diff >= 0) {
int i1 = id - diff, i2 = id + diff;
if (segs[i1].second != segs[i2].second) na = 1;
if (segs[i1].first + segs[i2].first < 3) na = 1;
diff++;
}
if (na)
printf("0");
else
printf("%d", segs[id].first + 1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void func(void) {
freopen("input.c", "r", stdin);
freopen("output.c", "w", stdout);
}
void print(vector<long long> &v) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
printf("%lld ", v[i]);
}
printf("\n");
}
void print(vector<pair<long long, long long> > &v) {
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
printf("%lld %lld\n", v[i].first, v[i].second);
}
}
void print(double d) { cout << fixed << setprecision(10) << d << endl; }
void print(string s, double d) {
cout << s << " ";
cout << fixed << setprecision(10) << d << endl;
}
const int N = 1e5 + 100;
struct info {
long long day, cost, place;
bool operator<(info k1) const { return day < k1.day; }
};
vector<info> in;
vector<info> out;
set<pair<long long, long long> > outFlight[N];
long long inFlight[N];
int main() {
long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m,
fg = 0, mx = 0, mx1 = 0, mn = 8e18,
mn1 = 8000000000000000000;
scanf("%lld %lld %lld", &n, &m, &k);
for (int i = 0; i < m; i++) {
scanf("%lld %lld", &x, &y);
scanf("%lld %lld", &z, &t);
info xx;
xx.day = x;
xx.place = y;
if (y == 0) xx.place = z;
xx.cost = t;
if (y)
in.push_back(xx);
else
out.push_back(xx);
}
sort(in.begin(), in.end());
sort(out.begin(), out.end());
for (int i = 0; i < N; i++) inFlight[i] = 1e10;
int ok = 0;
for (int i = 0; i < out.size(); i++) {
outFlight[out[i].place].insert({out[i].cost, i});
}
long long outSum = 0;
long long inSum = 0;
for (int i = 1; i <= n; i++) {
if (outFlight[i].size() == 0) {
printf("-1\n");
return 0;
}
auto it = outFlight[i].begin();
pair<long long, long long> p = *it;
outSum += p.first;
}
cnt = 0;
for (int i = 0; i < in.size(); i++) {
long long now = in[i].day;
long long place = in[i].place;
long long cost = in[i].cost;
if (inFlight[place] == 1e10) {
inFlight[place] = cost;
inSum += cost;
cnt++;
} else {
inSum -= inFlight[place];
inFlight[place] = min(inFlight[place], cost);
inSum += inFlight[place];
}
now += k + 1;
fg = 0;
for (; ok < out.size(); ok++) {
if (out[ok].day >= now) break;
auto it = outFlight[out[ok].place].begin();
pair<long long, long long> p = *it;
outSum -= p.first;
p = {out[ok].cost, ok};
outFlight[out[ok].place].erase(p);
if (outFlight[out[ok].place].size() == 0) {
fg = 1;
break;
}
it = outFlight[out[ok].place].begin();
p = *it;
outSum += p.first;
}
if (fg) break;
if (cnt == n) {
mn = min(mn, inSum + outSum);
}
}
if (mn == 8e18) mn = -1;
cout << mn << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, neg = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') neg = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
return x * neg;
}
inline int qpow(int x, int e, int _MOD) {
int ans = 1;
while (e) {
if (e & 1) ans = ans * x % _MOD;
x = x * x % _MOD;
e >>= 1;
}
return ans;
}
int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0;
vector<int> g[200005], cyc[200005];
int k = 0;
inline void dfs(int x) {
while (!g[x].empty()) {
int y = g[x].back();
g[x].pop_back();
dfs(a[y]);
cyc[k].push_back(y);
}
}
signed main() {
for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i];
sort(c + 1, c + n + 1);
for (int i = 1; i <= n; i++)
if (c[i] != c[i - 1]) d[++num] = c[i];
for (int i = 1; i <= n; i++)
a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i];
sort(b + 1, b + n + 1);
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (a[i] != b[i]) {
cnt++;
g[b[i]].push_back(i);
}
}
if (!cnt) return puts("0"), 0;
if (cnt > s) return puts("-1"), 0;
for (int i = 1; i <= num; i++) {
if (!g[i].empty()) {
k++;
dfs(i);
}
}
for (int i = 1; i <= k; i++) reverse(cyc[i].begin(), cyc[i].end());
if (k == 1) {
cout << 1 << endl << cyc[1].size() << endl;
for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++)
cout << *it << " ";
return 0;
}
if (cnt <= s - k) {
cout << 2 << endl;
cout << cnt << endl;
for (int i = 1; i <= k; i++) {
for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end();
it++)
cout << *it << " ";
}
puts("");
cout << k << endl;
for (int i = k; i >= 1; i--) cout << cyc[i][0] << " ";
puts("");
return 0;
} else {
int t = cnt - (s - k);
if (t == k)
cout << t << endl;
else if (t == k - 1)
cout << t + 1 << endl;
else
cout << t + 2 << endl;
for (int i = 1; i <= t; i++) {
cout << cyc[i].size() << endl;
for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end();
it++)
cout << *it << " ";
puts("");
}
int sum = 0;
if (t != k) {
for (int i = t + 1; i <= k; i++) sum += cyc[i].size();
cout << sum << endl;
for (int i = t + 1; i <= k; i++)
for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end();
it++)
cout << *it << " ";
if (t != k - 1) {
puts("");
cout << k - t << endl;
for (int i = k; i >= t + 1; i--) cout << cyc[i][0] << " ";
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void infile() { freopen("input.in", "r", stdin); }
void outfile() { freopen("output.in", "w", stdout); }
void fastt() { fast(); }
int n;
double arr[1000000], k;
bool valid(double x) {
double sum1 = 0, sum2 = 0;
for (int i = 0; i < n; i++) {
if (arr[i] > x)
sum1 += (arr[i] - x) - (arr[i] - x) * k;
else if (arr[i] < x)
sum2 += x - arr[i];
}
return sum1 - sum2 >= 0;
}
double fun() {
double l = 0, r = arr[n - 1];
double energy = 0;
for (int i = 0; i < 100; i++) {
double mid = (l + r) / 2;
if (valid(mid)) {
energy = mid;
l = mid;
} else
r = mid;
}
return energy;
}
int main() {
fastt();
cin >> n >> k;
k /= 100;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
cout << fixed << setprecision(6) << fun() << "\n";
}
| 2 |
#include <bits/stdc++.h>
template <typename T>
inline void read(T& x) {
int f = 0, c = getchar();
x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
if (f) x = -x;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <typename T>
inline void writeln(T x) {
write(x);
puts("");
}
template <typename T>
inline bool chkmin(T& x, const T& y) {
return y < x ? (x = y, true) : false;
}
template <typename T>
inline bool chkmax(T& x, const T& y) {
return x < y ? (x = y, true) : false;
}
const long long mod = 998244353, G = 3, Gi = 332748118;
const int maxn = 1e5 + 7;
inline long long qpow(long long x, long long k) {
long long s = 1;
for (; k; x = x * x % mod, k >>= 1)
if (k & 1) s = s * x % mod;
return s;
}
inline void ntt(long long* A, int* r, int lim, int tp) {
for (int i = 0; i < lim; ++i)
if (i < r[i]) std::swap(A[i], A[r[i]]);
for (int mid = 1; mid < lim; mid <<= 1) {
long long wn = qpow(tp == 1 ? G : Gi, (mod - 1) / (mid << 1));
for (int j = 0; j < lim; j += mid << 1) {
long long w = 1;
for (int k = 0; k < mid; ++k, w = w * wn % mod) {
long long x = A[j + k], y = w * A[j + k + mid] % mod;
A[j + k] = (x + y) % mod;
A[j + k + mid] = (x - y + mod) % mod;
}
}
}
if (tp == -1) {
long long inv = qpow(lim, mod - 2);
for (int i = 0; i < lim; ++i) A[i] = A[i] * inv % mod;
}
}
int n, r[maxn << 2], lim, l;
long long m, p[maxn], fac[maxn], ifac[maxn];
long long a[maxn << 2], b[maxn << 2], g[maxn];
int main() {
read(n, m);
for (int i = 0; i <= n; ++i) read(p[i]);
fac[0] = ifac[0] = 1;
for (int i = 1; i <= n + 1; ++i) fac[i] = fac[i - 1] * i % mod;
ifac[n + 1] = qpow(fac[n + 1], mod - 2);
for (int i = n; i; --i) ifac[i] = ifac[i + 1] * (i + 1) % mod;
for (int i = 0; i <= n; ++i) {
a[i] = ifac[i];
b[i] = fac[n - i] * p[n - i] % mod;
}
for (lim = 1; lim <= (n << 1); ++l) lim <<= 1;
for (int i = 0; i < lim; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
ntt(a, r, lim, 1);
ntt(b, r, lim, 1);
for (int i = 0; i < lim; ++i) a[i] = a[i] * b[i] % mod;
ntt(a, r, lim, -1);
for (int i = 0; i <= n; ++i)
g[i] = ifac[i] * a[n - i] % mod * qpow(qpow(i + 1, m), mod - 2) % mod;
for (int i = 0; i <= n; ++i) {
a[i] = ((i & 1) ? mod - 1 : 1ll) * ifac[i] % mod;
b[i] = fac[n - i] * g[n - i] % mod;
}
for (int i = n + 1; i < lim; ++i) a[i] = b[i] = 0;
ntt(a, r, lim, 1);
ntt(b, r, lim, 1);
for (int i = 0; i < lim; ++i) a[i] = a[i] * b[i] % mod;
ntt(a, r, lim, -1);
for (int i = 0; i <= n; ++i) write(ifac[i] * a[n - i] % mod), putchar(' ');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n < 4)
cout << "NO";
else if (n == 4) {
cout << "YES" << endl
<< "1 * 2 = 2" << endl
<< "2 * 3 = 6" << endl
<< "6 * 4 = 24";
} else if (n == 5) {
cout << "YES" << endl
<< "5 - 2 = 3" << endl
<< "3 + 1 = 4" << endl
<< "4 + 4 = 8" << endl
<< "8 * 3 = 24" << endl;
} else {
cout << "YES" << endl;
cout << "1 + " << n - 1 << " = " << n << endl;
cout << n << " - " << n << " = 0" << endl;
int i;
for (i = 5; i < (n - 1); i++) {
cout << "0 * " << i << " = 0" << endl;
}
cout << "2 * 3 = 6" << endl
<< "6 * 4 = 24" << endl
<< "24 + 0 = 24" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename tn>
void read(tn &a) {
tn x = 0, f = 1;
char c = ' ';
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
a = x * f;
}
int n, m, k, p;
long long a[101000], h[101000];
struct node {
long long H, a, h;
};
bool operator>(node a, node b) { return 1.l * a.H / a.a > 1.l * b.H / b.a; }
bool check(long long lim) {
priority_queue<node, vector<node>, greater<node> > q;
for (int i = 1; i <= n; i++) q.push({lim, a[i], h[i]});
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= k; j++) {
while (!q.empty()) {
auto u = q.top();
q.pop();
if (1.l * (u.H + 1) / u.a <= i) return 0;
if (1.l * (u.H - u.h) / u.a >= m) continue;
u.H += p;
q.push(u);
break;
}
if (q.empty()) return 1;
}
}
while (!q.empty()) {
auto u = q.top();
q.pop();
if (1.l * (u.H - u.h) / u.a < m) return 0;
}
return 1;
}
int main() {
read(n);
read(m);
read(k);
read(p);
for (int i = 1; i <= n; i++) {
read(h[i]);
read(a[i]);
}
long long l = -1, r = 1e15;
while (l + 1 < r) {
long long mid = l + r >> 1;
if (check(mid))
r = mid;
else
l = mid;
}
cout << r << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, a;
string str;
while (cin >> str >> m) {
map<int, int> mp;
while (m--) {
cin >> a;
if (!mp[a])
mp[a] = 1;
else
mp[a]++;
}
int sum = 0;
int len = str.length();
for (int i = 0; i < len / 2; i++) {
sum += mp[i + 1];
if (sum & 1) swap(str[i], str[len - 1 - i]);
}
cout << str << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,a,b) for (int i = (a); i < (b); i++)
typedef long long ll;
int main(void)
{
int n;
cin >> n;
rep(i, pow(2, n))
{
bitset<18> bit(i);
cout << i << ":";
rep(j, 18)
{
if (bit.test(j)) cout << " " << j;
}
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
fast_ios() {
cin.tie(nullptr), ios::sync_with_stdio(false),
cout << fixed << setprecision(20);
};
} fast_ios_;
template <typename T, typename V>
void ndarray(vector<T> &vec, const V &val, int len) {
vec.assign(len, val);
}
template <typename T, typename V, typename... Args>
void ndarray(vector<T> &vec, const V &val, int len, Args... args) {
vec.resize(len),
for_each(begin(vec), end(vec), [&](T &v) { ndarray(v, val, args...); });
}
template <typename T>
bool chmax(T &m, const T q) {
if (m < q) {
m = q;
return true;
} else
return false;
}
template <typename T>
bool chmin(T &m, const T q) {
if (m > q) {
m = q;
return true;
} else
return false;
}
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
return make_pair(l.first - r.first, l.second - r.second);
}
template <typename T>
vector<T> sort_unique(vector<T> vec) {
sort(vec.begin(), vec.end()),
vec.erase(unique(vec.begin(), vec.end()), vec.end());
return vec;
}
template <typename T>
int arglb(const std::vector<T> &v, const T &x) {
return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x));
}
template <typename T>
int argub(const std::vector<T> &v, const T &x) {
return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x));
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (auto &v : vec) is >> v;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
os << '[';
for (auto v : vec) os << v << ',';
os << ']';
return os;
}
template <typename T, size_t sz>
ostream &operator<<(ostream &os, const array<T, sz> &arr) {
os << '[';
for (auto v : arr) os << v << ',';
os << ']';
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &vec) {
os << "deq[";
for (auto v : vec) os << v << ',';
os << ']';
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &vec) {
os << '{';
for (auto v : vec) os << v << ',';
os << '}';
return os;
}
template <typename T, typename TH>
ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) {
os << '{';
for (auto v : vec) os << v << ',';
os << '}';
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &vec) {
os << '{';
for (auto v : vec) os << v << ',';
os << '}';
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) {
os << '{';
for (auto v : vec) os << v << ',';
os << '}';
return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << '(' << pa.first << ',' << pa.second << ')';
return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp) {
os << '{';
for (auto v : mp) os << v.first << "=>" << v.second << ',';
os << '}';
return os;
}
template <typename TK, typename TV, typename TH>
ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) {
os << '{';
for (auto v : mp) os << v.first << "=>" << v.second << ',';
os << '}';
return os;
}
struct Cubic {
using Int = __int128;
Int a;
Int lb, ub;
Cubic(Int a, Int ub) : a(a), lb(0), ub(ub) {}
Int slope(Int s) const {
Int lo = lb, hi = ub + 1;
while (hi - lo > 1) {
Int x = (lo + hi) / 2;
(next_cost(x) <= s ? lo : hi) = x;
}
return lo;
}
Int eval(Int x) const noexcept { return (x * x - a) * x; }
Int next_cost(Int x) const noexcept { return 3 * x * x - 3 * x + 1 - a; }
};
template <typename F, typename Int, Int INF>
struct QuadraticSumMinimizationUnderLinearConstraint {
std::pair<Int, std::vector<std::vector<std::pair<Int, Int>>>> operator()(
const std::vector<Int> &k, const std::vector<F> &f, Int C) {
assert(k.size() == f.size());
assert(k.size() > 0);
Int lbsum = 0, ubsum = 0;
for (auto func : f) lbsum += func.lb, ubsum += func.ub;
if (lbsum > C or ubsum < C) return {};
const int N = k.size();
Int few = -INF, enough = INF;
Int fewc = 0;
while (enough - few > 1) {
auto slope = few + (enough - few) / 2;
Int cnt = 0;
for (int i = 0; i < N; i++) {
auto tmp = f[i].slope(slope);
cnt += tmp * k[i];
if (cnt >= C) break;
}
if (cnt >= C) {
enough = slope;
} else {
few = slope;
fewc = cnt;
}
}
std::vector<std::vector<std::pair<Int, Int>>> ret(N);
std::vector<int> additional;
Int ctmp = 0;
Int sol = 0;
for (int i = 0; i < N; i++) {
auto xlo = f[i].slope(few);
auto xhi = f[i].slope(few + 1);
ctmp += k[i] * xlo;
ret[i].emplace_back(xlo, k[i]);
if (xlo < xhi) additional.push_back(i);
sol += k[i] * f[i].eval(xlo);
}
sol += (C - ctmp) * (few + 1);
while (additional.size()) {
int i = additional.back();
additional.pop_back();
Int add = C - ctmp > k[i] ? k[i] : C - ctmp;
auto x = ret[i][0].first;
if (add) {
ret[i][0].second -= add;
if (ret[i][0].second == 0) ret[i].pop_back();
ret[i].emplace_back(x + 1, add);
ctmp += add;
}
}
return {sol, ret};
}
};
__int128 eval(lint a, __int128 x) { return x * (a - x * x); }
__int128 differential(lint a, __int128 x) {
if (x <= 0) return 1e12;
return eval(a, x) - eval(a, x - 1);
}
lint argmaxf(lint a) {
lint bcand = sqrt(a / 3 + 1);
__int128 ret = max<lint>(0LL, min<lint>(bcand, a + 1));
for (int b = (max<lint>(bcand - 10, 0)),
b_end_ = (min<lint>(a + 1, bcand + 10));
b < b_end_; b++) {
if (eval(a, b) >= eval(a, ret)) ret = b;
}
return ret;
}
int main() {
int N;
lint K;
cin >> N >> K;
vector<lint> A(N);
cin >> A;
vector<Cubic> functions;
for (int i = (0), i_end_ = (N); i < i_end_; i++)
functions.emplace_back(A[i], A[i]);
QuadraticSumMinimizationUnderLinearConstraint<Cubic, lint, (1LL << 62) - 1>
solver;
auto ret = solver(vector<lint>(N, 1), functions, K);
if (ret.second.empty()) return 0;
for (auto p : ret.second) cout << lint(p[0].first) << ' ';
cout << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline bool smax(T &x, const U &y) {
return y > x ? (x = y, 1) : 0;
}
template <typename T, typename U>
inline bool smin(T &x, const U &y) {
return y < x ? (x = y, 1) : 0;
}
const int SIZE = 2e7;
char ch[SIZE + 7];
struct fastin {
char *now;
fastin() : now(ch - 1) { fread(ch, 1, SIZE, stdin); }
template <typename T>
inline fastin &operator>>(T &x) {
register bool p = 0;
while (!isdigit(*++now))
if (*now == '-') p = 1;
x = *now - '0';
while (isdigit(*++now)) x = x * 10 + *now - '0';
if (p) x = -x;
return *this;
}
inline int read() {
register int x;
*this >> x;
return x;
}
} din;
const int N = 1e5 + 7, inf = 0x3f3f3f3f;
int n, M;
struct node {
int v, w;
};
vector<node> g[N];
inline int fpow(int x, int k) {
int ans = 1;
while (k) {
if (k & 1) ans = 1ll * ans * x % M;
x = 1ll * x * x % M;
k >>= 1;
}
return ans;
}
int inv[N], cf[N], phi;
void init() {
int t = phi = M;
for (int i = 2, r = sqrt(M); i <= r; ++i) {
if (t % i == 0) {
do t /= i;
while (t % i == 0);
phi -= phi / i;
}
}
if (t > sqrt(M) && M % t == 0) phi -= phi / t;
int base = fpow(10, phi - 1);
cf[0] = inv[0] = 1;
for (int i = (1); i <= (n); ++i)
inv[i] = 1ll * inv[i - 1] * base % M, cf[i] = 10ll * cf[i - 1] % M;
}
int vis[N], sz, sum, siz[N], rt;
void dfs1(int x) {
siz[x] = vis[x] = 1;
int mx = 0;
for (auto e : g[x])
if (!vis[e.v]) dfs1(e.v), siz[x] += siz[e.v], smax(mx, siz[e.v]);
smax(mx, sum - siz[x]);
if (smin(sz, mx)) rt = x;
vis[x] = 0;
}
int up[N], down[N], dep[N], cnt, in[N], out[N], rid[N];
long long ans;
void dfs2(int x) {
vis[x] = 1;
in[x] = ++cnt;
rid[cnt] = x;
for (auto e : g[x])
if (!vis[e.v]) {
down[e.v] = (down[x] * 10ll + e.w) % M, dep[e.v] = dep[x] + 1;
up[e.v] = 1ll * cf[dep[x]] * e.w % M + up[x];
if (up[e.v] >= M) up[e.v] -= M;
dfs2(e.v);
}
vis[x] = 0;
out[x] = cnt;
}
map<int, int> p;
void solve(int x) {
sz = inf;
dfs1(x);
x = rt;
dep[x] = cnt = up[x] = down[x] = 0;
dfs2(x);
vis[x] = 1;
p.clear();
p[0] = 1;
for (auto e : g[x])
if (!vis[e.v]) {
for (int i = (in[e.v]); i <= (out[e.v]); ++i) {
int &y = rid[i];
int m = 1ll * (M - down[y]) * inv[dep[y]] % M;
auto iter = p.find(m);
if (iter != p.end()) ans += iter->second;
}
for (int i = (in[e.v]); i <= (out[e.v]); ++i) ++p[up[rid[i]]];
}
p.clear();
for (auto e = g[x].rbegin(); e != g[x].rend(); ++e)
if (!vis[e->v]) {
for (int i = (in[e->v]); i <= (out[e->v]); ++i) {
int &y = rid[i];
int m = 1ll * (M - down[y]) * inv[dep[y]] % M;
auto iter = p.find(m);
if (iter != p.end()) ans += iter->second;
}
for (int i = (in[e->v]); i <= (out[e->v]); ++i) ++p[up[rid[i]]];
}
ans += p[0];
for (auto e : g[x])
if (!vis[e.v]) sum = siz[e.v], solve(e.v);
}
int main() {
sum = n = din.read(), M = din.read();
if (M == 1) {
cout << 1ll * n * (n - 1) << endl;
return 0;
}
for (int i = (2); i <= (n); ++i) {
int x = din.read() + 1, y = din.read() + 1, z = din.read();
g[x].push_back({y, z}), g[y].push_back({x, z});
}
init();
solve(1);
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef int ll;
const ll MAX = 1e5 + 20;
const ll mod = 1e9 + 7;
const ll inf = 1e9;
ll n, m, q, in[MAX], out[MAX];
vector<ll> adj[MAX];
int main() {
cin >> n >> m;
ll u, v;
for (ll i = 0; i < (ll)m; i++) {
cin >> u >> v;
if (u > v) swap(u, v);
adj[u].push_back(v);
in[u]++, out[v]++;
}
long long cur = 0;
for (ll i = 1; i < (ll)n + 1; i++) cur += 1LL * in[i] * out[i];
cout << cur << endl;
cin >> q;
while (q--) {
cin >> u;
cur -= 1LL * in[u] * out[u];
for (ll v : adj[u]) {
cur -= 1LL * in[v] * out[v];
out[u]++, in[u]--;
in[v]++, out[v]--;
cur += 1LL * in[v] * out[v];
adj[v].push_back(u);
}
adj[u].clear();
cout << cur << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 5e5 + 10;
const double eps = 1e-8;
const double PI = acos(-1);
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long re() {
long long x;
scanf("%lld", &x);
return x;
}
long long qk(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b /= 2;
}
return ans;
}
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long a[1000];
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, max(0ll, a[i] - i));
}
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
;
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 405;
int a[N][N], n, m, f[N][N];
map<int, int> mp[N * N];
int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map<int, int>::iterator it;
int x = a[i][j];
for (it = mp[x].begin(); it != mp[x].end(); it++) {
int a = it->first;
int b = j;
if (a > b) swap(a, b);
f[a][b] = max(it->second, f[a][b]);
}
mp[x][j] = i + 1;
}
for (int t = 1; t < m; t++) {
for (int k = 0; k + t < m; k++) {
f[k][k + t] = max(f[k][k + t], f[k][k + t - 1]);
f[k][k + t] = max(f[k][k + t], f[k + 1][k + t]);
}
}
for (int t = 0; t < m; t++) {
for (int k = t; k < m; k++)
ans = max(ans, (i - f[t][k] + 1) * (k - t + 1));
}
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define pii Pi
#define pll PL
#define Fi first
#define Se second
#define pb(x) push_back(x)
//#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;
const double PI = acos(-1);
const double EPS = 1e-9;
const int MX = 1<<18;
const int MM = 1000000007;
const int L = 90;
ll F[105];
vector<pll> GP[L];
int main()
{
F[0] = F[1] = 1;
for(int i = 2; i <= L; i++){
F[i] = F[i-1] + F[i-2];
}
GP[1].emplace_back(1, 2);
GP[1].emplace_back(1, 3);
for(int t = 2; t <= L; t++){
for(pll e : GP[t-1]){
ll a = e.first, b = e.second;
while(b+a <= F[t+2]){
a += b;
GP[t].emplace_back(b, a);
}
}
}
int Q;
scanf("%d", &Q);
for(int t = 1; t <= Q; t++){
ll X, Y;
scanf("%lld%lld", &X, &Y);
if( X > Y ) swap(X, Y);
if( X == 1 || X == 2 && Y == 2 ){
printf("1 %lld\n", X*Y%MM);
continue;
}
if( X == 2 ){
printf("2 %lld\n", (Y-1)/2%MM);
continue;
}
int k = min(upper_bound(F, F+L+1, X) - F, upper_bound(F, F+L+1, Y) - F-1) - 1;
ll ans = 0;
auto f = [&](ll X, ll Y){
for(pll e : GP[k-1]){
if( e.second <= X ){
ans = (ans + (Y - e.first) / e.second) % MM;
}
}
};
f(X, Y); f(Y, X);
printf("%d %lld\n", k, ans);
}
}
| 0 |
#include<iostream>
#include<string>
using namespace std;
int h,w,ans;
string m[150];
void solve(int y,int x){
char c = m[y][x];
if(m[y][x] == '0') return;
m[y][x] = '0';
if(y+1 < h && m[y+1][x] == c) solve(y+1,x);
if(x+1 < w && m[y][x+1] == c) solve(y,x+1);
if(y-1 >= 0 && m[y-1][x] == c) solve(y-1,x);
if(x-1 >= 0 && m[y][x-1] == c) solve(y,x-1);
}
int main(void){
for(;;){
cin >> h >> w;
if(!h && !w) break;
ans = 0;
for(int i = 0;i < h; i++) cin >> m[i];
for(int i = 0;i < h; i++){
for(int j = 0; j < w; j++){
if(m[i][j] != '0'){
solve(i,j);
ans++;
}
}
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
inline void _debug() { cerr << "]" << endl; }
template <typename T>
inline void _debug(T a) {
cerr << a << ']' << endl;
}
template <typename T, typename... Args>
inline void _debug(T a, Args... args) {
cerr << a << ", ";
_debug(args...);
}
template <typename T>
inline void read(T &a) {
cin >> a;
}
template <typename T, typename... Args>
inline void read(T &a, Args &...args) {
cin >> a;
read(args...);
}
template <typename T>
inline void print(T &&a) {
cout << a;
}
template <typename T, typename... Args>
inline void print(T &&a, Args &&...args) {
cout << a;
print(forward<Args>(args)...);
}
template <typename T, typename... Args>
inline void printss(stringstream &ss, T &&a, Args &&...args) {
ss << a;
print(ss, forward<Args>(args)...);
}
vector<string> split(string_view s, char delim = ' ') {
vector<string> res;
string cur;
for (int i = 0; i < (int)((s).size()); ++i) {
if (s[i] == delim) {
if (!cur.empty()) {
res.push_back(cur);
cur = "";
}
} else {
cur += s[i];
}
}
if (!cur.empty()) {
res.push_back(cur);
}
return res;
}
bool is_prime(int a) {
for (int i = 2; i * i <= a; ++i) {
if (a % i == 0) {
return false;
}
}
return true;
}
template <typename T, typename Comp = less<T>>
vector<int> arg_sort(vector<T> &v, Comp comp = Comp()) {
vector<int> res(v.size());
iota((res).begin(), (res).end(), 0);
sort((res).begin(), (res).end(),
[&](int a, int b) { return comp(v[a], v[b]); });
return res;
}
template <typename T>
bool in(T a, T x, T y) {
return x <= a && a < y;
}
int ceil_div(int a, int b) { return (a - 1) / b + 1; }
int d4i[4] = {0, 1, 0, -1}, d4j[4] = {1, 0, -1, 0};
int d8i[8] = {1, 1, 1, 0, -1, -1, -1, 0}, d8j[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
inline void solve();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(7);
solve();
}
int n, m, k, ans;
char g[102][102][102];
inline void solve() {
read(n, m, k);
for (int i = 1; i <= (n); ++i)
for (int j = 1; j <= (m); ++j) {
string s;
read(s);
for (int t = 1; t <= (k); ++t) g[i][j][t] = s[t - 1] - '0';
}
for (int i = 1; i <= (n); ++i)
for (int j = 1; j <= (m); ++j)
for (int t = 1; t <= (k); ++t) {
if (g[i][j][t]) {
if (g[i - 1][j][t] && g[i + 1][j][t])
ans++;
else if (g[i][j - 1][t] && g[i][j + 1][t])
ans++;
else if (g[i][j][t - 1] && g[i][j][t + 1])
ans++;
else if (g[i - 1][j][t] && g[i][j + 1][t] && (!g[i - 1][j + 1][t]))
ans++;
else if (g[i - 1][j][t] && g[i][j][t + 1] && (!g[i - 1][j][t + 1]))
ans++;
else if (g[i][j - 1][t] && g[i + 1][j][t] && (!g[i + 1][j - 1][t]))
ans++;
else if (g[i][j - 1][t] && g[i][j][t + 1] && (!g[i][j - 1][t + 1]))
ans++;
else if (g[i][j][t - 1] && g[i + 1][j][t] && (!g[i + 1][j][t - 1]))
ans++;
else if (g[i][j][t - 1] && g[i][j + 1][t] && (!g[i][j + 1][t - 1]))
ans++;
}
}
print(ans);
}
| 4 |
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <climits>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define PB push_back
typedef vector<double> vd;
const ll M=1000000000;
class Runner {
public:
vd goal_times;
vd probabilities;
Runner() {
goal_times = vd();
probabilities = vd();
}
void inspect() {
cerr << "goal_times" << endl;
REP(i, goal_times.size()) {
cerr << " " << goal_times[i];
}
cerr << endl;
cerr << "probabilities" << endl;
REP(i, probabilities.size()) {
cerr << " " << probabilities[i];
}
cerr << endl;
}
};
ll combination(int n,int k){
ll ans=1;
FOR(i,1,k+1){
ans*=n-i+1;
ans/=i;
}
return ans;
}
const double EPS = 1e-9;
int main()
{
cout.precision(16);
int N, M, L; cin >> N >> M >> L;
vector<Runner> runners;
REP(i, N) {
Runner runner;
double P; int T, V; cin >> P >> T >> V; P /= 100.0;
double base_time = V > 0 ? (double)L / V : 1e20;
REP(t_break, M + 1) {
runner.goal_times.PB(base_time + T * t_break);
runner.probabilities.PB(
pow(P, t_break) * pow(1 - P, M - t_break) * combination(M, t_break) );
}
runners.PB(runner);
//runner.inspect();
}
REP(i, runners.size()) {
Runner &r = runners[i];
double ans = 0;
REP(t, M + 1) {
double prize_prob = r.probabilities[t];
REP(j, runners.size()) {
if (i == j) continue;
Runner &comp = runners[j];
double win_prob = 0;
REP(k, M+1) {
if(comp.goal_times[k] > r.goal_times[t] + EPS) {
win_prob += comp.probabilities[k];
}
}
prize_prob *= win_prob;
}
ans += prize_prob;
}
printf("%.8f\n",ans);
//cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
int main() {
int tests;
std::cin >> tests;
for (int t = 0; t < tests; ++t) {
std::size_t n;
std::cin >> n;
std::vector<std::size_t> as(n);
std::vector<std::vector<std::size_t>> sums(n + 1);
sums[0].resize(n);
std::vector<std::vector<std::size_t>> indices(n);
for (std::size_t i = 0; i < n; ++i) {
std::cin >> as[i];
--as[i];
sums[i + 1] = sums[i];
++sums[i + 1][as[i]];
indices[as[i]].push_back(i);
}
long long quadruples = 0;
for (std::size_t j = 1; j < n - 2; ++j) {
std::size_t aj = as[j];
for (std::size_t k = j + 1; k < n - 1; ++k) {
std::size_t ak = as[k];
auto prefix = sums[j][ak];
auto suffix = sums.back()[aj] - sums[k + 1][aj];
quadruples += prefix * suffix;
}
}
std::cout << quadruples << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t mod = 1e9 + 7;
const int32_t MM = 998244353;
bool prime[100000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> v(n + 1);
for (long long i = 1; i <= n; i++) {
cin >> v[i];
}
v[0] = 0;
for (long long i = 1; i <= n; i++) {
v[i] = v[i] + v[i - 1];
}
while (m--) {
long long q;
cin >> q;
auto i_d = lower_bound(v.begin(), v.end(), q) - v.begin();
long long ans = q - v[i_d - 1];
cout << i_d << " " << ans << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str, temp;
cin >> str;
temp = str;
transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
str[0] = temp[0];
cout << str;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define N 1001
using namespace std;
int n,m;
string s1,s2;
int mem[N][N],used[N][N];
int dfs(int I,int J){
if(I == n) return m-J;
if(J == m) return n-I;
if(used[I][J]++) return mem[I][J];
int res = 1e9;
res = min(res,1+dfs(I+1,J));//insert
res = min(res,1+dfs(I,J+1));//erase
res = min(res,(s1[I]!=s2[J])+dfs(I+1,J+1));//replace or same
return mem[I][J] = res;
}
int main(){
cin>>s1>>s2;
n = s1.size();
m = s2.size();
cout<<dfs(0,0)<<endl;;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, ans;
string s;
int main() {
cin >> n >> k >> s;
if (k == 2) {
string sp;
for (int i = 0; i < n; i++) {
if (i % 2 == 0)
sp += 'A';
else
sp += 'B';
if (sp[i] != s[i]) ans++;
}
cout << min(ans, n - ans) << endl;
if (ans <= n - ans)
cout << sp << endl;
else
for (int i = 0; i < n; i++) {
if (i % 2 == 1)
cout << "A";
else
cout << "B";
}
cout << endl;
return 0;
}
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1]) {
ans++;
if (s[i] == 'A') {
if (i < n - 1 && s[i + 1] != 'B')
s[i] = 'B';
else
s[i] = 'C';
} else {
if (i < n - 1 && s[i + 1] != 'A')
s[i] = 'A';
else {
if (s[i] == 'B')
s[i] = 'C';
else
s[i] = 'B';
}
}
}
}
cout << ans << endl << s << endl;
return 0;
}
| 3 |
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
while (true) {
int w, d;
cin >> w >> d;
if (w == 0 && d == 0) return 0;
int hf[w], hs[d];
for (int i = 0; i < w; i++) {
cin >> hf[i];
}
for (int i = 0; i < d; i++) {
cin >> hs[i];
}
sort(hf, hf + w);
sort(hs, hs + d);
int pf = w - 1, ps = d - 1;
int ans = 0;
while (pf >= 0 || ps >= 0) {
if (pf < 0) {
ans += hs[ps];
ps--;
} else if (ps < 0) {
ans += hf[pf];
pf--;
} else {
if (hf[pf] == hs[ps]) {
ans += hf[pf];
pf--;
ps--;
}
else if (hf[pf] > hs[ps]) {
ans += hf[pf];
pf--;
} else {
ans += hs[ps];
ps--;
}
}
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int color[100005], sz[100005], mapa[100005], bigboy[100005], cnt[100005];
long long maxi, sum, res[100005];
vector<vector<int> > adj;
void dfs(int u, int p) {
sz[u] = 1;
for (int v : adj[u])
if (v != p) {
dfs(v, u);
sz[u] += sz[v];
}
}
void add(int u, int p) {
cnt[color[u]]++;
if (cnt[color[u]] > maxi)
sum = color[u], maxi = cnt[color[u]];
else if (cnt[color[u]] == maxi)
sum += color[u];
for (int v : adj[u])
if (v != p and !bigboy[v]) add(v, u);
}
void rem(int u, int p) {
cnt[color[u]]--;
for (int v : adj[u])
if (v != p and !bigboy[v]) rem(v, u);
}
void solve(int u, int p, int keep) {
int bigChild = -1;
for (int v : adj[u])
if (v != p) {
if (bigChild == -1 || sz[v] > sz[bigChild]) bigChild = v;
}
for (int v : adj[u])
if (v != p and v != bigChild) solve(v, u, 0);
if (bigChild != -1) solve(bigChild, u, 1), bigboy[bigChild] = 1;
add(u, p);
res[u] = sum;
if (bigChild != -1) bigboy[bigChild] = 0;
if (!keep) rem(u, p), maxi = sum = 0;
}
int main() {
int n;
scanf("%d", &n);
adj.assign(n + 5, vector<int>());
for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, -1);
solve(1, -1, 0);
for (int i = 1; i <= n; i++) printf("%lld ", res[i]);
printf("\n");
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, d, s = 0, x;
cin >> n >> d;
vector<vector<int>> v(n, vector<int>(d));
for(int i = 0;i < n;i++) for(int j = 0;j < d;j++) cin >> v[i][j];
for(int i = 0;i < n;i++) {
for(int j = i + 1;j < n;j++) {
x = 0;
for(int k = 0;k < d;k++) x += (v[i][k] - v[j][k]) * (v[i][k] - v[j][k]);
if(x == (int)(sqrt(x))*sqrt(x)) s++;
}
}
cout << s << endl;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n, m;
scanf("%d%d", &n, &m);
if (n > m) {
int t = n;
n = m;
m = t;
}
if (n == 1) {
printf("%d\n", m - m / 3 - (m % 3 == 0 ? 0 : 1));
} else if (n == 2) {
printf("%d\n", m * n - 1 - m / 2);
} else if (n == 3) {
printf("%d\n", 2 * m + (m - 1) / 4);
} else if (n == 4) {
printf("%d\n", m * 3 - 1 + (m == 4 || m == 7 || m == 8 || m == 10));
} else if (n == 5) {
printf("%d\n", 4 * m - 1 - (m / 4));
} else {
puts("26");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
int i0 = 0;
for (int i = 0; i < t.size(); i++) {
if (s[i0] == t[i]) i0++;
}
cout << i0 + 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
struct bit {
long long n;
vector<long long> a;
bit(long long n) : n(n), a(n + 2) {}
void add(long long i, long long v) {
for (++i; i <= n + 1; i += i & -i) a[i] += v;
}
long long get(long long i) {
long long res = 0;
for (; i; i -= i & -i) res += a[i];
return res;
}
long long get(long long a, long long b) { return get(b) - get(a); }
};
struct query {
long long r, u, i;
bool operator<(const query &q) const { return u < q.u; }
};
long long n;
struct rectangle {
long long l, r, d, u;
void rotate() { tie(l, r, d, u) = make_tuple(n - u, n - d, l, r); }
bool operator<(const rectangle &r) const { return u < r.u; }
};
long long s(long long n) { return n * (n - 1) / 2; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q;
cin >> n >> q;
vector<rectangle> p(n);
for (long long i = (0); i < (n); i++) {
p[i].l = i, p[i].r = i + 1;
cin >> p[i].u;
p[i].d = p[i].u - 1;
}
vector<long long> res(q);
vector<rectangle> r(q);
for (long long i = (0); i < (q); i++) {
cin >> r[i].l >> r[i].d >> r[i].r >> r[i].u;
r[i].l--, r[i].d--;
res[i] = s(n);
res[i] -= s(r[i].l) + s(r[i].d) + s(n - r[i].r) + s(n - r[i].u);
}
for (long long dir = (0); dir < (4); dir++) {
vector<query> qs(q);
for (long long i = (0); i < (q); i++) qs[i] = {r[i].l, r[i].d, i};
sort(begin(qs), end(qs));
sort(begin(p), end(p));
bit b(n);
long long q_pnt = 0;
for (long long i = (0); i < (n); i++) {
while (q_pnt < q && qs[q_pnt].u == i) {
auto c = qs[q_pnt];
res[c.i] += s(b.get(c.r));
q_pnt++;
}
b.add(p[i].l, 1);
}
for (long long i = (0); i < (n); i++) p[i].rotate();
for (long long i = (0); i < (q); i++) r[i].rotate();
}
for (long long i = (0); i < (q); i++) cout << res[i] << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
struct node {
int no, s;
bool operator<(node a) const { return s < a.s; }
};
multiset<node> a, b;
set<node>::iterator pa, pb;
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int col, sum;
cin >> col >> sum;
node tmp;
tmp.no = i, tmp.s = sum;
if (col)
a.insert(tmp);
else
b.insert(tmp);
}
for (int i = 1; i < n; i++)
if (a.size() && b.size()) {
pa = a.begin();
pb = b.begin();
if (b.size() == 1 || (*pa).s < (*pb).s) {
pb = b.end();
pb--;
node tb = *pb;
printf("%d %d %d\n", (*pa).no, (*pb).no, (*pa).s);
tb.s -= (*pa).s;
a.erase(pa);
b.erase(pb);
b.insert(tb);
} else {
pa = a.end();
pa--;
node ta = *pa;
printf("%d %d %d\n", (*pb).no, (*pa).no, (*pb).s);
ta.s -= (*pb).s;
b.erase(pb);
a.erase(pa);
a.insert(ta);
}
} else
puts("*");
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 1e18;
template <typename T>
void read(T &x) {
x = 0;
int s = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') s = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
x *= s;
}
template <typename T>
void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
write(x);
puts("");
}
const int maxn = 1e5 + 5, maxm = 1e6 + 5;
struct Edge {
int u, v, c, id;
bool operator<(const Edge &rhs) const { return c < rhs.c; }
} e[maxm];
int n, m;
int fa[maxn];
vector<pair<int, int> > adj[maxn];
int f[maxn][20], dis[maxn][20], d[maxn];
vector<Edge> v;
int find(int v) { return v == fa[v] ? v : fa[v] = find(fa[v]); }
void dfs(int u, int fa) {
d[u] = d[fa] + 1;
for (pair<int, int> x : adj[u])
if (x.first ^ fa) {
int v = x.first, c = x.second;
f[v][0] = u;
dis[v][0] = c;
dfs(v, u);
}
}
int LCA(int x, int y, int op) {
int res = 0;
if (d[x] > d[y]) swap(x, y);
for (int t = 19; t >= 0; t--)
if (d[x] <= d[f[y][t]]) {
res = max(res, dis[y][t]);
y = f[y][t];
}
if (x == y) return op ? res : x;
for (int t = 19; t >= 0; t--)
if (f[x][t] != f[y][t]) {
res = max(res, max(dis[x][t], dis[y][t]));
x = f[x][t], y = f[y][t];
}
res = max(res, max(dis[x][0], dis[y][0]));
return op ? res : f[x][0];
}
int main() {
read(n), read(m);
for (int i = 1; i <= m; i++) {
read(e[i].u), read(e[i].v), read(e[i].c);
e[i].id = i;
}
sort(e + 1, e + 1 + m);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
int p = find(e[i].u), t = find(e[i].v);
if (p != t) {
adj[e[i].u].emplace_back(e[i].v, e[i].c);
adj[e[i].v].emplace_back(e[i].u, e[i].c);
fa[p] = t;
} else
v.emplace_back(e[i]);
}
dfs(1, 0);
for (int j = 1; j <= 19; j++)
for (int i = 1; i <= n; i++) {
f[i][j] = f[f[i][j - 1]][j - 1];
dis[i][j] = max(dis[i][j - 1], dis[f[i][j - 1]][j - 1]);
}
sort((v.begin()), (v.end()), [&](Edge x, Edge y) { return x.id < y.id; });
for (auto i : v) {
int lca = LCA(i.u, i.v, 0);
printf("%d\n", max(LCA(i.u, lca, 1), LCA(i.v, lca, 1)));
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long int curr = 1, ans = 0, x;
while (m--) {
cin >> x;
if (x >= curr) {
ans += x - curr;
curr = x;
} else {
ans += n - (curr - x);
curr = x;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 10;
int a[N], b[N], mark[N], c, E[N];
vector<int> vec, adj[N], cmp[N];
void dfs(int v) {
mark[v] = 1;
cmp[c].push_back(v);
E[c] += (int)(adj[v].size());
for (int u : adj[v])
if (!mark[u]) dfs(u);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
vec.push_back(a[i]);
vec.push_back(b[i]);
}
sort((vec).begin(), (vec).end());
vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
for (int i = 1; i <= n; i++) {
int u = lower_bound((vec).begin(), (vec).end(), a[i]) - vec.begin();
int v = lower_bound((vec).begin(), (vec).end(), b[i]) - vec.begin();
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 0; i < (int)(vec.size()); i++) {
if (mark[i]) continue;
dfs(i);
E[c] /= 2;
c++;
}
int ans = 0;
for (int i = 0; i < c; i++) {
if ((int)(cmp[i].size()) < E[i]) return cout << -1, 0;
if ((int)(cmp[i].size()) == E[i]) {
for (int v : cmp[i]) ans = max(ans, vec[v]);
} else {
int mx = 0;
for (int v : cmp[i]) mx = max(mx, v);
for (int v : cmp[i])
if (v != mx) ans = max(ans, vec[v]);
}
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const long long inf = 1e18 + 10;
long long n, m, a[maxn];
int main() {
int t;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] < 0) a[i] = -a[i];
}
sort(a + 1, a + 1 + n);
a[n + 1] = inf;
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += (upper_bound(a + 1, a + 2 + n, a[i] * 2) - a - 1 - i);
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
void solution() {
cin >> n >> m >> k;
if (m - k > n) {
puts("0");
return;
}
double ret = 1;
for (int i = 0, _n = (k + 1); i < _n; ++i)
ret *= (m - k + i) * 1. / (n + 1 + i);
if (ret > 1 + 1e-7) throw 1;
printf("%.8lf\n", 1.0 - ret);
}
int main() {
solution();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class P, class Q>
inline P smin(P &a, Q b) {
if (b < a) a = b;
return a;
}
template <class P, class Q>
inline P smax(P &a, Q b) {
if (a < b) a = b;
return a;
}
const int N = 1e5 + 5, lgN = 18;
int n, adr[N], res[N << 1], weed[N << 2], nut[N << 2], stamp, par[N][lgN],
st[N], ed[N], h[N], ql1[N << 1], ql2[N << 1], qr1[N << 1], qr2[N << 1];
vector<int> proc[N], adj[N];
void give(int v, int x) {
weed[v] += x;
nut[v] += x;
}
void sprout(int v, int l, int r) { weed[v] = max(weed[l], weed[r]); }
void absorb(int v, int l, int r) {
give(l, nut[v]);
give(r, nut[v]);
nut[v] = 0;
}
void plant(int v = 1, int b = 0, int e = n) {
if (b + 1 == e) {
weed[v] = h[adr[b]];
return;
}
int m = b + e >> 1, l = v << 1, r = l | 1;
plant(l, b, m);
plant(r, m, e);
sprout(v, l, r);
}
void water(int i, int j, int x, int v = 1, int b = 0, int e = n) {
if (i >= e || b >= j) return;
if (i <= b && e <= j) {
give(v, x);
return;
}
int m = b + e >> 1, l = v << 1, r = l | 1;
absorb(v, l, r);
water(i, j, x, l, b, m);
water(i, j, x, r, m, e);
sprout(v, l, r);
}
int smoke(int i, int j, int v = 1, int b = 0, int e = n) {
if (i >= e || b >= j) return 0;
if (i <= b && e <= j) return weed[v];
int m = b + e >> 1, l = v << 1, r = l | 1;
absorb(v, l, r);
return max(smoke(i, j, l, b, m), smoke(i, j, r, m, e));
}
void dfs_order(int v = 0, int p = 0) {
adr[stamp] = v;
st[v] = stamp++;
if (v) h[v] = h[p] + 1;
par[v][0] = p;
for (int i = 1; i < lgN; ++i) par[v][i] = par[par[v][i - 1]][i - 1];
for (int u : adj[v])
if (u != p) dfs_order(u, v);
ed[v] = stamp;
}
int go_up(int v, int d) {
for (int i = 0; i < lgN; ++i)
if (d >> i & 1) v = par[v][i];
return v;
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
u = go_up(u, h[u] - h[v]);
if (u == v) return v;
for (int i = lgN - 1; i >= 0; --i)
if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
return par[v][0];
}
void getseg(int u, int v, int id) {
if (h[u] > h[v]) swap(u, v);
int p;
if (st[u] <= st[v] && st[v] < ed[u]) {
p = go_up(v, (h[v] - h[u]) / 2);
} else {
int l = lca(u, v);
int d = h[u] + h[v] - h[l] - h[l];
p = go_up(v, d - 1 >> 1);
}
int lid = id << 1, rid = lid | 1;
ql1[lid] = st[p];
qr1[lid] = ed[p];
proc[v].push_back(lid);
ql1[rid] = 0;
qr1[rid] = st[p];
ql2[rid] = ed[p];
qr2[rid] = n;
proc[u].push_back(rid);
}
void go(int v = 0, int p = 0) {
for (int x : proc[v]) {
smax(res[x >> 1], smoke(ql1[x], qr1[x]));
smax(res[x >> 1], smoke(ql2[x], qr2[x]));
}
for (int u : adj[v])
if (u != p) {
water(0, n, 1);
water(st[u], ed[u], -2);
go(u, v);
water(st[u], ed[u], 2);
water(0, n, -1);
}
}
int read() {
int x;
cin >> x;
return x;
}
int32_t main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n = read();
for (int i = 1; i < n; ++i) {
int u = read() - 1, v = read() - 1;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs_order();
plant();
int m = read();
for (int i = 0; i < m; ++i) {
int u = read() - 1, v = read() - 1;
getseg(u, v, i);
}
go();
for (int i = 0; i < m; ++i) cout << res[i] << '\n';
}
| 4 |
#include <iostream>
using namespace std;
int m;
int sx[200], sy[200];
int n;
int hx[1000], hy[1000];
int find(int tx, int ty) {
for (int i = 0; i < m; i++) {
bool flag = false;
for (int j = 0; j < n; j++) {
if (sx[i] + tx == hx[j] && sy[i] + ty == hy[j]) {
flag = true;
break;
}
}
if (!flag) return false;
}
return true;
}
int main() {
while (cin >> m) {
if (!m) return 0;
for (int i = 0; i < m; i++) cin >> sx[i] >> sy[i];
cin >> n;
for (int i = 0; i < n; i++) cin >> hx[i] >> hy[i];
for (int i = 0; i < n; i++) {
int tx = hx[i] - sx[0];
int ty = hy[i] - sy[0];
if (find(tx, ty)) {
cout << tx << " " << ty << endl;
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
class Point {
public:
double x, y;
Point(double, double);
void print() { printf(" %d %d\n", (int)x, (int)y); }
};
Point::Point(double _x, double _y) {
x = _x;
y = _y;
}
class Quad {
public:
vector<Point> quad;
Quad(Point, Point);
};
Quad::Quad(Point P, Point O) {
int a = P.x - O.x, b = P.y - O.y;
quad.push_back(P);
for (int i = 0; i < 3; ++i) {
int _x = O.x - b, _y = O.y + a;
quad.push_back(Point(_x, _y));
int temp = b;
b = a;
a = -temp;
}
}
double area(vector<Point> P) {
P.push_back(P[0]);
double area = 0.0;
for (int i = 0; i < P.size() - 1; ++i) {
area += P[i].x * P[i + 1].y - P[i + 1].x * P[i].y;
}
area /= 2.0;
return fabs(area);
}
double dist2(Point p1, Point p2) {
return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}
double angle(Point p, Point r) {
return atan2(r.x * p.y - r.y * p.x, r.x * p.x + r.y * p.y);
}
void angularSort(Point* V) {
double cx = 0.0;
double cy = 0.0;
for (int i = 0; i < 4; ++i) {
cx += V[i].x;
cy += V[i].y;
}
cx /= 4.0;
cy /= 4.0;
Point r = Point(1, 0);
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; ++j) {
Point pi = Point(V[i].x - cx, V[i].y - cy);
Point pj = Point(V[j].x - cx, V[j].y - cy);
if (angle(pi, r) > angle(pj, r)) swap(V[i], V[j]);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
vector<Quad> Q;
for (int k = 0, x, y, ox, oy; k < 4; k++) {
cin >> x >> y >> ox >> oy;
Q.push_back(Quad(Point(x, y), Point(ox, oy)));
}
int mn = (1 << 30);
for (int i1 = 0; i1 < Q[0].quad.size(); ++i1) {
for (int i2 = 0; i2 < Q[1].quad.size(); ++i2) {
for (int i3 = 0; i3 < Q[2].quad.size(); ++i3) {
for (int i4 = 0; i4 < Q[3].quad.size(); ++i4) {
Point p1 = Q[0].quad[i1];
Point p2 = Q[1].quad[i2];
Point p3 = Q[2].quad[i3];
Point p4 = Q[3].quad[i4];
Point a[4] = {p1, p2, p3, p4};
angularSort(a);
vector<Point> v(a, a + sizeof(a) / sizeof(a[0]));
if (dist2(v[0], v[2]) == dist2(v[1], v[3]) &&
dist2(v[0], v[1]) == dist2(v[1], v[2]) &&
dist2(v[1], v[2]) == dist2(v[2], v[3]) &&
dist2(v[3], v[0]) == dist2(v[2], v[3]) &&
fabs(area(v) - dist2(v[0], v[1])) <= 1e-9 && area(v) > 0.0) {
mn = min(mn, i1 + i2 + i3 + i4);
}
}
}
}
}
if (mn == (1 << 30))
cout << -1 << endl;
else
cout << mn << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int a[] = {0, 0, 1, 1, 1, 2, 1, 1, 14, 8, 4, 2, 41,
13, 21, 1, 22, 4, 19, 19, 4, 2, 1, 16, 13, 4,
35, 19, 4, 2, 41, 4, 87, 41, 115, 2, 59, 31, 49,
8, 28, 4, 31, 44, 50, 13, 151, 16, 91, 56, 14};
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) printf("%d ", (long long)a[n] >> i & 1);
puts("1");
for (int i = 0; i < n; i++) printf("%d ", 1);
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main() {
int n, k, x, y; cin >> n >> k >> x >> y;
cout << (n > k ? (k*x + (n-k)*y) : n*x) << endl;
}
| 0 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;
#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
if (n == 0)return 1;
if (n == 1)return x%MOD;
ll res = sq(mod_pow(x, n / 2));
res %= MOD;
if (n % 2 == 1) {
res *= x;
res %= MOD;
}
return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }
ll n;
ll a[1010];
int main() {
cin >> n;
rep(i, n)cin >> a[i];
sort(a, a + n);
rep(i, n) {
FOR(j, i + 1, n) {
if ((a[j] - a[i]) % (n - 1) == 0) {
cout << a[j] << " " << a[i] << endl;
return 0;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long ceil(long long a, long long b) { return (a + b - 1) / b; }
long long expo(long long a, long long b, long long mod) {
long long r = 1;
while (b > 0) {
if (b & 1) r = (r * a) % mod;
a = (a * a) % mod;
b = b >> 1;
}
return r;
}
void solve() {
int n;
cin >> n;
vector<string> s(n - 2);
for (int i = 0; i < n - 2; i++) {
cin >> s[i];
}
string ans;
ans += s[0];
for (int i = 1; i < n - 2; i++) {
if (s[i][0] == s[i - 1][1]) {
ans += s[i][1];
} else {
ans += s[i];
}
}
while (ans.size() != n) {
ans += "a";
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(10);
int t = 1;
cin >> t;
for (int tt = 0; tt < t; tt++) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long Bin_expo(long long n, long long b, long long m) {
long long res = 1;
while (b > 0) {
if (b & 1) {
res = (res * n) % m;
}
n = (n * n) % m;
b /= 2;
}
return res % m;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tc;
cin >> tc;
while (tc--) {
long long n;
cin >> n;
vector<long long> v(n);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
long long f = 0;
for (long long i = 2; i < n; i++) {
if (v[i] >= v[0] + v[1]) {
cout << "1 2 " << i + 1 << "\n";
f = 1;
break;
}
}
if (f) continue;
cout << "-1\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4017;
bool away[maxn] = {0};
int v[maxn] = {0}, d[maxn] = {0}, n = 0, lst[maxn] = {0};
long long p[maxn] = {0};
int main() {
scanf("%d", &n);
int tot = 0;
for (int i = 1; i <= n; ++i) scanf("%d%d%d", &v[i], &d[i], &p[i]);
for (int i = 1; i <= n; ++i) {
if (away[i]) continue;
lst[tot++] = i;
for (int j = i + 1; j <= n && v[i] > 0; ++j)
if (!away[j]) p[j] -= v[i]--;
for (int j = i + 1; j <= n; ++j)
if (!away[j] && p[j] < 0) {
away[j] = 1;
for (int k = j + 1; k <= n; ++k) p[k] -= d[j];
}
}
printf("%d\n", tot);
for (int i = 0; i < tot - 1; ++i) printf("%d ", lst[i]);
printf("%d\n", *(lst + tot - 1));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U >> 1;
const int Mod = int(1e9) + 7;
const int maxN = 200000 + 100;
template <class T>
void tension(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
void relax(T &a, const T &b) {
if (b > a) a = b;
}
void addMod(int &x, int y) {
x += y;
if (x > Mod) x -= Mod;
}
int n, m, x;
int f[2][22000];
int main() {
int ans = 0, now = 0, pre = 1;
f[now][10000] = 1;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
swap(now, pre);
memset(f[now], (0), sizeof(f[now]));
for (int i = 0; i < 22000; ++i)
if (f[pre][i]) {
if (i + x < 22000) addMod(f[now][i + x], f[pre][i]);
if (i - x >= 0) addMod(f[now][i - x], f[pre][i]);
}
addMod(ans, f[now][10000]);
addMod(f[now][10000], 1);
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
long long ans = 0;
for (int i = (0); i < (n); ++i) {
long long t, T, x, cost;
cin >> t >> T >> x >> cost;
if (t >= T) {
ans += cost + m * x;
continue;
}
long long aux1 = cost;
if (m > (T - t)) aux1 += m * x;
long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
aux2 *= cost;
ans += min(aux1, aux2);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[1000007], dp[1007][1007], flag, mod, n;
int rec(int pos, int sum, int cnt) {
if (pos >= n) {
if (sum == 0 and cnt > 0)
return 1;
else
return 0;
}
if (dp[pos][sum] != -1) {
return dp[pos][sum];
}
dp[pos][sum] =
rec(pos + 1, (a[pos] + sum) % mod, cnt + 1) | rec(pos + 1, sum, cnt);
return dp[pos][sum];
}
int main() {
cin >> n >> mod;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i] = a[i] % mod;
if (a[i] == 0) flag = 1;
}
if (n > mod || flag) {
cout << "YES" << endl;
return 0;
}
memset(dp, -1, sizeof(dp));
if (rec(0, 0, 0))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long sum, n, arr[64], dp[64][4];
long long rec(int pos, bool who) {
if (dp[pos][who] != 0) return dp[pos][who];
if (pos == n) {
if (who == 0)
return arr[pos];
else
return 0;
}
if (who == 0) {
return dp[pos][who] = max(rec(pos + 1, 1) + arr[pos], rec(pos + 1, 0));
} else
return dp[pos][who] = min(rec(pos + 1, 1) + arr[pos], rec(pos + 1, 0));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
sum += arr[i];
}
long long k = rec(1, 0);
cout << sum - k << " " << k << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 200007;
int le[N], ri[N];
int id[N], fn[N];
int a[N], nxt[N], clc;
int tag[N], dup[N];
string s;
void dfs(int x) {
if (x == 0) return;
dfs(le[x]);
id[fn[x] = ++clc] = x;
a[clc] = s[x] - 'a' + 1;
dfs(ri[x]);
}
int gao(int x, int r) {
if (x == 0) return 0;
if (r <= 0) return 0;
int use = gao(le[x], r - 1);
if (use) {
dup[x] = 1;
r -= use + 1;
return gao(ri[x], r) + use + 1;
} else {
if (tag[x]) {
dup[x] = 1;
return gao(ri[x], r - 1) + 1;
} else {
dup[x] = 0;
return 0;
}
}
}
void solve() {
int n, k;
cin >> n >> k;
cin >> s;
s = ' ' + s;
for (int i = 1; i <= n; i++) {
cin >> le[i] >> ri[i];
}
clc = 0;
dfs(1);
a[clc + 1] = 0;
for (int i = clc; i >= 1; i--) {
if (a[i] != a[i + 1]) {
nxt[i] = a[i + 1];
} else {
nxt[i] = nxt[i + 1];
}
assert(nxt[i] != a[i]);
if (nxt[i] > a[i]) {
tag[id[i]] = 1;
} else {
tag[id[i]] = 0;
}
}
for (int i = 1; i <= n; i++) dup[i] = 0;
gao(1, k);
for (int i = 1; i <= n; i++) {
char ch = s[id[i]];
cout << ch;
if (dup[id[i]]) cout << ch;
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--) solve();
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.