solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
inline int readChar();
template <class T = int>
inline T readInt();
template <class T>
inline void writeInt(T x, char end = 0);
inline void writeChar(int x);
inline void writeWord(const char *s);
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len) return -1;
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c <= 32) c = getChar();
return c;
}
template <class T>
inline T readInt() {
int s = 1, c = readChar();
T x = 0;
if (c == '-') s = -1, c = getChar();
while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar(int x) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
template <class T>
inline void writeInt(T x, char end) {
if (x < 0) writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n) s[n++] = '0' + x % 10, x /= 10;
while (n--) writeChar(s[n]);
if (end) writeChar(end);
}
inline void writeWord(const char *s) {
while (*s) writeChar(*s++);
}
struct Flusher {
~Flusher() {
if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
} flusher;
inline void readWord(char *s) {
int c = readChar();
while (c > 32) *s++ = c, c = getChar();
*s = 0;
}
inline void writeDouble(double x, int output_len) {
if (x < 0) writeChar('-'), x = -x;
int t = (int)x;
writeInt(t), x -= t;
writeChar('.');
for (int i = output_len - 1; i > 0; i--) {
x *= 10;
t = min(9, (int)x);
writeChar('0' + t), x -= t;
}
x *= 10;
t = min(9, (int)(x + 0.5));
writeChar('0' + t);
}
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using ull = unsigned long long;
const int maxn = 5e5;
const int mod = 998244353;
const int M = 1e9 + 7;
const int inf = 1e9;
const ll inf64 = 1e18L;
const ld pi = acos((ld)-1.0);
const ld eps = (ld)1e-6;
const int di[] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dj[] = {2, -2, 2, -2, 1, -1, 1, -1};
const int dii[] = {0, 1, -1, 0};
const int djj[] = {1, 0, 0, -1};
const int dx[] = {1, 1, -1, -1};
const int dy[] = {1, -1, 1, -1};
const int SQ = 1000;
const int L = 19;
const int HF = 65536;
struct DSU {
int n;
vi p, s, he;
DSU(int n) : p(n + 1), s(n + 1, 1), he(n + 1, n) {
iota(p.begin(), p.end(), 0);
s[n] = 0;
}
int get(int v) { return p[v] == v ? v : p[v] = get(p[v]); }
bool dif(int u, int v) {
if ((u = get(u)) == (v = get(v)))
return false;
else {
if (he[u] != v) {
int A = u, B = he[v];
int C = v, D = he[u];
{
if (s[A] < s[B]) swap(A, B);
s[A] += s[B];
p[B] = A;
}
{
if (s[C] < s[D]) swap(C, D);
s[C] += s[D];
p[D] = C;
}
he[A] = C;
he[C] = A;
}
return true;
}
}
};
struct edge {
int u, v, w, id;
bool operator<(const edge &he) const { return w > he.w; }
} a[499500];
int n, m, q;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
n = readInt();
m = readInt();
q = readInt();
for (int i = 0; i < m; i++) {
a[i] = {readInt() - 1, readInt() - 1, readInt(), i};
}
sort(a, a + m);
while (q--) {
int l = readInt() - 1, r = readInt() - 1;
int ans = -1;
DSU dsu(n);
for (int i = 0; i < m && ans == -1; i++) {
if (l <= a[i].id && a[i].id <= r && !dsu.dif(a[i].u, a[i].v)) {
ans = a[i].w;
}
}
writeInt(ans, '\n');
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int cnt[1000009];
int main() {
int n, a;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
cnt[a]++;
}
int count = 0;
for (int i = 0; i <= 1000000; i++) {
int p = cnt[i];
if (p & 1) count++;
cnt[i + 1] += p / 2;
}
long long sum = cnt[1000001];
count += __builtin_popcountll(sum);
printf("%d\n", count);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long double n, m, h;
int main() {
cin >> n >> m >> h;
int sum = 0;
vector<int> s(m + 1);
h--;
for (int i = 0; i < m; i++) {
cin >> s[i];
sum += s[i];
}
s[h]--;
sum--;
n--;
if (sum < n) {
cout << -1;
return 0;
}
if (sum - s[h] < n) {
cout << 1;
return 0;
}
long double x = 1;
for (long double i = 0; i < n; i++) {
x *= (sum - s[h] - i) / (sum - i);
}
cout << 1 - x;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int n, m;
vector<vector<int>> g;
vector<bool> visited;
vector<int> ord;
vector<int> low;
vector<int> parent;
int timer = 0;
vector<pair<int,int>> bridge;
void dfs(int cur, int prev) {
ord[cur] = low[cur] = timer++;
visited[cur] = true;
for (int next : g[cur]) {
if (visited[next] == false) {
parent[next] = cur;
dfs(next, cur);
low[cur] = min(low[cur], low[next]);
if (ord[cur] < low[next]) {
if (cur < next) bridge.push_back({cur,next});
else bridge.push_back({next,cur});
}
} else if (next != prev) {
low[cur] = min(low[cur], ord[next]);
}
}
}
void art_points() {
set<int> ap;
timer = 1;
dfs(0, 1);
int child = 0;
rep(i, n) {
if (i == 0) continue;
if (parent[i] == 0) child++;
else if (ord[parent[i]] <= low[i]) ap.insert(parent[i]);
}
if (child > 1) ap.insert(0);
}
int main() {
cin >> n;
cin >> m;
g.resize(n);
visited.resize(n, false);
ord.resize(n);
low.resize(n);
parent.resize(n);
rep(i, m) {
int s, t ;
cin >> s;
cin >> t;
g[s].push_back(t);
g[t].push_back(s);
}
dfs(0,1);
sort(bridge.begin(), bridge.end());
rep(i, bridge.size()) {
printf("%d %d\n", bridge[i].first, bridge[i].second);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m;
cin >> m;
while (m--) {
int a, b;
cin >> a >> b;
cout << 24 * 60 - (60 * a + b) << endl;
}
}
| 1 |
#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
const int N=1e7+5;
char s[N], t[N];
int mem[2*N], *cnt=mem+N;
int main() {
int n, x, y=0, k=0;
long long ans=0;
assert(scanf("%d%s%s", &n, s, t)==3);
x=(s[0]!=t[0]);
for(int i=0; i<n; ++i) {
++cnt[x-y];
k+=(x>=y);
ans+=x>y?x-y:y-x;
x+=s[i+1]!=s[i];
y+=t[i+1]!=t[i];
}
if(2*k-n+cnt[-1]<0)
for(int i=-1, d; (d=4*k-2*n+2*cnt[i])<0; i-=2) ans+=d, k+=cnt[i]+cnt[i-1];
else
for(int i=0, d; (d=2*n-4*k+4*cnt[i]+2*cnt[i+1])<0; i+=2) ans+=d, k-=cnt[i]+cnt[i+1];
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, M = 2 * N;
int n, k;
struct Trie {
int node[N], nxt[N][26], id;
Trie() {
memset(memw, -1, sizeof memw);
memset(meml, -1, sizeof meml);
iota(node, node + N, 0);
id = 0;
makeNode();
}
int makeNode() {
memset(nxt[id], -1, sizeof nxt[id]);
return id++;
}
void addString(const char *str) {
int cur = 0;
for (int i = 0; str[i]; ++i) {
int &ni = nxt[cur][str[i] - 'a'];
if (!~ni) ni = makeNode();
cur = ni;
}
}
int memw[N];
int canWin(int u = 0) {
int &ret = memw[u];
if (~ret) return ret;
ret = 0;
for (int i = 0; i < 26; ++i) {
int &v = nxt[u][i];
if (~v) ret |= !canWin(v);
}
return ret;
}
int meml[N];
int canLose(int u = 0) {
int &ret = meml[u];
if (~ret) return ret;
ret = 1;
bool any = 0;
for (int i = 0; i < 26; ++i) {
int &v = nxt[u][i];
if (~v) any = 1, ret &= canLose(v);
}
return !any ? 1 : !ret;
}
} trie;
char str[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%s", str);
trie.addString(str);
}
bool cw = trie.canWin(), cl = trie.canLose();
if (!cw) {
puts("Second");
} else if (cl) {
puts("First");
} else {
puts(k & 1 ? "First" : "Second");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, sum = 0;
cin >> n >> k;
int a[k][n / k];
for (int i = 0; i < n / k; i++) {
for (int j = 0; j < k; j++) cin >> a[j][i];
}
for (int i = 0; i < k; i++) {
int a1 = 0, a2 = 0;
for (int j = 0; j < n / k; j++) {
if (a[i][j] == 2) a2++;
if (a[i][j] == 1) a1++;
}
if (a1 < a2)
sum += a1;
else
sum += a2;
}
cout << sum;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 111;
int n, m;
bool in[N][N];
int ans[N][N], ban[N][N];
int calc(int x) {
for (int i = 0;; ++i)
if (!(x >> i & 1)) return i;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
cin >> n >> m;
for (int i = (1); i < (n + 1); i++)
for (int j = (1); j < (m + 1); j++) in[i][j] = 1;
memset(ans, -1, sizeof(ans));
for (int i = (1); i < (n + 1); i++)
for (int j = (1); j < (m + 1); j++)
if (ans[i][j] == -1) {
int c = calc(ban[i][j]), k;
for (int t = (1); t < (n + 1); t++) {
bool ok = 1;
if (i + t - 1 > n || j + t - 1 > m) ok = 0;
for (int x = (i); x < (i + t); x++)
if (ans[x][j + t - 1] != -1 || calc(ban[x][j + t - 1]) > c) ok = 0;
for (int y = (j); y < (j + t); y++)
if (ans[y][i + t - 1] != -1 || calc(ban[y][i + t - 1]) > c) ok = 0;
if (!ok) break;
k = t;
if (j + t <= m && c > calc(ban[i][j + t])) break;
}
for (int x = (i); x < (i + k); x++)
for (int y = (j); y < (j + k); y++) ans[x][y] = c;
for (int x = (i); x < (i + k); x++)
ban[x][j - 1] |= (1 << c), ban[x][j + k] |= (1 << c);
for (int y = (j); y < (j + k); y++)
ban[i - 1][y] |= (1 << c), ban[i + k][y] |= (1 << c);
}
for (int i = (1); i < (n + 1); i++) {
for (int j = (1); j < (m + 1); j++) cout << (char)(ans[i][j] + 'A');
cout << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long m;
const long long mod = 998244353;
long long bin_num(long long num, long long pow) {
if (pow == 0) {
return 1LL;
}
if (pow == 1) {
return num % mod;
}
if (pow == 2) {
return ((num * num) % mod);
}
long long ans = bin_num(num, pow / 2);
ans = (ans * ans) % mod;
if (pow % 2 == 1) {
ans = (ans * num) % mod;
}
return ans;
}
long long shanks(long long a, long long b) {
map<long long, long long> values;
long long n = (long long)sqrt(mod) + 1;
for (long long i = n; i >= 1; i--) {
values[bin_num(a, i * n)] = i;
}
for (long long i = 0; i <= n; i++) {
long long cur = (bin_num(a, i) * b) % mod;
if (values.count(cur) > 0) {
long long ans = values[cur] * n - i;
if (ans < mod) {
return ans;
}
}
}
return -1;
}
vector<vector<long long>> mul(const vector<vector<long long>>& one,
const vector<vector<long long>>& second) {
vector<vector<long long>> x(m, vector<long long>(m, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < m; k++) {
x[i][j] =
(x[i][j] + ((one[i][k] * second[k][j]) % (mod - 1))) % (mod - 1);
}
}
}
return x;
}
vector<vector<long long>> bin_(const vector<vector<long long>>& m,
long long pow) {
if (pow == 1) {
return m;
}
if (pow == 2) {
return mul(m, m);
}
vector<vector<long long>> t_ = bin_(m, pow / 2);
t_ = mul(t_, t_);
if (pow % 2 == 1) {
t_ = mul(t_, m);
}
return t_;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> m;
vector<long long> b(m);
for (int i = 0; i < m; i++) {
cin >> b[i];
}
long long n, k;
cin >> n >> k;
vector<vector<long long>> t_(m, vector<long long>(m, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
if (j == 0) {
t_[i][j] = b[i];
} else if (j == i + 1) {
t_[i][j] = 1;
}
}
}
long long pow_ = bin_(t_, n - m)[0][0];
long long new_os = bin_num(3, pow_);
long long ans = shanks(new_os, k);
if (ans == -1) {
cout << -1 << endl;
return 0;
}
ans = bin_num(3, ans);
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, p, s, q, t, i, j, k, x, y, z, c, d, l, r, lst, a, b, ans, mn, mx, cnt,
l1, l2;
int main() {
cin >> t;
for (q = 0; q < t; q++) {
cin >> d;
if (d < 4 && d > 0)
cout << "N\n";
else {
cout << "Y ";
cout << setprecision(15)
<< double(double(d - sqrt(d * (d - 4))) / double(2)) << " ";
cout << setprecision(15)
<< double(d - double(d - sqrt(d * (d - 4))) / double(2)) << "\n";
}
}
}
| 3 |
#include "iostream"
#include "random"
#include "string"
#include "bitset"
#include "algorithm"
#include "map"
#include "queue"
#include "list"
#include "set"
#include "climits"
#include "iomanip"
#include "functional"
#include "stack"
#include "numeric"
using namespace std;
const long long int MOD = 1000000007;
long long int N, M, K, L, R, H, W;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s;
getline(cin, s);
while (s != "#") {
int v[3] = {};
int index = 0;
//cout << s << endl;
for (int i = 7; i < s.size(); i++) {
//cout << i <<" "<<index<< endl;
if (s[i] == ' ') {
index++;
}
else {
v[index] *= 10;
v[index] += s[i] - '0';
}
}
string ret;
if (v[0] <= 30 || (v[0] == 31 && v[1] <= 4)) {
ret += "HEISEI " + to_string(v[0]) + " " + to_string(v[1]) + " " + to_string(v[2]);
}
else {
ret += "? " + to_string((v[0] - 30)) + " " + to_string(v[1]) + " " + to_string(v[2]);
}
cout << ret << endl;
getline(cin, s);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t = 1;
while (t--) {
long double H, L;
cin >> H >> L;
long double h = ((L * L) - (H * H)) / (2 * H);
cout << fixed << setprecision(12) << h;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long LLINF = 1LL << 61;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int f[1 << 24];
int a[55], b[2], na[1 << 24];
int n, k, res;
long long s[1 << 24];
inline void add(int &a, const int &b) {
a += b;
if (a >= MOD) a -= MOD;
}
int main() {
int n;
cin >> n;
for (int i = 0, _n = (int)(n); i < _n; ++i) {
cin >> a[i];
na[1 << i] = a[i];
}
cin >> k;
b[0] = b[1] = -1;
for (int i = 0, _n = (int)(k); i < _n; ++i) cin >> b[i];
f[0] = 1;
for (int mask = 0, _n = (int)(1 << n); mask < _n; ++mask) {
s[mask] = s[mask ^ (mask & -mask)] + na[mask & -mask];
if (s[mask] == b[0] || s[mask] == b[1]) continue;
for (int t = mask; t; t ^= t & -t) {
add(f[mask], f[mask ^ (t & -t)]);
}
}
cout << f[(1 << n) - 1] << endl;
return 0;
}
| 5 |
#include<iostream>
int main(){
int n,k;
std::cin >> n >> k;
std::cout << n-k+1;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
long long x;
scanf("%lld", &x);
return x;
}
vector<int> ans;
long long bal, m;
vector<int> avail;
string s;
bool putw(int left, int last, bool chance) {
if (left <= 0) return true;
for (int x : avail) {
if (x != last) {
long long new_bal = bal;
if (chance)
new_bal -= x;
else
new_bal += x;
if ((new_bal > 0 and bal < 0) or (new_bal < 0 and bal > 0)) {
bal = new_bal;
ans.push_back(x);
if (!putw(left - 1, x, !chance)) {
if (chance)
bal += x;
else
bal -= x;
ans.pop_back();
} else {
return true;
}
}
}
}
return false;
}
void solve() {
cin >> s;
cin >> m;
bal = 0;
for (int i = (int)(0); i <= (int)(9); i++) {
if (s[i] == '1') avail.push_back(i + 1);
}
for (int x : avail) {
bal = x;
if (putw(m - 1, x, 1)) {
cout << "YES\n";
cout << x << ' ';
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
cout << endl;
;
return;
}
}
cout << "NO\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t = 1;
while (t--) solve();
cout << "\n\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[110];
bool vis[110], tmp[110];
bool ok;
void dfs(int cur, int l, int f, int p) {
if (l > 1 && cur == f) {
if (l & 1) ok = true;
return;
}
int len = g[cur].size();
for (int i = 0; i < len; i++) {
if (!vis[g[cur][i]] && g[cur][i] != p) {
vis[g[cur][i]] = true;
dfs(g[cur][i], l + 1, f, cur);
}
}
}
int main() {
int n, m;
int u, v;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
int tot = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ok = false;
dfs(i, 0, i, 0);
if (ok) tot++;
vis[i] = true;
}
}
if ((n - tot) & 1) tot++;
printf("%d\n", tot);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long dp[2];
long long tp[2];
long long inf = 1e17;
int n;
long long r1, r2, r3;
long long d;
long long a[2000000];
int main() {
dp[0] = 0;
dp[1] = inf;
scanf("%d", &n);
scanf("%lld %lld %lld", &r1, &r2, &r3);
scanf("%lld", &d);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
long long ans = inf;
for (int i = 1; i <= n; i++) {
if (i < n) {
tp[0] = tp[1] = inf;
tp[0] = min(tp[0], dp[0] + r2 + d + d + r1 + d);
tp[0] = min(tp[0], dp[1] + r2 + d + r1 + d + r1 + d);
tp[0] = min(tp[0], dp[0] + (a[i] + 1) * r1 + d + d + r1 + d);
tp[0] = min(tp[0], dp[1] + (a[i] + 1) * r1 + d + r1 + d + r1 + d);
tp[0] = min(tp[0], dp[0] + a[i] * r1 + r3 + d);
tp[0] = min(tp[0], dp[1] + a[i] * r1 + r3 + d + r1 + d + d);
tp[1] = min(tp[1], dp[0] + r2 + d);
tp[1] = min(tp[1], dp[1] + r2 + d + r1 + d + d);
tp[1] = min(tp[1], dp[0] + (a[i] + 1) * r1 + d);
tp[1] = min(tp[1], dp[1] + (a[i] + 1) * r1 + d + r1 + d + d);
dp[0] = tp[0];
dp[1] = tp[1];
} else {
ans = min(ans, dp[0] + r2 + d + d + r1);
ans = min(ans, dp[1] + r2 + d + r1 + d + r1);
ans = min(ans, dp[0] + (a[i] + 1) * r1 + d + d + r1);
ans = min(ans, dp[1] + (a[i] + 1) * r1 + d + r1 + d + r1);
ans = min(ans, dp[0] + a[i] * r1 + r3);
ans = min(ans, dp[1] + a[i] * r1 + r3 + d + r1);
}
}
printf("%lld", ans);
return 0;
}
| 3 |
#include<cstdio>
int d[35][35],a[35][3];
int main()
{
int n,c;scanf("%d%d",&n,&c);
for(int i=1;i<=c;i++)
for(int j=1;j<=c;j++) scanf("%d",&d[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int x;scanf("%d",&x);
a[x][(i+j)%3]++;
}
int ans=0x3f3f3f3f;
for(int i=1;i<=c;i++)
for(int j=1;j<=c;j++)
for(int k=1;k<=c;k++)
{
if(i==j||j==k||i==k) continue;
int s=0;
for(int ii=1;ii<=c;ii++) s+=d[ii][i]*a[ii][0];
for(int ii=1;ii<=c;ii++) s+=d[ii][j]*a[ii][1];
for(int ii=1;ii<=c;ii++) s+=d[ii][k]*a[ii][2];
if(ans>s) ans=s;
}
printf("%d\n",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool isDigitOrLetter(char ch) {
if ('a' <= ch && ch <= 'z') return true;
if ('A' <= ch && ch <= 'Z') return true;
if ('0' <= ch && ch <= '9') return true;
return false;
}
void insertProhibits(string &s, set<string> &prohibit, vector<string> &users) {
string token;
vector<string>::iterator it;
for (char c : s) {
if (isDigitOrLetter(c))
token += c;
else {
if (!token.empty()) {
it = lower_bound(users.begin(), users.end(), token);
if (it != users.end() && *it == token) prohibit.insert(token);
}
token.clear();
}
}
if (!token.empty()) {
it = lower_bound(users.begin(), users.end(), token);
if (it != users.end() && *it == token) prohibit.insert(token);
}
}
void solve() {
int N;
cin >> N;
vector<string> all(N);
string s;
for (int i = 0; i < N; i++) {
cin >> s;
all[i] = s;
}
sort(all.begin(), all.end());
int M;
cin >> M;
getline(cin, s);
vector<string> user(M);
vector<string> message(M);
for (int i = 0; i < M; i++) {
getline(cin, s);
for (int j = 0; j < s.size(); j++) {
if (s[j] == ':') {
user[i] = s.substr(0, j);
message[i] = s.substr(j + 1);
break;
}
}
}
vector<set<string> > prohibit(M);
for (int i = 0; i < M; i++) {
if (user[i] != "?") continue;
if (i > 0 && user[i - 1] != "?") prohibit[i].insert(user[i - 1]);
if (i < M - 1 && user[i + 1] != "?") prohibit[i].insert(user[i + 1]);
insertProhibits(message[i], prohibit[i], all);
}
set<int> seen;
for (int i = 0; i < M; i++)
if (user[i] != "?") seen.insert(i);
while (true) {
int max_p = -1, pos = -1;
for (int i = 0; i < M; i++) {
if (seen.find(i) != seen.end()) continue;
if ((int)prohibit[i].size() > max_p) {
max_p = prohibit[i].size();
pos = i;
}
}
seen.insert(pos);
if (pos == -1) break;
if (max_p == N) {
cout << "Impossible\n";
return;
}
for (int i = 0; i < N; i++) {
if (prohibit[pos].find(all[i]) == prohibit[pos].end()) {
user[pos] = all[i];
if (pos > 0) prohibit[pos - 1].insert(user[pos]);
if (pos < M - 1) prohibit[pos + 1].insert(user[pos]);
break;
}
}
}
for (int i = 0; i < M; i++) cout << user[i] << ":" << message[i] << "\n";
}
int main() {
int T;
cin >> T;
while (T) {
T--;
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long A = 257;
const int MAXN = 600005;
int N, M;
long long p[MAXN];
void init() {
p[0] = 1;
for (int i = 1; i < MAXN; i++) p[i] = A * p[i - 1] % MOD;
}
long long hashi(string s) {
long long res = 0;
for (int i = 0; i < s.size(); i++) {
res = (A * res + s[i]) % MOD;
}
return res;
}
set<long long> h;
string S;
int main() {
cin >> N >> M;
init();
for (int i = 0; i < N; i++) {
cin >> S;
h.insert(hashi(S));
}
for (int i = 0; i < M; i++) {
cin >> S;
long long H = hashi(S);
bool ok = 0;
for (int j = 0; j < S.size(); j++) {
for (int k = 'a'; k < 'd'; k++) {
if (k == S[j]) continue;
long long x = (H + p[S.size() - j - 1] * (k - S[j]) + 3 * MOD) % MOD;
if (h.find(x) != h.end()) ok = 1;
}
}
cout << (ok ? "YES" : "NO") << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int in[200005], id[200005];
vector<int> G[200005];
vector<pair<int, int>> undir, ret;
int main() {
ios::sync_with_stdio(0);
int T, n, m;
cin >> T;
while (T--) {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
G[i].clear();
in[i] = id[i] = 0;
}
undir.clear();
ret.clear();
for (int i = 1, op, u, v; i <= m; ++i) {
cin >> op >> u >> v;
if (op == 1) {
in[v]++;
G[u].push_back(v);
ret.push_back({u, v});
} else {
if (u > v) swap(u, v);
undir.push_back({u, v});
}
}
sort(undir.begin(), undir.end());
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 1; i <= n; i++)
if (in[i] == 0) q.push(i);
vector<int> ans;
while (!q.empty()) {
int p = q.top();
q.pop();
ans.push_back(p);
for (auto to : G[p]) {
in[to]--;
if (in[to] == 0) q.push(to);
}
}
if (ans.size() < n) {
cout << "NO\n";
continue;
}
for (int i = 0; i < n; ++i) id[ans[i]] = i + 1;
bool ok = 1;
for (auto i : undir) {
if (id[i.first] < id[i.second])
ret.push_back(i);
else
ret.push_back({i.second, i.first});
}
cout << "YES\n";
for (auto i : ret) cout << i.first << " " << i.second << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, arr[100000 + 5];
void rd_1() {
int i;
cin >> n;
for (i = 0; i < n; i++) cin >> arr[i];
}
int main() {
ios::sync_with_stdio(0);
int i, j;
rd_1();
sort(arr, arr + n);
reverse(arr, arr + n);
int sum = 0;
for (i = 1; i < n; i++) sum += arr[i];
cout << arr[0] - sum + 1 << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int test(string str) {
int len = str.length();
int a = 0, b = 0, c = 0;
for (int i = 0; i < len; i++) {
if (str[i] == 'A')
a += 1;
else if (str[i] == 'B')
b += 1;
else if (str[i] == 'C')
c += 1;
}
if (a <= b && b - a == c) return 1;
return 0;
}
int main() {
int t;
cin >> t;
string s;
while (t--) {
cin >> s;
if (test(s))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
while (!isdigit(ch = getchar()))
if (ch == '-') f = -1;
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f * x;
}
const int N = 2e5 + 5;
int m, n, T, s[2][N], c[2], f[2], ans, q[N], t[N], type[N], tot;
long long a, b;
int main() {
m = read();
while (m--) {
n = read(), T = read(), a = read(), b = read();
c[0] = c[1] = f[0] = f[1] = ans = 0;
for (int i = 1; i <= n; ++i) type[i] = read(), s[0][i] = s[1][i] = 0;
for (int i = 1; i <= n; ++i) q[i] = t[i] = read();
sort(q + 1, q + 1 + n), tot = unique(q + 1, q + 1 + n) - q - 1;
for (int i = 1; i <= n; ++i) {
int j = lower_bound(q + 1, q + 1 + tot, t[i]) - q;
++s[type[i]][j], ++c[type[i]];
}
q[tot + 1] = max(q[tot] + 1, T + 1);
for (int i = 0; i <= tot; ++i) {
f[0] += s[0][i], f[1] += s[1][i];
int t = min(q[i + 1] - 1, T), resa = c[0] - f[0], resb = c[1] - f[1];
;
if (f[0] * a + f[1] * b > t) continue;
int rest = t - (f[0] * a + f[1] * b);
if (resa * a <= rest)
ans = max(f[0] + f[1] + resa + min((rest - resa * a) / b, 1ll * resb),
1ll * ans);
else
ans = max(f[0] + f[1] + rest / a, 1ll * ans);
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e15 + 7;
struct Dinic {
long long n, s, t;
struct Edge {
long long to, cap, flow, rev;
Edge(long long to = 0, long long cap = 0, long long flow = 0,
long long rev = 0)
: to(to), cap(cap), flow(flow), rev(rev) {}
};
vector<long long> d;
vector<long long> ptr;
vector<vector<Edge>> g;
Dinic(long long n = 0, long long s = 0, long long t = 0)
: n(n), s(s), t(t), d(n + 2), ptr(n + 2), g(n + 2) {}
void addEdge(long long u, long long v, long long cap) {
g[u].emplace_back(v, cap, 0, g[v].size());
g[v].emplace_back(u, 0, 0, g[u].size() - 1);
}
bool bfs() {
fill(d.begin(), d.end(), -1);
fill(ptr.begin(), ptr.end(), 0);
queue<long long> q;
d[s] = 0;
q.push(s);
while (!q.empty()) {
long long u = q.front();
q.pop();
for (auto e : g[u]) {
if (d[e.to] == -1 && e.cap > e.flow) {
d[e.to] = d[u] + 1;
q.push(e.to);
}
}
}
return d[t] != -1;
}
long long dfs(long long u, long long flow) {
if (u == t) {
return flow;
}
for (long long &i = ptr[u]; i < (long long)g[u].size(); i++) {
auto &e = g[u][i];
if (e.cap <= e.flow || d[e.to] != d[u] + 1) continue;
long long nxt = dfs(e.to, min(flow, e.cap - e.flow));
if (nxt) {
e.flow += nxt;
g[e.to][e.rev].flow -= nxt;
return nxt;
}
}
return 0;
}
long long maxFlow() {
long long ans = 0;
while (bfs()) {
while (true) {
long long now = dfs(s, inf);
if (now)
ans += now;
else
break;
}
}
return ans;
}
};
const long long N = 1e5 + 2;
const long long M = 52;
pair<pair<long long, long long>, pair<long long, long long>> rect[M];
pair<long long, long long> row_lis[N], col_lis[N];
vector<long long> lis, lis1;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n, i, j, k, l, m, num_row = 0, num_col = 0, z, t1, z1;
cin >> n >> m;
if (m == 0) {
cout << 0;
return 0;
}
for (i = 1; i <= m; i++) {
cin >> rect[i].first.first >> rect[i].first.second >>
rect[i].second.first >> rect[i].second.second;
lis.push_back(rect[i].first.first - 1);
lis.push_back(rect[i].second.first);
lis1.push_back(rect[i].first.second - 1);
lis1.push_back(rect[i].second.second);
}
sort(lis.begin(), lis.end());
sort(lis1.begin(), lis1.end());
lis.erase(unique(lis.begin(), lis.end()), lis.end());
lis1.erase(unique(lis1.begin(), lis1.end()), lis1.end());
num_row = lis.size() - 1;
num_col = lis1.size() - 1;
long long s = num_row + num_col + 1, t = num_row + num_col + 2;
Dinic dn(num_row + num_col + 2, s, t);
for (i = 1; i < lis.size(); i++) {
dn.addEdge(s, i, lis[i] - lis[i - 1]);
}
for (i = 1; i < lis1.size(); i++) {
dn.addEdge(num_row + i, t, lis1[i] - lis1[i - 1]);
}
for (i = 1; i <= m; i++) {
l = lower_bound(lis.begin(), lis.end(), rect[i].first.first - 1) -
lis.begin();
z1 =
lower_bound(lis.begin(), lis.end(), rect[i].second.first) - lis.begin();
z = lower_bound(lis1.begin(), lis1.end(), rect[i].first.second - 1) -
lis1.begin();
t1 = lower_bound(lis1.begin(), lis1.end(), rect[i].second.second) -
lis1.begin();
for (j = l + 1; j <= z1; j++) {
for (k = z + 1; k <= t1; k++) {
dn.addEdge(j, k + num_row, inf);
}
}
}
cout << dn.maxFlow();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct node {
bool friend operator<(node n1, node n2) {
if (n1.val == n2.val) {
return n1.id > n2.id;
} else {
return n1.val > n2.val;
}
}
int id, val;
};
priority_queue<node> que;
vector<int> pre[maxn];
int degree[maxn];
int n;
int solve() {
node cur, tmp;
int i, res, cnt, u, v;
while (!que.empty()) {
que.pop();
}
res = 0, cnt = 0;
for (i = 1; i <= n; i++) {
if (degree[i] == 0) {
tmp.id = i, tmp.val = 1;
que.push(tmp);
res = max(res, tmp.val), cnt++;
}
}
while (!que.empty()) {
cur = que.top();
que.pop();
u = cur.id;
for (i = 0; i < pre[u].size(); i++) {
v = pre[u][i];
degree[v]--;
if (degree[v] == 0) {
tmp.id = v;
if (v < u)
tmp.val = cur.val + 1;
else
tmp.val = cur.val;
que.push(tmp);
res = max(res, tmp.val), cnt++;
}
}
}
if (cnt < n)
return -1;
else
return res;
}
int main() {
int t, i, j, k;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
pre[i].clear();
degree[i] = 0;
}
for (i = 1; i <= n; i++) {
scanf("%d", &k);
while (k--) {
scanf("%d", &j);
pre[j].push_back(i);
degree[i]++;
}
}
printf("%d\n", solve());
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#include <deque>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,a,b) for (int i = (a); i < (b); i++)
typedef long long ll;
int main(void)
{
int n, q, query, t, x;
cin >> n >> q;
vector<deque<int> > S(n);
rep(i, q)
{
cin >> query >> t;
if (query == 0)
{
cin >> x;
S[t].push_back(x);
}
else if (query == 1)
{
if (S[t].size())
{
cout << S[t].back() << endl;
}
}
else
{
if (S[t].size())
{
S[t].pop_back();
}
}
}
}
| 0 |
#include<iostream>
using namespace std;
int main(){
int r,a;
cin>>r;
a=3*r*r;
cout<<a;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long ans;
long long m, n, s;
long long maxx;
int q;
int main() {
bool tip = true;
if (tip == true) {
cin >> q;
while (q--) {
cin >> m >> n >> s;
maxx = max(m, n);
if (maxx > s) {
cout << "-1" << endl;
continue;
} else if ((m + n) % 2 == 0) {
ans = maxx;
if ((s - maxx) % 2 == 0) {
ans += s - maxx;
} else {
ans += s - maxx - 2;
}
} else {
ans = maxx - 1;
ans += s - ans - 1;
}
cout << ans << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
namespace DECLARATIONS {
using namespace std;
using ll = long long;
using PI = pair<int, int>;
template <class T>
using V = vector<T>;
using VI = V<int>;
template <class T>
inline void debug(T &A) {}
template <class T>
inline void debug_dim2(T &A) {}
template <typename... Args>
inline void debug(const char *format, Args const &...args) {}
template <typename... Args>
string format(const std::string &fmt, Args... args) {
size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args...);
std::vector<char> buf(len + 1);
std::snprintf(&buf[0], len + 1, fmt.c_str(), args...);
return std::string(&buf[0], &buf[0] + len);
}
} // namespace DECLARATIONS
using namespace DECLARATIONS;
const int MOD = 1000000007;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
string S;
cin >> S;
V<VI> dp(N + 1, VI(N + 1));
dp[0][0] = 1;
VI last(26);
for (int i = 0; i < N; ++i) {
int c = S[i] - 'a';
for (int j = last[c]; j <= i; ++j) {
for (int k = 1; k < N + 1; ++k) {
dp[i + 1][k] += dp[j][k - 1];
dp[i + 1][k] = min(dp[i + 1][k], K);
}
}
debug(dp[i + 1]);
last[c] = i + 1;
}
int total = 0;
int ans = 0;
for (int j = N; j >= 0; --j) {
int acc = 0;
for (int i = 0; i <= N; ++i) {
acc += dp[i][j];
acc = min(acc, K);
}
int cnt = min(K - total, acc);
total += cnt;
ans += (N - j) * cnt;
}
cout << (total == K ? ans : -1);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int mp[1010000], ans;
int main() {
int n, m, x;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
mp[x]++;
}
for (int i = 0; i <= 1e6 + 21; i++) {
int num = i;
int numcnt = mp[i];
if (numcnt % 2 == 1) ans++;
mp[num + 1] += numcnt / 2;
}
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, k, h[200003], nw[200003];
long long f[200003], inv[200003];
long long mypow(long long x, long long times) {
if (x == 0) return times == 0;
if (times == 0) return 1;
long long ret = mypow(x, times / 2);
ret = ret * ret % mod;
if (times % 2) ret = ret * x % mod;
return ret;
}
long long C(long long x, long long y) {
return (f[x] * inv[y] % mod) * inv[x - y] % mod;
}
int main() {
f[0] = 1;
for (int i = 1; i < 200003; i++) f[i] = f[i - 1] * i % mod;
for (int i = 0; i < 200003; i++) inv[i] = mypow(f[i], mod - 2);
cin >> n >> k;
if (k == 1) {
cout << 0;
return 0;
}
for (int i = 0; i < n; i++) scanf("%d", h + i);
for (int i = 0; i < n; i++) nw[i] = h[(i + 1) % n];
int cnt = 0;
for (int i = 0; i < n; i++) cnt += (h[i] != nw[i]);
long long ans = mypow(k, cnt);
for (int i = 0; i <= cnt / 2; i++)
ans =
(ans + mod -
C(cnt, i) * (C(cnt - i, i) * mypow(k - 2, cnt - i * 2) % mod) % mod) %
mod;
ans = ans * mypow(k, n - cnt) % mod;
cout << ans * 499122177 % mod;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, n, ans = 0;
cin >> n;
int v[n + 1];
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] != -1) v[i]--;
}
for (int i = 0; i < n; i++) {
int j = v[i];
x = 1;
while (j != -1) {
x++;
j = v[j];
}
ans = max(x, ans);
}
cout << ans;
return 0;
}
| 1 |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1000000007;
int n,C[51][51],dp[51][101];
int main()
{
for(int i=0;i<=50;++i)
{
C[i][0]=1;
for(int j=1;j<=i;++j)(C[i][j]=C[i-1][j-1]+C[i-1][j])>=mod?C[i][j]-=mod:0;
}
scanf("%d",&n);
dp[1][n]=n,dp[1][n-1]=n;
for(int i=1;i<n;++i)
for(int j=-n;j<=n;++j)
if(dp[i][j+n])
for(int k=max(1,abs(j));k<=n-i;++k)//num of 2^i
if(!(j+k&1))
for(int l=0;l<=k;++l)//negative
{
int t=(k-2*l+j)>>1;
if(t>=-n&&t<=n)dp[i+k][t+n]=(dp[i+k][t+n]+1ll*C[n-i][k]*C[k][l]%mod*dp[i][j+n])%mod;
}
printf("%d",dp[n][n]);
return 0;
} | 0 |
#include<cstdio>
#include<cmath>
int main(){
int n;
for(;;){
scanf("%d",&n);
if(n==-1)break;
double x=1,y=0;
double rad=atan2(-y,-x);
for(int i=2;i<=n;i++){
rad-=M_PI_2;
x+=cos(rad);
y+=sin(rad);
rad=atan2(-y,-x);
}
printf("%lf\n%lf\n",x,y);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool dbg = 0;
void bad(string mes = "Impossible") {
cout << mes;
exit(0);
}
template <typename T>
string bin(T x, int st = 2) {
string ans = "";
while (x > 0) {
ans += char('0' + x % st);
x /= st;
}
reverse(ans.begin(), ans.end());
return ans.empty() ? "0" : ans;
}
template <typename T>
T input() {
T ans = 0, m = 1;
char c = ' ';
while (!((c >= '0' && c <= '9') || c == '-')) {
c = getchar();
}
if (c == '-') m = -1, c = getchar();
while (c >= '0' && c <= '9') {
ans = ans * 10 + (c - '0'), c = getchar();
}
return ans * m;
}
template <typename T>
void read(T& a) {
a = input<T>();
}
template <typename T>
void read(T& a, T& b) {
read(a), read(b);
}
template <typename T>
void read(T& a, T& b, T& c) {
read(a, b), read(c);
}
template <typename T>
void read(T& a, T& b, T& c, T& d) {
read(a, b), read(c, d);
}
void reads(string& second) {
string ans = "";
char c = '\n';
while (c == '\n' || c == ' ') c = getchar();
while (c != '\n' && c != ' ' && c) ans += c, c = getchar();
second = ans;
}
const int inf = 1e9;
const double eps = 1e-9;
const int maxn = 1e6 + 10, base = 1e9 + 7;
const long long llinf = 1e18 + 5;
template <typename T>
T binpow(T n, T second) {
if (second <= 0) return 1LL;
if (second % 2 == 0) {
T b = binpow(n, second / 2);
return (1LL * b * b) % base;
} else {
return (1LL * binpow(n, second - 1) * n) % base;
}
}
int v[maxn], c[maxn];
int lg[maxn];
int mnSt[maxn][22];
int n, k;
void buildMn() {
for (int i = 0; i < n; i++) mnSt[i][0] = c[i];
for (int i = 1; i < 22; i++)
for (int j = 0; j + (1 << i) <= n; j++) {
mnSt[j][i] = min(mnSt[j][i - 1], mnSt[j + (1 << (i - 1))][i - 1]);
}
}
int mxSt[maxn][22];
void buildMx() {
for (int i = 0; i < n; i++) mxSt[i][0] = v[i];
for (int i = 1; i < 22; i++)
for (int j = 0; j + (1 << i) <= n; j++) {
mxSt[j][i] = max(mxSt[j][i - 1], mxSt[j + (1 << (i - 1))][i - 1]);
}
}
int get_min(int l, int r) {
int j = lg[r - l + 1];
return min(mnSt[l][j], mnSt[r - (1 << j) + 1][j]);
}
int get_max(int l, int r) {
int j = lg[r - l + 1];
return max(mxSt[l][j], mxSt[r - (1 << j) + 1][j]);
}
int main() {
for (int i = 2; i < maxn; i++) {
lg[i] = lg[i / 2] + 1;
}
read(n, k);
for (int i = 0; i < n; i++) read(v[i]), v[i] *= 100;
for (int i = 0; i < n; i++) read(c[i]);
buildMn();
buildMx();
long double res = 0;
vector<int> mda;
for (int i = 0; i < n; i++) {
int l = i, r = n - 1;
while (r - l > 1) {
int m = (l + r) >> 1;
if (get_min(i, m) < get_max(i, m))
r = m;
else
l = m;
}
int kek = -inf;
for (int j = max(i, l - 5); j <= min(n - 1, l + 5); j++) {
kek = max(kek, min(get_min(i, j), get_max(i, j)));
}
mda.push_back(kek);
}
sort((mda).begin(), (mda).end());
long double cf = (long double)n / k;
int first = n - 1;
for (int i = 0; first >= k - 1; i++, first--) {
res += (long double)mda[i] / cf;
cf *= (long double)n - i - 1;
cf /= (long double)n - k - i;
}
cout << fixed << setprecision(20) << res;
return 0;
}
| 5 |
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
int main() {
int l, k;
cin >> l >> k;
vector<vector<ll>> dp(l+1, vector<ll>(2));
dp[0][1] = 1;
for (int i = 1; i <= l; ++i) {
dp[i][0] += dp[i-1][1];
dp[i][1] += dp[i-1][0];
if(i >= k){
dp[i][0] += dp[i-k][1];
}
}
ll ans = 0;
for (int i = 1; i <= l; ++i) {
ans += dp[i][0];
}
cout << ans << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const int MAX = 50000;
int n, m;
char a[20][20];
int vis[20][20];
struct Pos {
int step;
queue<pair<int, int> > q;
int cnt[16];
Pos() {
step = 0;
memset(cnt, 0, sizeof(cnt));
}
inline void push(pair<int, int> x) { q.push(x); }
inline pair<int, int> front() { return q.front(); }
inline void pop() { q.pop(); }
inline void add() { step++; }
inline void Print() {
queue<pair<int, int> > tmp = q;
while (!q.empty()) {
pair<int, int> x = q.front();
printf("%d %d\n", x.first, x.second);
q.pop();
}
q = tmp;
}
inline void Color() {
queue<pair<int, int> > tmp = q;
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
a[x.first][x.second] = '#';
}
q = tmp;
}
inline void Recover() {
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
a[x.first][x.second] = '.';
}
}
};
struct Queue {
Pos p[MAX + 5];
int head, rear;
Queue() { head = 0, rear = 1; }
inline bool empty() {
return (head == rear - 1 || (rear == 1 && head == MAX));
}
inline int size() {
if (head < rear && !empty())
return head + MAX - rear + 1;
else
return head - rear + 1;
}
inline void push(Pos x) {
p[++head] = x;
if (head > MAX) head = 1;
}
inline Pos pop() {
Pos res = p[rear++];
if (rear > MAX) rear = 1;
return res;
}
} Q;
inline bool inBoard(int x, int y) {
return (x > 0 && x <= n && y > 0 && y <= m);
}
inline void Print(Pos p) {
int idx = 1;
int sz = p.q.size();
char map[20][20];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) map[i][j] = a[i][j];
while (!p.q.empty()) {
pair<int, int> tmp = p.q.front();
p.q.pop();
map[tmp.first][tmp.second] = char('0' + sz - idx + 1);
idx++;
}
for (int i = 1; i <= n; i++, putchar('\n'))
for (int j = 1; j <= m; j++) putchar(map[i][j]);
putchar('\n');
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
a[i][j] = getchar();
while (a[i][j] == ' ' || a[i][j] == '\n') a[i][j] = getchar();
}
Pos init;
bool flag = true;
int k = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] >= '0' && a[i][j] <= '9') k = max(k, int(a[i][j] - '0'));
for (; k >= 1; k--)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == char(k + '0')) {
init.push(make_pair(i, j));
a[i][j] = '.';
}
Q.push(init);
while (!Q.empty()) {
Pos cur = Q.pop();
cur.pop();
pair<int, int> tmp = cur.q.back();
int x = tmp.first, y = tmp.second;
cur.Color();
for (int i = 0; i < 4; i++) {
Pos next = cur;
int nx = x + dx[i], ny = y + dy[i];
if (!inBoard(nx, ny)) continue;
if (a[nx][ny] == '#') continue;
if (vis[nx][ny] > 200) continue;
vis[nx][ny]++;
next.push(make_pair(nx, ny));
next.add();
Q.push(next);
if (a[nx][ny] == '@') {
printf("%d\n", next.step);
return 0;
}
}
cur.Recover();
}
printf("-1\n");
return 0;
}
| 4 |
#include <cctype>
#include <cstdio>
#include <algorithm>
#define rep(i,x,y) for (int i=x; i<=y; ++i)
#define repd(i,x,y) for (int i=x; i>=y; --i)
int get()
{
char c;
while (!isdigit(c=getchar()));
int k=c-'0';
for (; isdigit(c=getchar()); k=k*10+c-'0');
return k;
}
using namespace std;
const int N=3010,mod=1e9+7;
char s[N];
int n,m,ans,c[N][N],f[N][N],a[N];
struct data
{
int l,r;
bool operator<(const data &x) const
{
return l<x.l || (l==x.l && r>x.r);
}
} b[N];
int main()
{
n=get(),m=get();
rep(i,0,n)
{
c[i][0]=1;
rep(j,1,i)
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
scanf("%s",s+1);
rep(i,1,n)
a[i]=a[i-1]+s[i]-48;
rep(i,1,m)
b[i].l=get(),b[i].r=get();
sort(b+1,b+1+m);
int t=0;
rep(i,1,m)
if (b[i].r>b[t].r)
b[++t]=b[i];
m=t,b[m+1].l=n+1,f[0][0]=1;
rep(i,1,m)
{
int w=a[b[i].r]-a[max(b[i].l-1,b[i-1].r)];
int l=min(b[i].r,b[i+1].l-1)-b[i].l+1;
int mx=b[i].r-b[i].l+1-l;
rep(j,0,l)
rep(k,max(0,w-j),mx)
f[i][k]=(f[i][k]+1ll*f[i-1][j+k-w]*c[l][j])%mod;
}
rep(i,0,n)
ans=(ans+f[m][i])%mod;
printf("%d",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int d[3][100010], n, m, D;
vector<int> g[100010], v;
bool has[100010];
void dfs(int f, int x, int id, int h) {
d[id][x] = h;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i];
if (to != f) dfs(x, to, id, h + 1);
}
}
int main() {
memset(d, -1, sizeof(d));
scanf("%d%d%d", &n, &m, &D);
for (int i = 0; i < m; i++) {
int x;
scanf("%d", &x);
x--;
v.push_back(x);
has[x] = true;
}
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(-1, v[0], 0, 0);
int maxn = -1;
int l, r;
for (int i = 0; i < n; i++)
if (d[0][i] > maxn && has[i]) {
maxn = d[0][i];
l = i;
}
dfs(-1, l, 1, 0);
maxn = -1;
for (int i = 0; i < n; i++)
if (d[1][i] > maxn && has[i]) {
maxn = d[1][i];
r = i;
}
dfs(-1, r, 2, 0);
int ans = 0;
for (int i = 0; i < n; i++)
if (d[1][i] <= D && d[2][i] <= D) ans++;
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int a[105];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
int ans = 0;
while (1) {
ans++;
int cnt = 1;
for (int i = 2; i <= n; i++) {
cnt += a[i] != a[i - 1];
}
if (cnt <= m) {
cout << ans << '\n';
return;
}
cnt = 0;
for (int i = 1; i <= n; i++) {
cnt += a[i] != a[i - 1] || i == 1;
if (cnt == m) {
if (!a[i]) {
cout << -1 << '\n';
return;
}
for (int j = i + 1; j <= n; j++) a[j] -= a[i];
for (int j = 1; j <= i; j++) a[j] = 0;
break;
}
}
}
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int> > v;
vector<long long int> s, a;
long long int n;
long long int dfs1(long long int u, long long int par) {
s[u] = 1;
for (auto it : v[u]) {
if (it != par) {
s[u] += dfs1(it, u);
a.push_back(s[it] * 1ll * (n - s[it]));
}
}
return s[u];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
cin >> t;
while (t--) {
long long int m, i, j, x, y, ans = 0;
cin >> n;
v.clear();
s.clear();
a.clear();
v.resize(n + 1);
s.resize(n + 1);
a.clear();
for (i = 1; i < n; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cin >> m;
vector<long long int> p(m);
for (i = 0; i < m; i++) cin >> p[i];
sort(p.begin(), p.end(), greater<long long int>());
vector<long long int> q;
i = 1;
if (m > (n - 1)) {
while (i <= (m - (n - 1))) {
p[i] = (p[i] * p[i - 1]) % 1000000007;
i++;
}
}
i--;
while (i < m) {
q.push_back(p[i]);
i++;
}
while (q.size() < (n - 1)) q.push_back(1);
sort(q.begin(), q.end(), greater<long long int>());
dfs1(1, -1);
sort(a.begin(), a.end(), greater<long long int>());
for (i = 0; i < (n - 1); i++)
ans = (ans + (a[i] * 1ll * q[i]) % 1000000007) % 1000000007;
cout << ans << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100005;
int C[MaxN], n, m;
bool guilty[MaxN];
int G[MaxN], nG[MaxN], ngCount;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> C[i];
if (C[i] > 0)
G[C[i]]++;
else {
nG[-C[i]]++;
ngCount++;
}
}
int guiltyCount = 0;
for (int i = 1; i <= n; i++)
if ((G[i] + ngCount - nG[i]) == m) {
guilty[i] = true;
guiltyCount++;
}
for (int i = 1; i <= n; i++)
if (C[i] > 0) {
if (!guilty[C[i]])
cout << "Lie\n";
else if (guiltyCount == 1)
cout << "Truth\n";
else
cout << "Not defined\n";
} else {
if (!guilty[-C[i]])
cout << "Truth\n";
else if (guiltyCount == 1)
cout << "Lie\n";
else
cout << "Not defined\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
int main() {
string line;
getline(cin, line);
int c4 = 0, c7 = 0;
for (int i = (0); i < (int((line).size())); ++i) {
if (line[i] == '4') ++c4;
if (line[i] == '7') ++c7;
}
if (!c4 && !c7) {
printf("-1\n");
return 0;
}
if (c4 >= c7)
printf("4\n");
else
printf("7\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, count = 0;
double k;
cin >> n >> k;
long long int upper = 2 * k;
long long int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (upper >= a[i]) {
upper = max(upper, 2 * a[i]);
} else {
while (upper < a[i]) {
upper *= 2;
count++;
}
upper = max(upper, 2 * a[i]);
}
}
cout << count;
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <functional>
#include <map>
#include <queue>
#include <string>
#include <deque>
#include <set>
#include <bitset>
#include <cstring>
#include <unordered_map>
//#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define p_b push_back
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define mk_p make_pair
typedef long long ll;
const char sl='\n';
const int Msize = 105;
//// C....
const int N = 2e5;
int acum[N+1];
ll carmen[N+1];
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
carmen[3] = 1;
for(int i = 4;i<N+1;++i)
{
carmen[i] = carmen[i-1] + (1LL*(i-2)*(i-1)/2);
}
int t; cin >> t;
while(t--)
{
int n; cin >> n;
for(int i =1;i<=n;++i)
{
acum[i] = 0;
}
for(int i = 0;i<n;++i)
{
int x; cin >> x;
acum[x]++;
}
ll ans =0;
for(int i = 1;i<=n-2;++i)
{
ans += (acum[i]*acum[i+1]*acum[i+2]);
}
for(int i = 1;i<=n-1;++i)
{
ans += (((1LL*acum[i]*(acum[i]-1))/2) * acum[i+1]);
ans += (((1LL*acum[i+1]*(acum[i+1]-1))/2) * acum[i]);
}
for(int i = 1;i<=n-2;++i)
{
ans += (((1LL*acum[i]*(acum[i]-1))/2) * acum[i+2]);
ans += (((1LL*acum[i+2]*(acum[i+2]-1))/2) * acum[i]);
}
for(int i = 1;i<=n;++i)
{
if(acum[i])
{
ans+=carmen[acum[i]];
}
}
cout<<ans<<sl;
}
return 0;
} | 5 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
ll x = 0;
for (ll i = 2; i < N; i++) x ^= A[i];
ll sum = A[0]+A[1];
if (sum < x || (sum-x)%2 == 1) {
cout << -1 << endl;
return 0;
}
ll b = (sum-x)/2;
if (b > A[0]) {
cout << -1 << endl;
return 0;
}
for (int i = 50; i >= 0; i--) {
if ((b>>i)&1LL) continue;
if (((x>>i)&1LL) == 0) continue;
if (b+(1LL<<i) > A[0]) continue;
b += (1LL<<i);
}
if (b == 0 || (b^(sum-b)) != x) {
cout << -1 << endl;
return 0;
}
cout << A[0]-b << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
int main() {
long double a, b, c;
std::cin >> a >> b >> c;
puts(((c - a - b) * (c - a - b) <= 4 * a * b) || (c < a + b) ? "No" : "Yes");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int N, k, a[100113], t, f, l;
bool solve() {
if (k == 0) {
if (a[0] == 0 || a[0] == 12345 && f == 1) {
return true;
}
return false;
}
if (t != 0) {
return l == 1;
}
for (int i = int(0); i < int(N); i++) {
if (a[i] % k != 0) {
return false;
}
a[i] /= k;
a[i + 1] -= a[i];
}
return a[N] == 0;
}
int main() {
scanf("%d%d", &N, &k);
k = -k;
for (int i = int(0); i < int(N + 1); i++) {
int x;
if (scanf("%d", &x) != 1) {
scanf(" ?");
x = 12345;
t++;
}
a[i] = x;
}
f = (N - t + 1) % 2;
l = N % 2;
if (solve()) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string s;
int n, a, c, g, t, q, maxi, k;
int main() {
cin >> n >> s;
if (n % 4 != 0) {
cout << "===";
return 0;
}
k = n / 4;
for (int i = 0; i < n; i++) {
if (s[i] == 'A') a++;
if (s[i] == 'C') c++;
if (s[i] == 'G') g++;
if (s[i] == 'T') t++;
}
maxi = max(a, max(c, max(g, t)));
if (k < maxi) {
cout << "===";
return 0;
}
a = k - a;
g = k - g;
c = k - c;
t = k - t;
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
if (a > 0) {
s[i] = 'A';
a--;
} else if (g > 0) {
s[i] = 'G';
g--;
} else if (c > 0) {
s[i] = 'C';
c--;
} else if (t > 0) {
s[i] = 'T';
t--;
}
}
}
cout << s;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct code {
int x, y;
};
int sarala(code a, code b) { return a.x < b.x or (a.x == b.x and a.y < b.y); }
int gcd(int a, int b) {
while (b != 0) {
a = a % b;
swap(a, b);
}
return a;
}
int cnt;
string number(int n) {
if (n == 0) return "zero";
if (n == 1) return "one";
if (n == 2) return "two";
if (n == 3) return "three";
if (n == 4) return "four";
if (n == 5) return "five";
if (n == 6) return "six";
if (n == 7) return "seven";
if (n == 8) return "eight";
if (n == 9) return "nine";
if (n == 10) return "ten";
if (n == 11) return "eleven";
if (n == 12) return "twelve";
if (n == 13) return "thirteen";
if (n == 14) return "fourteen";
if (n == 15) return "fifteen";
if (n == 16) return "sixteen";
if (n == 17) return "seventeen";
if (n == 18) return "eighteen";
if (n == 19) return "nineteen";
}
string numbe(int n) {
n = n / 10;
if (n == 2) return "twenty";
if (n == 3) return "thirty";
if (n == 4) return "forty";
if (n == 5) return "fifty";
if (n == 6) return "sixty";
if (n == 7) return "seventy";
if (n == 8) return "eighty";
if (n == 9) return "ninety";
}
int main() {
int n, i = 1;
cin >> n;
if (n < 20) {
cout << number(n);
return 0;
} else {
cout << numbe(n);
if (n % 10 != 0) {
cout << "-" << number(n % 10);
}
}
}
| 1 |
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
enum FACE { BACK, RIGHT, TOP, BOTTOM, LEFT, FRONT };
template <class T>
class Dice
{
public:
Dice() {
id[TOP] = 2; id[FRONT] = 5; id[LEFT] = 4;
id[RIGHT] = 1; id[BACK] = 0; id[BOTTOM] = 3;
}
T& operator[] (FACE f) { return var[id[f]]; }
const T& operator[] (FACE f) const { return var[id[f]]; }
void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
void roll(FACE a, FACE b, FACE c, FACE d) {
int tmp = id[a];
id[a] = id[b];
id[b] = id[c];
id[c] = id[d];
id[d] = tmp;
}
T var[6];
int id[6];
};
int repaint(vector<Dice<int> >& cubes) {
int res = 0, N = cubes.size();
for(int j=0; j<6; j++) {
int C[50] = {0}, same = 0;
for(int i=0; i<N; i++) {
same = max(same, ++C[cubes[i][(FACE)j]]);
}
res += N - same;
}
return res;
}
int dfs(int p, int N, vector<Dice<int> >& cubes) {
if(p == N) return repaint(cubes);
int res = (1<<28);
Dice<int>& d = cubes[p];
for(int k=0; k<6; (k&1?d.roll_y():d.roll_x()),k++) {
for(int i=0; i<4; d.roll_z(), i++) {
res = min(res, dfs(p+1, N, cubes));
}
}
return res;
}
int main()
{
int N;
while(cin >> N, N) {
vector<Dice<int> > cubes(N);
map<string, int> dic;
int color = 0;
for(int i=0; i<N; i++) {
for(int j=0; j<6; j++) {
string s;
cin >> s;
if(!dic.count(s)) dic[s] = color++;
cubes[i][(FACE)j] = dic[s];
}
}
cout << dfs(0, N, cubes) << endl;
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
#define MAX_N 100005
int N,Q;
bool t[MAX_N];
bool u[MAX_N];
int main(){
cin>>N>>Q;
t[1]=true;
u[1]=u[2]=true;
for(int i=0;i<Q;i++){
int a,b;
cin>>a>>b;
if(t[a-1]||t[a+1]){
u[b]=true;
}
if(t[b-1]||t[b+1]){
u[a]=true;
}
swap(t[a],t[b]);
swap(u[a],u[b]);
if(t[a])u[a-1]=u[a]=u[a+1]=true;
if(t[b])u[b-1]=u[b]=u[b+1]=true;
}
int ans=0;
for(int i=1;i<=N;i++){
if(u[i]){
ans++;
// cout<<i<<endl;
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
class Ruler {
public:
template <typename Iterator>
Ruler(Iterator begin, Iterator end) : marks_(begin, end) {}
void CanMeasure(int distance, bool &can_measure, int &left_mark,
int &right_mark) {
for (int left = 0; left < marks_.size(); ++left) {
int to_search = distance + marks_[left];
int found_place = BinarySearch(to_search, left, marks_.size());
if (found_place != -1) {
left_mark = left;
right_mark = found_place;
can_measure = true;
return;
}
}
can_measure = false;
}
void CanMeasureRight(int distance, bool &can_measure, int &left_mark,
int &right_mark) {
for (int left = marks_.size() - 1; left >= 0; --left) {
int to_search = distance + marks_[left];
int found_place = BinarySearch(to_search, left, marks_.size());
if (found_place != -1) {
left_mark = left;
right_mark = found_place;
can_measure = true;
return;
}
}
can_measure = false;
}
int BinarySearch(int value, int left_bound, int right_bound) {
while (left_bound < right_bound - 1) {
int middle = (left_bound + right_bound) / 2;
if (marks_[middle] == value) {
return middle;
} else if (marks_[middle] > value) {
right_bound = middle;
} else {
left_bound = middle;
}
}
return -1;
}
private:
vector<int> marks_;
};
vector<int> GetExtraMarksToMeasure(const vector<int> &marks, int girl_distance,
int boy_distance) {
Ruler ruler(marks.begin(), marks.end());
int left_mark, right_mark;
bool can_measure_boy, can_measure_girl, can_measure_sum, can_measure_diff;
ruler.CanMeasure(girl_distance, can_measure_girl, left_mark, right_mark);
ruler.CanMeasure(boy_distance, can_measure_boy, left_mark, right_mark);
if (can_measure_boy && can_measure_girl) {
return {};
}
if (can_measure_girl) {
return {boy_distance};
}
if (can_measure_boy) {
return {girl_distance};
}
ruler.CanMeasure(girl_distance + boy_distance, can_measure_sum, left_mark,
right_mark);
if (can_measure_sum) {
return {marks[left_mark] + girl_distance};
}
int length = *marks.rbegin();
ruler.CanMeasure(boy_distance - girl_distance, can_measure_diff, left_mark,
right_mark);
if (can_measure_diff) {
if (length - marks[right_mark] >= girl_distance) {
return {marks[right_mark] + girl_distance};
}
}
ruler.CanMeasureRight(boy_distance - girl_distance, can_measure_diff,
left_mark, right_mark);
if (can_measure_diff) {
if (marks[left_mark] >= girl_distance) {
return {marks[left_mark] - girl_distance};
}
}
return {girl_distance, boy_distance};
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t marks_count, length, girl_distance, boy_distance;
cin >> marks_count >> length >> girl_distance >> boy_distance;
vector<int> marks(marks_count);
for (size_t i = 0; i < marks_count; ++i) {
cin >> marks[i];
}
vector<int> extra_marks =
GetExtraMarksToMeasure(marks, girl_distance, boy_distance);
cout << extra_marks.size() << endl;
for (int mark : extra_marks) {
cout << mark << ' ';
}
cout << endl;
return 0;
}
| 2 |
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;
signed main(){
int n; cin >> n;
string s; cin >> s;
int d; cin >> d;
set<int> st;
for(int i = 0; d > 0 && i < (int)s.size(); i++){
if(s[i] == '0'){
d--;
s[i] = '1';
st.insert(i);
}
}
for(int i = (int)s.size() - 1; d > 0 && i >= 0; i--){
if(s[i] == '1' && st.find(i) == st.end()){
d--;
s[i] = '0';
}
}
cout << s << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int a[1009][1009];
int n,m,k;
int x;
vector<int> v,v2,v3;
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=k;i++)
{
cin>>x;
for(int j=0;j<x;j++)
v.push_back(i);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int ans1=i*m+j;
int ans2=i*m+m-1-j;
if(i%2==0)
cout<<v[ans1]<<" ";
else
cout<<v[ans2]<<" ";
}
cout<<endl;
}/*
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
*/}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, mod = 1e9 + 7;
int bit[maxn << 1];
int s[maxn], t[maxn], n, m;
vector<int> v;
vector<pair<int, int> > p;
void add(int i, int val) {
while (i <= v.size()) {
bit[i] += val;
if (bit[i] >= mod) bit[i] -= mod;
;
i += i & -i;
}
}
int sum(int i) {
int cnt = 0;
while (i) {
cnt += bit[i];
if (cnt >= mod) cnt -= mod;
i &= i - 1;
}
return cnt;
}
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> s[i] >> t[i];
v.push_back(s[i]);
v.push_back(t[i]);
}
v.push_back(0);
v.push_back(n);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 0; i < m; i++) {
s[i] = lower_bound(v.begin(), v.end(), s[i]) - v.begin() + 1;
t[i] = lower_bound(v.begin(), v.end(), t[i]) - v.begin() + 1;
p.push_back(pair<int, int>(t[i], s[i]));
}
sort(p.begin(), p.end());
add(1, 1);
for (int i = 0; i < p.size(); i++)
add(p[i].first, (sum(p[i].first - 1) - sum(p[i].second - 1) + mod) % mod);
cout << (sum(v.size()) - sum(v.size() - 1) + mod) % mod << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 11;
const int Mod = 1e9 + 7;
void at(long long& a) { a %= Mod; }
struct Matrix {
int n;
long long arr[N];
void clear() {
n = 0;
memset(arr, 0, sizeof(arr));
}
friend Matrix operator*(const Matrix& a, const Matrix& b) {
assert(a.n == b.n);
Matrix tmp;
tmp.clear();
tmp.n = a.n;
for (int i = 0; i < tmp.n; ++i)
for (int j = 0, k = i; j <= i; ++j, --k) {
tmp.arr[i] += a.arr[j] * b.arr[k];
at(tmp.arr[i]);
}
return tmp;
}
void print() {
for (int i = 0; i < n - 1; ++i) {
printf("%lld ", arr[i]);
}
printf("%lld\n", arr[n - 1]);
}
};
Matrix tmp, ans, all;
int n, k;
void init() {
tmp.n = n;
for (int i = 0; i < tmp.n; ++i) tmp.arr[i] = 1;
ans.clear();
ans.n = n;
ans.arr[0] = 1;
all.clear();
all.n = n;
for (int i = 0; i < n; ++i) {
scanf("%lld", &all.arr[i]);
}
}
void solve() {
while (k) {
if (k & 1) ans = ans * tmp;
tmp = tmp * tmp;
k >>= 1;
}
ans = ans * all;
ans.print();
}
int main() {
while (scanf("%d%d", &n, &k) == 2) {
init();
solve();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int main() {
string a;
getline(cin, a);
int n;
int m = a.size();
reverse((a).begin(), (a).end());
std::vector<int> c(10);
scanf("%d\n", &n);
std::vector<string> b(n);
for (int i = 0; i < n; i++) {
getline(cin, b[i]);
reverse((b[i]).begin(), (b[i]).end());
}
for (int i = 0; i < 10; i++) scanf("%d", &c[i]);
std::vector<std::vector<int> > ord(1100);
for (int i = 0; i < n; i++) ord[0].push_back(i);
for (int d = 1; d < 1010; d++) {
std::vector<int> pos(n);
std::vector<int> nd(n, 0);
for (int i = 0; i < n; i++) pos[ord[d - 1][i]] = i;
for (int i = 0; i < n; i++)
if (b[i].size() >= d) nd[i] = b[i][d - 1] - '0';
for (int dd = 0; dd < 10; dd++) {
for (int i = 0; i < n; i++)
if (nd[ord[d - 1][i]] == dd) ord[d].push_back(ord[d - 1][i]);
}
}
int INF = 2e9;
std::vector<std::vector<int> > d(1100, std::vector<int>(1100, -INF));
d[0][0] = 0;
for (int i = 0; i < m; i++) {
std::vector<int> cnt(10, 0);
for (int j = 0; j < n; j++) {
if (b[j].size() <= i)
cnt[0]++;
else
cnt[b[j][i] - '0']++;
}
int per = 0;
for (int j = 0; j < n + 1; j++) {
int cp = per;
for (int dig = 0; dig < 10; dig++) {
if (a[i] != '?' && a[i] != ('0' + dig)) {
cp += cnt[9 - dig];
continue;
}
if (i == m - 1 && dig == 0) {
cp += cnt[9 - dig];
continue;
}
int add = 0;
for (int dwas = 0; dwas < 10; dwas++)
add += cnt[dwas] * c[(dwas + dig) % 10];
d[i + 1][cp] = max(d[i + 1][cp], d[i][j] + add);
cp += cnt[9 - dig];
}
if (j == n) break;
int inc = ord[i][n - 1 - j];
int dig = 0;
if (b[inc].size() > i) dig = b[inc][i] - '0';
if (dig == 9) per++;
cnt[dig]--;
cnt[(dig + 1) % 10]++;
}
}
int best = 0;
int over = 0;
for (int i = 0; i < n; i++) {
for (int k = m; k < b[i].size(); k++) over += c[b[i][k] - '0'];
}
for (int i = 0; i < n + 1; i++) {
best = max(d[m][i] + over, best);
if (i == n) break;
int ind = ord[m][n - 1 - i];
for (int k = m; k < b[ind].size(); k++) over -= c[b[ind][k] - '0'];
int carry = 1;
for (int k = m; k < b[ind].size(); k++) {
int nd = (b[ind][k] - '0') + carry;
if (nd == 10) {
nd = 0;
} else
carry = 0;
over += c[nd];
}
if (carry == 1) over += c[1];
}
cout << best;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
constexpr int MAXN = 2e5 + 12;
constexpr int MOD = 998244353;
constexpr int INF = 1e9 + 1;
constexpr int LOG = 2e1 + 5;
constexpr int SQ = 7e2 + 10;
constexpr int ALP = 60;
int n, q, cnt[3][3], a, b, fact[MAXN], invfact[MAXN];
string s;
int add(int a, int b) {
int c = a + b;
if (c >= MOD) {
c -= MOD;
}
if (c < 0) {
c += MOD;
}
return c;
}
int mul(int a, int b) { return 1ll * a * b % MOD; }
int pwr(int a, int b) {
if (!b) {
return 1;
}
int ret = pwr(a, b >> 1);
ret = mul(ret, ret);
if (b & 1) {
ret = mul(ret, a);
}
return ret;
}
int inv(int p) { return pwr(p, MOD - 2); }
int C(int x, int k) {
int ret = fact[x];
ret = mul(ret, invfact[k]);
ret = mul(ret, invfact[x - k]);
return ret;
}
int coding(char c) {
if (c == 'W') return 0;
if (c == 'B') return 1;
return 2;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
fact[0] = 1;
invfact[0] = inv(fact[0]);
for (int i = 1; i <= 2 * n; i++) {
fact[i] = mul(fact[i - 1], i);
invfact[i] = inv(fact[i]);
}
for (int i = 1; i <= n; i++) {
cin >> s;
int x = coding(s[0]);
int y = coding(s[1]);
a += (x == 0) + (y == 0);
b += (y == 1) + (x == 1);
cnt[x][y]++;
}
q = 2 * n - a - b;
if (a > n || b > n) {
cout << 0;
return 0;
}
if (a == n || b == n) {
cout << 1;
return 0;
}
if (cnt[0][0] || cnt[1][1]) {
cout << C(2 * n - a - b, n - a);
return 0;
} else {
if (cnt[0][1] && cnt[1][0]) {
cout << add(C(q, n - a), -pwr(2, cnt[2][2]));
} else {
int ans = 0;
if (cnt[0][1] == 0 && cnt[2][1] == 0 && cnt[0][2] == 0) {
ans++;
}
if (cnt[1][0] == 0 && cnt[2][0] == 0 && cnt[1][2] == 0) {
ans++;
}
for (int i = 1; i <= cnt[2][2]; i++) {
ans = add(ans, mul(pwr(2, i - 1),
C(q - 2 - 2 * (i - 1), n - a - 2 - (i - 1))));
ans = add(ans, mul(pwr(2, i - 1),
C(q - 2 - 2 * (i - 1), n - b - 2 - (i - 1))));
}
a += cnt[2][2];
b += cnt[2][2];
q -= 2 * cnt[2][2];
if (a != n && b != n) {
for (int i = 1; i <= cnt[0][2] + cnt[2][0]; i++) {
ans = add(ans, mul(pwr(2, cnt[2][2]), C(q - 1 - (i - 1), a - 1)));
}
}
cout << ans;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
int main(){
ll a, b, x;
cin >> a >> b >> x;
if(b > x) cout << x % MOD << endl;
else cout << (x % MOD + (((x-b)/(a-b)%MOD)*(b%MOD)) % MOD) % MOD << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[1005][1005];
int nextt[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
{0, 1}, {1, -1}, {1, 0}, {1, 1}};
int main() {
int ans = 0, cnt = 0;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", a[i]);
for (int j = 0; j < m; j++) {
if (a[i][j] == '#') ans++;
}
}
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
int co = 0;
for (int k = 0; k < 8; k++) {
int tx = i + nextt[k][0];
int ty = j + nextt[k][1];
if (a[tx][ty] == '#' || a[tx][ty] == '1')
co++;
else
continue;
}
if (co == 8) {
for (int k = 0; k < 8; k++) {
int tx = i + nextt[k][0];
int ty = j + nextt[k][1];
if (a[tx][ty] == '#') cnt++;
a[tx][ty] = '1';
}
}
}
}
if (ans == cnt)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
long long int ind[N], a[N], l[N], r[N], fen[N], ans[N];
vector<long long int> v[N], d[N];
void upd(int x) {
for (int i = x; i <= N - 100; i += i & (-i)) fen[i]++;
}
long long int sum(int x) {
long long int ret = 0;
for (int i = x; i > 0; i -= i & (-i)) ret += fen[i];
return ret;
}
int main() {
ios::sync_with_stdio(0);
long long int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
ind[a[i]] = i + 1;
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
v[max(ind[i], ind[j])].emplace_back(min(ind[i], ind[j]));
}
}
for (int i = 0; i < q; i++) {
cin >> l[i] >> r[i];
d[r[i]].emplace_back(i);
}
for (int i = 1; i <= n; i++) {
for (int j : v[i]) upd(j);
for (int j : d[i]) {
ans[j] = sum(r[j]) - sum(l[j] - 1);
}
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int s, k;
int flag[100100];
int ans[100100];
int p, q;
int main() {
while (~scanf("%d%d", &s, &k)) {
p = q = 0;
flag[0] = 0;
flag[++p] = 1;
for (int i = 1; i <= k; i++) {
p++;
if (i == 1)
flag[p] = 1;
else
flag[p] = flag[p - 1] * 2;
if (flag[p] >= s) break;
}
if (flag[p] >= s) {
for (int i = p; i && s; i--) {
if (flag[i] <= s) {
ans[++q] = flag[i];
s -= flag[i];
}
}
} else {
for (int i = 1;; i++) {
p++;
flag[p] = flag[p - 1] * 2 - flag[i];
if (flag[p] >= s) break;
}
for (int i = p; i && s; i--) {
if (flag[i] <= s) {
ans[++q] = flag[i];
s -= flag[i];
}
}
}
if (q >= 2) {
printf("%d\n", q);
for (int i = 1; i < q; i++) printf("%d ", ans[i]);
printf("%d\n", ans[q]);
} else {
printf("%d\n", 2);
printf("%d %d\n", 0, ans[1]);
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
set<pair<signed long long int, signed long long int> >::iterator it;
set<pair<signed long long int, signed long long int> > v;
vector<signed long long int> adj[2000001];
vector<signed long long int> adj1[2000001];
signed long long int ans[2000001];
signed long long int valid[2000001];
signed long long int id;
signed long long int flag;
void dfs(signed long long int src) {
ans[id] = src;
id += 2;
for (signed long long int i = 0; i < (signed long long int)adj[src].size();
i++) {
v.erase(make_pair(src, adj[src][i]));
if (adj[src][i] != 0) dfs(adj[src][i]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
signed long long int n;
cin >> n;
memset(valid, 0, sizeof(valid));
for (signed long long int i = 1; i <= n; i++) {
signed long long int a, b;
cin >> a >> b;
v.insert(make_pair(a, b));
adj[a].push_back(b);
}
id = 0;
dfs(0);
for (it = v.begin(); it != v.end(); it++) {
signed long long int x = it->first;
signed long long int y = it->second;
valid[x] = 1;
valid[y] = 1;
adj1[y].push_back(x);
}
signed long long int src = 0;
for (signed long long int i = 0; i <= 2000000; i++) {
if (valid[i]) {
if (adj1[i].size() == 0) src = i;
}
}
id = 1;
dfs(src);
for (signed long long int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long ksm(long long x, long long y) {
long long ret = 1;
x %= mod;
while (y) {
if (y & 1ll) ret = ret * x % mod;
y >>= 1ll;
x = x * x % mod;
}
return ret;
}
long long qpow(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x;
y >>= 1ll;
x = x * x;
}
return ret;
}
long long n, m, fac[100010], inv[100010], mp[100010], l, op, cnt, ggg[100010];
int f[500][100010];
char s[100010];
long long C(long long n, long long m) {
if (m > n || m < 0) return 0;
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
void dp(int x) {
f[mp[x]][x] = 1;
for (register int i = x + 1; i <= 100000; ++i)
f[mp[x]][i] =
(26ll * f[mp[x]][i - 1] % mod + ggg[i - x] * C(i - 1, x - 1)) % mod;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
fac[0] = inv[0] = 1;
for (register int i = 1; i <= 100000; ++i) fac[i] = fac[i - 1] * i % mod;
for (register int i = 1; i <= 100000; ++i) inv[i] = ksm(fac[i], mod - 2);
for (register int i = 0; i <= 100000; ++i) ggg[i] = ksm(25, i);
cin >> n >> s, l = strlen(s);
mp[l] = ++cnt, dp(l);
for (register int _ = 1; _ <= n; ++_) {
cin >> op;
if (op == 1) {
cin >> s, l = strlen(s);
if (!mp[l]) mp[l] = ++cnt, dp(l);
}
if (op == 2) {
cin >> m;
cout << f[mp[l]][m] << '\n';
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int xxx, yyy;
char str[1000];
int xx[1000], yy[1000];
int x, y;
int main() {
scanf("%d%d", &xxx, &yyy);
scanf(" %s", str);
int l = strlen(str);
int x = 0, y = 0;
for (int i = 0; i < l; i++) {
xx[i] = x;
yy[i] = y;
if (str[i] == 'R') x++;
if (str[i] == 'L') x--;
if (str[i] == 'U') y++;
if (str[i] == 'D') y--;
}
for (int i = 0; i < l; i++) {
if (x != 0 && y != 0) {
if (((xxx - xx[i]) % x == 0) && ((yyy - yy[i]) % y == 0) &&
((xxx - xx[i]) / x >= 0) &&
(((xxx - xx[i]) / x) == ((yyy - yy[i]) / y))) {
printf("Yes\n");
return 0;
}
}
if (x == 0 && y != 0) {
if (xxx == xx[i] && (yyy - yy[i]) % y == 0 && ((yyy - yy[i]) / y >= 0)) {
printf("Yes\n");
return 0;
}
}
if (y == 0 && x != 0) {
if (yyy == yy[i] && ((xxx - xx[i]) % x == 0) &&
((xxx - xx[i]) / x >= 0)) {
printf("Yes\n");
return 0;
}
}
if (x == 0 && y == 0) {
if (yyy == yy[i] && xxx == xx[i]) {
printf("Yes\n");
return 0;
}
}
}
printf("No\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
vector<vector<int>> arr(n, vector<int>(m));
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == '.')
arr[i][j] = 0;
else if (i == 0 || j == 0)
arr[i][j] = 1;
else
arr[i][j] =
min(arr[i - 1][j - 1], min(arr[i - 1][j], arr[i][j - 1])) + 1;
vector<vector<int>> b(n, vector<int>(m));
int l = 0, r = 1e6;
while (r - l > 1) {
int mid = (l + r) >> 1;
int x = 2 * mid + 1;
bool flag = true;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (arr[i][j] >= x)
b[i][j] = x;
else
b[i][j] = 0;
for (int i = n - 1; i >= 0; i--)
for (int j = m - 1; j >= 0; j--) {
if (i > 0) b[i - 1][j] = max(b[i - 1][j], b[i][j] - 1);
if (j > 0) b[i][j - 1] = max(b[i][j - 1], b[i][j] - 1);
if (i > 0 && j > 0) b[i - 1][j - 1] = max(b[i - 1][j - 1], b[i][j] - 1);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (arr[i][j] == 0) continue;
if (b[i][j] == 0) flag = false;
}
if (flag)
l = mid;
else
r = mid;
}
cout << l << '\n';
int x = 2 * l + 1;
vector<string> ans(n, string(m, '.'));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (arr[i][j] >= x) ans[i - l][j - l] = 'X';
for (int i = 0; i < n; i++) cout << ans[i] << '\n';
return 0;
}
| 3 |
#include<iostream>
#include<algorithm>
using namespace std;
static const int MAX = 1000;
static const int VMAX = 10000;
int n, A[MAX], s;
int B[MAX], T[VMAX + 1];
//s:与えられた数列の中での最小値
//A:与えられた数列
//B:ソート済みの数列
//T:与えられた数列の中の数字がソート済みの数列のなかで来る位置(イテレータ)
int solve(){
int ans = 0;
bool V[MAX]; //V:各数字がどのループに属するか判明したかどうか
for (int i = 0; i < n; i++){
B[i] = A[i];
V[i] = false;
}
sort(B, B+n);
for (int i = 0; i < n; i++) T[B[i]] = i;
for (int i = 0; i < n; i++){
if (V[i]) continue;
int cur = i; //検討する数字がソート後に来る位置
int S = 0; //各ループの要素の数字の和
int m = VMAX; //各ループの最小値
int an = 0; //各ループの要素数
while (1) {
V[cur] = true;
an++;
int v = A[cur];
m = min(m, v);
S += v;
cur = T[v];
if (V[cur]) break;
}
ans += min(S + (an - 2) * m, m + S + (an + 1) * s);
}
return ans;
}
int main(){
cin >> n;
s = VMAX;
for (int i = 0; i < n; i++){
cin >> A[i];
s = min(s, A[i]);
}
int ans = solve();
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, s, p, k, m, t, x, y, ans, res, mn, mx;
cin >> n;
long long a[n + 1];
ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (i != 1)
ans += fabs(a[i] - a[i - 1]);
else
ans += fabs(a[i]);
}
cout << ans << endl;
return 0;
}
| 2 |
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
//const int mo=0;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}
struct node{
int l,r;
void read(){
gn(l);gn(r);
}
}a[5555];
int cmp(const node&a,const node&b){
return a.r+a.l>b.r+b.l;
}
ll f[2][5055][2];
int n;
int main()
{
#ifdef JCVB
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int _time_jc=clock();
#endif
gn(n);
rep(i,1,n+1){
a[i].read();
}
fil(f[0],63);
f[0][0][0]=0;
sort(a+1,a+1+n,cmp);
ll ans=0x3f3f3f3f3f3f3f3fll;
for (int d=0;d<n;d++){
fil(f[(d&1)^1],63);
rep(l,0,d+1){
int r=d-l;
upmin(f[(d&1)^1][l][0],f[d&1][l][0]+a[d+1].l+1ll*r*(a[d+1].r+a[d+1].l));
upmin(f[(d&1)^1][l+1][0],f[d&1][l][0]+a[d+1].r+1ll*l*(a[d+1].r+a[d+1].l));
upmin(f[(d&1)^1][l][1],f[d&1][l][0]+1ll*(n/2)*(a[d+1].r+a[d+1].l));
if(d-l-1>=0){
r=d-l-1;
upmin(f[(d&1)^1][l][1],f[d&1][l][1]+a[d+1].l+1ll*r*(a[d+1].r+a[d+1].l));
upmin(f[(d&1)^1][l+1][1],f[d&1][l][1]+a[d+1].r+1ll*l*(a[d+1].r+a[d+1].l));
}
}
}
rep(i,0,n+1){
upmin(ans,f[n&1][i][0]);
}
if(n%2==1){
upmin(ans,f[n&1][n/2][1]);
}
cout<<ans<<endl;
#ifdef JCVB
debug("time: %d\n",int(clock()-_time_jc));
#endif
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O3", "unroll-loops")
using namespace std;
template <typename T1, typename T2>
inline void chmin(T1& a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1& a, T2 b) {
if (a < b) a = b;
}
const int MOD = 1e9 + 7;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const long long INF = 2e18;
void solve() {
int N, M;
cin >> N >> M;
vector<vector<char>> A(N, vector<char>(M)), ans(N, vector<char>(M));
vector<vector<int>> U(N, vector<int>(M)), D(N, vector<int>(M)),
L(N, vector<int>(M)), R(N, vector<int>(M));
vector<vector<int>> SU(N, vector<int>(M)), SD(N, vector<int>(M)),
SL(N, vector<int>(M)), SR(N, vector<int>(M));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cin >> A[i][j];
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (A[i][j] != 'X') continue;
if (i == 0) {
U[i][j] = i;
} else if (A[i - 1][j] != 'X')
U[i][j] = i;
else
U[i][j] = U[i - 1][j];
if (j == 0) {
L[i][j] = j;
} else if (A[i][j - 1] != 'X')
L[i][j] = j;
else
L[i][j] = L[i][j - 1];
}
}
for (int i = N - 1; i >= 0; --i) {
for (int j = M - 1; j >= 0; --j) {
if (A[i][j] != 'X') continue;
if (i == N - 1) {
D[i][j] = i;
} else if (A[i + 1][j] != 'X')
D[i][j] = i;
else
D[i][j] = D[i + 1][j];
if (j == M - 1) {
R[i][j] = j;
} else if (A[i][j + 1] != 'X')
R[i][j] = j;
else
R[i][j] = R[i][j + 1];
}
}
int mn = 1e9;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (A[i][j] != 'X') continue;
mn = min(mn, D[i][j] - U[i][j] + 1);
mn = min(mn, R[i][j] - L[i][j] + 1);
}
}
int T = (mn - 1) / 2;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (A[i][j] != 'X') continue;
if (R[i][j] - j >= T && j - L[i][j] >= T) {
if (i == 0)
SU[i][j] = i;
else if (R[i - 1][j] - j < T || j - L[i - 1][j] < T)
SU[i][j] = i;
else
SU[i][j] = SU[i - 1][j];
}
if (i - U[i][j] >= T && D[i][j] - i >= T) {
if (j == 0)
SL[i][j] = j;
else if (i - U[i][j - 1] < T || D[i][j - 1] - i < T)
SL[i][j] = j;
else
SL[i][j] = SL[i][j - 1];
}
}
}
for (int i = N - 1; i >= 0; --i) {
for (int j = M - 1; j >= 0; --j) {
if (A[i][j] != 'X') continue;
if (R[i][j] - j >= T && j - L[i][j] >= T) {
if (i == N - 1)
SD[i][j] = i;
else if (R[i + 1][j] - j < T || j - L[i + 1][j] < T)
SD[i][j] = i;
else
SD[i][j] = SD[i + 1][j];
}
if (i - U[i][j] >= T && D[i][j] - i >= T) {
if (j == M - 1)
SR[i][j] = j;
else if (i - U[i][j + 1] < T || D[i][j + 1] - i < T)
SR[i][j] = j;
else
SR[i][j] = SR[i][j + 1];
}
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
ans[i][j] = '.';
if (A[i][j] != 'X') continue;
if (T == 0) ans[i][j] = 'X';
if (i - U[i][j] >= T && D[i][j] - i >= T && R[i][j] - j >= T &&
j - L[i][j] >= T) {
if (i - SU[i][j] >= T && SD[i][j] - i >= T && SR[i][j] - j >= T &&
j - SL[i][j] >= T) {
ans[i][j] = 'X';
}
}
}
}
cout << (T) << '\n';
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cout << ans[i][j];
}
cout << '\n';
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool islucky(int x) {
if (x < 0) x = -x;
int rem = x % 10;
while (x) {
if (rem == 8) return true;
x = x / 10;
rem = x % 10;
}
return false;
}
int main() {
int a, b = 1;
scanf("%d", &a);
a++;
while (!islucky(a)) {
a++;
b++;
}
printf("%d\n", b);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char store[3][4] = {
{'.', 'x', 'x', '\0'}, {'x', '.', 'x', '\0'}, {'x', 'x', '.', '\0'}};
char arr[4][5];
int i;
for (i = 0; i < 4; ++i) {
cin >> arr[i];
}
int c[4] = {0}, r[4] = {0}, j;
for (i = 0; i < 4; i++) {
string s = arr[i];
for (j = 0; j < 3; j++) {
size_t temp = s.find(store[j]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
}
for (i = 0; i < 4; i++) {
char ch[5] = {arr[0][i], arr[1][i], arr[2][i], arr[3][i], '\0'};
string s = ch;
for (j = 0; j < 3; j++) {
size_t temp = s.find(store[j]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
}
for (i = 0; i < 3; i++) {
char ch[4] = {arr[0][1], arr[1][2], arr[2][3], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
for (i = 0; i < 3; i++) {
char ch[4] = {arr[1][0], arr[2][1], arr[3][2], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
for (i = 0; i < 3; i++) {
char ch[5] = {arr[0][0], arr[1][1], arr[2][2], arr[3][3], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
for (i = 0; i < 3; i++) {
char ch[4] = {arr[0][2], arr[1][1], arr[2][0], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
for (i = 0; i < 3; i++) {
char ch[4] = {arr[1][3], arr[2][2], arr[3][1], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
for (i = 0; i < 3; i++) {
char ch[5] = {arr[0][3], arr[1][2], arr[2][1], arr[3][0], '\0'};
string s = ch;
size_t temp = s.find(store[i]);
if (temp != string::npos) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 2 |
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
if(x<0){putchar('-');x=-x;}
if(!x){putchar('0');return;}
int top=0;
while(x)stack[++top]=x%10,x/=10;
while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=1505;
const int mod=998244353;
int pow_mod(int a,int b)
{
int ret=1;
for(;b;b>>=1,a=1LL*a*a%mod)if(b&1)ret=1LL*ret*a%mod;
return ret;
}
char ch[MAXN][MAXN];int n;
char mp[MAXN][MAXN];
int dat[MAXN],vis[MAXN],cnt,ans;
vector<int> nxt[MAXN];
void ins(int x,int y){nxt[x].emplace_back(y);}
bool check(int x,int up)
{
vis[x]=1;
for(auto y:nxt[x])if(!vis[y])check(y,x);
if(dat[x]&1)
{
if(!up)return false;
dat[x]^=1;dat[up]^=1;
}return true;
}
int solve(int N)
{
++N;
if(!(N&1))
{
int ret=0;
for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)ret+=mp[i][j]=='?';
return pow_mod(2,ret);
}
memset(dat,0,sizeof(dat));for(int i=1;i<=N;i++)nxt[i].clear();
int sum=0;
for(int i=0;i<N;i++)
for(int j=i+1;j<N;j++)
{
int u=i+1,v=j+1;
if(mp[i][j]=='?')ins(u,v),ins(v,u),++sum;
else if(mp[i][j]=='o')dat[u]^=1,dat[v]^=1;
}
memset(vis,0,sizeof(vis));bool flag=true;
for(int i=1;i<=N;i++)if(!vis[i])
{
flag&=check(i,0);
if(!flag)return 0;
++sum;
}--sum;sum-=(N-1);
return pow_mod(2,sum);
}
int getxpos(int x,int y){return x+y;}
int getypos(int x,int y){return 0+(y-x);}
int main()
{
n=read()-1;for(int i=1;i<=n;i++)scanf("%s",ch[i]+1);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(ch[i][j]!='?')
{
if(ch[j][i]!='?'&&ch[j][i]!=ch[i][j])return pr2(0),0;
ch[j][i]=ch[i][j];
}
for(int i=1;i<=n;i++)reverse(ch[i]+1,ch[i]+1+n);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(ch[i][j]!='?')
{
if(ch[j][i]!='?'&&ch[j][i]!=ch[i][j])return pr2(0),0;
ch[j][i]=ch[i][j];
}ans=1;
for(int i=1;i<=n-i+1;i++)
for(int j=i;j<=n-i+1;j++)if(!((i+j)&1))
{
chkmax(cnt,getxpos(i,j)>>1);chkmax(cnt,getypos(i,j)>>1);
mp[getxpos(i,j)>>1][getypos(i,j)>>1]=mp[getypos(i,j)>>1][getxpos(i,j)>>1]=ch[i][j];
}
ans=1LL*ans*solve(cnt)%mod;
cnt=0;
for(int i=1;i<=n-i+1;i++)
for(int j=i;j<=n-i+1;j++)if(((i+j)&1))
{
chkmax(cnt,getxpos(i,j)>>1);chkmax(cnt,getypos(i,j)>>1);
mp[getxpos(i,j)>>1][getypos(i,j)>>1]=mp[getypos(i,j)>>1][getxpos(i,j)>>1]=ch[i][j];
}
ans=1LL*ans*solve(cnt)%mod;
pr2(ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ai, xi, neg = 0, pos = 0, count = 0;
vector<pair<int, int> > a;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> xi >> ai;
a.push_back(pair<int, int>(xi, ai));
if (xi < 0)
neg++;
else
pos++;
count += ai;
}
sort(a.begin(), a.end());
int no = abs(neg - pos) - 1;
if (no > 0) {
if (neg > pos) {
for (int i = 0; i < no; i++) count -= a[i].second;
} else {
for (int i = n - 1; i >= n - no; i--) count -= a[i].second;
}
}
cout << count;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, q, c;
const int C = 101;
const int P = 11;
int cnt[P][C][C];
int main(void) {
scanf("%d %d %d", &n, &q, &c);
for (int i = 0; i < n; i++) {
int x, y, s;
scanf("%d %d %d", &x, &y, &s);
cnt[s][x][y]++;
}
for (int p = 0; p <= c; p++)
for (int i = 1; i < C; i++)
for (int j = 1; j < C; j++)
cnt[p][i][j] +=
cnt[p][i - 1][j] + cnt[p][i][j - 1] - cnt[p][i - 1][j - 1];
for (int i = 0; i < q; i++) {
int t, x1, y1, x2, y2;
scanf("%d %d %d %d %d", &t, &x1, &y1, &x2, &y2);
int ans = 0;
for (int p = 0; p <= c; p++) {
int brightness = (p + t) % (c + 1);
int amt = cnt[p][x2][y2] - cnt[p][x1 - 1][y2] - cnt[p][x2][y1 - 1] +
cnt[p][x1 - 1][y1 - 1];
ans += brightness * amt;
}
printf("%d\n", ans);
}
return false;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int v[500005];
int main() {
long long i, j, mx = 0, n;
int k;
cin >> n;
for (i = 1; i <= n; ++i) {
for (j = 2; j * j <= i; ++j)
if (i % j == 0) break;
if (i % j == 0)
v[i] = i / j;
else
v[i] = 1;
}
sort(v + 1, v + n + 1);
for (i = 2; i <= n; ++i) cout << v[i] << ' ';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, r, cnt = 0;
cin >> n >> x;
if (x > n)
r = n;
else if (x <= n)
r = x;
cnt = abs(x - n) / 2;
cout << r << " " << cnt;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int t;
long long x;
scanf("%d", &t);
while (t--) {
scanf("%lld", &x);
if (x == 1 || x % 4 == 2) {
printf("-1\n");
continue;
}
bool found = false;
for (long long b = 1LL; b * b < x + 10000; b++) {
long long a = (long long)sqrt(x + b * b);
if (a * a == x + b * b) {
long long m = a / b;
if (a / m == b) {
printf("%lld %lld\n", a, m);
found = true;
break;
}
}
}
if (!found) printf("-1\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n1, n2, k1, k2;
scanf("%d%d%d%d", &n1, &n2, &k1, &k2);
if (n1 > n2)
printf("First\n");
else
printf("Second\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<pair<int, int> > v;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int a, b;
scanf("%d %d", &a, &b);
v.push_back(make_pair(a, b));
}
sort(v.begin(), v.end());
int rsp = 0;
for (size_t i = 0; i < v.size(); ++i) {
if (v[i].second >= rsp) {
rsp = v[i].second;
} else {
rsp = v[i].first;
}
}
printf("%d\n", rsp);
return 0;
}
| 1 |
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int main(){
while(true){
int N,T,L,B;
cin>>N>>T>>L>>B;
bool l[101]={},b[101]={};
if(N==0) break;
rep(i,L){
int x;
cin>>x;
l[x]=true;
}
rep(i,B){
int x;
cin>>x;
b[x]=true;
}
double dp[102][101]={};
dp[0][0]=1;
rep(i,T){
rep(j,N){
rep1(k,6){
int nxt=j+k;
if(nxt>N) nxt=N*2-nxt;
if(l[nxt]){
dp[i+2][nxt]+=dp[i][j]/6;
}else if(b[nxt]){
dp[i+1][0]+=dp[i][j]/6;
}else{
dp[i+1][nxt]+=dp[i][j]/6;
}
}
}
dp[i+1][N]+=dp[i][N];
}
printf("%.12f\n",dp[T][N]);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
char ch[102][28];
int main() {
int count1[200005];
memset(count1, 0, sizeof(count1));
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
count1[temp] = i + 1;
}
long long int ans = 0;
for (int i = 2; i <= n; i++) {
ans += (abs(count1[i] - count1[i - 1]));
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MAXA = 1 << 20;
int n, m, k, niz[MAXN], pref[MAXN], currentL, currentR;
long long ans = 0, cnt[MAXA];
const int BLOCK = 317;
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
if (a.first / BLOCK == b.first / BLOCK)
return a.second.first < b.second.first;
return a.first < b.first;
}
pair<int, pair<int, int> > queries[MAXN];
long long ispis[MAXN];
void add(int position) {
ans += cnt[pref[position] ^ k];
cnt[pref[position]]++;
}
void remove(int position) {
cnt[pref[position]]--;
ans -= cnt[pref[position] ^ k];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &niz[i]);
pref[i] = pref[i - 1] ^ niz[i];
}
currentL = 1, currentR = 1;
add(0);
add(1);
for (int i = 0; i < m; i++) {
scanf("%d%d", &queries[i].first, &queries[i].second.first);
queries[i].second.second = i;
}
sort(queries, queries + m, cmp);
for (int i = 0; i < m; i++) {
int l = queries[i].first, r = queries[i].second.first;
while (currentL < l) {
remove(currentL - 1);
currentL++;
}
while (currentL > l) {
currentL--;
add(currentL - 1);
}
while (currentR < r) {
currentR++;
add(currentR);
}
while (currentR > r) {
remove(currentR);
currentR--;
}
ispis[queries[i].second.second] = ans;
}
for (int i = 0; i < m; i++) printf("%I64d\n", ispis[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 110, mod = int(1e9) + 7;
int n, k, m, a;
int g[N], cnt[N], last[N], p[N], pn;
int main() {
cin >> n >> k >> m >> a;
if (n == 1) {
cout << 1;
return 0;
}
for (int i = 1; i <= a; i++) {
cin >> g[i];
}
for (int i = 1; i <= n; i++) {
memset(cnt, 0, sizeof(cnt));
pn = 0;
for (int j = 1; j <= a; j++) {
cnt[g[j]]++;
last[g[j]] = j;
}
int x = 0;
for (int j = 1; j <= n; j++)
if (j != i) {
if (cnt[j] < cnt[i]) {
p[++pn] = cnt[i] - cnt[j] + 1;
} else if (cnt[j] == cnt[i] && last[j] > last[i]) {
p[++pn] = 1;
} else {
if (cnt[j] == 0)
p[++pn] = 1;
else
x++;
}
}
sort(p + 1, p + pn + 1);
int need = k - x;
int ans = 0;
int res = 0;
for (int j = 1; j <= need; j++) {
res += p[j];
}
if (pn >= need && res <= m - a || need <= 0 || !cnt[i]) {
memset(cnt, 0, sizeof(cnt));
memset(last, 0, sizeof(last));
for (int j = 1; j <= a; j++) {
cnt[g[j]]++;
last[g[j]] = j;
}
for (int j = a + 1; j <= m; j++) {
cnt[i]++;
last[i] = j;
}
pn = 0;
x = 0;
for (int j = 1; j <= n; j++)
if (j != i) {
if (cnt[j] > cnt[i]) x++;
if (cnt[j] == cnt[i] && last[j] < last[i]) x++;
}
if (x < k && cnt[i])
cout << 2 << " ";
else
cout << 3 << " ";
} else
cout << 1 << " ";
}
return 0;
}
| 6 |
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
const int MAX = 32768+1;
bool p[MAX];
for (int i = 0; i < MAX; ++i)
p[i] = true;
p[0] = p[1] = false;
for (int i = 2; i*i <= MAX; ++i) {
if (p[i]) {
for (int j = i+i; j < MAX; j+=i)
p[j] = false;
}
}
vector<int> prime;
for (int i = 0; i < MAX; ++i) {
if (p[i])
prime.push_back(i);
}
int n;
while (~scanf("%d", &n)) {
if (n == 0)
break;
int ans = 0;
for (unsigned int i = 0; i < prime.size(); ++i) {
for (unsigned int j = i; j < prime.size(); ++j) {
if (prime[i]+prime[j] == n)
++ans;
}
}
printf("%d\n", ans);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int val[maxn];
long long poww[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
string t;
cin >> t;
poww[0] = 1;
for (int i = 1; i < maxn; ++i) {
poww[i] = poww[i - 1] * 2 % 1000000007;
}
for (int i = 0; i < t.size(); ++i) {
a[i + 1] = t[i] - '0';
}
for (int i = 1; i <= n; ++i) {
val[i] = val[i - 1] + a[i];
}
int l, r;
for (int i = 1; i <= q; ++i) {
cin >> l >> r;
int cnt1 = val[r] - val[l - 1];
int cnt0 = r - l + 1 - cnt1;
long long ans = 0;
ans += (poww[cnt1] - 1 + 1000000007) % 1000000007;
if (cnt0) {
long long tans;
tans = ((poww[cnt0 - 1] * ans * 2) % 1000000007 - ans + 1000000007) %
1000000007;
ans += tans;
ans %= 1000000007;
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
bool hehit(int x, int y, int i, int j) {
if (x == i && y == j) return true;
if (x - i == 1 || i - x == 1)
return y - j == 2 || j - y == 2;
else if (x - i == 2 || i - x == 2)
return y - j == 1 || j - y == 1;
return false;
}
bool hithe(int x, int y, int i, int j) {
if (x == i || y == j) return true;
return false;
}
int main() {
char c, endofline[30];
int u1, v1, u2, v2;
scanf("%c%d", &c, &v1);
u1 = c - 'a' + 1;
gets(endofline);
scanf("%c%d", &c, &v2);
u2 = c - 'a' + 1;
int cnt = 64;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
bool flag = true;
if (hehit(i, j, u1, v1)) flag = false;
if (hehit(i, j, u2, v2)) flag = false;
if (hithe(u1, v1, i, j)) flag = false;
if (!flag) cnt--;
}
printf("%d", cnt);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dir1[8][2] = {1, 2, 2, 1, 2, -1, 1, -2, -1, 2, -2, 1, -1, -2, -2, -1};
int dir2[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int dir3[8][2] = {1, 0, 0, 1, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
bool isPrime(int x) {
if (x == 1)
return false;
else if (x == 2)
return true;
for (int i = 2; i <= sqrt(x * 1.0); ++i) {
if (x % i == 0) return false;
}
return true;
}
int sqr(int x) { return x * x; }
struct Node {
string name;
int score;
string level;
} x[1200];
bool cmp(Node a, Node b) { return a.name < b.name; }
int main() {
int n;
set<string> st;
while (~scanf("%d", &n)) {
string s;
int m;
int ans = 0;
st.clear();
for (int i = 0; i < n; ++i) {
cin >> s >> m;
if (st.find(s) == st.end()) {
st.insert(s);
x[ans].name = s;
x[ans].score = m;
ans++;
} else {
for (int j = 0; j < ans; ++j) {
if (x[j].name == s) {
x[j].score = max(x[j].score, m);
break;
}
}
}
}
for (int i = 0; i < ans; ++i) {
int cnt = 0;
for (int j = 0; j < ans; ++j) {
if (x[i].score >= x[j].score) cnt++;
}
if (100 * cnt < 50 * ans)
x[i].level = "noob";
else if (100 * cnt < 80 * ans)
x[i].level = "random";
else if (100 * cnt < 90 * ans)
x[i].level = "average";
else if (100 * cnt < 99 * ans)
x[i].level = "hardcore";
else
x[i].level = "pro";
}
sort(x, x + ans, cmp);
printf("%d\n", ans);
for (int i = 0; i < ans; ++i) {
cout << x[i].name << " " << x[i].level << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
string str;
int freq[26];
int main() {
scanf("%d %d", &n, &k);
cin >> str;
int i, j;
for (i = 0; i < n; i++) freq[str[i] - 97]++;
int xx = -1;
for (i = 0; i < 26; i++) {
if (freq[i] != 0) {
xx = i;
break;
}
}
if (k > n) {
cout << str;
for (i = n + 1; i <= k; i++) {
printf("%c", (char)(xx + 97));
}
return 0;
} else {
string str1 = "";
for (i = 0; i < k; i++) str1 += str[i];
for (i = k - 1; i >= 0; i--) {
int xxx = str1[i] - 97;
int co = -1;
for (j = xxx + 1; j < 26; j++) {
if (freq[j]) {
co = j;
break;
}
}
if (co != -1) {
str1[i] = (char)(co + 97);
for (j = i + 1; j < k; j++) {
str1[j] = (char)(xx + 97);
}
break;
}
}
cout << str1 << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using lln = long long int;
using ls = string;
using ch = char;
using lld = long double;
using lf = float;
using ll = int;
using ld = double;
void solve() {
ll ans, num1, num2, ans1, ans2;
cin >> num1 >> num2;
ans = num1 / num2;
ans1 = num1 % num2;
if (ans1 == 0) {
for (int i = 0; i < num2; i++) {
cout << ans << " ";
}
} else {
for (int i = 0; i < num2 - ans1; i++) {
cout << ans << " ";
}
for (int i = 0; i < ans1; i++) {
cout << ans + 1 << " ";
}
}
cout << endl;
}
int main() {
solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, num[200005];
int one, two;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (register int i = 1; i <= n; i++) {
cin >> num[i];
if (num[i] == 1)
one++;
else
two++;
}
if (two > one)
cout << one;
else if (two < one)
cout << two + (one - two) / 3;
else
cout << two;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
if (x == 3)
cout << 5 << endl;
else {
for (int i = 1;; i += 2) {
if ((i / 2 + 1) * (i / 2 + 1) + (i / 2) * (i / 2) >= x) {
cout << i << endl;
cin >> i;
return 0;
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t;
cin >> s >> t;
int n, m, res = 0, k, par = 0;
n = s.size();
m = t.size();
k = n - m + 1;
for (int i = 0; i < m; i++) {
if (s[i] != t[i]) par++;
}
par %= 2;
if (par % 2 == 0) res++;
for (int i = 1; i < k; i++) {
if (s[i - 1] != s[i + m - 1]) par = 1 - par;
if (par % 2 == 0) res++;
}
cout << res << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int NICO = 200000 + 10;
int n, k, cnt, res;
int a[NICO], b[NICO];
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x < 0) a[++cnt] = i;
}
if (cnt > k) {
printf("-1\n");
return 0;
}
if (cnt == 0) {
printf("0\n");
return 0;
}
k -= cnt;
res = 2 * cnt;
for (int i = 1; i < cnt; i++) {
b[i] = a[i + 1] - a[i] - 1;
}
sort(b + 1, b + cnt);
for (int i = 1; i < cnt; i++) {
if (b[i] <= k) k -= b[i], res -= 2;
}
if (n - a[cnt] <= k) res--;
cout << res << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
cout << (n + 999) / 1000 * 1000 - n << endl;
} | 0 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
int N;
double inf=1e18;
double x[1002],y[1002],r[1002];
double d[1002];
bool used[1002];
double yosupot(double x,double y){return sqrt(x*x+y*y);}
int main(){
cin>>x[0]>>y[0]>>x[1]>>y[1];
cin>>N;
rep(i,N){
cin>>x[i+2]>>y[i+2]>>r[i+2];
}
N+=2;
rep(i,N) d[i]=inf;
d[0]=0;
rep(i,N){
int v=-1;
rep(j,N) if(!used[j]&&(v==-1||d[v]>d[j])) v=j;
used[v]=1;
rep(u,N){
double dist=max(0.0,yosupot(x[v]-x[u],y[v]-y[u])-r[v]-r[u]);
chmin(d[u],d[v]+dist);
}
}
printf("%.12f\n",d[1]);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, S, T;
char s[2555];
vector<int> adj[2555];
int val[2555], d[2555], p[2555];
string ans;
void check(int x) {
string now;
for (; d[x]; x = p[x]) now += s[x];
reverse(now.begin(), now.end());
if (now.length() < ans.length())
ans = now;
else if (now.length() == ans.length() && now < ans)
ans = now;
}
bool cmp_val(int i, int j) { return val[i] < val[j]; }
void solve(int mask) {
memset(d, -1, sizeof d);
vector<int> u, v;
d[S] = 0, val[S] = 0, u.push_back(S);
while (!u.empty()) {
v.clear();
for (int i = 0; i < u.size(); i++) {
int x = u[i];
for (int j = 0; j < adj[x].size(); j++) {
int y = adj[x][j];
if (y == T) {
check(x);
return;
}
if (d[y] != -1 || !(mask >> (s[y] - 'a') & 1)) continue;
d[y] = d[x] + 1, p[y] = x, val[y] = val[x] * 2555 + s[y],
v.push_back(y);
}
}
sort(v.begin(), v.end(), cmp_val);
for (int i = 0, prv; i < v.size(); i++) {
if (i == 0)
prv = val[v[0]], val[v[0]] = 0;
else {
int tmp = val[v[i]];
val[v[i]] = val[v[i - 1]] + (tmp != prv);
prv = tmp;
}
}
u = v;
}
}
bool good(int i, int j) { return 0 <= i && i < n && 0 <= j && j < m; }
int node(int i, int j) { return i * m + j; }
bool cmp_s(int i, int j) { return s[i] < s[j]; }
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++) scanf("%s", s + i * m);
ans = string(3000, 'a');
for (int i = 0; i < n * m; i++) {
if (s[i] == 'S') S = i;
if (s[i] == 'T') T = i;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (good(i + 1, j)) adj[node(i, j)].push_back(node(i + 1, j));
if (good(i - 1, j)) adj[node(i, j)].push_back(node(i - 1, j));
if (good(i, j + 1)) adj[node(i, j)].push_back(node(i, j + 1));
if (good(i, j - 1)) adj[node(i, j)].push_back(node(i, j - 1));
}
for (int i = 0; i < n * m; i++) sort(adj[i].begin(), adj[i].end(), cmp_s);
for (int i = 0; i < (1 << 26); i++)
if (__builtin_popcount(i) == k) solve(i);
if (ans.length() == 3000) ans = "-1";
cout << ans << endl;
}
| 3 |
Subsets and Splits