solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()
int main()
{
int x, y, dx, dy, maxl, tx, ty;
int n; cin >> n;
rep(j, n)
{
x = y = tx = ty = maxl = 0;
while(cin >> dx >> dy && (dx || dy))
{
x += dx;
y += dy;
if( x * x + y * y > maxl) { tx = x; ty = y; maxl = x * x + y * y;}
if(x * x + y * y == maxl && x > tx) {tx = x; ty = y;}
}
cout << tx << " " << ty << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
string a[1010];
string s;
int main() {
int n = 0;
while (getline(cin, s)) {
a[n] = s;
n++;
}
int max = 0;
for (int i = 0; i < n; ++i)
if (a[i].size() > max) max = a[i].size();
for (int i = 0; i < max + 2; ++i) cout << "*";
cout << "\n";
int c = 0;
for (int i = 0; i < n; ++i) {
cout << "*";
int k = 0;
int free = max - a[i].size();
if (free % 2 == 0) {
k = free / 2;
} else {
if (c % 2 == 0)
k = free / 2;
else
k = free / 2 + 1;
c++;
}
for (int z = 0; z < k; ++z) cout << " ";
cout << a[i];
for (int z = 0; z < free - k; ++z) cout << " ";
cout << "*\n";
}
for (int i = 0; i < max + 2; ++i) cout << "*";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const long long N = 2e6 + 5;
const long long INF = 1e18L;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << ": " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
long long ar[N];
long long dp[N];
long long lseg[N], rseg[N];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long T = 1;
while (T--) {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> ar[i];
}
long long op = 0;
for (long long i = 0; i <= n; i++) {
dp[i] = ar[i + 1] - ar[i];
op += max(0ll, dp[i]);
}
long long l = 0, r = 0;
cout << op << '\n';
for (long long i = 0; i <= n; i++) {
if (dp[i] > 0) {
for (long long j = 0; j < dp[i]; j++) lseg[l++] = i + 1;
} else {
for (long long j = 0; j < -dp[i]; j++) rseg[r++] = i;
}
}
for (long long i = 0; i < op; i++) {
cout << lseg[i] << " " << rseg[i] << "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;
const double PI = M_PI;
const double EPS = 1e-15;
const int MaxM = 500;
const int OFFSET = MaxM >> 1;
int n, t;
int C[MaxM][MaxM];
int used[MaxM][MaxM];
int utime;
int X[2][MaxM * MaxM];
int Y[2][MaxM * MaxM];
int size[2];
void push(int idx, int r, int c) {
if (used[r][c] != utime && C[r][c] >= 4) {
used[r][c] = utime;
X[idx][size[idx]] = r;
Y[idx][size[idx]] = c;
size[idx] = size[idx] + 1;
}
}
void put(int k) {
int curr = 0;
C[OFFSET][OFFSET] = k;
size[curr] = 1;
X[curr][0] = OFFSET;
Y[curr][0] = OFFSET;
if (k < 4) return;
while (size[curr] > 0) {
for (int i = 0; i < size[curr]; i++) {
int r = C[X[curr][i]][Y[curr][i]] / 4;
C[X[curr][i]][Y[curr][i]] -= r * 4;
C[X[curr][i] - 1][Y[curr][i]] += r;
C[X[curr][i] + 1][Y[curr][i]] += r;
C[X[curr][i]][Y[curr][i] - 1] += r;
C[X[curr][i]][Y[curr][i] + 1] += r;
}
utime = utime + 1;
size[1 - curr] = 0;
for (int i = 0; i < size[curr]; i++) {
push(1 - curr, X[curr][i], Y[curr][i]);
push(1 - curr, X[curr][i] - 1, Y[curr][i]);
push(1 - curr, X[curr][i] + 1, Y[curr][i]);
push(1 - curr, X[curr][i], Y[curr][i] - 1);
push(1 - curr, X[curr][i], Y[curr][i] + 1);
}
curr = 1 - curr;
}
}
int main() {
cin.sync_with_stdio(false);
cin >> n >> t;
put(4 * (n / 4));
put(n % 4);
for (int i = 0; i < t; i++) {
int _x, _y;
cin >> _x >> _y;
_x += OFFSET;
_y += OFFSET;
if (0 <= _x && _x < MaxM && 0 <= _y && _y < MaxM)
cout << C[_x][_y] << "\n";
else
cout << 0 << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, j, m, k, l, p, q;
cin >> n >> m;
while (m > 0) {
for (i = 1; i <= n; i++) {
if (m < i) {
cout << m;
return 0;
} else {
m = m - i;
}
if (m == 0) {
cout << 0;
return 0;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,p;
cin>>a>>p;
cout<<(int)((3*a+p)/2);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXM = 100010;
int n, m;
struct edge {
int ne, to, val;
edge(int N = 0, int T = 0) : ne(N), to(T) {}
} e[MAXM << 1];
int fir[MAXN], num = 1;
inline void join(int a, int b) {
e[++num] = edge(fir[a], b);
fir[a] = num;
}
int dfn[MAXN], low[MAXN], cnt = 0, stk[MAXN], top = 0, tot = 0, pos[MAXM << 1];
vector<int> dv[MAXN], de[MAXN];
int col[MAXN];
bool vis[MAXN];
int dep[MAXN], anc[MAXN][20], lg[MAXN];
void tarjan(int u, int la) {
dfn[u] = low[u] = ++cnt;
for (int i = fir[u]; i; i = e[i].ne) {
if (i == (la ^ 1) || pos[i]) continue;
int v = e[i].to;
stk[++top] = i;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int x = 0;
dv[++tot].push_back(u);
do {
x = stk[top--];
pos[x] = pos[x ^ 1] = tot;
dv[tot].push_back(e[x].to);
de[tot].push_back(x);
} while (x != i);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
bool color(int u, int la, int c) {
if (~col[u]) return col[u] == col[e[la ^ 1].to];
col[u] = c;
for (int i = fir[u]; i; i = e[i].ne) {
if (pos[i] != pos[la] || i == (la ^ 1)) continue;
int v = e[i].to;
if (color(v, i, c ^ 1)) return 1;
}
return 0;
}
int sum[MAXN];
void dfs(int u, int fa) {
dep[u] = dep[anc[u][0] = fa] + 1;
for (int i = 1; i <= lg[dep[u]]; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1];
vis[u] = 1;
for (int i = fir[u]; i; i = e[i].ne) {
int v = e[i].to;
if (vis[v]) continue;
sum[v] = sum[u] + e[i].val;
dfs(v, u);
}
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = lg[dep[x]]; ~i; i--)
if (dep[anc[x][i]] >= dep[y]) x = anc[x][i];
if (x == y) return x;
for (int i = lg[dep[x]]; ~i; i--)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
int pa[MAXN];
int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) pa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
join(x, y);
join(y, x);
pa[find(x)] = find(y);
}
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= tot; i++) {
for (int j = 0; j < (int)dv[i].size(); j++) col[dv[i][j]] = -1;
pos[0] = i;
if (color(dv[i][0], 0, 0)) {
for (int j = 0; j < (int)de[i].size(); j++) e[de[i][j]].val = 1;
}
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
int q;
scanf("%d", &q);
while (q--) {
int x = -1, y = -1;
scanf("%d%d", &x, &y);
if (find(x) != find(y)) {
puts("No");
continue;
}
int z = LCA(x, y);
if ((dep[x] + dep[y]) & 1)
puts("Yes");
else if (sum[x] + sum[y] - sum[z] * 2 > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| 5 |
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <set>
#include <string>
#include <vector>
#include <array>
#define MOD 1000000007
#define INT_MAX 2000000000
using namespace std;
int gcd(int a,int b){
if (!a) return b;
if (!b) return a;
a -= b;
return gcd(max(a,b), min(a,b));
}
int main()
{
string t, b;
cin >> t >> b;
int dp[1001][1001] = {};
if (b[0] == t[0])
dp[0][0] = 1;
for (int i = 1; t.length() > i; i++){
for (int j = 1; b.length() > j; j++){
if (t[i] == b[j])
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j];
dp[i][j] %= MOD;
}
dp[i][0] = dp[i - 1][0];
if (t[i] == b[0])
dp[i][0]++;
dp[i][0] %= MOD;
}
cout << dp[t.length() - 1][b.length() - 1] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
int t, found, i;
cin >> t;
while (t--) {
string s;
cin >> s;
for (i = s.size() - 1; i >= 0; i--) {
if (s[i] == 'o' && s[i - 1] == 'p') {
cout << "FILIPINO" << endl;
break;
}
if ((s[i] == 'u' && s[i - 1] == 's' && s[i - 2] == 'e' &&
s[i - 3] == 'd') ||
(s[i] == 'u' && s[i - 1] == 's' && s[i - 2] == 'a' &&
s[i - 3] == 'm')) {
cout << "JAPANESE" << endl;
break;
}
if (s[i] == 'a' && s[i - 1] == 'd' && s[i - 2] == 'i' &&
s[i - 3] == 'n' && s[i - 4] == 'm') {
cout << "KOREAN" << endl;
break;
}
}
s.clear();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, i, x[52], y[52], r[52];
int read() {
char c = getchar();
int w = 0;
while (c < '0' || c > '9') c = getchar();
while (c <= '9' && c >= '0') {
w = w * 10 + c - '0';
c = getchar();
}
return w;
}
void solve(int *a) {
for (int i = 1; i < n; i++) printf("(");
for (int i = 1; i < n; i++) {
printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1))))", a[i] / 2, i - 1,
i - 1);
if (i != 1) printf(")");
printf("+");
}
printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1))))", a[n] / 2, n - 1, n - 1);
if (n != 1) printf(")");
puts("");
}
int main() {
n = read();
for (i = 1; i <= n; i++) x[i] = read(), y[i] = read(), r[i] = read();
solve(x);
solve(y);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[330][330];
int dp[607][307][307];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) cin >> a[i][j];
}
memset(dp, -0x3f, sizeof(dp));
dp[1][1][1] = a[1][1];
for (int i = 2; i <= 2 * n - 1; i++) {
for (int j = 1; j <= min(i, n); j++) {
if (i - j + 1 >= 1 && i - j + 1 <= n) {
for (int k = 1; k <= min(i, n); k++) {
if (i - k + 1 >= 1 && i - k + 1 <= n) {
if (j != k)
dp[i][j][k] =
max(dp[i - 1][j][k],
max(dp[i - 1][j - 1][k],
max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]))) +
a[j][i - j + 1] + a[k][i - k + 1];
else
dp[i][j][k] =
max(dp[i - 1][j][k],
max(dp[i - 1][j - 1][k],
max(dp[i - 1][j][k - 1], dp[i - 1][j - 1][k - 1]))) +
a[j][i - j + 1];
}
}
}
}
}
cout << dp[2 * n - 1][n][n];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long a[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> a[i];
}
long long vis[n + 1];
memset(vis, 0, sizeof(vis));
long long ini = 0;
for (long long i = 1; i <= n; i++) {
if (vis[i]) {
continue;
}
long long v = i;
while (!vis[v]) {
ini++;
vis[v] = 1;
v = a[v];
}
ini--;
}
long long m;
cin >> m;
while (m--) {
long long l, r;
cin >> l >> r;
ini += (r - l + 1) / 2;
if (ini % 2) {
cout << "odd" << '\n';
} else {
cout << "even" << '\n';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, x, y, cnt[N];
long long ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x >> y;
cnt[x]++;
cnt[y]++;
}
for (int i = 1; i <= n; i++) {
ans += cnt[i] * (cnt[i] - 1) / 2;
}
cout << ans;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
long long dp[100009];
bool f[100009];
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int a;
cin>>a;
f[a]=true;
}
dp[0]=1;
if(f[1]==false)dp[1]=dp[0];
for(int i=2;i<=n;i++){
if(f[i]==false){
dp[i]=dp[i-1]+dp[i-2];
dp[i]%=mod;
}
}
cout<<dp[n]<<endl;
return(0);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mx = 100005;
struct TrieNode {
struct TrieNode *children[26];
bool isLeaf;
bool odd, even;
};
struct TrieNode *getNode(void) {
struct TrieNode *pNode = new TrieNode;
if (pNode) {
long long i;
pNode->isLeaf = false;
pNode->odd = false;
pNode->even = false;
for (i = 0; i < 26; i++) pNode->children[i] = NULL;
}
return pNode;
}
void insert(struct TrieNode *root, string key) {
long long length = key.length();
long long index;
struct TrieNode *pCrawl = root;
for (long long level = 0; level < length; level++) {
long long index = key[level] - 'a';
if (!pCrawl->children[index]) pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isLeaf = true;
pCrawl->odd = true;
pCrawl->even = false;
}
void doit(struct TrieNode *root) {
for (long long i = 0; i < 26; i++) {
if (root->children[i]) {
doit(root->children[i]);
root->odd |= root->children[i]->even;
root->even |= root->children[i]->odd;
}
}
}
void solve() {
long long n, k;
cin >> n >> k;
struct TrieNode *root = getNode();
for (long long i = 0; i < n; i++) {
string s;
cin >> s;
insert(root, s);
}
doit(root);
long long flag1 = 0, flag2 = 0;
for (long long i = 0; i < 26; i++) {
struct TrieNode *t = root->children[i];
if (t and (t->odd and !(t->even))) {
flag1 = 1;
}
if (t and (t->even and !(t->odd))) {
flag2 = 1;
}
}
if (flag1 and flag2) {
cout << "First\n";
return;
}
if (!flag2 and flag1) {
if (k % 2) {
cout << "First\n";
} else {
cout << "Second\n";
}
return;
}
if (!flag1) {
cout << "Second\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
for (long long T = 1; T <= t; T++) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char fz[13] = {'8', 'x', 'x', '3', '6', '9', '4', '7', '0', '5'};
char a[10010];
int main() {
cin >> a;
int n = strlen(a);
for (int i = 0; i < n / 2 + 1; i++) {
if (a[n - i - 1] != fz[a[i] - '0']) {
cout << "No";
return 0;
}
}
cout << "YES";
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const long long MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, 1};
const int dy[] = {0, 0, 1, 1};
const long long N = (long long)5001;
const int H = 22;
const double EPS = 1e-10;
const double PI = 3.14159265358979323846;
const int SZ = 550;
const long long INF = 1e15;
const int MAXN = 2e5;
int n, m;
vector<long long> g[MAXN];
long long ord[MAXN];
vector<long long> pr[MAXN];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
int ty, x;
cin >> ty >> x;
ty--;
g[ty].push_back(x);
}
for (int i = 0; i < m; i++)
sort((g[i]).begin(), (g[i]).end()), reverse((g[i]).begin(), (g[i]).end());
for (int i = 0; i < m; i++) {
long long sum = 0;
for (int j = 0; j < g[i].size(); j++) {
sum = max(0ll, sum + g[i][j]);
ord[j + 1] += sum;
}
}
cout << *max_element(ord, ord + n + 1);
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
map<int,int>m;
int x;
for(int i=0;i<n;i++) cin>>x,m[x]++;
cout<<(m.size()==n?"YES":"NO");
return 0;
}
| 0 |
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <string>
#define SIZE 10005
#define INF 10000005
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
int V;
ll K;
struct edge{int to,cost;};
vector<edge> G[SIZE];
int d[SIZE];
void dijkstra(int s){
priority_queue<P, vector<P>, greater<P>> que;
fill(d,d+V,INF);
d[s] = 0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top();que.pop();
int v = p.second;
if(d[v] < p.first)continue;
for(int i=0;i<G[v].size();i++){
edge e = G[v][i];
if(d[e.to] > d[v] + e.cost){
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main()
{
scanf("%d",&V);
int u,k,v,c;
for(int i=0;i<V;i++){
scanf("%d%d",&u,&k);
for(int j=0;j<k;j++){
scanf("%d%d",&v,&c);
G[u].push_back((edge){v,c});
}
}
dijkstra(0);
for(int i=0;i<V;i++){
printf("%d %d\n",i,d[i]);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> x;
if (n <= 2) {
cout << 1 << endl;
} else {
int f = 1;
int i = 2;
while (i < n) {
i += x;
f++;
}
cout << f << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long l, r, x, y, k;
cin >> l >> r >> x >> y >> k;
for (long long i = x; i <= y; i++) {
long long a = i * k;
if (a >= l && a <= r) {
cout << "yes\n";
return 0;
}
}
cout << "no\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
while (cin >> s1 >> s2) {
int l1 = s1.size();
int l2 = s2.size();
int arr1[125] = {0};
int arr2[125] = {0};
for (int i = 0; i < l1; i++) {
arr1[(int)s1[i]]++;
}
for (int i = 0; i < l2; i++) {
arr2[(int)s2[i]]++;
}
int res = 0;
int cnt = 0;
for (int i = 97; i <= 122; i++) {
if (arr1[i] != 0 && arr2[i] != 0) {
res += min(arr1[i], arr2[i]);
cnt++;
}
}
int p = 0;
for (int i = 97; i <= 122; i++) {
if (arr2[i] != 0) p++;
}
if (res == 0 || cnt != p)
printf("-1\n");
else
printf("%d\n", res);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return 0;
}
void solve() {
int N;
cin >> N;
vector<int> P(N);
for (auto& p : P) {
cin >> p;
}
vector<int> ans;
ans.push_back(P[0]);
ans.push_back(P[1]);
bool inc = P[1] > P[0];
for (int i = (int)2; i < (int)N; ++i) {
if (inc) {
if (P[i - 1] < P[i]) {
ans.pop_back();
} else {
inc = false;
}
ans.push_back(P[i]);
} else {
if (P[i - 1] > P[i]) {
ans.pop_back();
} else {
inc = true;
}
ans.push_back(P[i]);
}
}
cout << ans.size() << endl;
for (auto& a : ans) {
cout << a << ' ';
}
cout << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
for (int _ = (int)0; _ < (int)T; ++_) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.size();
int mod = 1000000007;
int res = 0;
int k = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'b') {
k++;
} else {
res += k;
res %= mod;
k *= 2;
k %= mod;
}
}
cout << res;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int alphabet[26] = {0};
int key = 0, flag = 1;
cin >> key;
string s;
cin >> s;
for (int i = 0; s[i] != 0; i++) {
alphabet[s[i] - 97]++;
}
for (int i = 0; i < 26; i++) {
if (alphabet[i] % key != 0) {
cout << -1 << endl;
flag = 0;
break;
}
}
if (flag) {
for (int i = 0; i < key; i++) {
for (int j = 0; j < 26; j++) {
if (alphabet[j] > 0) {
for (int k = 0; k < alphabet[j] / key; k++) cout << char(97 + j);
}
}
}
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int N, L, R, X, i, x, j, p, max, min;
cin >> N >> L >> R >> X;
long long int a[N + 5];
for (i = 0; i < N; i++) cin >> a[i];
sort(a, a + N);
long long int count = 0;
x = pow(2, N);
for (i = 3; i < x; i++) {
int b[20];
p = log2(i);
if (pow(2, p) != i) {
p = i;
for (j = 0; j <= N; j++) b[j] = 0;
for (j = N; j >= 0; j--) {
if (p >= pow(2, j)) {
b[j] = 1;
p = p - pow(2, j);
} else
b[j] = 0;
}
max = 0;
min = 1000000009;
long long int sum = 0;
for (j = N - 1; j >= 0; j--) {
if (b[j] == 1) {
sum += a[N - 1 - j];
if (min > a[N - 1 - j]) min = a[N - 1 - j];
if (max < a[N - 1 - j]) max = a[N - 1 - j];
}
}
if (sum <= R && sum >= L && (max - min) >= X) count += 1;
}
}
cout << count << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn];
int ans[maxn];
struct FenwickTree {
int bit[maxn];
void upd(int x, int v) {
for (; x < maxn; x += (x & -x)) bit[x] += v;
}
int soma(int x) {
int ans = 0;
for (; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
int get(int p, int k) {
int pos = 0, soma = 0;
for (int i = 20; i >= 0; i--)
if (pos + (1 << i) <= p && soma + bit[pos + (1 << i)] >= k)
pos += (1 << i), soma += bit[pos];
return pos;
}
} B;
pair<pair<int, int>, int> query[maxn];
int main(void) {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= q; i++) {
int l, r;
scanf("%d %d", &l, &r);
query[i] = {{l + 1, n - r}, i};
}
sort(query + 1, query + q + 1,
[&](pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
return a.first.second < b.first.second;
});
int ant = 0;
for (int qq = 1; qq <= q; qq++) {
int l = query[qq].first.first, r = query[qq].first.second;
for (int i = ant + 1; i <= r; i++) {
if (i - a[i] < 0) continue;
int p = B.get(i, i - a[i]);
B.upd(1, 1);
B.upd(p + 1, -1);
}
ant = r;
ans[query[qq].second] = B.soma(l);
}
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
}
| 3 |
#include <bits/stdc++.h>
int main(){
int a;
std::cin >> a;
std::cout << a+a*a+a*a*a << std::endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
void solve() {
long long n;
cin >> n;
vector<long long> v;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
long long a = (n - 1) / i;
a++;
long long b = (a * (2 + (a - 1) * i)) / 2;
v.push_back(b);
if (n / i != i) {
long long j = n / i;
long long a = (n - 1) / j;
a++;
long long b = (a * (2 + (a - 1) * j)) / 2;
v.push_back(b);
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
}
int main() {
int t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int main(){
int a[6];
for(int i=0;i<6;i++) cin >> a[i];
if(a[5]<a[4]-a[0]) cout << ":(" << endl;
else cout << "Yay!" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<bool> was;
int dfs(int x, vector<vector<int> > &g) {
was[x] = true;
int ans = 1;
for (int i = 0; i < g[x].size(); ++i) {
if (!was[g[x][i]]) {
ans += dfs(g[x][i], g);
}
}
return ans;
}
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<int> > g(n);
for (int i = 0; i < m; ++i) {
int l, r;
cin >> l >> r;
--l;
--r;
g[l].push_back(r);
g[r].push_back(l);
}
was = vector<bool>(n, false);
vector<int> a;
for (int i = 0; i < n; ++i) {
if (!was[i]) {
a.push_back(dfs(i, g));
}
}
for (int i = 0; i < a.size(); ++i) a[i] = min(a[i], k);
int c = a.size();
int sum = 0;
for (int i = 0; i < c; ++i) sum += a[i];
if (sum >= 2 * (c - 1)) {
cout << 0;
return 0;
}
if (k == 1) {
cout << c - 2;
} else {
int ans = 0;
while (sum < 2 * (c - 1)) {
--c;
++ans;
}
cout << ans;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
long long mul(long long a, long long b) { return (a * b) % (1000000007); }
long long add(long long a, long long b) { return (a + b) % (1000000007); }
long long sub(long long a, long long b) {
return ((a - b) % (1000000007) + (1000000007)) % (1000000007);
}
void upd(long long &a, long long b) {
a = (a % (1000000007) + b % (1000000007)) % (1000000007);
}
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int a[123456];
int p[(123456)], tot;
bool b[(123456)] = {0};
void make_prime(int n) {
tot = 0;
for (int i = 2; i <= n; i++) {
if (!b[i]) p[++tot] = i;
for (int j = 1; j <= tot; j++) {
if (i * p[j] > n) break;
b[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}
unsigned long long c[(123456)];
int n;
int gauss(int n, unsigned long long *a) {
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++)
if (a[j] > a[i]) swap(a[i], a[j]);
if (a[i] == 0) return i - 1;
for (int k = 1; k <= n; k++)
if (i ^ k) a[k] = min(a[k], a[k] ^ a[i]);
}
return n;
}
int main() {
make_prime(70);
int n = read();
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= tot; j++)
if (a[i] % p[j] == 0) {
int t = 0;
while (a[i] % p[j] == 0) t ^= 1, a[i] /= p[j];
if (t) c[i] |= (1ULL << j - 1);
}
}
int p = gauss(n, c);
p = n - p;
int t = 1;
for (int i = 1; i <= p; i++) t = t * 2 % (1000000007);
t = sub(t, 1);
cout << t << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long c, hr, hb, wr, wb;
inline long long max(long long a, long long b) {
if (a > b)
return a;
else
return b;
}
int main() {
cin >> c >> hr >> hb >> wr >> wb;
long long ans = 0;
int t = c / wr;
for (int i = 0; i <= t && i < 100000; i++) {
ans = max(ans, i * hr + (c - i * wr) / wb * hb);
}
swap(hr, hb);
swap(wr, wb);
t = c / wr;
for (int i = 0; i <= t && i < 100000; i++) {
ans = max(ans, i * hr + (c - i * wr) / wb * hb);
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const long long LINF = 1e18;
const int N = 2100;
const int MOD = 1e9 + 7;
int n, k;
int p[N];
vector<long long> A, B;
int sum = 0;
void dfs(int h, int Max, long long now, vector<long long> &v) {
if (h > Max) {
v.push_back(now);
} else {
while (1) {
dfs(h + 1, Max, now, v);
if (1e18 / p[h] < now) break;
now *= p[h];
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
cin >> k;
dfs(0, min(6, n - 1), 1, A);
dfs(min(6, n - 1) + 1, n - 1, 1, B);
sort(A.begin(), A.end());
sort(B.begin(), B.end());
long long l = 0, r = 1e18;
while (l < r) {
long long mid = (l + r) >> 1, cnt = 0;
for (int i = A.size() - 1, j = 0; i >= 0; i--) {
while (j < B.size() && mid / A[i] >= B[j]) j++;
cnt += j;
}
if (cnt < k)
l = mid + 1;
else
r = mid;
}
cout << l << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100004];
int n, m, x, y, p[100004], c[100004], dp[100004][18], l[100004], val, flag,
child;
long long f[100004], sum[100004], ans, ways;
void dfs(int v, int u) {
p[v] = u;
l[v] = (v == u) ? 0 : l[u] + 1;
int sz = adj[v].size();
for (int i = 0; i < sz; i++) {
if (adj[v][i] == u) continue;
dfs(adj[v][i], v);
c[v] += c[adj[v][i]];
f[v] += (f[adj[v][i]] + c[adj[v][i]]);
}
c[v]++;
}
int find(int x, int y) {
int res = 0;
for (int i = 18 - 1; i >= 0; i--) {
if (l[dp[x][i]] > l[y]) {
res += (1 << i);
x = dp[x][i];
}
}
if (l[x] > l[y]) {
if (p[x] == y) child = x;
x = p[x];
res++;
}
if (x == y) {
flag = 1;
return res;
}
for (int i = 18 - 1; i >= 0; i--) {
if (dp[x][i] != dp[y][i]) {
x = dp[x][i];
y = dp[y][i];
res += (1 << (i + 1));
}
}
return res + 2;
}
void findsum(int v, int u) {
if (v == u)
sum[v] = f[v];
else
sum[v] = sum[u] + n - 2 * c[v];
int sz = adj[v].size();
for (int i = 0; i < sz; i++)
if (adj[v][i] != u) findsum(adj[v][i], v);
}
int main() {
cin >> n >> m;
for (int i = 0; i < n - 1; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 1);
findsum(1, 1);
for (int i = 0; i < 18; i++)
for (int j = 1; j <= n; j++)
dp[j][i] = (i == 0) ? p[j] : dp[dp[j][i - 1]][i - 1];
for (int i = 0; i < m; i++) {
cin >> x >> y;
flag = 0;
if (l[y] > l[x]) swap(x, y);
val = find(x, y);
val++;
if (flag == 0) {
ans = 1LL * val * c[x] * c[y] + 1LL * c[x] * f[y] + 1LL * c[y] * f[x];
ways = 1LL * c[x] * c[y];
} else {
ans = 1LL * val * c[x] * (n - c[child]) + 1LL * f[x] * (n - c[child]) +
1LL * c[x] * (sum[y] - f[child] - c[child]);
ways = 1LL * c[x] * (n - c[child]);
}
printf("%.14lf\n", ((double)(ans)) / ways);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int findr(vector<long long> &v1, vector<long long> &v2, long long t,
long long left, int &right, long long a) {
if (left == 0) {
int m = 0;
for (int i = right; i < v2.size(); ++i) {
if (t >= v2[i]) {
m = i;
} else {
break;
}
}
right = m;
return m;
}
t -= v1[left];
t += v1[1];
t -= left * a - a;
int m = 0;
for (int i = right; i < v2.size(); ++i) {
if (t >= v2[i]) {
m = i;
} else {
break;
}
}
right = m;
return m;
}
long long foo(vector<long long> &v1, vector<long long> &v2, long long t,
long long a) {
int m = 0;
for (int i = 0; i < v1.size(); ++i) {
if (t >= v1[i]) {
m = i;
} else {
break;
}
}
int left = m;
int right = 0;
while (left) {
findr(v1, v2, t, left, right, a);
m = max(m, left + right - (left != 0 and right != 0));
--left;
}
return m;
}
int main() {
long long n, a, b, t;
string s;
cin >> n >> a >> b >> t >> s;
vector<long long> v1, v2;
v1.push_back(-a);
v2.push_back(-a);
for (int i = 0; i < s.size(); ++i) {
v1.push_back(v1.back() + a + 1 + b * (s[i] == 'w'));
}
s.push_back(s[0]);
reverse(s.begin(), s.end());
s.pop_back();
for (int i = 0; i < s.size(); ++i) {
v2.push_back(v2.back() + a + 1 + b * (s[i] == 'w'));
}
if (t >= v1.back()) {
cout << n;
return 0;
}
long long m = max(foo(v1, v2, t, a), foo(v2, v1, t, a));
cout << m;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> vec(m);
for (int i = 0; i < m; i++) cin >> vec[i];
if (m == 0) {
cout << "YES" << endl;
return 0;
}
sort(vec.begin(), vec.end());
if (vec[0] == 1 || vec[m - 1] == n) {
cout << "NO" << endl;
return 0;
}
int count = 0;
for (int i = 1; i < m; i++) {
if (vec[i] - vec[i - 1] == 1)
count++;
else
count = 0;
if (count == 2) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
}
| 2 |
#include <cmath>
#include <iostream>
using namespace std;
const int spl = 1048576;
const double pi = acos(-1.0);
class point3d {
public:
double x, y, z;
point3d() : x(0), y(0), z(0) {};
point3d(double x_, double y_, double z_) : x(x_), y(y_), z(z_) {};
point3d& operator+=(const point3d& p) { x += p.x; y += p.y; z += p.z; return *this; }
point3d& operator-=(const point3d& p) { x -= p.x; y -= p.y; z -= p.z; return *this; }
point3d& operator*=(const double v) { x *= v; y *= v, z *= v; return *this; }
point3d operator+(const point3d& p) const { return point3d(*this) += p; }
point3d operator-(const point3d& p) const { return point3d(*this) -= p; }
point3d operator*(const double v) const { return point3d(*this) *= v; }
double abs() const { return sqrt(x * x + y * y + z * z); }
double dot(point3d& p) const { return x * p.x + y * p.y + z * p.z; }
point3d cross(point3d& p) const { return point3d(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x); }
};
void input(point3d *v) {
cin >> (*v).x >> (*v).y >> (*v).z;
}
int main() {
point3d c1, vx1, vy1, c2, vx2, vy2;
input(&c1); input(&vx1); input(&vy1); input(&c2); input(&vx2); input(&vy2);
point3d vz2 = vx2.cross(vy2);
bool flag = false;
int cnt = 0;
double pre = -1.0;
for (int i = 0; i <= spl; ++i) {
double ang = i * 2 * pi / spl;
point3d p = c1 + vx1 * cos(ang) + vy1 * sin(ang);
point3d d = p - c2;
double t = d.dot(vz2);
if (i >= 1 && d.abs() <= 1.0 && pre * t <= 0.0) {
++cnt;
}
pre = t;
}
cout << (cnt == 1 ? "YES\n" : "NO\n");
return 0;
}
| 0 |
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define EPS (1e-10)
#define equals(a,b) (fabs((a)-(b)) < EPS)
static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;
class Point
{
public:
double x, y;
Point(double x = 0, double y = 0) :x(x), y(y) {}
Point operator + (Point p) { return Point(x + p.x, y + p.y); }
Point operator - (Point p) { return Point(x - p.x, y - p.y); }
Point operator * (double a) { return Point(a * x, a * y); }
Point operator / (double b) { return Point(x / b, y / b); }
friend istream& operator >> (istream & s, Point p) { s >> p.x >> p.y; }
double dot(Point a, Point b) { return a.x* b.x + a.y * b.y; }
double abs() { return sqrt(norm()); }
double norm() { return x * x + y * y; }
bool operator <(const Point & p)const { return x != p.x ? x < p.x : y < p.y; }
bool operator == (const Point & p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; }
};
typedef Point Vector;
struct Segment {
Point p1, p2;
Segment() {}
Segment(Point p1, Point p2) :p1(p1), p2(p2) {}
};
typedef Segment Line;
double norm(Vector a) { return a.x* a.x + a.y * a.y; }
double abs(Vector a) { return sqrt(norm(a)); }
double dot(Vector a, Vector b) { return a.x* b.x + a.y * b.y; }
double cross(Vector a, Vector b) { return a.x* b.y - a.y * b.x; }
int ccw(Point p0, Point p1, Point p2) { //判断有三个点构成的两个向量之间的关系(相反,同向(谁更大),逆时针方向,顺时针方向)
Vector a = p1 - p0;
Vector b = p2 - p0;
if (cross(a, b) > EPS) return COUNTER_CLOCKWISE; //如果外积为正,那么就是顺时针
if (cross(a, b) < -EPS) return CLOCKWISE; //如果外积为负,那么就是逆时针
if (dot(a, b) < -EPS) return ONLINE_BACK; //上面两种情况都不符合的时候,就一定在一条直线上,如果内积为负,那就是相反方向
if (a.norm() < b.norm()) return ONLINE_FRONT; //如果上面条件还不符合的话,那就按看谁的大了,
return ON_SEGMENT;
}
bool intersect(Point p1, Point p2, Point p3, Point p4) {
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 &&
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
}
int main()
{
int q;
cin >> q;
double x0, y0, x1, y1, x2, y2, x3, y3;
while (q--)
{
cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
Point p0 = Point(x0, y0), p1 = Point(x1, y1), p2 = Point(x2, y2), p3 = Point(x3, y3);
cout << intersect(p0, p1, p2, p3) << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int ar[600][600][2] = {};
int n, q;
cin >> n >> q;
ar[300][300][0] = n;
int l = 1;
int i = 0;
while (l) {
l = 0;
for (int x = 233; x < 367; x++)
for (int y = 233; y < 367; y++) ar[x][y][!i] = ar[x][y][i];
for (int x = 233; x < 367; x++)
for (int y = 233; y < 367; y++)
if (ar[x][y][i] >= 4) {
ar[x][y][!i] -= 4;
ar[x + 1][y][!i]++;
ar[x - 1][y][!i]++;
ar[x][y + 1][!i]++;
ar[x][y - 1][!i]++;
l++;
}
i = !i;
}
for (int o = 0; o < q; o++) {
int x, y;
cin >> x >> y;
if (x >= 100 || x <= -100 || y >= 100 || y <= -100)
cout << 0 << "\n";
else
cout << ar[x + 300][y + 300][i] << "\n";
}
}
| 2 |
#include <iostream>
#include <complex>
#include <vector>
#include <algorithm>
using namespace std;
class UnionFind
{
public:
vector<int> data;
UnionFind(int size):data(size, -1){}
bool unionSet(int x, int y)
{
x = root(x); y=root(y);
if(x != y)
{
if(data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y)
{
return root(x) == root(y);
}
int root(int x)
{
return data[x] < 0 ? x:data[x]=root(data[x]);
}
};
#define EPS 1e-8
typedef complex<double> P;
typedef vector<P> G;
struct L : public vector<P> {
L(const P &a, const P &b) {
push_back(a); push_back(b);
}
};
double dot(const P& a, const P& b)
{
return real(conj(a)*b);
}
double cross(const P& a, const P& b)
{
return imag(conj(a)*b);
}
int ccw(P a, P b, P c)
{
b -= a; c -= a;
if (cross(b, c) > +EPS) return +1;
if (cross(b, c) < -EPS) return -1;
if (dot(b, c) < -EPS) return +2;
if (norm(b) < norm(c)) return -2;
return 0;
}
bool intersectSS(const L &s, const L &t) {
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 &&
ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
bool intersectSP(const L &s, const P &p) {
return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
bool isIntoG(const G& g, const P& p)
{
for(int i=0; i<g.size(); i++)
{
if(ccw(p, g[i], g[(i+1)%g.size()]) != +1) return false;
}
return true;
}
bool isInto(const G& l, const G& r)
{
for(int i=0; i<r.size(); i++)
if(isIntoG(l, r[i])) return true;
return false;
}
bool intersect(const G l, const G r)
{
for(int i=0; i<l.size(); i++)
for(int j=0; j<r.size(); j++)
{
if(intersectSS(L(l[i], l[(i+1)%l.size()]), L(r[j], r[(j+1)%r.size()]))) return true;
if(abs(l[i]-r[j]) < EPS) return true;
if(intersectSP(L(l[i], l[(i+1)%l.size()]), r[j])) return true;
if(intersectSP(L(r[j], r[(j+1)%r.size()]), l[i])) return true;
}
return false;
}
bool isConnect(const G l, G r)
{
return (intersect(l,r)||isInto(l,r)||isInto(r,l));
}
int main()
{
int N,M;
while(cin >> N, N)
{
while(N--)
{
cin >> M;
vector<G> rect;
while(M--)
{
G t;
for(int i=0; i<4; i++)
{
int x,y;
cin >> x >> y;
t.push_back(P(x,y));
}
reverse(t.begin(), t.end());
rect.push_back(t);
}
UnionFind uf(rect.size());
for(int i=0; i<rect.size(); i++)
for(int j=i+1; j<rect.size(); j++)
if(isConnect(rect[i], rect[j])) uf.unionSet(i,j);
int ret=0;
bool checked[100]={0};
for(int i=0; i<rect.size(); i++)
{
if(checked[i]) continue;
checked[i]=1;
ret++;
for(int j=0; j<rect.size(); j++)
if(uf.findSet(i,j)) checked[j]=1;
}
cout << ret << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int povezave[105][105];
bool obiskano[105];
int pot[105];
int pojntr;
int cena;
int velikost;
bool DFS(int kje) {
pot[pojntr] = kje;
obiskano[kje] = 1;
pojntr++;
if (kje == 1) {
return 1;
}
for (int i = 0; i < velikost; i++) {
if (i == kje) {
continue;
}
if (!obiskano[i] && povezave[kje][i] > 0) {
int prej = cena;
if (povezave[kje][i] < cena) {
cena = povezave[kje][i];
}
if (DFS(i)) {
return 1;
}
cena = prej;
}
}
pojntr--;
return 0;
}
int ford_fulkerson() {
int flow = 0;
while (1) {
cena = 1000000;
pojntr = 0;
for (int i = 0; i < velikost; i++) {
obiskano[i] = 0;
}
if (!DFS(0)) {
return flow;
}
flow += cena;
pojntr--;
int cilj = pot[pojntr];
pojntr--;
while (pojntr >= 0) {
povezave[cilj][pot[pojntr]] += cena;
povezave[pot[pojntr]][cilj] -= cena;
cilj = pot[pojntr];
pojntr--;
}
}
return flow;
}
vector<bool> prime;
vector<int> plist;
void eratosten(long koliko) {
for (long i = 0; i < koliko; i++) {
prime.push_back(1);
}
prime.at(0) = 0;
prime.at(1) = 0;
for (long i = 0; i < koliko; i++) {
if (prime.at(i)) {
plist.push_back(i);
for (long j = i * 2; j < koliko; j += i) {
prime.at(j) = 0;
}
}
}
}
int f(int a, int b) {
int rez = 0;
while (a % b == 0) {
a /= b;
rez++;
}
return rez;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
velikost = n + 2;
int cifre[100];
bool nucano[100];
for (int i = 0; i < 100; i++) {
nucano[i] = 0;
}
int p1[100];
int p2[100];
eratosten(32000);
for (int i = 0; i < n; i++) {
scanf("%d", &cifre[i]);
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &p1[i], &p2[i]);
if (p1[i] % 2 == 0) {
swap(p1[i], p2[i]);
}
}
for (int i = 0; i < velikost; i++) {
for (int j = 0; j < velikost; j++) {
povezave[i][j] = 0;
}
}
long long ret = 0;
for (int i = 0; i < plist.size(); i++) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
povezave[0][j + 2] = f(cifre[j], plist[i]);
if (povezave[0][j + 2] > 0) {
nucano[j] = 1;
}
} else {
povezave[j + 2][1] = f(cifre[j], plist[i]);
if (povezave[j + 2][1] > 0) {
nucano[j] = 1;
}
}
}
for (int j = 0; j < m; j++) {
povezave[p1[j] + 1][p2[j] + 1] =
min(f(cifre[p1[j] - 1], plist[i]), f(cifre[p2[j] - 1], plist[i]));
}
ret += ford_fulkerson();
}
for (int i = 0; i < m; i++) {
if (cifre[p1[i] - 1] == cifre[p2[i] - 1] && !nucano[p1[i] - 1] &&
!nucano[p2[i] - 1]) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
povezave[0][j + 2] = f(cifre[j], cifre[p2[i] - 1]);
if (povezave[0][j + 2] > 0) {
nucano[j] = 1;
}
} else {
povezave[j + 2][1] = f(cifre[j], cifre[p2[i] - 1]);
if (povezave[j + 2][1] > 0) {
nucano[j] = 1;
}
}
}
for (int j = 0; j < m; j++) {
povezave[p1[j] + 1][p2[j] + 1] =
min(f(cifre[p1[j] - 1], cifre[p2[i] - 1]),
f(cifre[p2[j] - 1], cifre[p2[i] - 1]));
}
ret += ford_fulkerson();
}
}
cout << ret << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
const double PI = acos(-1.0), EPS = 1e-9;
int N;
double R, V, S, F;
int main() {
scanf("%d%lf%lf", &N, &R, &V);
while (N--) {
scanf("%lf%lf", &S, &F);
double len = F - S, ans = 0.0;
double remain = fmod(len, 2.0 * PI * R);
ans += len - remain;
remain *= 0.5;
double l = 0.0, r = PI;
while (remain - (R * l + R * sin(l)) > EPS * remain) {
double mid = 0.5 * (l + r);
if (R * mid + R * sin(mid) <= remain)
l = mid;
else
r = mid;
}
ans += 2.0 * (l * R);
printf("%.9f\n", ans / V);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int c = 0;
if (s.length() >= 3) {
for (int i = 0; i <= s.length() - 3; i++) {
if (s[i] == 'Q') {
for (int j = i; j < s.length() - 1; j++) {
if (s[j] == 'A') {
for (int m = j; m <= s.length(); m++) {
if (s[m] == 'Q') {
c++;
}
}
}
}
}
}
}
cout << c << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, MOD = 1e9 + 7;
inline int rd() {
char ch = getchar();
int ret = 0;
while (ch > '9' || ch < '0') ch = getchar();
while (ch <= '9' && ch >= '0') ret = ret * 10 + ch - '0', ch = getchar();
return ret;
}
inline void inc(int &a, int b) { a = (a + (b % MOD)) % MOD; }
inline void dec(int &a, int b) { a = (((a - b) % MOD) + MOD) % MOD; }
int f[N], g1[N], g2[N], g0[N];
int n, pw0[6], pw2[N];
int main() {
n = rd();
for (int i = 1; i <= n; ++i) {
int x = rd();
inc(g0[x], 1), inc(g1[x], x), inc(g2[x], 1ll * x * x % MOD);
}
pw0[0] = pw2[0] = 1;
for (int i = 1; i <= 5; ++i) pw0[i] = pw0[i - 1] * 10;
for (int i = 1; i <= N - 5; ++i) pw2[i] = (1ll * pw2[i - 1] * 2) % MOD;
for (int i = 0; i <= 5; ++i)
for (int j = N - 5; j >= 0; --j)
if ((j / pw0[i] % 10) < 9 && j + pw0[i] < N - 5) {
inc(g0[j], g0[j + pw0[i]]), inc(g1[j], g1[j + pw0[i]]),
inc(g2[j], g2[j + pw0[i]]);
}
for (int j = 0; j <= N - 5; ++j) {
if (!g0[j])
continue;
else if (g0[j] == 1)
inc(f[j], g2[j]);
else
inc(f[j], 1ll * g2[j] * pw2[g0[j] - 1] % MOD),
inc(f[j], 1ll * ((1ll * g1[j] * g1[j] % MOD - g2[j]) % MOD + MOD) %
MOD * pw2[g0[j] - 2] % MOD);
}
for (int i = 0; i <= 5; ++i)
for (int j = 0; j <= N - 5; ++j)
if (j / pw0[i] % 10 != 9) {
dec(f[j], f[j + pw0[i]]);
}
long long res = 0;
for (int i = 0; i <= 999999; ++i) res ^= 1ll * i * f[i];
printf("%lld\n", res);
return 0;
}
| 4 |
#include <bits/stdc++.h>
inline int read() {
int data = 0, w = 1;
char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(1e5 + 10);
int n, p[N];
void dfs(int m) {
if (m == 0) return;
int k = 1;
while ((k << 1) <= m) k <<= 1;
std::reverse(p + (k << 1) - m - 1, p + m + 1);
dfs((k << 1) - m - 2);
}
int main() {
n = read();
if (n & 1)
puts("NO");
else {
for (int i = 1; i <= n; i++) p[i] = i;
puts("YES"), dfs(n);
for (int i = 1; i <= n; i++) printf("%d%c", p[i], " \n"[i == n]);
}
if (n == (n & -n) || n == 3 || n == 5)
puts("NO");
else {
puts("YES");
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 2; i < n; i += 2) std::swap(p[i], p[i + 1]);
if (n & 1) {
printf("7 3 2 5 6 4 1%c", " \n"[n == 7]);
for (int i = 8; i <= n; i++) printf("%d%c", p[i], " \n"[i == n]);
} else {
p[1] = n - 1, p[n - 2] = n, p[n - 1] = 1, p[n] = n - 2;
for (int i = 1; i <= n; i++) printf("%d%c", p[i], " \n"[i == n]);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int i, q, a, b, n, s, c, r;
int main() {
cin >> q;
for (i = 1; i <= q; i++) {
cin >> a >> b >> n >> s;
c = s / n;
r = s % n;
if (r == 0) {
if (a >= c)
cout << "YES"
<< "\n";
else {
c -= a;
if (b >= c * n)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
} else {
if (a >= c) {
if (b >= r)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
} else {
c -= a;
if (b >= (c * n) + r)
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<string> v(51, "TONY_STARK");
long long int st, ed, st1, ed1;
long long int n;
long long int vis[51][51];
long long int dx[] = {-1, 0, 1, 0};
long long int dy[] = {0, 1, 0, -1};
set<pair<long long int, long long int> > A, B;
bool isvalid(long long int x, long long int y) {
if (x < 0 || y < 0 || x >= n || y >= n) {
return false;
}
if (vis[x][y] || v[x][y] == '1') {
return false;
} else {
return true;
}
}
void dfs(long long int x, long long int y, long long int porn) {
vis[x][y] = 1;
if (porn == 1) {
A.insert({x, y});
} else if (porn == 2) {
B.insert({x, y});
}
for (long long int j = 0; j < 4; j++) {
if (isvalid((x + dx[j]), (y + dy[j]))) dfs(x + dx[j], y + dy[j], porn);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
long long int ct = 0;
cin >> n;
cin >> st >> ed;
st--, ed--;
cin >> st1 >> ed1;
st1--, ed1--;
for (long long int i = 0; i < n; i++) {
cin >> v[i];
}
dfs(st, ed, 1);
memset(vis, 0, sizeof(vis));
dfs(st1, ed1, 2);
long long int ans = INT_MAX;
for (auto i : A) {
for (auto j : B) {
ans = min(ans, (i.first - j.first) * (i.first - j.first) +
(i.second - j.second) * (i.second - j.second));
}
}
cout << ans;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
const long long MOD = 1000000007;
long long n, l, k, m, p, s;
pair<int, bool> a[MAXN];
pair<int, int> b[MAXN];
long long dp[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> l >> k;
for (int i = 0; i < n; ++i) {
cin >> a[i].first;
a[i].second = (i < l % n);
}
sort(a, a + n);
for (int i = 0; i < n; ++i) {
if (i > 0 && a[i - 1].first != a[i].first) ++m;
++b[m].first;
b[m].second += a[i].second;
}
++m;
p = l / n;
for (int i = 1; i <= k; ++i) {
for (int j = 0; j < m; ++j) {
dp[j] = dp[j] * b[j].first % MOD;
dp[j] = (j > 0) ? (dp[j] + dp[j - 1]) % MOD : dp[j];
dp[j] = (i == 1) ? 1 : dp[j];
s = (s + dp[j] * b[j].first % MOD * (max(p - i + 1, 0ll) % MOD) % MOD) %
MOD;
s = (s + dp[j] * b[j].second * (p + 1 >= i) % MOD) % MOD;
}
}
cout << s << endl;
cin >> n;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b) {
if (b == 0) return 1;
long long res = binpow(a, b / 2);
res = (res * res) % 1000000007LL;
if (b % 2) {
res = (res * a) % 1000000007LL;
}
return res;
}
long long inv(long long n) { return binpow(n, 1000000007LL - 2); }
bool compare(const pair<long long, long long>& i,
const pair<long long, long long>& j) {
if (i.first != j.first)
return i.first < j.first;
else
return i.second > j.second;
}
vector<long long> factorize(long long n) {
vector<long long> v;
for (long long i = 2; i * i <= n; i++) {
while (n % i == 0) {
v.push_back(i);
n /= i;
}
}
if (n != 1) v.push_back(n);
return v;
}
long long nCrModpDP(long long n, long long r) {
long long C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++) {
for (long long j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % 1000000007LL;
}
return C[r];
}
long long binomialCoefficient(long long n, long long r) {
if (r == 0) return 1;
long long ni = n % 1000000007LL, ri = r % 1000000007LL;
return (binomialCoefficient(n / 1000000007LL, r / 1000000007LL) *
nCrModpDP(ni, ri)) %
1000000007LL;
}
vector<bool> sieve(long long n) {
vector<bool> PRIME(n + 1, true);
PRIME[0] = false;
PRIME[1] = false;
for (long long p = 2; p * p <= n; p++) {
if (PRIME[p] == true) {
for (long long i = p * p; i <= n; i += p) {
PRIME[i] = false;
}
}
}
return PRIME;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n, a;
vector<pair<long long, long long> > v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
v.push_back({a, i});
}
sort((v).begin(), (v).end());
long long maxm = v[0].second, minm = v[0].second;
for (long long i = 0; i < n; i++) {
maxm = max(maxm, v[i].second);
minm = min(minm, v[i].second);
if (maxm - minm == i)
cout << "1";
else
cout << "0";
}
cout << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
int n, m, l, k, s;
bool a[201][201];
int work(int q, int m) {
int i, j, x;
s = 0;
for (x = 3; x <= 100; x++) {
s += (x - 1) * (x - 2) / 2;
if (s > q) {
s -= (x - 1) * (x - 2) / 2;
break;
}
}
x--;
l = q - s;
for (i = m + 1; i <= m + x; i++)
for (j = i + 1; j <= m + x; j++) a[i][j] = a[j][i] = 1;
while (l) {
for (i = 1; i <= x; i++)
if (i * (i - 1) / 2 <= l && i * (i + 1) / 2 > l) {
l -= i * (i - 1) / 2;
x++;
for (j = m + 1; j <= m + i; j++) a[j][n + x] = a[n + x][j] = 1;
break;
}
}
n += x;
return l;
}
int main() {
int i, j;
scanf("%d", &k);
work(k, 0);
printf("%d\n", n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) printf("%d", a[i][j]);
printf("\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
int n, val[N + 5], cnt[N + 5], x, sum[N + 5], las[2 * N + 5], ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &val[i]), cnt[val[i]]++;
for (int i = 1; i <= n; i++)
if (cnt[i] > cnt[x]) x = i;
int lim = sqrt(n) + 1;
for (int i = 1; i <= n; i++)
if (cnt[i] >= lim && i != x) {
int y = i;
for (int j = 1; j <= n; j++)
if (val[j] == x)
sum[j] = 1;
else if (val[j] == y)
sum[j] = -1;
else
sum[j] = 0;
for (int j = 0; j <= 2 * n; j++) las[j] = -1;
las[n] = 0;
sum[0] = n;
for (int j = 1; j <= n; j++) {
sum[j] += sum[j - 1];
if (las[sum[j]] != -1)
ans = max(ans, j - las[sum[j]]);
else
las[sum[j]] = j;
}
}
for (int i = 1; i < lim; i++) {
for (int j = 1; j <= n; j++) sum[j] = 0;
int l = 1, num = 0;
for (int j = 1; j <= n; j++) {
sum[val[j]]++;
if (sum[val[j]] == i)
num++;
else
while (sum[val[j]] > i) {
if (sum[val[l]] == i) num--;
sum[val[l]]--;
l++;
}
if (num >= 2) ans = max(ans, j - l + 1);
}
}
printf("%d", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
long long a;
map<long long, long long> mp;
long long n;
int main() {
scanf("%lld", &n);
long long sum = 0, ans = 0;
mp[0] = 0;
long long last = 0;
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a);
sum += a;
if (mp.find(sum) != mp.end()) last = max(last, mp[sum] + 1);
ans += i - last;
mp[sum] = i;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:20000000")
using namespace std;
using mt = int64_t;
using fl = double;
template <typename T>
constexpr mt infValue = std::numeric_limits<T>::max() - 1000;
template <typename T>
constexpr mt maxValue =
std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;
constexpr mt INF = infValue<mt>;
constexpr int64_t MOD = 1000000007ll;
constexpr double EPS = 1e-6;
constexpr mt MAX = maxValue<mt>;
using pr = pair<mt, mt>;
constexpr auto N = 101;
constexpr auto K = 256;
constexpr auto P = 19;
constexpr auto M = 200111;
mt n, k, answ;
mt a[N];
struct Mat {
mt m[N][N] = {0};
Mat(mt s) {
for (mt i = 0; i < (N); ++i)
for (mt j = 0; j < (N); ++j) m[i][j] = 0;
for (mt i = 0; i < (n); ++i) m[i][i] = s;
}
friend Mat operator*(const Mat& a, const Mat& b) {
Mat res(0);
for (mt i = 0; i < (n); ++i) {
for (mt j = 0; j < (n); ++j) {
for (mt p = 0; p < (n); ++p) {
res.m[i][j] = (res.m[i][j] + a.m[i][p] * b.m[p][j] % MOD) % MOD;
}
}
}
return res;
}
} base(0);
inline bool f(mt x) {
mt v = 0;
while (x) {
v += x & 1;
x >>= 1;
}
return v % 3 == 0;
}
Mat binPow(Mat a, mt x) {
Mat res(1);
while (x) {
if (x & 1) res = res * a;
x >>= 1;
a = a * a;
}
return res;
}
int main(void) {
scanf("%lld %lld", &n, &k);
for (mt i = 0; i < (n); ++i) scanf("%lld", a + i);
for (mt i = 0; i < (n); ++i)
for (mt j = 0; j < (n); ++j) base.m[i][j] = f(a[i] ^ a[j]);
base = binPow(base, k - 1);
for (mt i = 0; i < (n); ++i)
for (mt j = 0; j < (n); ++j) answ = (answ + base.m[i][j]) % MOD;
printf("%lld\n", answ);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
int a[150];
int gcd(int x, int y);
int main() {
int n;
cin >> n;
int Max = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
Max = max(a[i], Max);
}
int ans = 0;
int Min = a[0];
for (int i = 0; i < n; i++) {
Min = gcd(Min, a[i]);
}
ans = Max / Min - n;
if (ans % 2 == 0)
cout << "Bob" << endl;
else
cout << "Alice" << endl;
return 0;
}
int gcd(int x, int y) {
int tem;
if (x < y) {
tem = x;
x = y;
y = tem;
}
if (y == 0)
return x;
else
return gcd(y, x % y);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
constexpr ll mod = 1e9+7;
struct mint {
ll x;
mint(ll x=0):x((x%mod+mod)%mod){}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vec<int> g(1<<20,0);
for(int i=0;i<N;i++){
int a;
cin >> a;
g[a]++;
}
for(int i=0;i<20;i++){
for(int S=(1<<20)-1;S>=0;S--) if(!(S&(1<<i))) g[S] += g[S^(1<<i)];
}
mint two = 2;
mint ans = 0;
for(int S=0;S<(1<<20);S++){
int d = __builtin_popcount(S);
if(d%2==0) ans += two.pow(g[S])-1;
else ans -= two.pow(g[S])-1;
}
cout << ans.x << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector<int> v;
int main() {
while (scanf("%d%d%d", &n, &m, &k) != EOF) {
v.clear();
for (int i = 0; i < k; i++) v.push_back(1);
v.push_back(n);
for (int i = 1; i < k; i++) v.push_back(n + 1);
int l = 2 * n;
if (k == 1) l--;
while (l < n + m + 1) {
v.push_back(l);
if (l + 1 == n + m + 1) break;
for (int i = 1; i < k; i++) v.push_back(l + 1);
l += n;
if (k == 1) l--;
}
printf("%d\n%d", v.size(), v[0]);
for (int i = 1; i < v.size(); i++) printf(" %d", v[i]);
puts("");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n;
long long t;
long long a[300000];
int p[300000];
pair<long long, long long> r[300000];
long long ans[300000];
int main() {
int i, j, k, l;
cin >> n >> t;
for (i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
for (i = 0; i < n; i++) scanf("%d", &p[i]), p[i]--;
for (i = 0; i < n; i++) {
if ((i != n - 1 && p[i] > p[i + 1]) || p[i] < i) {
printf("No\n");
return 0;
}
}
for (i = 0; i < n; i++) {
r[i].first = a[i] + t;
r[i].second = 0x3f3f3f3f3f3f3f3fll;
}
for (i = 0; i < n; i++) {
for (j = i; j < p[i]; j++) r[j].first = max(r[j].first, a[j + 1] + t);
if (p[i] != n - 1) {
r[p[i]].second = min(r[p[i]].second, a[p[i] + 1] + t - 1);
}
for (j = i; j <= p[i]; j++) {
if (p[j] != p[i]) {
printf("No\n");
return 0;
}
}
i = p[i];
}
for (i = 0; i < n; i++) {
if (r[i].first > r[i].second) {
printf("No\n");
return 0;
}
ans[i] = r[i].first;
if (i != n - 1) r[i + 1].first = max(r[i + 1].first, ans[i] + 1);
}
printf("Yes\n");
for (i = 0; i < n; i++) printf("%lld ", ans[i]);
return 0;
}
| 1 |
#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
int dp[1400][1400];
int f[1400][1400];
int main() {
int h, w; scanf("%d%d", &h, &w);
rep(i, h)rep(j, w)scanf("%d", &f[i][j]);
int Max = 0;
rep(i, h)rep(j, w){
if (!f[i][j]) {
dp[i][j] = 1;
Max = 1;
}
}
for (int i = 1; i < h; i++)for (int j = 1; j < w; j++) {
if (f[i][j])continue;
dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
Max = max(Max, dp[i][j]);
}
printf("%d\n", Max*Max);
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using ll = long long;
using namespace std;
int main() {
int l, v, h;
cin >> l >> v >> h;
if (l < v || l < h) {
cout << 0 << endl;
return 0;
}
if (l >= 4 * v + 2 * h) {
cout << 1 << endl;
return 0;
}
if (l / v >= 4) {
if (l - 4 * v > h || l >= 2 * h || l % h >= 2 * v)
cout << 2 << endl;
else if (l >= 2 * h)
cout << 2 << endl;
else
cout << 3 << endl;
return 0;
}
if (l / v == 3) {
if (l % v >= h || l >= 2 * h + v || l >= 2 * v + h)
cout << 2 << endl;
else if (l >= h + v)
cout << 3 << endl;
else if (l % h < v)
cout << 4 << endl;
else
cout << 3 << endl;
return 0;
}
if (l / v == 2) {
if (l % v > h)
cout << 2 << endl;
else if (l >= 2 * h || l >= v + h)
cout << 3 << endl;
else
cout << 4 << endl;
return 0;
}
if (l % v >= h)
cout << 4 << endl;
else if (l >= 2 * h)
cout << 5 << endl;
else
cout << 6 << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, k;
vector<pair<int, int> > edges[400000];
vector<pair<int, pair<int, int> > > forsort;
vector<int> specials;
int par[400000], cntspecial[400000], nodespecial[400000];
int idspecial[400000];
int ans[400000];
int fnd(int pos) {
if (par[pos] == pos) return pos;
return par[pos] = fnd(par[pos]);
}
int sbtree[400000], anss;
void dfs(int pos, int par) {
for (auto to : edges[pos]) {
if (to.first == par) continue;
dfs(to.first, pos);
if (sbtree[to.first] > 0 && sbtree[to.first] < k)
anss = max(anss, to.second);
sbtree[pos] += sbtree[to.first];
}
sbtree[pos] += cntspecial[pos];
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) par[i] = i, nodespecial[i] = -1;
for (int i = 1; i <= k; i++) {
scanf("%d", &x);
specials.push_back(x);
cntspecial[x] = 1;
idspecial[x] = i;
nodespecial[x] = x;
}
for (int i = 1; i <= m; i++) {
scanf("%d %d %d", &x, &y, &z);
forsort.push_back({z, {x, y}});
}
sort(forsort.begin(), forsort.end());
for (auto e : forsort) {
int xx = e.second.first;
int yy = e.second.second;
int x = xx, y = yy;
int cc = e.first;
xx = fnd(xx);
yy = fnd(yy);
if (xx == yy) continue;
par[yy] = xx;
edges[x].push_back({y, cc});
edges[y].push_back({x, cc});
}
dfs(1, 0);
for (int i = 1; i <= k; i++) {
printf("%d ", anss);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& a) {
out << "[";
for (int i = 0; i < int((a).size()); ++i) {
if (i != 0) {
out << ", ";
}
out << a[i];
}
out << "]";
return out;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> const& p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
const int N = 1e6;
struct Data {
int n;
int a[N];
bool read() {
if (scanf("%d", &n) != 1) {
return false;
}
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
return true;
}
long long ans;
void write() { printf("%d\n", (int)ans); }
virtual void solve() {}
virtual void clear() { *this = Data(); }
void gen() {
n = 1e6;
for (int i = 0; i < n; ++i) {
a[i] = i;
}
}
};
const int mod = 1e9 + 7;
int sum(int x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
return x;
}
const int L = 20;
const int C = 1 << L;
int cnt[C + 1], cnt2[C + 1];
int pw2[N + 1];
struct Solution : Data {
void solve() {
int mx = 0;
for (int i = 0; i < n; ++i) {
++cnt[a[i]];
}
for (int i = 0; i < L; ++i) {
for (int j = 0; j < C; ++j) {
if (j & (1 << i)) {
cnt[j ^ (1 << i)] += cnt[j];
}
}
}
pw2[0] = 1;
for (int i = 1; i <= n; ++i) {
pw2[i] = sum(pw2[i - 1], pw2[i - 1]);
}
ans = 0;
for (int i = 1; i < C; ++i) {
long long e = -1;
for (int j = 0; j < L; ++j) {
if (i & (1 << j)) {
e *= -1;
}
}
ans += e * (pw2[cnt[i]] - 1);
ans %= mod;
ans += mod;
ans %= mod;
};
ans = (pw2[n] - 1 - ans) % mod;
ans += mod;
ans %= mod;
}
Solution() {}
Solution(Data d) : Data(d) {}
void clear() {
*this = Solution();
memset(cnt, 0, sizeof(cnt));
memset(cnt2, 0, sizeof(cnt2));
}
};
Solution sol;
void test() {
const int C = 1e6;
long long cnt = 0;
for (int i = 0; i <= C; ++i) {
for (int j = i; j > 0; j = (j - 1) & i) {
++cnt;
}
};
}
int main() {
sol.read();
sol.solve();
sol.write();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool isprime(long long a) {
for (long long i = 2; i <= sqrt(a); i++) {
if (a % i == 0) return false;
}
return true;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / (gcd(a, b)); }
long long fact(long long n) {
long long s;
for (long long i = 1, s = 1; i <= n; i++) {
s *= i;
}
return s;
}
long long comb(long long a, long long b) {
return (fact(a) / (fact(a - b) * fact(b)));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
n % 2 == 0 ? m % 2 == 0 ? m <= (long long)sqrt(n) ? cout << "YES" << endl
: cout << "NO" << endl
: cout << "NO" << endl
: m % 2 != 0
? m <= (long long)sqrt(n) ? cout << "YES" << endl : cout << "NO" << endl
: cout << "NO" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 50;
const long long inf = 1e14;
namespace mcmf {
int src, des, ec = 1, tl;
int cur[N], g[N], nt[N], vt[N], c[N], walk[N], exi[N], q[N];
long long w[N], dis[N], ans;
inline void add(int x, int y, int cc, long long ww) {
nt[++ec] = g[x];
g[x] = ec;
vt[ec] = y;
c[ec] = cc;
w[ec] = ww;
nt[++ec] = g[y];
g[y] = ec;
vt[ec] = x;
c[ec] = 0;
w[ec] = -ww;
}
inline bool spfa() {
for (int i = 1; i <= des; i++) cur[i] = g[i], dis[i] = inf * 100, walk[i] = 0;
q[tl = 1] = src;
dis[src] = 0;
for (int i = 1; i <= tl; i++) {
int u = q[i];
exi[u] = 0;
for (int e = g[u]; e; e = nt[e])
if (c[e] && dis[vt[e]] > dis[u] + w[e]) {
dis[vt[e]] = dis[u] + w[e];
if (!exi[vt[e]]) exi[vt[e]] = 1, q[++tl] = vt[e];
}
}
return dis[des] != inf * 100;
}
inline int dinic(int x, int f, long long cost) {
if (x == des) {
ans += f * cost;
return f;
}
int rs = 0;
walk[x] = 1;
for (int &e = cur[x]; e; e = nt[e]) {
if (c[e] && (dis[vt[e]] == dis[x] + w[e]) && !walk[vt[e]]) {
int o = dinic(vt[e], min(f - rs, c[e]), cost + w[e]);
c[e] -= o;
c[e ^ 1] += o;
rs += o;
if (rs == f) return rs;
}
}
return dis[x] = inf * 100, rs;
}
int lim;
inline long long mcmf() {
while (lim && spfa() && dis[des] < 0) lim -= dinic(src, lim, 0);
return ans;
}
} // namespace mcmf
using mcmf::add;
int n, a[N], c[N];
int main() {
cin >> n >> mcmf::lim;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> c[i];
mcmf::src = 2 * n + 1;
mcmf::des = 2 * n + 2;
for (int i = 1; i <= n; i++)
add(mcmf::src, i, 1, c[a[i]]), add(i + n, mcmf::des, 1, 0),
add(i, i + n, 1, -inf);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (a[i] == a[j])
add(i + n, j, 1, 0);
else
add(i + n, j, 1, c[a[j]]);
cout << mcmf::mcmf() + n * inf << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int k = 1; k <= n; k++) {
if (k * (k + 1) / 2 <= n && n < (k + 1) * (k + 2) / 2) {
if (n - k * (k + 1) / 2 > k) {
cout << (k + 1) << endl;
for (int j = 1; j <= k; j++) {
cout << j << " ";
}
cout << (n - k * (k + 1) / 2) << endl;
} else {
cout << k << endl;
for (int j = 1; j <= k - 1; j++) {
cout << j << " ";
}
cout << (n - k * (k - 1) / 2) << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string FILENAME = "filename";
const long long mod = 998244853;
long long dp[4001][4001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
long long res = 0, l = 0;
dp[0][0] = 1;
for (int i = 1; i <= 4000; i++) {
dp[i][0] = 1;
for (int j = 1; j <= i; j++)
dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % mod;
}
for (long long i = n; i > 0 and i >= n - m; i--) {
long long t = (dp[n + m][n - i] - l + mod) % mod;
res = (res + t * i) % mod;
l += t;
l %= mod;
}
cout << res << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int N;
long long R;
int L[200005], T[200005];
int LL[200005], TT[200005];
int main() {
scanf("%d%I64d", &N, &R);
for (int i = 1; i <= N; i++) scanf("%d", &L[i]), LL[i] = L[i];
for (int i = 1; i <= N; i++) scanf("%d", &T[i]), TT[i] = T[i];
bool flag = 0;
long long resttime = 0, now = 0, ans = 0;
for (int i = 1; i <= N; i++) {
if (resttime > 0) {
long long tc = min(1ll * L[i], resttime);
L[i] -= tc;
T[i] -= tc;
resttime -= tc;
}
if (L[i] > T[i]) {
flag = 1;
break;
}
if (2 * L[i] > T[i]) {
long long ned = L[i] * 2 - T[i];
ans += ned / R + (ned % R > 0);
resttime = (R - ned % R);
if (resttime == R) resttime = 0;
}
}
if (flag) ans = -1;
printf("%I64d\n", ans);
if (ans <= 100000 && ans != -1) {
resttime = 0;
for (int i = 1; i <= N; i++) {
if (resttime > 0) {
long long tc = min(1ll * LL[i], resttime);
LL[i] -= tc;
TT[i] -= tc;
resttime -= tc;
now += tc;
}
if (2 * LL[i] > TT[i]) {
long long ned = LL[i] * 2 - TT[i];
resttime = R - ned % R;
if (resttime == R) resttime = 0;
long long cntnow = now + 2ll * (LL[i] - ned);
for (int i = 1; i <= ned / R + (ned % R > 0); i++) {
printf("%I64d ", cntnow);
cntnow += R;
}
now += TT[i];
} else {
now += 2 * LL[i];
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void desperate_optimization(int precision) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(precision);
}
int arr[2005][2005];
int vis[2005];
queue<int> q;
int num[2005], lo[2005];
int instk[2005];
stack<int> stk;
int numscc, scc;
int n;
void dfs(int idx) {
num[idx] = lo[idx] = ++numscc;
stk.push(idx);
instk[idx] = 1;
for (int i = 0; i < n; i++) {
if (arr[idx][i] == 0) continue;
if (num[i] == 0) {
dfs(i);
lo[idx] = min(lo[idx], lo[i]);
} else if (instk[i] == 1) {
lo[idx] = min(lo[idx], num[i]);
}
}
if (lo[idx] == num[idx]) {
scc++;
while (1) {
int x = stk.top();
stk.pop();
instk[x] = 0;
if (x == idx) break;
}
}
}
void hora(int tc) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> arr[i][j];
}
dfs(0);
cout << ((scc == 1 && numscc == n) ? "YES" : "NO") << '\n';
}
int main() {
desperate_optimization(10);
int ntc = 1;
for (int tc = 1; tc <= ntc; tc++) hora(tc);
return 0;
}
| 5 |
//直径がK以下の木とは?、直径の偶奇で場合分け
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, k;
int a[2000], b[2000];
vector<int> et[2000];
int dfs(int p, int v, int dist) {
if (dist == 0) return 1;
int sz = 0;
for (int i = 0; i < et[v].size(); i++) {
if (et[v][i] == p) continue;
sz += dfs(v, et[v][i], dist - 1);
}
sz++;
return sz;
}
int main() {
int i;
cin >> n >> k;
for (i = 0; i < n - 1; i++) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
et[a[i]].push_back(b[i]);
et[b[i]].push_back(a[i]);
}
int tree_size = 0;
if (k % 2) {
for (i = 0; i < n - 1; i++) {
tree_size = max(tree_size, dfs(b[i], a[i], k / 2) + dfs(a[i], b[i], k / 2));
}
}
else {
for (i = 0; i < n; i++) {
tree_size = max(tree_size, dfs(-1, i, k / 2));
}
}
cout << n - tree_size << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long l, r;
int a[100001];
void init() {
cin >> l >> r;
int b = 1;
int tot = r;
a[1] = 1;
for (int i = 2; i <= r + 1; i++) {
if (b == 1) {
a[i] = a[i - 1] + tot;
b = 0;
tot--;
} else {
a[i] = a[i - 1] - tot;
b = 1;
tot--;
}
}
for (int i = r + 2; i <= l; i++) a[i] = i;
for (int i = 1; i <= l; i++) cout << a[i] << " ";
cout << endl;
}
int main() {
init();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
scanf("%d", &N);
double x[10005], y[10005], z[10005];
for (int i = 0; i <= N; i++) cin >> x[i] >> y[i] >> z[i];
double vp, vs, px, py, pz;
cin >> vp >> vs >> px >> py >> pz;
double distance1 = 0, time = 0;
int flag = 0;
if (px == x[0] && py == y[0] && pz == z[0]) {
printf("YES\n");
printf("0\n");
printf("%lf %lf %lf\n", px, py, pz);
} else if (vs == 0 && vp != 0) {
printf("YES\n");
double xx = x[0] - px, yy = y[0] - py, zz = z[0] - pz,
dd = sqrt(xx * xx + yy * yy + zz * zz);
printf("%lf\n", dd / vp);
printf("%lf %lf %lf\n", x[0], y[0], z[0]);
} else if (vp == 0)
printf("NO\n");
else {
for (int i = 1; i <= N; i++) {
double xxd = x[i] - x[i - 1], yyd = y[i] - y[i - 1],
zzd = z[i] - z[i - 1];
distance1 += sqrt(xxd * xxd + yyd * yyd + zzd * zzd);
double xd = x[i] - px, yd = y[i] - py, zd = z[i] - pz,
distance2 = sqrt(xd * xd + yd * yd + zd * zd);
if (distance2 * vs <= distance1 * vp) {
double vx = x[i] - x[i - 1], vy = y[i] - y[i - 1], vz = z[i] - z[i - 1],
d = sqrt(vx * vx + vy * vy + vz * vz);
vx = (vs * vx / d), vy = (vs * vy / d), vz = (vs * vz / d);
double dx = x[i - 1] - px, dy = y[i - 1] - py, dz = z[i - 1] - pz;
double a = vs * vs - vp * vp,
b = 2 * (dx * vx + dy * vy + dz * vz) - 2 * vp * vp * time,
c = dx * dx + dy * dy + dz * dz - vp * vp * time * time;
double ans;
if (a != 0) {
double ans1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a),
ans2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
ans = (ans2 < 0) ? ans1 : ans2;
} else if (b != 0) {
ans = -c / b;
} else
break;
flag = 1;
printf("YES\n");
printf("%lf\n", time + ans);
printf("%lf %lf %lf\n", x[i - 1] + vx * ans, y[i - 1] + vy * ans,
z[i - 1] + vz * ans);
break;
}
time = distance1 / vs;
}
if (flag == 0) printf("NO\n");
}
return 0;
}
| 3 |
#include<iostream>
using namespace std;
int main(){
int n,a,b,sum=0,temp,s;
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
temp=i;
s=0;
while(temp>0){
s=s+(temp%10);
temp=temp/10;
}
if(a<=s&&s<=b){
sum+=i;
}
}
cout<<sum;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int X = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
return X * w;
}
const int mod = 1e9 + 7;
int qpow(int a, int b) {
int c = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) c = 1ll * c * a % mod;
return c;
}
int main() {
int n = read(), m = read();
printf("%d\n", 1ll * (n + 1 - m) * qpow(n + 1, mod - 2) % mod *
qpow(2 * (n + 1), m) % mod);
return 0;
}
| 4 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<ll, ll>
#define f first
#define s second
const int mxn = 200001;
int n, q;
ll a[mxn], b[mxn], r[mxn];
int f(int x){
return r[x] - x + 1;
}
auto cmp = [](pi x, pi y){
ll v = x.f * f(y.s) - y.f * f(x.s);
return v ? v > 0 : x.s < y.s;
};
set<pi, decltype(cmp)> s(cmp);
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
r[i] = i;
if(i < n) s.insert({a[i], i});
a[i] += a[i - 1];
}
for(int i = 0; i < q; i++) cin >> b[i];
for(int i = q - 1, j = 0; ~i; i--){
while(!s.empty() && b[i] * f(s.begin()->s) < s.begin()->f){
int x = s.begin()->s, y = r[x] + 1;
s.erase(s.begin());
r[x] = r[y];
if(r[y] != n){
s.erase({a[r[y]] - a[y - 1], y});
s.insert({a[r[x]] - a[x - 1], x});
}
j = max(j, f(x) - 1);
}
b[i] = j;
}
cout << b[0];
for(int i = 1; i < q; i++) cout << " " << b[i];
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int f[10005];
int asdasdasdsad = 0;
int countx = 0;
int aaa[100000];
int main() {
int n, k;
memset(aaa, 0, sizeof(aaa));
scanf("%d%d", &n, &k);
int coun = 0;
int nowd = 0;
for (int i = 1; i <= k + 1; i++) {
coun = i;
nowd = 0;
int minx = 0;
while (1) {
if (coun > n) {
minx = 1;
break;
}
int p = coun + k;
if (p > n) {
f[nowd++] = coun;
break;
} else
f[nowd++] = coun;
coun = p + k + 1;
}
if (minx == 0) break;
}
printf("%d\n", nowd);
for (int i = 0; i < nowd; i++) {
printf("%d ", f[i]);
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 601;
int pre[N], f[N], a[N], b[N], n, m, ans;
void Print(int x) {
if (x == 0) return;
Print(pre[x]);
printf("%d ", b[x]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
for (int i = 1; i <= n; i++) {
int last = 0;
for (int j = 1; j <= m; j++) {
if (a[i] == b[j])
f[j] = f[last] + 1, pre[j] = last;
else if (a[i] > b[j] && f[j] > f[last])
last = j;
}
}
for (int i = 1; i <= m; i++)
if (f[i] > f[ans]) ans = i;
printf("%d\n", f[ans]);
Print(ans);
}
| 4 |
#include <bits/stdc++.h>
int stack[26][200];
char xml[200 * 5 + 10];
int main() {
int i, j, l;
int space, idx;
while (scanf("%s", xml) != EOF) {
for (i = 0; i < 26; i++) {
stack[i][0] = 0;
}
space = 0;
for (i = 1; xml[i]; i += 3) {
if (xml[i] == '/') {
++i;
idx = xml[i] - 'a';
space = l = stack[idx][stack[idx][0]--];
for (j = 1; j <= l; j++) {
putchar(' ');
}
printf("</%c>\n", xml[i]);
} else {
for (j = 1; j <= space; j++) {
putchar(' ');
}
printf("<%c>\n", xml[i]);
idx = xml[i] - 'a';
stack[idx][++stack[idx][0]] = space;
space += 2;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int ans[100005];
long long now, x, y;
long long ex_gcd(long long a, long long b) {
if (b == 0) {
x = 1, y = 0;
return a;
}
long long d = ex_gcd(b, a % b);
long long temp = x;
x = y;
y = temp - a / b * y;
return d;
}
int main() {
long long n;
scanf("%I64d", &n);
if (n == 4) {
puts("YES\n1\n3\n2\n4\n");
return 0;
}
ans[1] = 1;
long long now = 1;
for (long long i = 2; i < n; ++i) {
long long d = ex_gcd(now, n);
if (i % d != 0) {
puts("NO");
return 0;
}
long long A = now, B = n;
ex_gcd(A, B);
while (x < 0) x += n;
x = x * i / d % n;
if (x == 0) x = n;
ans[i] = x;
now = now * x % n;
}
puts("YES");
ans[n] = n;
for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, tmp = 0, ans = 0;
string s;
cin >> s;
n = s.length();
for (char i = '0'; i <= '9'; ++i) {
for (char j = '0'; j <= '9'; ++j) {
for (int x = 0; x < n; ++x) {
if (tmp & 1) {
if (s[x] == i) ++tmp;
} else if (!(tmp & 1)) {
if (s[x] == j) ++tmp;
}
}
if (i != j) {
if (tmp & 1) --tmp;
}
ans = max(ans, tmp);
tmp = 0;
}
}
cout << n - ans << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, c0, d0;
cin >> n >> m >> c0 >> d0;
int a[15];
int b[15];
int c[15];
int d[15];
for (int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
int dp[15][1005][105];
for (int i = 0; i < 1005; i++) {
for (int j = 0; j < 105; j++) {
dp[0][i][j] = 0;
}
}
a[0] = 0;
b[0] = 0;
c[0] = 0;
d[0] = 0;
for (int i = 1; i <= m; i++) {
for (int dough = 0; dough <= n; dough++) {
for (int stuffing = 0; stuffing <= a[i]; stuffing++) {
dp[i][dough][stuffing] =
(((dough - c[i] >= 0 && stuffing - b[i] >= 0)
? (dp[i][dough - c[i]][stuffing - b[i]] + d[i])
: -9001) > (dp[i - 1][dough][a[i - 1]])
? ((dough - c[i] >= 0 && stuffing - b[i] >= 0)
? (dp[i][dough - c[i]][stuffing - b[i]] + d[i])
: -9001)
: (dp[i - 1][dough][a[i - 1]]));
}
}
}
for (int dough = 0; dough <= n; dough++) {
dp[m + 1][dough][0] =
(((dough - c0 >= 0) ? (dp[m + 1][dough - c0][0] + d0) : -9001) >
(dp[m][dough][a[m]])
? ((dough - c0 >= 0) ? (dp[m + 1][dough - c0][0] + d0) : -9001)
: (dp[m][dough][a[m]]));
}
cout << dp[m + 1][n][0] << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, x, lx, ls, rs, V0, V1, V2, pre0, pre1, pre2, V, la1, la2, la, mx1, mx2;
long long an, VV1, VV2;
char s[99];
struct P {
int x, z;
} p[333333];
bool cmp(P a, P b) { return a.x < b.x; }
int main() {
scanf("%d", &n);
ls = 1e9;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
scanf("%s", s);
if (s[0] == 'G') lx = 0, V0++;
if (s[0] == 'B') lx = 1, V1++;
if (s[0] == 'R') lx = 2, V2++;
p[i] = P{x, lx};
}
if (n == 1) return puts("0"), 0;
sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; i++) {
if (!p[i].z) ls = min(ls, i), rs = max(rs, i);
}
if (!V0) {
for (int i = 1; i <= n; i++) {
if (p[i].z == 1) {
if (pre1) an += p[i].x - pre1;
pre1 = p[i].x;
} else if (p[i].z == 2) {
if (pre2) an += p[i].x - pre2;
pre2 = p[i].x;
} else
for (;;)
;
}
return printf("%I64d\n", an), 0;
}
la = ls;
for (int i = ls + 1; i <= rs; i++) {
if (!p[i].z) {
la1 = la2 = p[la].x;
mx1 = mx2 = 0;
for (int j = la + 1; j < i; j++) {
if (p[j].z == 1) {
mx1 = max(mx1, p[j].x - la1);
la1 = p[j].x;
} else if (p[j].z == 2) {
mx2 = max(mx2, p[j].x - la2);
la2 = p[j].x;
}
}
mx1 = max(mx1, p[i].x - la1);
mx2 = max(mx2, p[i].x - la2);
VV1 = 3ll * (p[i].x - p[la].x) - mx1 - mx2;
VV2 = 2ll * (p[i].x - p[la].x);
an += min(VV1, VV2);
la = i;
}
}
la1 = la2 = p[ls].x;
for (int i = 1; i < ls; i++) {
if (p[i].z == 1) la1 = min(la1, p[i].x);
if (p[i].z == 2) la2 = min(la2, p[i].x);
}
an += p[ls].x - la1;
an += p[ls].x - la2;
la1 = la2 = p[rs].x;
for (int i = rs + 1; i <= n; i++) {
if (p[i].z == 1) la1 = max(la1, p[i].x);
if (p[i].z == 2) la2 = max(la2, p[i].x);
}
an += la1 - p[rs].x;
an += la2 - p[rs].x;
printf("%I64d\n", an);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n, x, a, b;
cin >> n >> x >> a >> b;
a--;
b--;
if (a < b) swap(a, b);
long long mx = abs(a - b) + min(n - 1 - a + b, x);
cout << mx << "\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Vc = vector<char>;
using Vs = vector<string>;
using Vll = vector<ll>;
using Vi = vector<int>;
using Sll = set<ll>;
using Ss = set<string>;
using Sc = set<char>;
using Si = set<int>;
using MiVi = map<int, Vi>;
using MllVll = map<ll, Vll>;
using Mllll = map<ll, ll>;
using Mii = map<int, int>;
using MllSll = map<ll, Sll>;
using Mlls = map<ll, string>;
template <class T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
ll bigmod(ll a, ll b, ll c) {
if (b == 0) return 1;
if (b % 2 == 0) {
ll x = bigmod(a, b / 2, c);
return (x * x) % c;
} else
return (a % c * bigmod(a, b - 1, c)) % c;
}
void vsInput() {}
template <class T1, class T2>
T2 sum(T1 a, T2 ans) {
ans = 0;
for (auto i : a) ans += i;
return ans;
}
template <class T>
void print(T a) {
for (auto i : a) cout << i << " ";
cout << endl;
}
template <class T1, class T2>
void printG(map<T1, T2> a) {
for (auto i : a) {
cout << i.first << " -> ";
print(i.second);
}
}
template <class T1, class T2>
void printM(map<T1, T2> a) {
for (auto i : a) {
cout << i.first << " -> ";
cout << i.second;
cout << endl;
}
}
Vll Vinput(ll n) {
Vll a;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
a.push_back(x);
}
return a;
}
bool cmp(ll a, ll b) { return a < b; }
MllVll adj;
ll trans[200][200];
void dfs(ll s, ll d) {
trans[s][d] = true;
for (auto i : adj[d]) {
if (not trans[s][i]) dfs(s, i);
}
}
void code() {
ll n;
cin >> n;
Vll need = Vinput(n);
Vll fav = Vinput(n);
for (int i = 1; i < n + 1; i++) {
ll d = fav[i - 1];
if (i - d >= 1) {
adj[i].push_back(i - d);
adj[i - d].push_back(i);
}
if (i + d <= n) {
adj[i].push_back(i + d);
adj[i + d].push_back(i);
}
}
for (int i = 1; i < n + 1; i++) {
dfs(i, i);
}
for (int i = 1; i < n + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (trans[i][j]) {
}
}
}
bool ok = true;
for (int i = 1; i < n + 1; i++) {
if (need[i - 1] != i and not trans[i][need[i - 1]]) ok = false;
}
cout << (ok ? "YES" : "NO");
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
ll t = 1;
vsInput();
while (t) {
t--;
code();
}
return 0;
}
| 2 |
#include <iostream>
using namespace std;
int a,b,c;
int main() {
cin>>a>>b>>c;
cout<<(a<b&&b<c ? "Yes":"No")<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
;
{
int n, k, q;
cin >> n >> k >> q;
long long t[n + 1];
for (int i = 1; i < n + 1; i++) cin >> t[i];
vector<pair<long long, long long>> v;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
set<long long> s;
set<long long>::iterator it;
for (int i = 0; i < q; i++) {
if (v[i].first == 1) {
if (s.size() < k)
s.insert(v[i].second);
else {
int mn = 1000000007, mn_idx;
for (it = s.begin(); it != s.end(); it++) {
if (t[*it] < mn) {
mn = t[*it];
mn_idx = *it;
}
}
if (mn < t[v[i].second]) {
s.erase(mn_idx);
s.insert(v[i].second);
}
}
} else if (v[i].first == 2) {
if (s.find(v[i].second) != s.end())
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
}
}
}
| 1 |
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long s64;
const int MaxNF = 1344;
const int M = 1000000007;
const int I2 = M + 1 >> 1;
inline int modpow(int a, const int &n)
{
int res = 1;
for (int i = n; i; i >>= 1)
{
if (i & 1)
res = (s64)res * a % M;
a = (s64)a * a % M;
}
return res;
}
int n, nK;
int nF = 0, fac[MaxNF];
int f[MaxNF];
int main()
{
cin >> n >> nK;
int l = floor(sqrt(n));
for (int i = 1; i <= l; ++i)
if (n % i == 0)
{
fac[nF++] = i;
if (i != n / i)
fac[nF++] = n / i;
}
sort(fac, fac + nF);
int res = 0;
for (int i = 0; i < nF; ++i)
{
f[i] = modpow(nK, fac[i] + 1 >> 1);
for (int k = 0; k < i; ++k)
if (fac[i] % fac[k] == 0)
f[i] = (f[i] + M - f[k]) % M;
int ways = (s64)f[i] * fac[i] % M;
if (~fac[i] & 1)
ways = (s64)ways * I2 % M;
res = (res + ways) % M;
}
cout << res << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) rep(i,0,n)
int main()
{
double a,b,c;
cin>>a>>b>>c;
cout<<fixed<<setprecision(6)<<a*b*sin(c/180*M_PI)/2<<endl<<a+b+sqrt(a*a+b*b-2*a*b*cos(c/180*M_PI))<<endl<<b*sin(c/180*M_PI)<<endl;
}
| 0 |
#include<iostream>
#include<cstdio>
using namespace std;
#define rep(i, n)for((i) = 0;(i) < (n);++(i))
string res[6] = {"1:", "2:", "3:", "4:", "5:", "6:"};
int main(void){
int i, j, n;
double h;
scanf("%d", &n);
for(;n--;){
scanf("%lf", &h);
rep(i, 5)if(h < 165.0 + (double)i * 5.0){
res[i] += "*";
break;
}
if(i == 5)res[5] += "*";
}
rep(i, 6)cout << res[i] << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> edge[100005];
int num[100005], nxt = 0;
int up[100005][21], dep[100005];
int rev[100005];
void dfs(int v, int u) {
rev[nxt + 1] = v;
num[v] = nxt++;
up[v][0] = u;
if (u == -1)
dep[v] = 0;
else
dep[v] = dep[u] + 1;
for (int i = 0; i < edge[v].size(); i++) {
int to = edge[v][i];
dfs(to, v);
}
}
struct RMQ {
pair<int, int> seg[(1 << 18)];
void update(int k, int a) {
int kk = k;
k += (1 << 18) / 2 - 1;
seg[k] = make_pair(a, kk);
while (k > 0) {
k = (k - 1) / 2;
if (seg[k * 2 + 1].first > seg[k * 2 + 2].first)
seg[k] = seg[k * 2 + 1];
else
seg[k] = seg[k * 2 + 2];
}
}
pair<int, int> query(int a, int b, int k, int l, int r) {
if (r < a || b < l || a > b) return make_pair(0, -1);
if (a <= l && r <= b)
return seg[k];
else {
pair<int, int> vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
pair<int, int> vr = query(a, b, k * 2 + 2, (l + r) / 2 + 1, r);
if (vl.first > vr.first)
return vl;
else
return vr;
}
}
} kaede[2];
int get_lca(int s, int t) {
if (dep[s] > dep[t]) swap(s, t);
for (int i = 0; i < 19; i++) {
if ((((dep[t] - dep[s]) >> i) & 1)) {
t = up[t][i];
}
}
if (s == t) return s;
for (int i = 19; i >= 0; i--) {
if (up[s][i] != up[t][i]) {
s = up[s][i];
t = up[t][i];
}
}
return up[s][0];
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 2; i <= n; i++) {
int a;
scanf("%d", &a);
edge[a].push_back(i);
}
dfs(1, -1);
for (int i = 1; i <= n; i++) {
kaede[0].update(i, num[i] + 1);
kaede[1].update(i, 1000000000 - num[i] - 1);
}
for (int j = 0; j < 20; j++) {
for (int i = 1; i <= n; i++) {
if (up[i][j] == -1)
up[i][j + 1] = -1;
else
up[i][j + 1] = up[up[i][j]][j];
}
}
for (int i = 0; i < q; i++) {
int a, b;
scanf("%d%d", &a, &b);
pair<int, int> p = kaede[0].query(a, b, 0, 0, (1 << 17) - 1);
pair<int, int> q = kaede[1].query(a, b, 0, 0, (1 << 17) - 1);
int mn = 1000000000 - q.first;
int mx = p.first;
pair<int, int> ep = kaede[0].query(a, p.second - 1, 0, 0, (1 << 17) - 1);
pair<int, int> eq = kaede[0].query(p.second + 1, b, 0, 0, (1 << 17) - 1);
int mxx = max(ep.first, eq.first);
ep = kaede[1].query(a, q.second - 1, 0, 0, (1 << 17) - 1);
eq = kaede[1].query(q.second + 1, b, 0, 0, (1 << 17) - 1);
int mnn = 1000000000 - max(ep.first, eq.first);
int e = get_lca(rev[mnn], rev[mx]);
int f = get_lca(rev[mn], rev[mxx]);
if (dep[e] > dep[f]) {
printf("%d %d\n", q.second, dep[e]);
} else {
printf("%d %d\n", p.second, dep[f]);
}
}
}
| 5 |
#include<bits/stdc++.h>
#define FULL(x,y) memset(x,y,sizeof(x))
#define ll long long
#define SZ(x) (int)x.size()
#define pb push_back
using namespace std;
int t,n;
string s;
char str[2]={'(',')'};
int check(char a, char b, char c) {
string ss="";
for(int i=0;i<n;i++) {
if (s[i]=='A') ss+=a;
else if (s[i]=='B') ss+=b;
else ss+=c;
}
int cnt0=0,cnt1=0;
for(int i=0;i<n;i++) {
if (ss[i]=='(') cnt0++;
else cnt1++;
if (cnt1>cnt0) return 0;
}
if (cnt0!=cnt1) return 0;
return 1;
}
int main() {
cin>>t;
while(t--) {
cin>>s;
n=s.size();
int f1=0,f2=0,f3=0;
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
for(int k=0;k<2;k++) {
if (check(str[i],str[j],str[k])) {
f1=1;
break;
}
}
if (f1) {
f2=1;
break;
}
}
if (f2) {
f3=1;
break;
}
}
if (f3) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 50;
int a[maxn], vis[maxn];
int main() {
int i, j, k, n, l, ans;
scanf("%d", &n);
j = -1;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
j = max(j, a[i]);
}
ans = 0;
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; i++) {
if (vis[i] == 1) continue;
if (a[i] == j) {
vis[i] = 1;
l = 1;
for (k = i + 1; k <= n; k++) {
if (a[k] == j) {
vis[k] = 1;
l++;
} else
break;
}
ans = max(ans, l);
}
}
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500;
const int oo = 1e9 + 5;
int du[] = {-1, 0, 0, 1};
int dv[] = {0, -1, 1, 0};
const long long mod = 1e9 + 7;
long long sqr(long long x) { return x * x; }
int n, dem, res = oo, lv[N + 5], trace[N + 5];
long long a[100005];
vector<int> g[N + 5];
void bfs(int u1) {
queue<int> q;
for (int i = 1; i <= n; i++) {
lv[i] = 0;
trace[i] = 0;
}
lv[u1] = 1;
trace[u1] = u1;
q.push(u1);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == trace[u]) continue;
if (lv[v]) {
res = min(res, lv[v] - lv[u1] + lv[u] - lv[u1] + 1);
continue;
}
lv[v] = lv[u] + 1;
trace[v] = u;
q.push(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
long long tmp;
cin >> tmp;
if (tmp) a[++dem] = tmp;
}
n = dem;
if (n > 130) {
cout << 3;
return 0;
}
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (a[i] & a[j]) {
g[i].push_back(j);
g[j].push_back(i);
}
for (int i = 1; i <= n; i++) bfs(i);
if (res >= oo)
cout << -1;
else
cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[1009];
vector<int> perm[6];
bool first[6][1009][1009];
bool visit[1009];
int len[1009];
void dfs(int u) {
if (len[u] != 0) return;
int sz = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!visit[v]) dfs(v);
sz = max(sz, 1 + len[v]);
}
len[u] = sz;
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < k; i++)
for (int j = 0; j < n; j++) {
int temp;
scanf("%d", &temp);
for (int l = 0; l < perm[i].size(); l++) {
first[i][perm[i][l]][temp] = true;
}
perm[i].push_back(temp);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
bool check = true;
for (int l = 0; l < k; l++) {
if (!first[l][i][j]) {
check = false;
}
}
if (check) g[i].push_back(j);
}
}
int ans = -1;
for (int i = 1; i <= n; i++) {
if (len[i] == 0) dfs(i);
ans = max(ans, len[i]);
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int p) {
if (p == 1) return false;
bool ans = true;
for (int i = 2; i * i <= p; i++) {
if (p % i == 0) ans = false;
}
return ans;
}
int power(int a, int b) {
if (b == 0) return 1;
if (b % 2 == 0) {
int temp = power(a, b / 2);
return temp * temp;
}
return a * power(a, b - 1);
}
vector<int> ans;
int main() {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
int j = 1;
while (power(i, j) <= n) {
ans.push_back(power(i, j));
j++;
}
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[200001], p[200001], n, m, l, r, x;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
p[i] = (a[i - 1] == a[i] ? p[i - 1] : i - 1);
}
while (m--) {
scanf("%d%d%d", &l, &r, &x);
printf("%d\n", a[r] != x ? r : p[r] < l ? -1 : p[r]);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 5, OFF = 100000000;
int n, a[N], res[N];
bool freq[2 * OFF + 5];
void fix(int &x) {
for (int i = 2, t; i * i <= abs(x); ++i) {
t = i * i;
while (x % t == 0) x /= t;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
fix(a[i]);
}
int cnt;
for (int i = 0; i < n; ++i) {
cnt = 0;
for (int j = i; j < n; ++j) {
if (freq[a[j] + OFF] == false && a[j] != 0) ++cnt;
freq[a[j] + OFF] = true;
++res[max(cnt, 1)];
}
for (int j = i; j < n; ++j) freq[a[j] + OFF] = false;
}
for (int i = 1; i <= n; ++i) printf("%d ", res[i]);
puts("");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const long long MOD = 1000000007;
long long N, T, ts[MAXN], pref[MAXN];
long long fac[MAXN], inv[MAXN];
long long C(long long n, long long k) {
return fac[n] * inv[k] % MOD * inv[n - k] % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
fac[0] = inv[0] = fac[1] = inv[1] = 1;
for (int i = 2; i < MAXN; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = -MOD / i * inv[MOD % i] % MOD;
}
for (int i = 2; i < MAXN; i++) {
inv[i] = inv[i - 1] * inv[i] % MOD;
}
int k = 1;
long long ans = 0, L = 2, deno = inv[2];
cin >> N >> T;
for (int i = 1; i <= N; i++) {
cin >> ts[i];
pref[i] = ts[i] + pref[i - 1];
if (pref[i] > T) {
break;
}
long long most = min((long long)i, T - pref[i]);
while (k < most) {
k++;
L = (L + C(i, k)) % MOD;
}
while (k > most) {
L = (L - C(i, k)) % MOD;
k--;
}
ans = (ans + deno * L) % MOD;
deno = deno * inv[2] % MOD;
L = (L * 2 - C(i, k)) % MOD;
}
cout << (ans + MOD) % MOD << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
const ll INF = 1e18;
ll dp[200005][4];
int main() {
int n;
cin>>n;
vector<int> a(n);
rep(i,n) cin >> a[i];
int k = 1+n%2;
rep(i,n+1)rep(j,k+1) dp[i][j] = -INF;
dp[0][0] = 0;
rep(i,n) {
rep(j,k+1) {
chmax(dp[i+1][j+1], dp[i][j]);
ll now = dp[i][j];
if ((i+j)%2 == 0) now += a[i];
chmax(dp[i+1][j], now);
}
}
ll ans = dp[n][k];
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
t = 1;
cin >> t;
for (int i1 = 0; i1 < t; ++i1) {
long long n, l, r;
cin >> n >> l >> r;
vector<long long> v;
long long c = (n - 1) * 2;
long long s = c;
vector<long long> v1;
for (int i = 0; i < n - 1; ++i) {
v.push_back(s);
v1.push_back(c / 2);
c -= 2;
s += c;
}
long long x = lower_bound(v.begin(), v.end(), l) - v.begin();
long long y = lower_bound(v.begin(), v.end(), r) - v.begin();
if (x == v.size()) {
cout << 1 << "\n";
continue;
}
long long res1 = v[x] - l + 1;
if (res1 % 2) {
res1 /= 2;
cout << n - res1 << " ";
l++;
} else
res1 /= 2;
if (x == y) {
long long dif = r - l + 1;
while (dif > 1) {
res1--;
cout << x + 1 << " " << n - res1 << " ";
dif -= 2;
}
if (dif == 1) cout << x + 1 << "\n";
continue;
}
for (int i = 0; i < res1; ++i) {
cout << x + 1 << " " << n - res1 + 1 + i << " ";
}
x++;
while (x < y) {
for (int i = 0; i < v1[x]; ++i) {
cout << x + 1 << " " << x + i + 2 << " ";
}
x++;
}
if (y == v.size()) {
cout << 1 << "\n";
continue;
}
long long res2 = r - v[y - 1];
res1 = res2;
res2 /= 2;
for (int i = 0; i < res2; ++i) {
cout << y + 1 << " " << y + 2 + i << " ";
}
if (res1 % 2) cout << y + 1;
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#define r(i,n) for(long long i = 0; i<n; i++)
typedef long long ll;
using namespace std;
int main() {
int a,b,m,A[100001]={0},B[100001]={0},ans=100000000,mina=1000000,minb=1000000;
cin >> a >> b >> m;
r(i,a){
cin >> A[i];
mina=min(mina,A[i]);
}
r(i,b){
cin>>B[i];
minb=min(minb,B[i]);
}
ans=mina+minb;
r(i,m){
int x,y,z;
cin >> x >> y >> z;
ans=min(ans,A[x-1]+B[y-1]-z);
}
cout<<ans<<endl;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.