solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, k;
long long res, cur;
cin >> n >> k;
res = 0;
cur = n * 2;
res += (cur + k - 1) / k;
cur = n * 5;
res += (cur + k - 1) / k;
cur = n * 8;
res += (cur + k - 1) / k;
cout << res << "\n";
return 0;
}
| 1 |
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int arr[17][17];
long long bmask[400005];
int main(){
scanf("%d",&n);
for (int x=0;x<n;x++){
for (int y=0;y<n;y++){
scanf("%d",&arr[x][y]);
}
}
for (int x=0;x<(1<<n);x++){ ///O(2^n n^2)
for (int i=0;i<n;i++){
for (int j=i+1;j<n;j++){
if (((1<<i)&x)==0 || ((1<<j)&x)==0) continue;
bmask[x]+=arr[i][j];
}
}
}
for (int x=0;x<(1<<n);x++){
for (int s=x;s;s=(s-1)&x){
bmask[x]=max(bmask[x],bmask[s]+bmask[s^x]);
}
}
printf("%lld\n",bmask[(1<<n)-1]);
}
| 0 |
#include<iostream>
#include<cstdio>
#include<cstring>
namespace Midium
{
const int N=201000;
int s[N],t[N];
int cnt[N];
int n,m,ans;
void initialize()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",s+i),t[s[i]]++;
for(int i=1;i<=n;i++)
for(int j=std::max(i-t[i]+1,1);j<=i;j++)
cnt[j]++;
for(int i=1;i<=n;i++)
if(!cnt[i])ans++;
}
void modify(int x,int y)
{
int tmp=x;
x=s[x];s[tmp]=y;
if(x==y)return;
int p1=x-t[x]+1,p2=y-t[y];
if(p1>0)
{
cnt[p1]--;
if(!cnt[p1])ans++;
}
if(p2>0)
{
if(!cnt[p2])ans--;
cnt[p2]++;
}
t[x]--;
t[y]++;
}
void solve()
{
initialize();
for(int x,y;m--;)
{
scanf("%d%d",&x,&y);
modify(x,y);
printf("%d\n",ans);
}
}
}
int main()
{
Midium::solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char ch[N];
int s[N], w[N];
int T, n, x;
int main() {
scanf("%d", &T);
for (; T; T--) {
scanf("%s", ch + 1);
n = strlen(ch + 1);
scanf("%d", &x);
for (int i = 1; i <= n; i++) {
w[i] = 1;
s[i] = ch[i] - '0';
}
for (int i = 1; i <= n; i++)
if (!s[i]) {
if (i + x <= n) w[i + x] = 0;
if (i - x >= 1) w[i - x] = 0;
}
bool bz = 1;
for (int i = 1; i <= n; i++)
if (s[i])
if (((i + x <= n && !w[i + x]) || i + x > n) &&
((i - x >= 1 && !w[i - x]) || i - x < 1)) {
bz = 0;
break;
}
if (!bz)
puts("-1");
else {
for (int i = 1; i <= n; i++) printf("%d", w[i]);
puts("");
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
inline int read() {
register int c = getchar(), sum(0), fg(1);
while (c < '0' || c > '9') {
if (c == '-') fg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') sum = sum * 10 + c - '0', c = getchar();
return sum * fg;
}
const int oo = 0x3f3f3f3f;
const int maxn = 3000;
int N, M;
inline bool check(const char &c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 0;
return 1;
}
char s[maxn + 5];
int main() {
scanf("%s", s + 1);
char last1, last2;
int cnt = 0;
for (int i = (1), i_end_ = (strlen(s + 1)); i <= i_end_; ++i)
if (!check(s[i]))
putchar(s[i]), cnt = 0;
else {
if (!cnt)
++cnt, last1 = s[i];
else if (cnt == 1)
++cnt, last2 = s[i];
else if (cnt == 2)
if (!(last1 == last2 && last2 == s[i]))
putchar(' '), cnt = 1, last1 = s[i];
putchar(s[i]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, i, j, cnt = 0, st, en, ans = 0;
char s[105][105];
cin >> n >> m;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> s[i][j];
}
}
for (j = 0; j < m; j++) {
if (s[n - 1][j] == 'B') {
cnt++;
en = j;
} else if (s[n - 1][j] == '.') {
if (cnt > 0) {
ans++;
}
cnt = 0;
}
}
if (en == m - 1) ans++;
cout << ans << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000010;
int to[N];
bool vis[N];
int n;
int sum[N];
long long a, b, c, d, e, f;
int cur;
int main() {
for (int i = 1; i < N; i++) {
to[i] = i;
}
for (int i = 2; i < N; i++) {
if (to[i] != i) continue;
for (int j = i; j < N; j += i) {
to[j] = to[j] - (to[j] / i);
}
}
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
b += (i - 1 - to[i]);
if (to[i] != i - 1) {
sum[i] = sum[i - 1];
continue;
}
if (i * 2 > n) {
a++;
}
if ((long long)i * i > n) f++;
for (int j = i; j <= n; j += i) {
if ((long long)i * i > n && j != i) e++;
if (vis[j]) continue;
vis[j] = true;
sum[i]++;
}
sum[i] += sum[i - 1];
}
a = (a + 1) * (n - a - 1) + a * (a + 1) / 2;
c = (long long)(n - f - 1) * (n - f - 2) / 2;
c -= (b - e);
for (int i = 2; i * 2 <= n; i++) {
if (to[i] != i - 1) continue;
if ((long long)i * i <= n) continue;
cur = n / i;
c += sum[cur];
}
c -= e;
d = (long long)n * (n - 1) / 2;
d -= (a + b + c);
long long ans = b + 2 * c + 3 * d;
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e9 + 7;
int main() {
double a, b, c, d;
cin >> a >> b >> c >> d;
if (a - b == b - c && b - c == c - d) {
double r = a - b;
cout << d - r;
return 0;
} else if (b / a == c / b && c / b == d / c) {
double r = b / a;
if (r * d == (int)(r * d)) {
cout << r * d;
return 0;
} else {
cout << 42;
return 0;
}
} else
cout << 42;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
void rec(long long n, long long a, long long b, long long &ra, long long &rb) {
if (n == 0) {
ra = 1;
rb = 0;
return;
};
if (n == 1) {
ra = a;
rb = b;
return;
};
long long cra, crb;
rec(n / 2, (a * a + b * b) % MOD, (2 * a * b) % MOD, cra, crb);
if (n & 1LL) {
ra = (cra * a + crb * b) % MOD;
rb = (cra * b + crb * a) % MOD;
} else {
ra = cra;
rb = crb;
}
}
int main(int argc, char const *argv[]) {
long long n, so, dmy;
cin >> n;
rec(n, 3, 1, so, dmy);
cout << so << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string arr[] = {"+------------------------+", "|#.#.#.#.#.#.#.#.#.#.#.|D|)",
"|#.#.#.#.#.#.#.#.#.#.#.|.|", "|#.......................|",
"|#.#.#.#.#.#.#.#.#.#.#.|.|)", "+------------------------+"};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
int i = 1, j = 1;
while (n--) {
if (arr[i][j] == '.') ++i;
arr[i][j] = 'O';
++i;
if (i == 5) i = 1, j += 2;
}
for (int i = 0; i < 6; ++i) {
cout << arr[i] << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int maxN = 2000;
int n, x;
vector<int> have;
int nx[maxN];
int a[maxN];
int used[maxN];
int dp[maxN][maxN];
int main() {
cin >> n >> x;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] != 0) {
nx[a[i]] = i;
}
}
int inchain = 0;
int f = n;
for (int i = 1; i <= n; ++i) {
if (a[i] == 0) {
int cnt = 1;
int cur = i;
int gd = 0;
if (cur == x) {
gd = 1;
inchain = 1;
}
while (nx[cur] != 0) {
++cnt;
cur = nx[cur];
if (cur == x) {
gd = 1;
inchain = cnt;
}
}
if (!gd) {
have.push_back(cnt);
}
}
}
dp[0][0] = 1;
for (int i = 1; i <= have.size(); ++i) {
int cur = have[i - 1];
for (int j = 0; j < maxN; ++j) {
dp[i][j] = dp[i - 1][j];
}
for (int j = 0; j < maxN; ++j) {
if (dp[i - 1][j]) {
dp[i][j + cur] = 1;
}
}
}
for (int i = 0; i < n; ++i) {
if (dp[have.size()][i]) {
printf("%d\n", i + inchain);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 15001000;
int n, q, rt, cnt;
int ls[N], rs[N], sumt[N], lazy[N];
void pup(int rt) { sumt[rt] = sumt[ls[rt]] + sumt[rs[rt]]; }
void pdo(int x, int l, int r) {
if (lazy[x] != -1) {
int mid = l + r >> 1;
if (!ls[x]) ls[x] = ++cnt;
sumt[ls[x]] = lazy[x] * (mid - l + 1);
lazy[ls[x]] = lazy[x];
if (!rs[x]) rs[x] = ++cnt;
sumt[rs[x]] = lazy[x] * (r - mid);
lazy[rs[x]] = lazy[x];
lazy[x] = -1;
}
}
void change(int &x, int L, int R, int l, int r, int v) {
if (!x) x = ++cnt;
if (l <= L && R <= r) {
sumt[x] = (R - L + 1) * v;
lazy[x] = v;
return;
}
pdo(x, L, R);
int mid = L + R >> 1;
if (l <= mid) change(ls[x], L, mid, l, r, v);
if (mid < r) change(rs[x], mid + 1, R, l, r, v);
pup(x);
}
int main() {
scanf("%d%d", &n, &q);
memset(lazy, -1, sizeof(lazy));
change(rt, 1, n, 1, n, 0);
while (q--) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
change(rt, 1, n, l, r, 2 - k);
printf("%d\n", n - sumt[1]);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = int(3e5) + 3;
int n;
int A[nmax];
int pos[nmax];
int aib[nmax];
inline void aibUpdate(int pos, int val) {
for (; pos <= n; pos += (pos & -pos)) {
aib[pos] += val;
}
}
inline int aibQuery(int pos) {
int ret = 0;
for (; pos > 0; pos -= (pos & -pos)) {
ret += aib[pos];
}
return ret;
}
inline void check(int a) {
if (a > n) return;
if (pos[a] > pos[a - 1]) {
if (aibQuery(a) - aibQuery(a - 1) > 0) {
aibUpdate(a, -1);
}
} else {
if (aibQuery(a) - aibQuery(a - 1) == 0) {
aibUpdate(a, 1);
}
}
}
inline void update(int a, int b) {
swap(A[a], A[b]);
swap(pos[A[a]], pos[A[b]]);
check(A[a]);
check(A[a] + 1);
check(A[b]);
check(A[b] + 1);
}
inline int query(int a, int b) { return aibQuery(b) - aibQuery(a) + 1; }
inline int queryBrute(int a, int b) {
int ret = 1;
for (int i = a + 1; i <= b; i++) {
if (pos[i] < pos[i - 1]) {
ret++;
}
}
return ret;
}
void generateData() {
srand(time(0));
cout << "100\n";
vector<int> v(100);
for (int i = 1; i <= 100; i++) {
v[i - 1] = i;
}
while (v.empty() == false) {
int p;
cout << v[p = rand() % v.size()] << " ";
v.erase(v.begin() + p);
}
cout << "\n";
cout << 1000 << "\n";
for (int i = 1; i <= 1000; i++) {
int val = rand() % 100 + 1;
cout << rand() % 2 + 1 << " " << val << " ";
cout << rand() % (100 - val + 1) + val << "\n";
}
exit(0);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
pos[A[i]] = i;
}
for (int i = 2; i <= n; i++) {
if (pos[i] < pos[i - 1]) {
aibUpdate(i, 1);
}
}
int q, t, a, b;
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> t >> a >> b;
if (t == 1) {
cout << query(a, b) << "\n";
} else {
update(a, b);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const long long mod = 998244353;
int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
const int N = 500001;
void stop() {}
struct point {
double x, y;
};
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long fixMod(long long a, long long b) { return ((a % b) + b) % b; }
double triangle_area(int x1, int y1, int x2, int y2, int x3, int y3) {
return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)) / 2.;
};
long long dis(point a, point b) {
long long x = a.x - b.x, y = a.y - b.y;
return x * x + y * y;
}
double traingleArea(double a, double b, double c) {
double s = (a + b + c) / 2.0;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
double circleArea(double r) { return acos(-1) * r * r; }
long long power(long long x, long long y) {
if (y == 0) return 1;
if (y == 1) return x;
long long r = power(x, y / 2) % mod;
return r % mod * r % mod * power(x, y % 2) % mod;
}
long long arr[200000];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
string g = "RGB";
sort(((g).begin()), ((g).end()));
int mn = 1e6;
string second;
do {
string x = s;
int ans = 0;
for (int i = 0; i < n; i += 3) {
if (x[i] != g[0]) ans++;
x[i] = g[0];
}
for (int i = 1; i < n; i += 3) {
if (x[i] != g[1]) ans++;
x[i] = g[1];
}
for (int i = 2; i < n; i += 3) {
if (x[i] != g[2]) ans++;
x[i] = g[2];
}
if (ans < mn) {
mn = ans;
second = x;
}
} while (next_permutation(((g).begin()), ((g).end())));
cout << mn << '\n';
cout << second;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
const int maxm = 210;
const int INF = 1e9;
const int mod1 = 1e9 + 7;
const int mod2 = 123456701;
int n, m;
struct Edge {
int u;
int v;
int cost;
int next;
} edge[maxn * 2];
int head[maxn];
int num;
void addedge(int u, int v, int cost) {
edge[num].u = u;
edge[num].v = v;
edge[num].cost = cost;
edge[num].next = head[u];
head[u] = num++;
}
vector<int> tst[maxn];
long long dis[maxn];
int vis[maxn];
void spfa(int s) {
int i, j;
for (i = 1; i <= n; i++) {
dis[i] = 1e16;
}
memset(vis, 0, sizeof(vis));
dis[s] = 0;
vis[s] = 1;
queue<int> que;
que.push(s);
while (!que.empty()) {
int u = que.front();
que.pop();
vis[u] = 0;
long long t = dis[u];
int x = lower_bound(tst[u].begin(), tst[u].end(), t) - tst[u].begin();
while (x < tst[u].size() && tst[u][x] == t) {
t++;
x++;
}
for (i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
long long c = edge[i].cost;
c = c + t;
if (c < dis[v]) {
dis[v] = c;
if (!vis[v]) {
que.push(v);
vis[v] = 1;
}
}
}
}
return;
}
int main() {
int i, j;
int t;
while (scanf("%d%d", &n, &m) != EOF) {
num = 0;
memset(head, -1, sizeof(head));
for (i = 1; i <= m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
addedge(x, y, z);
addedge(y, x, z);
}
for (i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
for (j = 1; j <= k; j++) {
int x;
scanf("%d", &x);
tst[i].push_back(x);
}
}
spfa(1);
for (i = 1; i <= n; i++) {
}
if (dis[n] == 1e16) {
printf("-1\n");
} else {
cout << dis[n] << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int ans[4];
int main() {
int T;
cin >> T;
while (T--) {
scanf("%d%d%d", &ans[1], &ans[2], &ans[3]);
sort(ans + 1, ans + 4);
if ((abs(ans[1] - ans[2]) <= 1) && (abs(ans[2] - ans[3]) <= 1))
printf("0\n");
else if (ans[3] == ans[2] || ans[2] == ans[1])
printf("%d\n", 2 * abs(ans[3] - ans[1]) - 4);
else if (abs(ans[1] - ans[2]) <= 1)
printf("%d\n", abs(ans[3] - ans[1]) + abs(ans[3] - ans[2]) - 3);
else if (abs(ans[2] - ans[3]) <= 1)
printf("%d\n", abs(ans[3] - ans[1]) + abs(ans[2] - ans[1]) - 3);
else
printf("%d\n", 2 * abs(ans[3] - ans[1]) - 4);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n;
cin >> n;
if (n == 3) {
cout << 1 << " " << 1 << " " << 1 << '\n';
return;
}
long long temp = 1;
if ((n - 2) % 3 == 0) {
temp++;
n--;
}
n -= 2;
cout << 1 << " " << temp << " " << n << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e5 + 10;
long long v[maxN], c[maxN], best[maxN];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, q;
cin >> n >> q;
for (long long i = 0; i < n; i++) cin >> v[i];
for (long long i = 0; i < n; i++) cin >> c[i];
while (q--) {
long long a, b, res = 0;
cin >> a >> b;
long long mx1 = 0, mx2 = 0;
for (long long i = 0; i < maxN; i++) best[i] = -1e18;
for (long long i = 0; i < n; i++) {
long long ans = b * v[i];
ans = max(ans, best[c[i]] + a * v[i]);
if (c[i] != mx1)
ans = max(ans, best[mx1] + b * v[i]);
else
ans = max(ans, best[mx2] + b * v[i]);
best[c[i]] = max(best[c[i]], ans);
if (mx1 == c[i] || mx2 == c[i]) {
if (best[mx2] > best[mx1]) swap(mx2, mx1);
} else {
if (ans > best[mx1]) {
mx2 = mx1;
mx1 = c[i];
} else if (ans > best[mx2])
mx2 = c[i];
}
res = max(res, ans);
}
cout << res << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a, b;
int main() {
scanf("%d%d%d", &n, &m, &k);
int flag = 1;
for (int i = 1; i * i <= m; i++) {
if (m % i == 0) {
a = i;
b = m / i;
if (a > 1 && b >= k) flag = 0;
if (b > 1 && a >= k) flag = 0;
}
}
if (flag == 1) {
printf("Marsel");
return 0;
}
if (n % 2 == 1) {
printf("Timur");
} else {
printf("Marsel");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[5];
int main() {
cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4];
cout << min(min(min(a[0], a[1]), min(a[2] / 2, a[3] / 7)), a[4] / 4);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int k, n, c;
int a[100000];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
c = a[0];
for (int i = 0; i < n; i++)
if (a[i] < c) c = a[i];
for (int i = 0; i < n; i++) {
if (a[i] % c != 0) {
k = 1;
break;
}
}
if (k == 0)
cout << c;
else
cout << "-1";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
const long long MOD = 1e9 + 7;
const double EBS = 1e-7;
int dX[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dY[] = {1, -1, 0, 0, 1, -1, -1, 1};
int n, ans;
vector<int> t1, t2;
void solve(int a, int b) {
int line = a + 2 * b;
if (t1[(t1.size()) - 1] - t1[a] + t2[(t2.size()) - 1] - t2[b] <= line)
ans = min(ans, line);
}
int main() {
scanf("%d", &n);
int t, w;
t1.push_back(0);
t2.push_back(0);
for (int i = 0; i < n; ++i) {
scanf("%d", &t), scanf("%d", &w);
ans += w;
if (t == 1)
t1.push_back(w);
else
t2.push_back(w);
}
sort(t1.begin() + 1, t1.end());
sort(t2.begin() + 1, t2.end());
reverse(t1.begin() + 1, t1.end());
reverse(t2.begin() + 1, t2.end());
for (int i = 1; i < (t1.size()); ++i) t1[i] += t1[i - 1];
for (int i = 1; i < (t2.size()); ++i) t2[i] += t2[i - 1];
for (int i = 0; i < (t1.size()); ++i)
for (int j = 0; j < (t2.size()); ++j) solve(i, j);
cout << ans;
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define INF (1LL<<60)
typedef long long ll;
typedef vector<ll> vec;
vec And(vec a,vec b){
sort(a.begin(),a.end());sort(b.begin(),b.end());
vec res;
int as=a.size(),bs=b.size();
int ai=0,bi=0;
while(ai<as||bi<bs){
ll av=(ai==as?INF:a[ai]);
ll bv=(bi==bs?INF:b[bi]);
if(av<bv){
ai++;
}else if(av==bv){
res.push_back(av);
ai++;
bi++;
}else{
bi++;
}
}
return res;
}
vec Or(vec a,vec b){
sort(a.begin(),a.end());sort(b.begin(),b.end());
vec res;
int as=a.size(),bs=b.size();
int ai=0,bi=0;
while(ai<as||bi<bs){
ll av=(ai==as?INF:a[ai]);
ll bv=(bi==bs?INF:b[bi]);
if(av<bv){
res.push_back(av);ai++;
}else if(av==bv){
res.push_back(av);ai++;bi++;
}else{
res.push_back(bv);bi++;
}
}
return res;
}
vec Dif(vec a,vec b){
sort(a.begin(),a.end());sort(b.begin(),b.end());
vec res;
int as=a.size(),bs=b.size();
int ai=0,bi=0;
while(ai<as||bi<bs){
ll av=(ai==as?INF:a[ai]);
ll bv=(bi==bs?INF:b[bi]);
if(av<bv){
res.push_back(av);ai++;
}else if(av==bv){
ai++;bi++;
}else{
bi++;
}
}
return res;
}
vec Xor(vec a,vec b){
return Or(Dif(a,b),Dif(b,a));
}
vec U;
vec Not(vec a){
return Dif(U,a);
}
map<char,vec> mp;
void func(char ch,int num){
vec v;
ll a;
for(int i=0;i<num;i++){
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
mp[ch]=v;
U=Or(U,v);
}
string s;
int pos,len;
vec solve();
vec getNum(){
if(s[pos]=='c'){
pos++;
return Not(getNum());
}else if(s[pos]=='('){
pos++;
vec res=solve();
pos++;
return res;
}else{
char ch=s[pos];
pos++;
return mp[ch];
}
}
vec solve(){
vec res=getNum();
while(pos<len&&s[pos]!=')'){
char ch=s[pos];
pos++;
vec num=getNum();
if(ch=='u')res=Or(res,num);
if(ch=='i')res=And(res,num);
if(ch=='d')res=Dif(res,num);
if(ch=='s')res=Xor(res,num);
}
return res;
}
int main(){
int num;
char ch;
while(cin>>ch>>num){
mp.clear();U.clear();
func(ch,num);
while(1){
if(ch=='R')break;
cin>>ch>>num;
if(ch=='R')break;
func(ch,num);
}
sort(U.begin(),U.end());
mp['U']=U;
pos=0;
cin>>s;
len=s.size();
vec ans=solve();
if(ans.size()==0)cout<<"NULL";
for(int i=0;i<(int)ans.size();i++){
if(i)cout<<' ';
cout<<ans[i];
}
cout<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 5005;
int n, b, sb;
int a[mn];
int d[2][mn * 2];
short fa[mn][mn * 2];
short used[mn][mn * 2];
int sa[mn];
int main() {
scanf("%d%d", &n, &b);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sa[i] = sa[i - 1] + a[i];
bool p = 0;
for (int i = 0; i < n; i++) {
memset(d[p ^ 1], 1, sizeof d[p ^ 1]);
for (int j = 0; j <= sb; j++) {
if (d[p][j] > 1e7) continue;
int nowb = b + j - (sa[i] - d[p][j]);
int t = min({a[i + 1] / 2, nowb});
for (int k = 0; k <= t; k++) {
int v = j + (a[i + 1] - k) / 10;
if (d[p ^ 1][v] > d[p][j] + a[i + 1] - k) {
fa[i + 1][v] = j;
used[i + 1][v] = k;
d[p ^ 1][v] = d[p][j] + a[i + 1] - k;
}
}
}
sb += a[i + 1] / 10;
p ^= 1;
}
int ans = 1e5 + 5, choose = 0;
for (int j = 0; j <= sb; j++) {
if (ans > d[p][j]) {
ans = d[p][j];
choose = j;
}
}
printf("%d\n", ans);
vector<int> v;
for (int i = n; i; i--) {
v.push_back(used[i][choose]);
choose = fa[i][choose];
}
reverse(v.begin(), v.end());
for (int i = 1; i <= n; i++) {
printf("%d ", v[i - 1]);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
long long n, q, l, ans;
vector<array<long long, 2>> adj[N];
long long tmp[N], dist[N], root, fa[N];
long long psum[N];
vector<array<long long, 2>> st;
void dfs1(long long v, long long p) {
for (auto u : adj[v]) {
if (u[0] == p) continue;
tmp[u[0]] = tmp[v] + u[1];
dfs1(u[0], v);
}
}
void dfs2(long long v, long long p) {
fa[v] = p;
st.push_back(array<long long, 2>{dist[v], v});
long long x = (*lower_bound(st.begin(), st.end(),
array<long long, 2>{dist[v] - l, 0}))[1];
if (fa[x] >= 0) psum[fa[x]]--;
psum[v]++;
for (auto u : adj[v]) {
if (u[0] == p) continue;
dfs2(u[0], v);
}
st.pop_back();
}
void dfs3(long long v, long long p) {
for (auto u : adj[v]) {
if (u[0] == p) continue;
dfs3(u[0], v);
psum[v] += psum[u[0]];
}
ans = max(ans, psum[v]);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long a, b, w;
cin >> a >> b >> w;
--a, --b;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
dfs1(0, -1);
long long _u = max_element(tmp, tmp + n) - tmp;
memset(tmp, 0, sizeof tmp);
dfs1(_u, -1);
long long _v = max_element(tmp, tmp + n) - tmp;
swap(tmp, dist);
dfs1(_v, -1);
for (long long i = 0; i < n; i++) dist[i] = max(dist[i], tmp[i]);
root = min_element(dist, dist + n) - dist;
cin >> q;
while (q--) {
cin >> l;
ans = 0;
memset(psum, 0, sizeof psum);
dfs2(root, -1);
dfs3(root, -1);
cout << ans << endl;
}
}
| 4 |
#include<bits/stdc++.h>
using namespace std ;
int main(){
int n,m;
cin>>n>>m;
printf("%d",(n*3+m)/2);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int cst = 8000100;
long long int INF = 1e18, n, m, k, p, sum, a[500100], b[500100], c[500100],
d[500100], f[500100], s, x, y, z, mx = -INF, mn = INF, ans, ind;
bool marked[1010][1010];
double PI = 3.1415926535897932384626433832795;
char ch[100][100], chr;
struct time1 {
int val1, val2, pos1, pos2, pos3;
double price;
} arr[500100];
bool cmp(time1 x, time1 y) { return x.val1 < y.val2; }
long long int min(long long int a, long long int b) {
if (a < b) {
return a;
} else {
return b;
}
}
long long int max(long long int a, long long int b) {
if (a > b) {
return a;
} else {
return b;
}
}
vector<pair<long long int, long long int>> vec(500100);
long long int bin_search(long long int val) {
int l = 0;
int r = z;
while (r - l > 10) {
int sr = l + (r - l) / 2;
if (vec[sr].second < val) {
l = sr;
} else {
r = sr;
}
}
for (int i = r; i >= l; i--) {
if (vec[i].second <= val) {
return vec[i].first;
}
}
return 0;
}
vector<int> v;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
k = 1;
for (int i = 0; i < n; i++) {
for (int j = k; j < a[i]; j++) {
if (m - j >= 0) {
v.push_back(j);
m -= j;
} else {
break;
}
}
k = a[i] + 1;
if (m - k < 0) {
break;
}
}
while (m - k >= 0) {
v.push_back(k);
m -= k;
k++;
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string w;
int k, q, s, n;
int l, r;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> w >> k;
n = w.size();
for (int i = 0; i < n; i++) {
if (w[i] == '?') q++;
if (w[i] == '*') s++;
}
l = n - 2 * (q + s);
if (s > 0)
r = 200;
else
r = n - (q + s);
if (!(k >= l and k <= r)) {
cout << "Impossible \n";
return 0;
}
int extra = k - (n - q - s);
if (extra >= 0) {
bool check = false;
for (int i = 0; i < n; i++) {
if (w[i] != '*' and w[i] != '?') cout << w[i];
if (i == n - 1) continue;
if (w[i + 1] == '*' and check == false) {
for (int j = 0; j < extra; j++) cout << w[i];
check = true;
}
}
cout << "\n";
return 0;
} else {
int am = -1 * extra;
for (int i = 0; i < n; i++) {
if (w[i] != '*' and w[i] != '?') {
if (i == n - 1) {
cout << w[i];
continue;
}
if ((w[i + 1] == '?' or w[i + 1] == '*') and am > 0) {
am--;
continue;
} else
cout << w[i];
}
}
cout << "\n";
return 0;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool g(pair<long long, long long> a, pair<long long, long long> b) {
return a.first < b.first;
}
int main() {
int n;
cin >> n;
vector<pair<long long, long long> > v(n);
for (int i = 0; i < n; i++) cin >> v[i].first;
for (int i = 0; i < n; i++) cin >> v[i].second;
sort(v.begin(), v.end(), g);
long long b = 0;
for (int i = 0; i < n - 1; i++) {
if (v[i].first == v[i + 1].first) {
b += v[i].second + v[i + 1].second;
for (int j = 0; j < i; j++) {
if (((v[j].first ^ v[i].first) & v[j].first) == 0) {
b += v[j].second;
v[j].second = 0;
}
}
v[i].second = 0;
v[i + 1].second = 0;
}
}
cout << b;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const double pi = acos(-1.0);
const double eps = 1e-9;
int n, a[200100], m[200100];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; i--) m[i] = max(m[i + 1], a[i]);
for (int i = 1; i <= n; i++) {
if (a[i] <= m[i + 1])
cout << m[i + 1] - a[i] + 1 << " ";
else
cout << 0 << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cmpD(double x, double y = 0, double tol = 1e-9) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
struct pt {
double x, y;
pt(double x = 0, double y = 0) : x(x), y(y) {}
pt operator+(pt p) { return pt(x + p.x, y + p.y); }
pt operator-(pt p) { return pt(x - p.x, y - p.y); }
pt operator*(double c) { return pt(x * c, y * c); }
pt operator/(double c) { return pt(x / c, y / c); }
double operator*(pt p) { return x * p.x + y * p.y; }
double operator%(pt p) { return x * p.y - y * p.x; }
int cmp(pt p) const {
int t = cmpD(x, p.x);
return t ? t : cmpD(y, p.y);
}
bool operator<(const pt &p) const { return cmp(p) < 0; }
bool operator<=(const pt &p) const { return cmp(p) <= 0; }
int prod(pt p, pt q) { return cmpD((*this - p) % (q - p)); }
};
pt pivot;
pt rotate(pt a, double teta, pt o = pt()) {
a = a - o;
pt cis(cos(teta), sin(teta));
return o + pt(a.x * cis.x - a.y * cis.y, a.x * cis.y + a.y * cis.x);
}
bool radial_lt(pt p, pt q) {
pt P = p - pivot, Q = q - pivot;
double R = P % Q;
if (cmpD(R)) return cmpD(R) > 0;
return cmpD(P * P, Q * Q) > 0;
}
vector<pt> convex_hull(vector<pt> T) {
int i, j = 0, k, n = T.size();
vector<pt> U(n);
pivot = *min_element((T).begin(), (T).end());
sort((T).begin(), (T).end(), radial_lt);
for (k = n - 2; k >= 0 && T[0].prod(T[k], T[n - 1]) == 0; k--)
;
reverse((k + 1) + (T).begin(), (T).end());
for (i = 0; i < n; i++) {
while (j > 1 && T[i].prod(U[j - 1], U[j - 2]) <= 0) j--;
U[j++] = T[i];
}
U.erase(j + (U).begin(), (U).end());
return U;
}
double poly_area(vector<pt> T) {
double s = 0;
int i, n = T.size();
for (i = 0; i < n; i++) s += T[i] % T[(i + 1) % n];
return s / 2;
}
double yline(pt a, pt b, double x) {
if (cmpD(x, b.x) == 0) return b.y;
return b.y + (x - b.x) * (a.y - b.y) / (a.x - b.x);
}
double xline(pt a, pt b, double y) {
if (cmpD(y, b.y) == 0) return b.x;
return b.x + (y - b.y) * (a.x - b.x) / (a.y - b.y);
}
vector<pt> A(4), B(4), inter;
int main() {
double h, w, teta, x, y;
int i, j;
scanf("%lf %lf %lf", &w, &h, &teta);
if (cmpD(teta) == 0 || cmpD(teta, 180) == 0) {
printf("%.9lf\n", w * h);
return 0;
}
teta = teta * acos(0) / 90;
A[0] = pt(-w / 2, -h / 2);
A[1] = pt(w / 2, -h / 2);
A[2] = pt(w / 2, h / 2);
A[3] = pt(-w / 2, h / 2);
for (i = 0; i < 4; i++) B[i] = rotate(A[i], teta);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
if (i % 2 == 0) {
x = xline(B[j], B[(j + 1) % 4], A[i].y);
if (cmpD(x, min(A[i].x, A[(i + 1) % 4].x)) < 0) continue;
if (cmpD(x, max(A[i].x, A[(i + 1) % 4].x)) > 0) continue;
inter.push_back(pt(x, A[i].y));
} else {
y = yline(B[j], B[(j + 1) % 4], A[i].x);
if (cmpD(y, min(A[i].y, A[(i + 1) % 4].y)) < 0) continue;
if (cmpD(y, max(A[i].y, A[(i + 1) % 4].y)) > 0) continue;
inter.push_back(pt(A[i].x, y));
}
}
printf("%.9lf\n", poly_area(convex_hull(inter)));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long bin_pow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res *= a;
a *= a;
b >>= 1;
}
return res;
}
int main() {
int n;
cin >> n;
if (n & 1) {
vector<int> v;
v.push_back(3);
n -= 3;
while (n != 0) {
v.push_back(2);
n -= 2;
}
cout << v.size() << endl;
for (auto i : v) cout << i << " ";
} else {
vector<int> v;
while (n != 0) {
v.push_back(2);
n -= 2;
}
cout << v.size() << endl;
for (auto i : v) cout << i << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;
// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//
const int MAX = 200010;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;
int32_t main() {
// sws;
int t;
cin >> t;
while(t--) {
int n, l, r;
cin >> n >> l >> r;
ll res = 0;
multiset<int> sl, sr;
forn(i, l) {
int val;
cin >> val;
sl.insert(val);
}
forn(i, r) {
int val;
cin >> val;
sr.insert(val);
}
// matching
multiset<int> tmp;
for(auto sock : sl) {
if(sr.find(sock) != sr.end()) {
sr.erase(sr.find(sock));
}
else {
tmp.insert(sock);
}
}
swap(tmp, sl);
// swap
if(sl.size() > sr.size()) swap(sl, sr);
forne(i, 1, n) {
while(sr.find(i) != sr.end() and sr.size() > sl.size()) {
sr.erase(sr.find(i));
if(sr.find(i) == sr.end()) {
sr.insert(i);
break;
}
else {
res++;
sr.erase(sr.find(i));
}
}
}
res += min(sr.size(), sl.size());
int dif = abs(int(sr.size())-int(sl.size()));
res += dif;
// cout << "sr = " << sr.size() << " sl = " << sl.size() << endl;
cout << res << endl;
}
// int overflow, array bounds special cases (n=1?)
// do smth instead of nothing and stay organized
// WRITE STUFF DOWN DON'T GET STUCK ON ONE APPROACH
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[100][100], n, m, l = 100, r = -1, t = 100, d = -1, s = 1, t1 = 0, t2;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string st1;
cin >> st1;
for (int j = 0; j < m; j++) {
if (st1[j] == 'W')
arr[i][j] = 0;
else {
t1++;
arr[i][j] = 1;
if (i > d) d = i;
if (i < t) t = i;
if (j < l) l = j;
if (j > r) r = j;
}
}
}
if (t1 < 1) {
cout << 1;
return 0;
}
if (r - l > d - t) {
s = r - l + 1;
t2 = s - (d - t + 1);
if (t + n - 1 - d >= t2) {
cout << s * s - t1;
return 0;
} else {
cout << -1;
return 0;
}
} else {
s = d - t + 1;
t2 = s - (r - l + 1);
if (l + m - 1 - r >= t2) {
cout << s * s - t1;
return 0;
} else {
cout << -1;
return 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char c;
set<char> color_set;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> c;
color_set.insert(c);
}
}
if (color_set.size() > 3) {
cout << "#Color" << endl;
} else {
if (color_set.find('C') != color_set.end() ||
color_set.find('M') != color_set.end() ||
color_set.find('Y') != color_set.end()) {
cout << "#Color" << endl;
} else {
cout << "#Black&White" << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, swl, sws;
int p[2001], s[2001], pi[2001], si[2001], sh[2001];
short sw[4000000][2];
void swap(int i, int j) {
int x = p[i], y = p[j];
p[i] = y;
pi[y] = i;
sh[i] = si[p[i]] - i;
p[j] = x;
pi[x] = j;
sh[j] = si[p[j]] - j;
sw[swl][0] = i;
sw[swl][1] = j;
++swl;
sws += abs(i - j);
}
int main(int argn, char* args[]) {
int i, j;
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> p[i];
pi[p[i]] = i;
}
for (i = 1; i <= n; ++i) {
cin >> s[i];
si[s[i]] = i;
}
for (i = 1; i <= n; ++i) {
if (p[i] == s[i])
sh[i] = 0;
else
sh[i] = si[p[i]] - i;
}
swl = sws = 0;
multimap<int, int> ix;
for (i = 1; i <= n; ++i)
if (sh[i] != 0) ix.insert(make_pair(abs(sh[i]), i));
for (multimap<int, int>::reverse_iterator ixi = ix.rbegin(); ixi != ix.rend();
++ixi) {
i = (*ixi).second;
if ((*ixi).first > abs(sh[i])) {
continue;
}
if (sh[i] > 0) {
for (j = i + 1; j <= i + sh[i]; ++j) {
if (sh[j] < 0 && abs(sh[j]) >= j - i) {
swap(i, j);
if (sh[i] != 0) ix.insert(make_pair(abs(sh[i]), i));
if (sh[j] != 0) ix.insert(make_pair(abs(sh[j]), j));
}
}
} else {
for (j = i - 1; j >= i + sh[i]; --j) {
if (sh[j] > 0 && abs(sh[j]) >= i - j) {
swap(i, j);
if (sh[i] != 0) ix.insert(make_pair(abs(sh[i]), i));
if (sh[j] != 0) ix.insert(make_pair(abs(sh[j]), j));
}
}
}
}
cout << sws << endl;
cout << swl << endl;
for (i = 0; i < swl; ++i) cout << sw[i][0] << " " << sw[i][1] << endl;
return 0;
}
| 5 |
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define dd double
#define ld long double
#define sl(n) scanf("%lld", &n)
#define si(n) scanf("%d", &n)
#define sd(n) scanf("%lf", &n)
#define pll pair <ll, ll>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define inf (1LL << 61)
#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc)
#define for1(i, stop) for(ll i = 1; i <= stop; ++i)
#define for0(i, stop) for(ll i = 0; i < stop; ++i)
#define rep1(i, start) for(ll i = start; i >= 1; --i)
#define rep0(i, start) for(ll i = (start-1); i >= 0; --i)
#define ms(n, i) memset(n, i, sizeof(n))
#define casep(n) printf("Case %lld:", ++n)
#define pn printf("\n")
#define pf printf
#define EL '\n'
#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
// === Debug macro starts here ===
#ifndef ONLINE_JUDGE
#define DEBUG
#define SYS_COL system("COLOR")
#endif
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; SYS_COL; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os, const Cont& v){
os<<"[";
for(auto& x:v){os<<x<<", ";}
return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p){
return os<<"{"<<p.first<<", "<<p.second<<"}";
}
#else
#define dbg(x)
#endif
// === Debug macro ends here ===
#define ff first
#define ss second
const ll sz = 3e5 + 10, mod = 998244353;
vector <ll> dp[sz], way[sz];
vector <char> mat[sz];
ll n, m;
ll way1(ll r, ll c)
{
if(r > n)
return 1;
ll &ret = way[r][c];
if(ret != -1)
return ret;
if(c == m) {
if(mat[r][c] != 'o')
return ret = way1(r+1, 1);
return ret = (2*way1(r+1, 1)) % mod;
}
ret = way1(r, c+1);
if(mat[r][c] == 'o') ret = (2*ret) % mod;
return ret;
}
ll way2(ll r, ll c)
{
if(c > m)
return 1;
ll &ret = way[r][c];
if(ret != -1)
return ret;
if(r == n) {
if(mat[r][c] != 'o')
return ret = way2(1, c+1);
return ret = (2*way2(1, c+1)) % mod;
}
ret = way2(r+1, c);
if(mat[r][c] == 'o') ret = (2*ret) % mod;
return ret;
}
ll solve1(ll r, ll c)
{
if(r > n)
return 0;
ll &ret = dp[r][c];
if(ret != -1)
return ret;
ret = 0;
if(c == m) {
ret = solve1(r+1, 1);
if(mat[r][c] == 'o')
ret = 2*ret;
if(ret >= mod) ret -= mod;
return ret;
}
if(mat[r][c] == 'o') ret = solve1(r, c+1);
if(mat[r][c] != 'o' || mat[r][c+1] != 'o') {
ret += solve1(r, c+1);
if(ret >= mod) ret -= mod;
return ret;
}
if(c+1 == m) {
ret += way1(r+1, 1) +solve1(r+1, 1);
ret += solve1(r+1, 1);
}
else {
ret += way1(r,c+2) + solve1(r, c+2);
ret += solve1(r, c+2);
}
ret %= mod;
//cerr << r << " " << c << " || " << ret << endl;
return ret;
}
ll solve2(ll r, ll c)
{
if(c > m)
return 0;
ll &ret = dp[r][c];
if(ret != -1)
return ret;
ret = 0;
if(r == n) {
ret = solve2(1, c+1);
if(mat[r][c] == 'o')
ret = 2*ret;
if(ret >= mod) ret -= mod;
return ret;
}
if(mat[r][c] == 'o') ret = solve2(r+1, c);
if(mat[r][c] != 'o' || mat[r+1][c] != 'o') {
ret += solve2(r+1, c);
if(ret >= mod) ret -= mod;
return ret;
}
if(r+1==n) {
ret += way2(1, c+1) + solve2(1, c+1);
ret += solve2(1, c+1);
}
else {
ret += way2(r+2, c) + solve2(r+2, c);
ret += solve2(r+2, c);
}
ret %= mod;
return ret;
}
int main()
{
cin >> n >> m;
for1(i, n) {
dp[i].resize(m+2, -1);
way[i].resize(m+2, -1);
mat[i].resize(m+2);
}
for1(i, n) {
for1(j, m) {
scanf(" %c", &mat[i][j]);
}
}
ll ans1 = solve1(1, 1);
//dbg(ans1);
for1(i, n)
for1(j, m)
dp[i][j] = way[i][j] = -1;
ll ans2 = solve2(1, 1);
//dbg(ans2);
cout << (ans1+ans2) % mod << EL;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, pair<long long, long long> > f;
void get(long long z, long long &x, long long &y) {
long long base = 1;
y = 0;
while (z >= 10) {
y += (z % 10) * base;
z /= 10;
base *= 10;
}
x = z;
}
pair<long long, long long> dfs(long long now, long long digit) {
if (f.find(make_pair(now, digit)) != f.end()) return f[make_pair(now, digit)];
if (now <= 0) return make_pair(0, 0);
pair<long long, long long> key = make_pair(now, digit);
pair<long long, long long> ans = make_pair(0, 0);
while (now >= 10) {
long long x, y;
get(now, x, y);
pair<long long, long long> add = dfs(y, max(x, digit));
ans.first += add.first;
now -= y;
if (add.second > 0)
now -= add.second;
else {
now -= max(x, digit);
ans.first++;
}
}
if (now != 0) {
ans.first++;
if (digit > now) ans.second = digit - now;
}
f[key] = ans;
return ans;
}
int main() {
f.clear();
long long s;
cin >> s;
cout << dfs(s, 0).first << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<pair<int, int>> v;
vector<pair<int, int>> c;
int n, m;
cin>>n>>m;
while(n--){
int x,y;
cin>>x>>y;
v.emplace_back(x,y);
}
while(m--){
int x,y;
cin>>x>>y;
c.emplace_back(x,y);
}
for(auto& u:v){
int min=1000000000;
int count=0,t;
for(auto& p:c){
count++;
int x=abs(u.first-p.first)+abs(u.second-p.second);
if(x<min){
min=x;
t=count;
}
}
cout<<t<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long a[n + 1];
for (int i = 0; i < n; i++) cin >> a[i];
long long x, f, curr, count = 0;
cin >> x >> f;
for (int i = 0; i < n; i++) {
curr = a[i];
if (curr >= x + f) {
a[n] = curr / (x + f);
curr = curr % (x + f);
count += (a[n]) * f;
}
if (curr > x) count += f;
}
cout << count << endl;
return 0;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define MAX_N 15
#define MAX_M 100005
int n,d,x;
int p[MAX_N][MAX_N];
int dp[MAX_N];
int DP[MAX_N][MAX_M];
int t[MAX_N],u[MAX_N];
void solve(int v,int x){
for(int i=0;i<n;i++){
t[i]=p[v][i]-p[v-1][i];
u[i]=p[v-1][i];
}
for(int i=0;i<n;i++){
for(int j=0;j<=x;j++){
if(i)DP[i][j]=DP[i-1][j];
else DP[i][j]=0;
if(j-u[i]>=0)
DP[i][j]=max(DP[i][j],DP[i][j-u[i]]+t[i]);
}
}
}
int main(){
cin>>n>>d>>x;
dp[0]=x;
for(int i=0;i<d;i++){
for(int j=0;j<n;j++)cin>>p[i][j];
if(i==0)continue;
solve(i,dp[i-1]);
dp[i]=dp[i-1]+DP[n-1][dp[i-1]];
}
cout<<dp[d-1]<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 100005;
int n, a[mn];
int main() {
n = ({
int t;
scanf("%d", &t);
t;
});
for (int i = 0; i < n; i++)
a[i] = ({
int t;
scanf("%d", &t);
t;
});
sort(a, a + n);
bool ans = 1;
int p1 = 0, p2 = 0;
while (p1 < n) {
p2 = p1 + 1;
while (p2 < n && a[p2] == a[p1]) p2++;
if (n - p2 < p2 - p1) {
ans = 0;
goto finish;
}
if (a[p2] != a[p1] + 1) {
ans = 0;
goto finish;
}
if (2 * p2 - p1 < n && a[2 * p2 - p1] != a[p2]) {
ans = 0;
goto finish;
}
if (2 * p2 - p1 == n && a[2 * p2 - p1 - 1] != a[p2]) {
ans = 0;
goto finish;
}
p1 = 2 * p2 - p1;
}
finish:;
if (ans)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
const int M = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
int t;
int n, q;
string str;
int l, r;
int a[N];
int main() {
cin >> t;
while (t--) {
memset(a, 0, sizeof(a));
cin >> n >> q >> str;
for (int i = 0; i < n; i++) {
if (str[i] == '+') {
if (i & 1)
a[i + 1] = a[i] + 1;
else
a[i + 1] = a[i] - 1;
} else {
if (i & 1)
a[i + 1] = a[i] - 1;
else
a[i + 1] = a[i] + 1;
}
}
while (q--) {
cin >> l >> r;
int temp = a[r] - a[l - 1];
if (temp == 0)
cout << 0 << '\n';
else if (temp & 1)
cout << 1 << '\n';
else
cout << 2 << '\n';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long a, b, c;
long long mygcd(long long x, long long y) {
long long t;
while (y != 0) t = y, y = x % y, x = t;
return x;
}
int main() {
scanf("%d", &n);
while (n--) {
scanf("%lld%lld%lld", &a, &b, &c);
long long g = mygcd(a, b);
a /= g;
b /= g;
while (b != 1 && (c = mygcd(b, c)) != 1) b /= c;
if (b != 1)
puts("Infinite");
else
puts("Finite");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, x, y;
cin >> n >> m >> x >> y;
--x, --y;
vector<vector<pair<long long, long long>>> E(n);
for (int j = 0; j < m; ++j) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
E[u].push_back(make_pair(v, w));
E[v].push_back(make_pair(u, w));
}
vector<int> t(n), c(n);
for (int i = 0; i < n; ++i) cin >> t[i] >> c[i];
vector<vector<long long>> dist(n, vector<long long>(n, LINF));
for (int second = 0; second < n; ++second) {
dist[second][second] = 0;
priority_queue<pair<long long, long long>,
vector<pair<long long, long long>>,
greater<pair<long long, long long>>>
pq;
pq.push(make_pair(0, second));
while (!pq.empty()) {
pair<long long, long long> front = pq.top();
pq.pop();
long long d = front.first, u = front.second;
if (d > dist[second][u]) continue;
for (auto [v, w] : E[u])
if (dist[second][u] + w < dist[second][v]) {
dist[second][v] = dist[second][u] + w;
pq.push(make_pair(dist[second][v], v));
}
}
}
vector<vector<pair<long long, long long>>> E_cost(n);
for (int u = 0; u < n; ++u)
for (int v = 0; v < n; ++v)
if (u != v && dist[u][v] <= t[u]) E_cost[u].push_back(make_pair(v, c[u]));
vector<long long> cost(n, LINF);
cost[x] = 0;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
greater<pair<long long, long long>>>
pq;
pq.push(make_pair(0, x));
while (!pq.empty()) {
pair<long long, long long> front = pq.top();
pq.pop();
long long d = front.first, u = front.second;
if (d > cost[u]) continue;
for (auto [v, w] : E_cost[u])
if (cost[u] + w < cost[v]) {
cost[v] = cost[u] + w;
pq.push(make_pair(cost[v], v));
}
}
if (cost[y] == LINF)
cout << -1 << '\n';
else
cout << cost[y] << '\n';
exit(0);
}
| 4 |
#include "bits/stdc++.h"
using namespace std;
int main(){
double a,b;
cin>>a>>b;
cout<<fixed<<setprecision(5)<<a*b/3.305785<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
class Graph {
int V;
list<int>* adj;
bool isCyclicUtil(int v, bool visited[], int parent);
public:
Graph(int V);
void addEdge(int v, int w);
bool isCyclic();
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w) {
adj[v].push_back(w);
adj[w].push_back(v);
}
bool Graph::isCyclicUtil(int v, bool visited[], int parent) {
visited[v] = true;
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i) {
if (!visited[*i]) {
if (isCyclicUtil(*i, visited, v)) return true;
} else if (*i != parent)
return true;
}
return false;
}
bool Graph::isCyclic() {
bool* visited = new bool[V];
for (int i = 0; i < V; i++) visited[i] = false;
for (int u = 0; u < V; u++)
if (!visited[u])
if (isCyclicUtil(u, visited, -1)) return true;
return false;
}
int main() {
long long int n, m;
cin >> n >> m;
map<char, long long int> mm;
char ma[n][m];
for (long long int il = 0; il < n; il++)
for (long long int jl = 0; jl < m; jl++)
cin >> ma[il][jl], mm[ma[il][jl]]++;
map<char, long long int>::iterator it;
for (it = mm.begin(); it != mm.end(); it++) {
char c = (*it).first;
long long int sz = (*it).second;
Graph g(m * n);
if (sz < 4) continue;
set<pair<long long int, long long int> > s;
for (long long int il = 0; il < n; il++)
for (long long int jl = 0; jl < m; jl++) {
long long int x = (il * m + jl), y;
if (ma[il][jl] == c) {
if (il > 0 && ma[il - 1][jl] == c) {
y = (il - 1) * m + jl;
s.insert(make_pair(min(x, y), max(x, y)));
}
if (jl > 0 && ma[il][jl - 1] == c) {
y = (il)*m + jl - 1;
s.insert(make_pair(min(x, y), max(x, y)));
}
if (il < n - 1 && ma[il + 1][jl] == c) {
y = (il + 1) * m + jl;
s.insert(make_pair(min(x, y), max(x, y)));
}
if (jl < m - 1 && ma[il][jl + 1] == c) {
y = (il)*m + jl + 1;
s.insert(make_pair(min(x, y), max(x, y)));
}
}
}
set<pair<long long int, long long int> >::iterator il;
for (il = s.begin(); il != s.end(); il++)
g.addEdge((*il).first, (*il).second);
if (g.isCyclic()) {
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main() {
double a[100],n;
cin>>n;
double sum=0,m;
for(int i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
sum/=n;
for(int k=0;k<n;k++){
a[k]=abs(a[k]-sum);
}
m=a[0];
int ans=0;
for(int l=1;l<n;l++){
if(m>a[l]){
m=a[l];
ans=l;
}
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
long long int n, m, k;
vector<long long int> a, b;
while (t--) {
cin >> n >> m;
cout << n << " " << n * 2 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string s, t;
int dp[30];
int main() {
cin >> s >> t;
memset(dp, -1, sizeof(dp));
int i, l = 0, ma = 0;
for (i = 0; i < s.size(); ++i) {
if (s[i] == t[ma]) dp[s[i] - 'a'] = ++ma;
if (s[i] == t[l]) ++l;
if (dp[s[i] - 'a'] < l) l = dp[s[i] - 'a'];
if (l < 0) break;
}
int lim = t.size();
if (l < lim)
cout << "No\n";
else
cout << "Yes\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
if (a % b == 0) {
cout << 0 << endl;
} else {
cout << b - a % b << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x, y;
long long k, round, remainder;
cin >> n >> m >> k >> x >> y;
vector<vector<long long>> count(n, vector<long long>(m, 0));
if (n == 1) {
round = k / m;
remainder = k % m;
} else {
round = k / ((n + n - 2) * m);
remainder = k % ((n + n - 2) * m);
}
for (int c = 0; c < m; c++) {
count[0][c] = round;
count[n - 1][c] = round;
}
for (int r = 1; r < n - 1; r++)
for (int c = 0; c < m; c++) count[r][c] = round * 2;
if (remainder) {
for (int i = 0; i < n; i++) {
if (remainder) {
for (int j = 0; j < m; j++) {
if (remainder) {
count[i][j]++;
remainder--;
} else
break;
}
} else
break;
}
}
if (remainder) {
for (int i = n - 2; i >= 0; i--) {
if (remainder) {
for (int j = 0; j < m; j++) {
if (remainder) {
count[i][j]++;
remainder--;
} else
break;
}
} else
break;
}
}
long long maximum = count[0][0], minimum = count[0][0];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (count[i][j] > maximum) maximum = count[i][j];
if (count[i][j] < minimum) minimum = count[i][j];
}
cout << maximum << " " << minimum << " " << count[x - 1][y - 1] << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long IN() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void OUT(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
OUT(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
int main() {
int a = IN(), b = IN(), cnt;
char c[29];
cnt = sprintf(c, "%d", b);
reverse(c, c + cnt);
sscanf(c, "%d", &b);
OUT(a + b);
}
| 1 |
#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>
using namespace std;
typedef long long ll;
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(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll 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
#define MAX_N 100100
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; }
vector<vector<bool>> cht(string s) {
vector<vector<bool>> res;
vector<bool> buf;
rep(i, s.size()) {
if (s[i] == 'b')buf.push_back(1);
else if (s[i] == '/') {
res.push_back(buf);
buf.clear();
}
else {
rep(j, s[i] - '0')buf.push_back(0);
}
}
res.push_back(buf);
return res;
}
string chs(vector<vector<bool>> t) {
string res;
rep(i, t.size()) {
ll cnt = 0;
rep(j, t[i].size()) {
if (t[i][j]) {
string ss = "0";
if (cnt != 0) {
ss[0] += cnt;
res += ss;
}
res += "b";
cnt = 0;
}
else cnt++;
}
string ss = "0";
if (cnt != 0) {
ss[0] += cnt;
res += ss;
}
if (i != t.size() - 1)res += "/";
}
return res;
}
string s;
int main() {
while (cin >> s&&s != "#") {
int a, b, c, d;
cin >> a >> b >> c >> d;
a--;
b--;
c--;
d--;
vector<vector<bool>> tt = cht(s);
tt[a][b] = 0;
tt[c][d] = 1;
cout << chs(tt) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long brocheck(string s1, long long n, vector<vector<long long> >& v,
vector<vector<long long> >& v1) {
if (s1[0] == s1[1]) {
if (v[n - 1][s1[0] - 'a'] >= 2) {
long long x;
x = v[n - 1][s1[0] - 'a'] * (v[n - 1][s1[0] - 'a'] - 1);
x = x / 2;
return x;
} else
return 0;
} else {
long long sum = 0;
if (v[n - 1][s1[0] - 'a'] > 0 && v[n - 1][s1[1] - 'a'] > 0) {
for (long long i = 0; i < v[n - 1][s1[0] - 'a']; i++) {
sum =
sum + (v[n - 1][s1[1] - 'a'] - v[v1[s1[0] - 'a'][i]][s1[1] - 'a']);
}
return sum;
} else
return 0;
}
}
int main() {
int t = 1;
while (t--) {
string s;
cin >> s;
long long n = s.size();
if (n == 1) {
cout << 1;
return 0;
}
vector<vector<long long> > v(s.size(), vector<long long>(26, 0));
vector<vector<long long> > v1(26);
v[0][s[0] - 'a'] = 1;
v1[s[0] - 'a'].push_back(0);
for (int i = 1; i < s.size(); i++) {
for (int j = 0; j < 26; j++) {
v[i][j] = v[i - 1][j];
}
v[i][s[i] - 'a'] = v[i - 1][s[i] - 'a'] + 1;
v1[s[i] - 'a'].push_back(i);
}
map<string, long long> m;
long long ans = -2;
for (char i = 'a'; i <= 'z'; i++) {
for (char j = 'a'; j <= 'z'; j++) {
string s1 = "ab";
s1[0] = i;
s1[1] = j;
m[s1] = brocheck(s1, n, v, v1);
if (m[s1] > ans) ans = m[s1];
}
}
for (int i = 0; i < 26; i++) {
if (v[n - 1][i] > ans) ans = v[n - 1][i];
}
cout << ans;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a,b=0;
cin>>a;
vector<long long int> vec(a);
for(long long int i=0;i<a;i++){
cin>>vec.at(i);
}
for(long long int i=0;i<a-1;i++){
if(vec.at(i)>=vec.at(i+1)){
b++;
}
}
cout<<b+1<<endl;
cout<<a<<endl;
}
| 0 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
int M[1000];
while(true){
int N, Q, d, ans;
cin >> N >> Q;
if(N == 0 && Q == 0){
break;
}
for(int i = 0; i < 1000; i++){
M[i] = 0;
}
int num;
for(int loop = 0; loop < N; loop++){
cin >> num;
for(int i = 0; i < num; i++){
cin >> d;
M[d]++;
}
}
ans = 0;
for(int i = 0; i < 1000; i++){
if(M[i] >= Q){
ans = i;
Q = M[i] + 1;
}
}
printf("%d\n", ans);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = ((int)1e6) + 5;
int tree[maxn];
int read(int idx) {
int sum = 0;
while (idx > 0) {
sum = (sum + tree[idx]) % ((int)1e9 + 7);
idx -= (idx & -idx);
}
return sum;
}
void update(int idx, int val) {
if (val < 0) val += ((int)1e9 + 7);
while (idx < maxn) {
tree[idx] = (tree[idx] + val) % ((int)1e9 + 7);
idx += (idx & -idx);
}
}
vector<int> arr;
int readSingle(int idx) {
int sum = tree[idx] % ((int)1e9 + 7);
if (idx > 0) {
int z = idx - (idx & -idx);
idx--;
while (idx != z) {
sum = (sum - tree[idx] + ((int)1e9 + 7)) % ((int)1e9 + 7);
idx -= (idx & -idx);
}
}
return sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
arr.resize(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
long long int sum = read(arr[i]);
sum++;
int newVal = arr[i] * 1ll * sum % ((int)1e9 + 7);
int arri = readSingle(arr[i]);
update(arr[i], -arri);
update(arr[i], newVal);
}
cout << read(maxn - 1);
return 0;
}
| 5 |
#include "bits/stdc++.h"
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int64_t a = K*pow(K-1,N-1);
cout << a;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int n, m, i;
void fm(void) {}
long long A[100001];
map<string, int> v;
int main() {
scanf("%lld", &n);
long long a[n + 1];
scanf("%lld", &a[0]);
for (i = 1; i < n; i++) {
scanf("%lld", &m);
a[i] = m + a[i - 1];
}
long long p = a[n - 1] / 2;
if (a[n - 1] % 2) p++;
for (i = 0; i < n; i++) {
if (a[i] >= p) {
printf("%lld\n", i + 1);
return 0;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long n, resp = INT_MAX;
cin >> n;
long long acc = 0;
vector<long long> vec(n);
for (auto &x : vec) cin >> x;
acc = -vec[0], resp = min(resp, acc);
for (long long i = 1; i < n; i++) {
acc += (vec[i - 1] - vec[i]);
resp = min(resp, acc);
}
cout << abs(resp);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{int64_t A, V, B, W, T; cin >> A >> V >> B >> W >> T;
puts(V > W and (abs(A - B) + T - 1) / T <= V - W ? "YES" : "NO");} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
long long mod = 1000000 + 3;
long long binpow(long long a, int n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n /= 2;
}
return res;
}
int a[1005][1005];
int main() {
int q;
scanf("%d", &q);
for (int tt = 0; tt < q; tt++) {
int h, w;
scanf("%d%d", &h, &w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) scanf("%d", &a[i][j]);
}
bool bad = 0;
for (int i = 1; i < h; i++) {
int sum = 0, cnt1 = 0, cnt2 = 0, cnt0 = 0, more = 0;
for (int j = 0; j < w; j++) {
sum += abs(a[i][j] - a[i - 1][j]);
cnt0 += abs(a[i][j] - a[i - 1][j]) < 7;
cnt1 += abs(a[i][j] - a[i - 1][j]) < 10;
cnt2 += abs(a[i][j] - a[i - 1][j]) < 20;
if (abs(a[i][j] - a[i - 1][j]) < abs(a[0][j] - a[h - 1][j]) ||
abs(a[0][j] - a[h - 1][j]) > 10)
continue;
else
more++;
}
if ((cnt1 > 15 || cnt2 > 50) && (more < 50 || cnt1 > 50))
continue;
else {
bad = 1;
break;
}
}
if (bad)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 100000000 + 5;
const int mod = 1000000007;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k, s;
cin >> n >> k >> s;
long long tmp = n - 1;
if (k * tmp < s || k > s)
cout << "NO" << endl;
else {
long long y = s / k;
long long x = y * k;
long long tmp = s - x;
long long now = 1;
cout << "YES" << endl;
for (int i = 0; i < tmp; i++) {
if (now + y + 1 <= n) {
cout << now + y + 1 << " ";
now = now + y + 1;
} else {
cout << now - y - 1 << " ";
now = now - y - 1;
}
}
for (int i = 0; i < k - tmp; i++) {
if (now + y <= n) {
cout << now + y << " ";
now = now + y;
} else {
cout << now - y << " ";
now = now - y;
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int ML = 200100;
int op[ML];
double s[ML], S[ML], p[ML], f[2][ML];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
s[i] = s[i - 1] + x;
p[i] = p[i - 1] + (double)(1.0000000000 / x);
S[i] = S[i - 1] + (double)(s[i] / x);
}
for (int kk = 1; kk <= k; kk++) {
int no = kk & 1, la = !(kk & 1);
op[0] = kk - 1;
for (int i = kk, H = 0, T = 1; i <= n; i++) {
if (kk - 1)
for (;
H + 1 < T && (p[i]) * ((s[op[H]]) - (s[op[H + 1]])) <
((f[la][op[H]] - S[op[H]] + s[op[H]] * p[op[H]]) -
(f[la][op[H + 1]] - S[op[H + 1]] +
s[op[H + 1]] * p[op[H + 1]]));
H++)
;
int j = op[H];
f[no][i] = f[la][j] + S[i] - S[j] - s[j] * (p[i] - p[j]);
for (; H + 1 < T &&
((f[la][op[T - 2]] - S[op[T - 2]] + s[op[T - 2]] * p[op[T - 2]]) -
(f[la][op[T - 1]] - S[op[T - 1]] + s[op[T - 1]] * p[op[T - 1]])) *
((s[op[T - 1]]) - (s[i])) >
((f[la][op[T - 1]] - S[op[T - 1]] +
s[op[T - 1]] * p[op[T - 1]]) -
(f[la][i] - S[i] + s[i] * p[i])) *
((s[op[T - 2]]) - (s[op[T - 1]]));
T--)
;
op[T++] = i;
}
}
printf("%.10f", f[k & 1][n]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, pos[111111];
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &k);
pos[k] = i;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &k);
printf("%d ", pos[k]);
}
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
int n, m, e, c, d, k, l, ans;
int main() {
scanf("%d%d%d%d%d", &c, &d, &n, &m, &k);
int l = n * m - k;
while (l > 0) {
if (l - n >= 0) {
if (c > d * n)
ans += d * n;
else
ans += c;
} else {
if (c > d * l)
ans += d * l;
else
ans += c;
}
l -= n;
}
printf("%d\n", ans);
return 0;
}
| 1 |
#define NDEBUG
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
using namespace std;
#define ALL(x) begin(x), end(x)
char Letter(string::const_iterator&);
string String(string::const_iterator&);
string Cipher(string::const_iterator&);
char Letter(string::const_iterator &it) {
if(*it == '+') {
++it;
char res = Letter(it);
if(res != '?' && ++res > 'Z') res = 'A';
return res;
}
else if(*it == '-') {
++it;
char res = Letter(it);
if(res != '?' && --res < 'A') res = 'Z';
return res;
}
else if(isupper(*it) || *it == '?') {
const char res = *it;
++it;
return res;
}
assert(false);
}
string String(string::const_iterator &it) {
if(*it == '[') {
++it;
string res = Cipher(it);
assert(*it == ']');
++it;
reverse(ALL(res));
return res;
}
return string(1, Letter(it));
}
string Cipher(string::const_iterator &it) {
string res = "";
while(*it != '\0' && *it != ']') {
res += String(it);
}
return res;
}
string parse(const string &text) {
auto it = text.cbegin();
string res = Cipher(it);
assert(it == text.cend());
for(auto &e : res) {
if(e == '?') e = 'A';
}
return res;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
for(string text; cin >> text && text != ".";) {
cout << parse(text) << endl;
}
return EXIT_SUCCESS;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void Code_Crush() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
int main() {
Code_Crush();
long long t, n, k;
cin >> t;
map<long long, long long> ind;
for (long long i = 1, index = 1; i <= 2 * 1e5; i++)
ind[index] = i, index += i;
while (t-- && cin >> n >> k) {
auto p = ind.upper_bound(k);
--p;
string s(n, 'a');
s[n - (k - p->first) - 1] = s[n - p->second - 1] = 'b';
cout << s << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, m, k, mod = 1e9 + 7, ans, a[200011];
string s, s2;
bool flag, vis[2000111];
vector<long long> v, tmp;
bool okD(long long d, long long mid, long long i) {
return tmp[i] - 3 * d >= mid;
}
bool ok(long long mid) {
tmp = v;
for (long long i = n - 1; i >= 2; i--) {
long long d = tmp[i] - mid;
d = min(v[i], d);
if (d < 0) return false;
d /= 3;
tmp[i] -= d * 3;
tmp[i - 1] += d;
tmp[i - 2] += 2 * d;
}
long long ret = 1e9;
for (long long i = 0; i < n; i++) ret = min(ret, tmp[i]);
return ret >= mid;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
v.resize(n);
for (long long i = 0; i < n; i++) cin >> v[i];
long long L = 1, R = 1e9;
while (L <= R) {
long long mid = (L + R + 1) / 2;
if (ok(mid))
ans = mid, L = mid + 1;
else
R = mid - 1;
}
cout << ans << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a14;
inline int rd(int l, int r) { return rand() % (r - l + 1) + l; }
const int mxn = 2e5 + 3, mxm = mxn * 20;
int n;
char s[mxn];
long long ans = 2;
int to[mxn], nxt[mxn], fir[mxn], gn;
int dfn[mxn], dfa[mxn], dt, top[mxn], zs[mxn], siz[mxn], dep[mxn];
inline void gadd(int x, int y) { to[++gn] = y, nxt[gn] = fir[x], fir[x] = gn; }
vector<int> ga[mxn];
struct asdagw {
inline void ext(int c) {
int p = lst, np = ++tt, q, nq;
len[np] = len[p] + 1, lst = np;
for (; p && !ts[p][c]; p = fa[p]) ts[p][c] = np;
if (!p)
fa[np] = 1;
else {
q = ts[p][c];
if (len[q] == len[p] + 1)
fa[np] = q;
else {
nq = ++tt, len[nq] = len[p] + 1, fa[nq] = fa[q],
memcpy(ts[nq], ts[q], sizeof(ts[q]));
fa[q] = fa[np] = nq;
for (; ts[p][c] == q; p = fa[p]) ts[p][c] = nq;
}
}
}
int len[mxn], tt, lst, fa[mxn], ts[mxn][26], po[mxn];
inline void b1() {
tt = lst = 1;
for (int i = 1; i < n; ++i) ext(s[i] - 'a'), po[i] = lst;
for (int i = 2; i <= tt; ++i) ans += len[i] - len[fa[i]];
ext(s[n] - 'a');
for (int i = 2; i <= tt; ++i) ans += len[i] - len[fa[i]];
for (int i = 2; i <= tt; ++i) ga[fa[i]].push_back(i);
}
inline void b2() {
tt = lst = 1;
for (int i = n; i > 1; --i) ext(s[i] - 'a'), po[i] = lst;
for (int i = 2; i <= tt; ++i) ans += len[i] - len[fa[i]], gadd(fa[i], i);
}
} G, H;
inline void dfs0(int x) {
siz[x] = 1;
for (register int i = fir[x]; i; i = nxt[i]) {
dep[to[i]] = dep[x] + 1, dfs0(to[i]);
siz[x] += siz[to[i]];
if (siz[to[i]] > siz[zs[x]]) zs[x] = to[i];
}
}
inline void dfs1(int x) {
int f = H.fa[x];
dfn[x] = ++dt, dfa[dt] = x;
top[x] = x == zs[f] ? top[f] : x;
if (zs[x]) dfs1(zs[x]);
for (register int i = fir[x]; i; i = nxt[i])
if (to[i] != zs[x]) dfs1(to[i]);
}
inline int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = H.fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
inline int llca(int x, int y) {
if (!x || !y) return 0;
assert(x && y);
return H.len[lca(dfa[x], dfa[y])];
}
long long h1[mxn], h2[mxn];
set<int> hh[mxn];
inline void mer(int x, int y) {
h1[x] += h1[y];
if (hh[x].size() < hh[y].size()) swap(hh[x], hh[y]), swap(h2[x], h2[y]);
for (auto i : hh[y]) {
if (hh[x].size()) {
assert(hh[x].find(i) == hh[x].end());
set<int>::iterator p = hh[x].lower_bound(i);
int n1 = p == hh[x].end() ? 0 : *p;
int n2 = p == hh[x].begin() ? 0 : *--p;
h2[x] += llca(n1, i) + llca(n2, i) - llca(n1, n2);
} else
assert(0);
hh[x].insert(i);
}
}
inline void dfs(int x) {
for (auto y : ga[x]) dfs(y), mer(x, y);
ans += (G.len[x] - G.len[G.fa[x]]) * (h1[x] - h2[x]);
}
int main() {
a14 = scanf("%s", s + 1), n = strlen(s + 1);
G.b1(), H.b2();
dfs0(1), dfs1(1);
for (int i = 1; i <= n - 2; ++i) {
int x = G.po[i], y = H.po[i + 2];
h1[x] = H.len[y];
hh[x].insert(dfn[y]);
}
dfs(1);
printf("%lld\n", ans);
return 0;
}
| 6 |
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n + 110) / 111 * 111 << '\n';
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, t, a, b, da, db;
cin >> x >> t >> a >> b >> da >> db;
if (x == 0) {
cout << "YES" << endl;
return 0;
} else {
for (int i = 0; i < t; i++) {
if (a - (da * i) == x) {
cout << "YES" << endl;
return 0;
}
}
for (int i = 0; i < t; i++) {
if (b - (db * i) == x) {
cout << "YES" << endl;
return 0;
}
}
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
if (a - (da * i) + b - (db * j) == x) {
cout << "YES" << endl;
return 0;
}
}
}
}
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c = 0;
cin >> a >> b;
for (int i = 0; i < a * b; i++) {
char t;
cin >> t;
if (t == 'C' || t == 'M' || t == 'Y') c++;
}
if (c)
cout << "#Color\n";
else
cout << "#Black&White\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int sum = 0, count = 0;
string s;
while (getline(cin, s)) {
if (s[0] == '+')
sum++;
else if (s[0] == '-')
sum--;
else {
long long int i = 0;
while (s[i] != ':') {
i++;
}
count += (s.size() - i - 1) * sum;
}
}
cout << count << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int main() {
int n;
cin >> n;
vector<int> c(n);
vector<int> a(n);
int ans = 0, t;
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
int q = 0, q1;
while (q < n) {
q1 = q;
while (q + 1 < n && a[q] != a[q + 1]) ++q;
ans = max(ans, (q - q1) / 2);
if ((q - q1) % 2) {
t = 0;
while (q1 + t < q - t) {
c[q1 + t] = a[q1];
c[q - t] = a[q];
++t;
}
} else {
for (int i = q1; i <= q; ++i) c[i] = a[q1];
}
++q;
}
cout << ans << endl;
for (int i = 0; i < n; ++i) printf("%d ", c[i]);
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int a[100001],b[100001];
int Pow(int p,int q){
int s=1;
for(;q;q>>=1,p=1LL*p*p%mod){
if(q&1){
s=1LL*s*p%mod;
}
}
return s;
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;++i){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;++i){
b[i]=Pow(i,mod-2);
}
long long p=1,ans=a[m];
for(int i=m+1;i<=n;++i){
p=p*(i-1)%mod;
p=p*b[i-m]%mod;
ans=(ans+a[i]*p%mod)%mod;
}
ans=ans-a[n-m+1];
p=1;
for(int i=m+1;i<=n;++i){
p=p*(i-1)%mod;
p=p*b[i-m]%mod;
ans=(ans-a[n-i+1]*p%mod)%mod;
}
if(ans<0){
ans=ans+mod;
}
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long X; cin >> X;
for(long long a=-1000; a<=1000; a++){
for(long long b=-1000; b<=1000; b++){
if(a*a*a*a*a-b*b*b*b*b==X){
cout << a << " " << b << endl;
return 0;
}
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int Mod = 1000 * 1000 * 1000 + 7;
template <typename T, T fenmod = 0>
struct BIT {
T *fen;
int size;
BIT(int size) { fen = new T[size + 1], this->size = size; }
void inc(int idx, T val) {
for (idx++; idx <= size; idx += idx & -idx) {
fen[idx] += val;
if (fenmod) fen[idx] %= fenmod;
}
}
T get(int idx) {
T sum = 0;
for (; idx; idx -= idx & -idx) {
sum += fen[idx];
if (fenmod) sum %= fenmod;
}
return sum;
}
T get(int st, int en) { return get(en) - get(st); }
};
struct SegmentTree {
int *seg;
int segL, segR;
SegmentTree(int segl, int segr) {
segL = segl;
segR = segr;
seg = new int[(segr - segl) << 2];
}
int build(int l, int r, int id) {
if (r - l == 1) {
seg[id] = l;
return seg[id];
}
int mid = (l + r) >> 1;
int res = 0;
res += build(l, mid, id << 1 | 0);
res += build(mid, r, id << 1 | 1);
seg[id] = res;
return res;
}
int build() { return build(segL, segR, 1); }
int pointQuery(int idx, int val, int l, int r, int id) {
if (idx < l or r <= idx) return 0;
if (r - l == 1) {
seg[id] = val;
return seg[id];
}
int mid = (l + r) >> 1;
int res = 0;
res += pointQuery(idx, val, l, mid, id << 1 | 0);
res += pointQuery(idx, val, mid, r, id << 1 | 1);
return res;
}
int pointQuery(int idx, int val) {
return pointQuery(idx, val, segL, segR, 1);
}
int segQuery(int st, int en, int val, int l, int r, int id) {
if (st >= r or en <= l) return 0;
if (st <= l and r <= en) return seg[id];
int mid = (l + r) >> 1;
int res = 0;
res += segQuery(st, en, val, l, mid, id << 1 | 0);
res += segQuery(st, en, val, mid, r, id << 1 | 1);
return res;
}
int segQuery(int st, int en, int val) {
return segQuery(st, en, val, segL, segR, 1);
}
};
class SuffixStructure {
string s;
int *sa;
int *pos;
int *lcp;
int tn;
int *par;
int *h;
vector<pair<int, pair<int, int>>> *adj;
SuffixStructure(string str) { s = str + "$"; }
void build_sa() {
int n = ((int)(s).size());
sa = new int[n];
pos = new int[n];
for (int i = 0; i < n; i++) sa[i] = i, pos[i] = s[i];
int gap;
auto cmp = [&](int i, int j) -> bool {
if (pos[i] != pos[j]) return pos[i] < pos[j];
i += gap, j += gap;
return (i < n and j < n) ? pos[i] < pos[j] : i > j;
};
for (gap = 1;; gap <<= 1) {
sort(sa, sa + n, cmp);
int tmp = 0;
pos[sa[0]] = 0;
for (int i = 1; i < n; i++) {
tmp += cmp(sa[i - 1], sa[i]);
pos[sa[i]] = tmp;
}
if (tmp == n - 1) break;
}
}
void build_lcp() {
int n = ((int)(s).size());
lcp = new int[n];
for (int i = 0, cur = 0; i < n; i++) {
if (int j = pos[i] + 1; j < n) {
while (max(i, j) + cur < n and s[i + cur] == s[j + cur]) cur++;
lcp[pos[i] + 1] = cur;
}
if (cur) cur--;
}
}
void build_tree() {
int n = ((int)(s).size());
tn = 2 * n - 1;
adj = new vector<pair<int, pair<int, int>>>[tn];
par = new int[tn];
h = new int[tn];
memset(par, -1, tn * sizeof(int));
memset(h, 0, tn * sizeof(int));
stack<pair<int, pair<int, int>>> stc;
int verts = n;
stc.push(make_pair(verts, pair<int, int>(0, 0)));
verts++;
auto seg_break = [](pair<int, int> seg, int len) {
pair<int, int> le = seg, ri = seg;
le.second = le.first + len;
ri.first = le.second;
return make_pair(le, ri);
};
for (int i = 0; i < n; i++) {
while (h[stc.top().first] > lcp[i]) {
auto top = stc.top();
stc.pop();
if (h[stc.top().first] < lcp[i]) {
pair<pair<int, int>, pair<int, int>> brk =
seg_break(top.second, lcp[i] - h[stc.top().first]);
stc.push(make_pair(verts, brk.first));
h[verts] = lcp[i];
verts++;
top.second = brk.second;
}
par[top.first] = stc.top().first;
adj[stc.top().first].push_back(top);
}
h[i] = n - sa[i];
stc.push(make_pair(i, pair<int, int>(i + h[stc.top().first], n)));
}
while (h[stc.top().first] > 0) {
auto top = stc.top();
stc.pop();
par[top.first] = stc.top().first;
adj[stc.top().first].push_back(top);
}
}
};
long long int Pow(long long int x, long long int y) {
long long int ans = 1, an = x % Mod;
while (y) {
if (y & 1LL) ans = ans * an % Mod;
an = an * an % Mod;
y >>= 1;
}
return ans;
}
long long int Inv(long long int x) { return Pow(x, Mod - 2); }
int solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) solve();
return 0;
}
const int maxn = 100 * 1001;
int cnt[maxn], n;
bool check(int dis) {
int cur[n];
memcpy(cur, cnt, n * sizeof(int));
set<pair<int, int>> s;
for (int i = 0; i < n; i++)
if (cur[i]) s.insert(make_pair(-cur[i], i));
int res[n];
for (int i = 0; i < n; i++) {
if (i >= dis and cur[res[i - dis]])
s.insert(pair<int, int>(-cur[res[i - dis]], res[i - dis]));
if (((int)(s).size()) == 0) return false;
int cand = s.begin()->second;
s.erase(s.begin());
cur[cand]--;
res[i] = cand;
}
return true;
}
int solve() {
cin >> n;
memset(cnt, 0, n * sizeof(int));
for (int i = 0; i < n; i++) {
int x;
cin >> x, x--;
cnt[x]++;
}
int lo = 1, hi = n;
while (hi - lo > 1) {
int mid = (lo + hi) >> 1;
if (check(mid))
lo = mid;
else
hi = mid;
}
cout << lo - 1 << '\n';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const long long mod = 998244353;
int ar[maxn], n, m;
char p[maxn], ch[maxn];
int ad[maxn << 2], mx[maxn << 2], mn[maxn << 2];
int x, y, val;
void update(int l, int r, int o) {
if (x <= l && y >= r) {
ad[o] += val;
mx[o] += val;
mn[o] += val;
} else {
int mid = l + r >> 1;
if (x <= mid) update(l, mid, o << 1);
if (y > mid) update(mid + 1, r, o << 1 | 1);
mx[o] = max(mx[o << 1], mx[o << 1 | 1]) + ad[o];
mn[o] = min(mn[o << 1], mn[o << 1 | 1]) + ad[o];
}
}
int mx_ans = 0, mn_ans = 0;
void que(int l, int r, int o, int add) {
if (x <= l && y >= r) {
mx_ans = max(mx_ans, add + mx[o]);
mn_ans = min(mn_ans, add + mn[o]);
} else {
int mid = l + r >> 1;
if (x <= mid) que(l, mid, o << 1, add + ad[o]);
if (y > mid) que(mid + 1, r, o << 1 | 1, add + ad[o]);
}
}
int main() {
scanf("%d", &n);
scanf("%s", ch + 1);
for (int i = 1; i <= n; i++) p[i] = '\0';
int now = 1, fir = 1;
for (int i = 1; i <= n; i++) {
if (ch[i] == 'L')
now = max(now - 1, 1);
else if (ch[i] == 'R')
now++;
else {
val = 0;
if (p[now] == '(') {
val--;
} else if (p[now] == ')') {
val++;
}
if (ch[i] == '(') {
val++;
} else if (ch[i] == ')') {
val--;
}
x = now;
y = n;
if (val != 0) {
update(1, n, 1);
}
p[now] = ch[i];
}
if (fir)
fir = 0;
else
printf(" ");
x = y = n;
mx_ans = -1;
que(1, n, 1, 0);
if (mx_ans != 0) {
printf("-1");
continue;
}
x = 1, y = n, mn_ans = n, mx_ans = 0;
que(1, n, 1, 0);
if (mn_ans < 0) {
printf("-1");
} else {
printf("%d", mx_ans);
}
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int LIM1 = 1000000;
const int LIM2 = 1500000;
int n, fa[100100], A, B;
bool rev[100100];
struct Vector {
int x, y;
Vector(int X = 0, int Y = 0) { x = X, y = Y; }
friend Vector operator+(const Vector &u, const Vector &v) {
return Vector(u.x + v.x, u.y + v.y);
}
friend Vector operator-(const Vector &u, const Vector &v) {
return Vector(u.x - v.x, u.y - v.y);
}
friend bool operator<=(const Vector &u, const int &v) {
return 1ll * u.x * u.x + 1ll * u.y * u.y <= 1ll * v * v;
}
} v[100100];
int main() {
scanf("%d", &n);
if (n <= 1) {
puts("1");
return 0;
}
for (int i = 1; i <= n; i++) scanf("%d%d", &v[i].x, &v[i].y);
A = 1, B = 2;
for (int i = 3; i <= n; i++) {
if (v[i] + v[A] <= LIM1) {
v[i] = v[i] + v[A], fa[A] = i, A = B, B = i;
continue;
}
if (v[i] - v[A] <= LIM1) {
v[i] = v[i] - v[A], fa[A] = i, rev[A] ^= 1, A = B, B = i;
continue;
}
if (v[i] + v[B] <= LIM1) {
v[i] = v[i] + v[B], fa[B] = i, B = i;
continue;
}
if (v[i] - v[B] <= LIM1) {
v[i] = v[i] - v[B], fa[B] = i, rev[B] ^= 1, B = i;
continue;
}
if (v[B] + v[A] <= LIM1) {
v[B] = v[B] + v[A], fa[A] = B, A = B, B = i;
continue;
}
if (v[B] - v[A] <= LIM1) {
v[B] = v[B] - v[A], fa[A] = B, rev[A] ^= 1, A = B, B = i;
continue;
}
}
if (v[B] + v[A] <= LIM2)
v[B] = v[B] + v[A], fa[A] = B;
else
v[B] = v[B] - v[A], fa[A] = B, rev[A] ^= 1;
for (int i = n; i; i--)
if (fa[i]) rev[i] ^= rev[fa[i]];
for (int i = 1; i <= n; i++) printf("%d ", rev[i] ? -1 : 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin >> n;
int a[2] = {0, 0};
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
a[tmp - 1]++;
}
if (a[0] == 0) {
for (int i = 0; i < n - 1; i++) {
cout << "2 ";
}
cout << "2" << endl;
return 0;
}
if (a[1] == 0) {
for (int i = 0; i < n - 1; i++) {
cout << "1 ";
}
cout << "1" << endl;
return 0;
}
cout << "2 ";
cout << "1 ";
for (int i = 0; i < a[1] - 1; i++) {
cout << "2 ";
}
for (int i = 0; i < a[0] - 2; i++) {
cout << "1 ";
}
if (a[0] >= 2)
cout << "1\n";
else
cout << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<cstring>
#include<string>
#include <math.h>
#include<algorithm>
// #include <boost/multiprecision/cpp_int.hpp>
#include<functional>
#define int long long
#define inf 1000000007
#define pa pair<int,int>
#define ll long long
#define pal pair<double,pa>
#define ppa pair<int,int>
#define ppap pair<int,pa>
#define ssa pair<string,int>
#define mp make_pair
#define pb push_back
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b))<EPS)
using namespace std;
class Point{
public:
double x,y;
Point(double x=0,double y=0):x(x),y(y) {}
Point operator + (Point p) {return Point(x+p.x,y+p.y);}
Point operator - (Point p) {return Point(x-p.x,y-p.y);}
Point operator * (double a) {return Point(x*a,y*a);}
Point operator / (double a) {return Point(x/a,y/a);}
double absv() {return sqrt(norm());}
double norm() {return x*x+y*y;}
bool operator < (const Point &p) const{
return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const Point &p) const{
return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS;
}
};
typedef Point Vector;
struct Segment{
Point p1,p2;
};
double dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
double cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
bool parareru(Point a,Point b,Point c,Point d){
// if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl;
return abs(cross(a-b,d-c))<EPS;
}
double distance_ls_p(Point a, Point b, Point c) {
if ( dot(b-a, c-a) < EPS ) return (c-a).absv();
if ( dot(a-b, c-b) < EPS ) return (c-b).absv();
return abs(cross(b-a, c-a)) / (b-a).absv();
}
bool is_intersected_ls(Segment a,Segment b) {
if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false;
if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&¶reru((a.p2),(a.p1),(a.p1),(b.p1))){
// cout<<"sss"<<endl;
if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true;
if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true;
if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true;
if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true;
return false;
}
else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS );
}
double segment_dis(Segment a,Segment b){
if(is_intersected_ls(a,b))return 0;
double r=distance_ls_p(a.p1, a.p2, b.p1);
r=min(r,distance_ls_p(a.p1, a.p2, b.p2));
r=min(r,distance_ls_p(b.p1, b.p2, a.p2));
r=min(r,distance_ls_p(b.p1, b.p2, a.p1));
return r;
}
Point intersection_ls(Segment a, Segment b) {
Point ba = b.p2-b.p1;
double d1 = abs(cross(ba, a.p1-b.p1));
double d2 = abs(cross(ba, a.p2-b.p1));
double t = d1 / (d1 + d2);
return a.p1 + (a.p2-a.p1) * t;
}
string itos( int i ) {
ostringstream s ;
s << i ;
return s.str() ;
}
int gcd(int v,int b){
if(v>b) return gcd(b,v);
if(v==b) return b;
if(b%v==0) return v;
return gcd(v,b%v);
}
double distans(double x1,double y1,double x2,double y2){
double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(rr);
}
/*
int pr[100010];
//int inv[100010];
*/
int beki(int wa,int rr,int warukazu){
if(rr==0) return 1ll;
if(rr==1) return wa%warukazu;
if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu;
int zx=beki(wa,rr/2,warukazu);
return (zx*zx)%warukazu;
}
/*
void gya(){
pr[0]=1;
for(int i=1;i<100010;i++){
pr[i]=(pr[i-1]*i)%inf;
}
for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2);
}
*/
//----------------kokomade tenpure------------
int sum[100020]={0};
int a[100002];
signed main(){
int n,t;
cin>>n>>t;
for(int i=0;i<n;i++) {
cin>>a[i];
sum[a[i]]++;
}
for(int i=1;i<=100010;i++) sum[i] += sum[i-1];
int ans=1;
sort(a,a+n);
for(int i=0;i<n;i++){
int r=0;
if(a[i]-t>1) r=sum[a[i]-t-1];
r=i-r+1;
// cout<<r<<endl;
ans*=r;
ans %=inf;
}
cout<<ans<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K, d;
cin >> N >> K;
d = log(N) / log(K) + 1;
cout << d << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
long long n, ans = 0, tot = 0, lll = 0, dotot = 0, tp = 1, lis[N];
long long fa[N], top[N], son[N], id[N], gap[N];
long long dep[N], dfn[N], siz[N], dis[N];
struct A {
long long fir, nex, go, w;
} a[N << 1];
inline bool cmp(long long x, long long y) {
return dis[x] * siz[y] < dis[y] * siz[x];
}
void add(long long x, long long y, long long z) {
a[++tot].nex = a[x].fir;
a[x].fir = tot;
a[tot].go = y;
a[tot].w = z;
}
inline long long read() {
long long x = 0, minus = 0;
char ch;
while (!isdigit(ch = getchar())) minus |= (ch == '-');
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
return minus ? -x : x;
}
void dfs1(long long x, long long f) {
fa[x] = f;
dep[x] = dep[f] + 1;
siz[x] = 1;
for (register long long i = a[x].fir; i; i = a[i].nex) {
long long y = a[i].go;
if (y == f) continue;
dis[y] = a[i].w;
gap[y] = gap[x] + a[i].w;
dfs1(y, x);
dis[x] += dis[y];
siz[x] += siz[y];
if (siz[son[x]] < siz[y]) son[x] = y;
}
}
void dfs2(long long x, long long topf) {
top[x] = topf;
if (!son[x]) return;
dfs2(son[x], topf);
for (register long long i = a[x].fir; i; i = a[i].nex) {
long long y = a[i].go;
if (y == fa[x] || y == son[x]) continue;
dfs2(y, y);
}
}
void dfs3(long long x) {
dfn[x] = ++dotot;
id[dotot] = x;
if (!siz[x]) return;
long long l = tp;
for (register long long i = a[x].fir; i; i = a[i].nex) {
long long y = a[i].go;
if (y == fa[x]) continue;
lis[tp++] = y;
}
long long r = tp;
sort(lis + l, lis + r, cmp);
for (register long long i = l; i < r; ++i) dfs3(lis[i]);
}
inline long long lca(long long x, long long y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
signed main() {
n = read();
for (register long long i = 1, x, y, z; i < n; ++i)
x = read(), y = read(), z = read(), add(x, y, z), add(y, x, z);
dfs1(1, 0), dfs2(1, 1), dfs3(1);
for (register long long i = 2; i <= n; ++i) {
long long lc = lca(id[i - 1], id[i]);
ans += gap[id[i - 1]] + gap[id[i]] - gap[lc] - gap[lc];
lll += ans;
}
double ww = (double)lll / (n - 1);
printf("%lf\n", ww);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n, k;
cin >> n >> k;
string s;
cin >> s;
char a[k];
for (int i = 0; i < k; i++) {
cin >> a[i];
}
vector<unsigned long long> v;
int count = 0;
for (int i = 0; i < n; i++) {
int f = 0;
for (int j = 0; j < k; j++) {
if (a[j] == s[i]) {
f = 1;
break;
}
}
if (f == 1) {
count++;
}
if (f == 0 || i == n - 1) {
v.push_back(count);
count = 0;
}
}
unsigned long long sum = 0;
for (int i = 0; i < v.size(); i++) {
sum += (v[i] * (v[i] + 1)) / 2;
}
cout << sum;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int Read() {
int x = 0, f = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 30005;
const int MAXM = 205;
int n, m, idx[MAXN], par[MAXN], p[MAXM][MAXN], d[MAXM][MAXN];
vector<int> adj[MAXN];
vector<pair<int, int> > sub[MAXN];
bool v[MAXN];
inline void GG() {
puts("-1");
exit(0);
}
inline void Dfs(int x, int *d, int dep, int p) {
if (dep ^ d[x]) GG();
for (auto y : adj[x])
if (y ^ p) Dfs(y, d, dep + 1, x);
}
int main() {
n = Read(), m = Read();
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (!(d[i][j] = Read())) {
if (idx[i]) GG();
idx[i] = j;
}
if (!idx[i]) GG();
}
p[1][0] = idx[1], v[idx[1]] = true;
for (int i = 2; i <= m; i++) {
for (int j = 1; j <= n; j++)
if (d[1][j] + d[i][j] == d[1][idx[i]]) {
if (p[i][d[1][j]]) GG();
p[i][d[1][j]] = j, v[j] = true;
}
for (int j = 1; j <= d[1][idx[i]]; j++) {
if (!p[i][j]) GG();
if (par[p[i][j]] && par[p[i][j]] != p[i][j - 1]) GG();
par[p[i][j]] = p[i][j - 1];
}
}
for (int i = 1; i <= n; i++)
if (!v[i]) {
int x = -1, pos = 0;
for (int j = 1; j <= m; j++) {
int dis = d[1][i] + d[1][idx[j]] - d[j][i] >> 1;
if (x < dis) x = dis, pos = p[j][dis];
}
sub[pos].push_back(make_pair(d[1][i], i));
}
for (int i = 1; i <= n; i++) {
sort(sub[i].begin(), sub[i].end());
for (int j = 0, dep = d[1][i], lst = i; j < sub[i].size(); j++) {
if (j && (sub[i][j].first ^ sub[i][j - 1].first))
lst = sub[i][j - 1].second, dep = d[1][lst];
if (sub[i][j].first - dep - 1) GG();
par[sub[i][j].second] = lst;
}
}
for (int i = 1; i <= n; i++)
if (par[i])
adj[par[i]].push_back(i), adj[i].push_back(par[i]);
else if (i ^ idx[1])
GG();
for (int i = 1; i <= m; i++) Dfs(idx[i], d[i], 0, 0);
for (int x = 1; x <= n; x++)
if (par[x]) printf("%d %d\n", x, par[x]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
long long n, t, m, l, k, ans, i, j, res = 0, fl;
t = 1;
while (t--) {
string s;
cin >> n;
cin >> s;
cin >> s;
long long ct = 0;
if (s == "week") {
n--;
long long start = 4;
for (; start - 4 < 366; ++start) {
if (start % 7 == n) ct++;
}
cout << ct << '\n';
} else {
if (n < 30)
cout << 12 << '\n';
else if (n == 30)
cout << 11 << '\n';
else if (n == 31)
cout << 7 << '\n';
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, x[110], vb, vs, xu, yu;
int ans;
double mn = 1e10;
double r;
double dist(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
cin >> n >> vb >> vs;
for (int i = 0; i < n; i++) cin >> x[i];
cin >> xu >> yu;
for (int i = 1; i < n; i++) {
double t1 = dist(0, 0, x[i], 0) / vb;
double t2 = dist(x[i], 0, xu, yu) / vs;
if (t1 + t2 < mn) {
mn = t1 + t2;
ans = i + 1;
r = dist(x[i], 0, xu, yu);
} else if (t1 + t2 == mn) {
double d = dist(x[i], 0, xu, yu);
if (d < r) {
r = d;
ans = i + 1;
}
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int val[maxn];
vector<pair<int, int> > interv;
int ans[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int t, l, r;
cin >> t >> l >> r;
if (t == 1) {
for (int j = l; j < r; j++) {
val[j] = 1;
}
} else {
interv.push_back(pair<int, int>(l, r));
}
}
ans[1] = 1;
for (int i = 2; i <= n; i++) {
if (val[i - 1] == 1)
ans[i] = ans[i - 1] + 1;
else
ans[i] = ans[i - 1] - 1;
}
for (pair<int, int> i : interv) {
int prev = ans[i.first];
bool good = false;
for (int j = i.first + 1; j <= i.second; j++) {
if (ans[j] < prev) {
good = true;
break;
}
prev = ans[j];
}
if (!good) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
cout << ans[i] + 1001 << " ";
}
cout << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline int read() {
int x = 0, f = 1;
char ch;
ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch & 15);
ch = getchar();
}
if (f)
return x;
else
return -x;
}
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
int n, a[N], p[N], f[N], Sf[N], ans;
long long Sum[N];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long LCM(long long &a, long long b) { return a = b / gcd(a, b); }
int main() {
n = read();
for (int i = 1; i <= n; i++) a[i] = read(), Sum[i] = 1ll * a[i];
for (int i = 2; i <= n; i++) p[i] = read();
for (int i = n; i > 1; i--) Sum[p[i]] += Sum[i];
for (int i = n; i >= 1; i--)
if ((Sum[i] = Sum[1] / gcd(Sum[i], Sum[1])) <= n) f[Sum[i]]++;
for (int i = n; i >= 1; i--)
for (int j = 2; j * i <= n; j++) f[j * i] += f[i];
Sf[1] = 1;
for (int i = 1; i <= n; i++)
if (f[i] == i) {
ans = (ans + Sf[i]) % mod;
for (int j = 2; j * i <= n; j++) Sf[j * i] = (Sf[j * i] + Sf[i]) % mod;
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void sieve(vector<int> &s, vector<int> &spf) {
for (int i = 2; i * i < s.size(); i++) {
if (s[i] == 1) {
for (int j = i; j * i < s.size(); j++) {
if (s[i * j] == 1) spf[i * j] = i;
s[i * j] = 0;
}
}
}
}
vector<int> fact(vector<int> &spf, int n) {
vector<int> r;
while (spf[n] != 1) {
r.push_back(spf[n]);
n /= spf[n];
}
if (n != 1) r.push_back(n);
return r;
}
int main() {
vector<int> spf(pow(10, 6), 1);
vector<int> s(pow(10, 6), 1);
sieve(s, spf);
int n, m;
cin >> n >> m;
vector<int> x;
for (int i = 0; i <= n; i++) x.push_back(0);
vector<vector<int>> v;
for (int i = 0; i <= n; i++) {
vector<int> t;
v.push_back(t);
}
for (int i = 0; i < m; i++) {
char a;
int b;
cin >> a >> b;
if (a == '+') {
if (x[b] == 1)
cout << "Already on" << endl;
else {
int f = 1;
vector<int> h = fact(spf, b);
for (int i = 0; i < h.size(); i++) {
if (v[h[i]].size() == 0)
continue;
else {
cout << "Conflict with " << v[h[i]][0] << endl;
f = 0;
break;
}
}
if (f) {
x[b] = 1;
cout << "Success" << endl;
for (int i = 0; i < h.size(); i++) {
v[h[i]].push_back(b);
}
}
}
} else {
if (x[b] == 0)
cout << "Already off" << endl;
else {
cout << "Success" << endl;
vector<int> k;
k = fact(spf, b);
for (int i = 0; i < k.size(); i++) {
v[k[i]].erase(remove(v[k[i]].begin(), v[k[i]].end(), b),
v[k[i]].end());
}
x[b] = 0;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
//input
int N;
cin >> N;
int A[N];
for(int i=0;i<N;i++) cin >> A[i];
//setup
sort(A,A+N);
reverse(A,A+N);
long long Total=0;
for(int i=0;i<N;i++){
Total+=A[i/2];
}
Total-=A[0];
//output
cout << Total << endl;
} | 0 |
#include <iostream>
using namespace std;
int main(void){
int p[4] = {6000,4000,3000,2000};
int t,n;
for(int i = 0 ; i < 4 ; i++){
cin >> t >> n;
cout << p[t-1]*n <<endl;
}
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <cstring>
#include <sstream>
using namespace std;
int N;
string ss[10001];
int hit[10001];
int blow[10001];
bool isValid[10001];
bool check(string org,string tgt,int hits,int blows){
int h=0;
int b=0;
for(int i=0;i<tgt.size();i++){
if(tgt[i]==org[i]){
h++;
}
else{
bool ok=false;
for(int j=0;j<org.size();j++){
if(org[j]==tgt[i]){
ok=true;
break;
}
}
if(ok){
b++;
}
}
}
return (hits==h)&&(blows==b);
}
pair<int,int> getPii(string org,string tgt){
int h=0;
int b=0;
for(int i=0;i<tgt.size();i++){
if(tgt[i]==org[i]){
h++;
}
else{
bool ok=false;
for(int j=0;j<org.size();j++){
if(org[j]==tgt[i]){
ok=true;
break;
}
}
if(ok){
b++;
}
}
}
return make_pair(h,b);
}
int main(){
while(cin>>N&&N){
memset(isValid,0,sizeof(isValid));
for(int i=0;i<N;i++)
cin>>ss[i]>>hit[i]>>blow[i];
// ÃÈðvZ
int cnt=0;
vector<string> cands;
for(int i=123;i<10001;i++){
int a=i;
set<int> s;
while(a){
s.insert(a%10);
a/=10;
}
if(i<1000){
s.insert(0);
}
bool ok=true;
stringstream sss;
sss<<i;
string org=sss.str();
if(s.size()==4){
if(org.size()==3)org='0'+org;
// eÉ¢ÄhitÆblowª³µ¢©vZ
for(int j=0;j<N;j++){
ok&=check(org,ss[j],hit[j],blow[j]);
if(!ok)break;
}
}
else ok=false;
isValid[i]=ok;
if(isValid[i]){
cnt++;
cands.push_back(org);
}
}
if(cnt==0)cout<<"????"<<endl;
else if(cnt==1){
for(int i=123;i<=10000;i++){
if(isValid[i]){
stringstream sss;
sss<<i;
string os=sss.str();
if(os.size()==3)os='0'+os;
cout<<os<<endl;
break;
}
}
}
// ¡ó⪠éê,Ôɲ×é
else{
bool ok=false;
for(int i=123;i<10001;i++){
stringstream sss;
sss<<i;
string org=sss.str();
if(org.size()==3)org='0'+org;
set<char> sc;
for(int i=0;i<org.size();i++)sc.insert(org[i]);
if(sc.size()!=4)continue;
set<pair<int,int> > spii;
// hitÆblowÌð¦é
int prv=0;
for(int j=0;j<cands.size();j++){
spii.insert(getPii(org,cands[j]));
if(spii.size()!=prv+1)break;
else prv=spii.size();
}
if(spii.size()==cands.size()){
cout<<org<<endl;
ok=true;
break;
}
}
if(!ok)cout<<"????"<<endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct Inter {
int d;
int f;
int id;
int k;
Inter(){};
bool operator<(const Inter& i) const {
if (d != i.d) return d < i.d;
if (f != i.f) return f < i.f;
return id < i.id;
}
};
int N, M;
Inter notes[100001];
Inter gens[100001];
int ans[100001];
map<int, vector<pair<int, int> > > ls_ends;
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d%d", ¬es[i].d, ¬es[i].f);
notes[i].id = i;
}
sort(notes + 1, notes + N + 1);
scanf("%d", &M);
for (int i = 1; i <= M; i++) {
scanf("%d%d%d", &gens[i].d, &gens[i].f, &gens[i].k);
gens[i].id = i;
}
sort(gens + 1, gens + M + 1);
int i = 1;
for (int part = 1; part <= N; part++) {
while ((i <= M) && (gens[i].d <= notes[part].d)) {
ls_ends[gens[i].f].push_back(make_pair(gens[i].k, gens[i].id));
i++;
}
map<int, vector<pair<int, int> > >::iterator to_rem =
ls_ends.lower_bound(notes[part].f);
if (to_rem == ls_ends.end()) {
printf("NO\n");
exit(0);
}
vector<pair<int, int> >& ls_possibles = to_rem->second;
ans[notes[part].id] = ls_possibles.back().second;
ls_possibles.back().first--;
if (!ls_possibles.back().first) ls_possibles.pop_back();
if (ls_possibles.empty()) ls_ends.erase(to_rem);
}
printf("YES\n");
for (i = 1; i < N; i++) printf("%d ", ans[i]);
printf("%d\n", ans[N]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 100010;
const long long Q = 1e18 + 7;
int main() {
srand(time(NULL));
long long n, x, y, i, l, r, mid, a;
cin >> n >> x >> y;
for (i = 0; i < n; i++) {
l = 0;
r = Q;
cin >> a;
while (l != r - 1) {
mid = (l + r) / 2;
if (mid / y + mid / x >= a)
r = mid;
else
l = mid;
}
if (r % y == 0 && r % x == 0) printf("Both\n");
if (r % y == 0 && r % x != 0) printf("Vanya\n");
if (r % y != 0 && r % x == 0) printf("Vova\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s, t;
cin >> s >> t;
vector<int> cnts(26), cntt(26);
for (int i = 0; i < n; i++) {
++cnts[s[i] - 'a'];
++cntt[t[i] - 'a'];
}
bool ok = true;
bool good = false;
for (int i = 0; i < 26; i++) {
ok &= cnts[i] == cntt[i];
good |= cnts[i] > 1;
}
if (!ok) {
cout << "NO" << endl;
continue;
}
if (good) {
cout << "YES" << endl;
continue;
}
int invs = 0, invt = 0;
for (int l = 0; l < n; l++) {
for (int j = 0; j < l; j++) {
invs += s[l] > s[j];
invt += t[l] > t[j];
}
}
ok &= (invs & 1) == (invt & 1);
if (ok)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
long n;
std::cin >> n;
std::vector<long> degree(n, 0);
std::vector<long> s(n, 0);
std::deque<long> queue;
for (long p = 0; p < n; p++) {
std::cin >> degree[p] >> s[p];
if (degree[p] == 1) {
queue.push_front(p);
}
}
std::vector<std::pair<long, long> > edges;
while (!queue.empty()) {
long leaf = queue.back();
queue.pop_back();
if (degree[leaf] < 1) {
continue;
}
long parent = s[leaf];
edges.push_back(std::pair<long, long>(leaf, parent));
--degree[parent];
if (degree[parent] == 1) {
queue.push_front(parent);
}
s[parent] ^= leaf;
}
std::cout << edges.size() << std::endl;
for (long p = 0; p < edges.size(); p++) {
std::cout << edges[p].first << " " << edges[p].second << std::endl;
}
return 0;
}
| 3 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 300005
using namespace std;
struct P{
int x,y,id;
}a[N];
int W,H,n,w[N],m,ans=0,X[N],Y[N];
pair<int,int> b[N];
void get(){
m=0;
for(int i=1;i<=n;i++) b[i]=make_pair(a[i].y,i);
b[n+1]=make_pair(H,0);
b[n+2]=make_pair(0,0);
sort(b+1,b+n+3);
for(int i=1;i<=n+2;i++){
if(i==1||b[i].first!=b[i-1].first) w[++m]=b[i].first;
a[b[i].second].y=m;
}
}
bool cmpid(P a,P b){
return a.id<b.id;
}
bool cmpy(P a,P b){
return a.y<b.y;
}
struct trie{
int mx,add;
}t[N<<2];
void pwd(int k,int l,int r){
if(l==r) return;
if(t[k].add){
t[k<<1].add+=t[k].add;
t[k<<1|1].add+=t[k].add;
t[k<<1].mx+=t[k].add;
t[k<<1|1].mx+=t[k].add;
t[k].add=0;
}
}
void build(int k,int l,int r){
t[k].add=t[k].mx=0;
if(l==r){
t[k].mx=-w[l];
return;
}
int mid=l+r>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx);
}
int s1[N],top1,s2[N],top2;
void update(int k,int l,int r,int L,int R,int val){
if(L>R) return;
pwd(k,l,r);
if(L<=l&&R>=r){
t[k].mx+=val;
t[k].add+=val;
return;
}
int mid=l+r>>1;
if(L<=mid) update(k<<1,l,mid,L,R,val);
if(R>mid) update(k<<1|1,mid+1,r,L,R,val);
t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx);
}
void solve(){
get();
build(1,1,m);
sort(a+1,a+n+1,cmpy);
ans=max(ans,H-w[a[n].y]+W);
ans=max(ans,w[a[1].y]+W);
top1=top2=0;
int nx=W/2;
a[0].y=1;
for(int i=1,p=1;i<=m;i++){
ans=max(ans,w[i]+t[1].mx);
update(1,1,m,i,i,W);
if(a[p].y==i){
if(a[p].x<=nx){
update(1,1,m,a[s1[top1]].y,i-1,-a[p].x);
while(top1&&a[s1[top1]].x<a[p].x){
update(1,1,m,a[s1[top1-1]].y,a[s1[top1]].y-1,-a[p].x+a[s1[top1]].x);
top1--;
}
s1[++top1]=p;
}
if(a[p].x>=nx){
update(1,1,m,a[s2[top2]].y,i-1,a[p].x-W);
while(top2&&a[s2[top2]].x>a[p].x){
update(1,1,m,a[s2[top2-1]].y,a[s2[top2]].y-1,a[p].x-a[s2[top2]].x);
top2--;
}
s2[++top2]=p;
}
p++;
}
}
}
int main(){
scanf("%d%d%d",&W,&H,&n);
ans=max(W,H)+1;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
X[i]=a[i].x;Y[i]=a[i].y;
}
solve();
sort(a+1,a+n+1,cmpid);
for(int i=1;i<=n;i++) a[i].x=Y[i],a[i].y=X[i];
swap(W,H);
solve();
printf("%d",ans+ans);
return 0;
} | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.