solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int a[110];
int num[110];
memset(num, 0, sizeof(num));
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
sort(a + 1, a + m + 1);
int index = 0;
set<int> s;
for (int i = 1; i <= m; i++) {
if (s.count(a[i])) {
num[index]++;
} else {
s.insert(a[i]);
num[++index]++;
}
}
int maxn = 0;
int temp;
for (int i = 1; i <= 100; i++) {
temp = 0;
for (int j = 1; j <= index; j++) {
temp += (num[j] / i);
}
if (temp >= n)
maxn = i;
else
break;
}
cout << maxn << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 100005;
int a[maxN], n, b[maxN];
vector<int> q;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int cnt = 0;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) cnt++;
if (cnt > 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
long long n, sx, sy, dx, dy, t;
void mod(long long &x) {
x %= n;
x += n;
x %= n;
if (x == 0) x = n;
}
long long m1[100][4][4];
long long d1[2][4][4];
long long m2[100][2][2];
long long d2[2][2][2];
long long dab1[4];
long long dab2[4];
int two[100];
void calctwo() {
long long now = t, now2 = 0;
while (now > 0) {
if (now % 2 == 1)
two[now2++] = 1;
else
two[now2++] = 0;
now /= 2;
}
}
int main() {
int i, j, k, l;
scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &n, &sx, &sy, &dx, &dy, &t);
n *= 2;
calctwo();
m1[0][0][0] = 2, m1[0][0][1] = 1, m1[0][0][2] = 1, m1[0][0][3] = 0;
m1[0][1][0] = 1, m1[0][1][1] = 2, m1[0][1][2] = 0, m1[0][1][3] = 1;
m1[0][2][0] = 1, m1[0][2][1] = 1, m1[0][2][2] = 1, m1[0][2][3] = 0;
m1[0][3][0] = 1, m1[0][3][1] = 1, m1[0][3][2] = 0, m1[0][3][3] = 1;
for (i = 1; i < 100; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
m1[i][j][k] += m1[i - 1][j][l] * m1[i - 1][l][k];
mod(m1[i][j][k]);
}
}
}
}
d1[0][0][0] = d1[0][1][1] = d1[0][2][2] = d1[0][3][3] = 1;
for (i = 0; i <= 60; i++) {
if (two[i]) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) {
for (l = 0; l < 4; l++) {
d1[1][j][k] += d1[0][j][l] * m1[i][l][k];
mod(d1[1][j][k]);
}
}
}
for (j = 0; j < 4; j++) {
for (k = 0; k < 4; k++) d1[0][j][k] = d1[1][j][k], d1[1][j][k] = 0;
}
}
}
dab1[0] =
d1[0][0][0] * sx + d1[0][0][1] * sy + d1[0][0][2] * dx + d1[0][0][3] * dy;
dab1[1] =
d1[0][1][0] * sx + d1[0][1][1] * sy + d1[0][1][2] * dx + d1[0][1][3] * dy;
dab1[2] =
d1[0][2][0] * sx + d1[0][2][1] * sy + d1[0][2][2] * dx + d1[0][2][3] * dy;
dab1[3] =
d1[0][3][0] * sx + d1[0][3][1] * sy + d1[0][3][2] * dx + d1[0][3][3] * dy;
for (i = 0; i < 4; i++) mod(dab1[i]);
m2[0][0][0] = 4, m2[0][0][1] = -1;
m2[0][1][0] = 1, m2[0][1][1] = 0;
for (i = 1; i < 100; i++) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
for (l = 0; l < 2; l++) {
m2[i][j][k] += m2[i - 1][j][l] * m2[i - 1][l][k];
mod(m2[i][j][k]);
}
}
}
}
d2[0][0][0] = d2[0][1][1] = 1;
for (i = 0; i <= 60; i++) {
if (two[i]) {
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) {
for (l = 0; l < 2; l++) {
d2[1][j][k] += d2[0][j][l] * m2[i][l][k];
mod(d2[1][j][k]);
}
}
}
for (j = 0; j < 2; j++) {
for (k = 0; k < 2; k++) d2[0][j][k] = d2[1][j][k], d2[1][j][k] = 0;
}
}
}
dab2[0] = d2[0][0][0] * 1 + d2[0][0][1] * 0;
dab2[1] = d2[0][1][0] * 1 + d2[0][1][1] * 0;
mod(dab2[0]), mod(dab2[1]);
long long rdab2 = dab2[1] - t;
if (rdab2 % 2 == 0)
rdab2 /= 2;
else
rdab2 = (rdab2 + n) / 2;
mod(rdab2);
pair<long long, long long> rdab;
rdab = pair<long long, long long>(dab1[0] + rdab2, dab1[1] + rdab2);
n /= 2;
mod(rdab.first), mod(rdab.second);
printf("%I64d %I64d", rdab.first, rdab.second);
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b; cin>>a>>b;
if (b<a) swap(a,b);
for (int i=0; i<b; i++) printf("%d",a);
}
| 0 |
#include <iostream>
using namespace std;
int main(void){
int n,a,b,k=0,l=0,m=0;
cin>>n>>a>>b;
for(int i=0;i<n;i++){
int p;
cin>>p;
if(p<=a){
k++;
}else if(p<=b){
l++;
}else{
m++;
}
}
cout<<min(min(l,m),k)<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[50000 + 5];
int main() {
int N, m = 101, sum = 0, ans = 100000000;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
sum += a[i];
m = min(a[i], m);
}
ans = sum;
for (int i = 0; i < N; i++) {
for (int j = 1; j * j <= a[i]; j++)
if (a[i] % j == 0)
ans = min(sum - m - a[i] + j + m * a[i] / j,
min(ans, sum - m - a[i] + a[i] / j + m * j));
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int p[10000], br, sol, br1;
string s;
int main() {
cin >> s;
br = 1;
sol = (s.size() + 1) * 26 - s.size();
printf("%d", sol);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int pos, val, ans;
} arr[100005];
bool cmp(node a, node b) {
if (a.val == b.val) return a.pos < b.pos;
return a.val < b.val;
}
bool cmp2(node a, node b) { return a.pos < b.pos; }
int n;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &arr[i].val);
arr[i].pos = i;
}
sort(arr + 1, arr + n + 1, cmp);
int max1 = -1;
for (int i = 1; i <= n; ++i) {
arr[i].ans = max(max1, arr[i].pos) - arr[i].pos;
max1 = max(max1, arr[i].pos);
}
sort(arr + 1, arr + n + 1, cmp2);
for (int i = 1; i <= n; ++i) {
printf("%d ", arr[i].ans - 1);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 500;
int n, m, vis[maxn], dfn[maxn], low[maxn], have, dfs_clock;
vector<int> G[maxn];
stack<int> s;
void dfs(int x, int y) {
dfn[x] = low[x] = ++dfs_clock;
s.push(x);
for (auto it : G[x]) {
if (it == y) continue;
if (!dfn[it]) {
dfs(it, x);
low[x] = min(low[x], low[it]);
} else
low[x] = min(low[x], dfn[it]);
}
if (low[x] == dfn[x]) {
int cnt = 0;
while (1) {
int u = s.top();
s.pop();
cnt++;
if (u == x) break;
}
if (cnt > 1) have = 1;
}
}
int main(int argc, char *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
int ans = 0;
for (int i = 1; i <= n; ++i)
if (!dfn[i]) {
have = 0;
dfs(i, -1);
if (!have) ans++;
}
cout << ans << endl;
return 0;
}
| 5 |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define INF (1e9+1)
//#define INF (1LL<<59)
//verified AOJ GRL_1
#define MAX_V 100000
struct edge{int to,cost;};
vector<edge> G[MAX_V];
void dijkstra(int s, vector<int> &d){
priority_queue< pii,vector<pii>,greater<pii> > que;
rep( i,d.size() )d[i]=INF;
d[s] = 0;
que.push( pii(0,s) );
while( que.size() ){
pii p=que.top();
que.pop();
int v=p.second;
if(d[v]<p.first)continue;
rep(i,G[v].size()){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(pii(d[e.to],e.to));
}
}
}
}
string replace(string origin,string from,string to){
string ret = "";
for(int i=0;i<origin.size();){
if(i+from.size()<=origin.size()){
bool isSame = true;
for(int j=i;j<i+from.size();j++){
if(origin[j]!=from[j-i])isSame = false;
}
if(isSame){
ret += to;
i+=from.size();
}
else ret += origin[i++];
}
else ret += origin [i++];
}
return ret;
}
int main(){
assert( replace("aaxaaa","aa","bca")=="bcaxbcaa" );
int n;
while(cin>>n&&n){
vector<pair<string,string>> vp;
rep(i,MAX_V)G[i].clear();
rep(i,n){
string a,b;
cin>>a>>b;
vp.pb(make_pair(a,b));
}
string s,t;
cin>>s>>t;
map<string,int> num;
queue<string> que;
que.push(s);
int c=0;
num[s]=c++;
while(que.size()){
string q = que.front();
que.pop();
// cout<<q<<endl;
rep(i,vp.size()){
string a =vp[i].first,b=vp[i].second;
if( q.find(a)!=string::npos ){
string res = replace(q,a,b);
if(res.size()>t.size()+1 || num.count(res)!=0)continue;
que.push(res);
num[res]=c++;
G[num[q]].pb(edge{c-1,1});
}
}
}
assert(c<MAX_V);
vector<int> d(c);
dijkstra(num[s],d);
if(num.count(t)==0)cout<<-1<<endl;
else cout<<d[num[t]]<<endl;
}
}
| 0 |
#include <iostream>
using namespace std;
int main()
{
int n,t,x[200001],i,ans=0;
cin >> n >> t;
for(i=0;i<n;i++){
cin >> x[i];
}
for(i=0;i<n-1;i++){
if(x[i+1]-x[i]>=t){
ans += t;
}
else{
ans += x[i+1]-x[i];
}
}
ans += t;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005, MAXT = 1 << 20;
int mx[MAXT], tag[MAXT], srt[MAXN], nn, n, d;
void pushdown(int k) {
if (!tag[k]) return;
int &t = tag[k], ls = k << 1, rs = ls | 1;
tag[ls] += t, tag[rs] += t;
mx[ls] += t, mx[rs] += t;
t = 0;
}
void update(int a, int b, int x, int l = 1, int r = nn, int k = 1) {
if (a > r || b < l) return;
if (a <= l && b >= r) {
tag[k] += x;
mx[k] += x;
return;
}
pushdown(k);
int mid = (l + r) >> 1, ls = k << 1, rs = ls | 1;
update(a, b, x, l, mid, ls);
update(a, b, x, mid + 1, r, rs);
mx[k] = max(mx[ls], mx[rs]);
}
struct Po {
int x, y;
} po[MAXN];
void add(int p, int x) {
int k = lower_bound(srt + 1, srt + 1 + nn, srt[p] - d) - srt;
update(k, p, x);
}
int main() {
scanf("%d%d", &n, &d);
d *= 2;
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d%d", &x, &y);
po[i] = (Po){x - y, x + y};
}
for (int i = 1; i <= n; i++) srt[++nn] = po[i].x;
sort(srt + 1, srt + 1 + nn);
nn = unique(srt + 1, srt + 1 + nn) - srt - 1;
for (int i = 1; i <= n; i++)
po[i].x = lower_bound(srt + 1, srt + 1 + nn, po[i].x) - srt;
sort(po + 1, po + 1 + n, [&](const Po &a, const Po &b) { return a.y < b.y; });
int res = 0;
for (int i = 1, j = 1; i <= n; i++) {
for (; j <= n && po[j].y - po[i].y <= d; ++j) add(po[j].x, 1);
res = max(res, mx[1]);
add(po[i].x, -1);
}
printf("%d\n", res);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INF = 1e9 + 10;
int a[N], b[N], n, c;
bool use[N];
int check(long long x) {
long long second = 0;
for (int i = 0; i < n; i++) {
second += 1 + x * a[i] / b[i];
if (second > c) return 1;
}
if (second == c) return 0;
return -1;
}
int main() {
cin >> n >> c;
if (n > c) {
cout << 0;
return 0;
}
bool allNill = true;
for (int i = 0; i < n; i++) {
scanf("%d%d", &a[i], &b[i]);
allNill &= a[i] == 0;
}
if (allNill) {
if (n == c)
cout << -1;
else
cout << 0;
return 0;
}
int cnt = 0;
for (int x = 1; x <= 100000; x++) {
int second = n;
for (int i = 0; i < n; i++) second += x * a[i] / b[i];
if (second == c) {
if (cnt && !use[x - 1]) assert(0);
use[x] = true;
cnt++;
}
}
cout << cnt;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, max = 0;
cin >> n;
long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
max = 1;
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (a[i] >= max) {
a[i] = max;
max++;
}
}
cout << a[n - 1] + 1;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 52;
long double dp[nmax][nmax][nmax];
int n, p;
int a[nmax];
long double fact[52];
double solve() {
dp[0][0][0] = 1.0;
for (int i = 1; i <= n; i++) {
dp[i][0][0] = 1.0;
for (int k = 1; k <= i; k++) {
for (int j = 1; j <= p; j++) {
dp[i][k][j] = dp[i - 1][k][j];
if (j - a[i] >= 0) {
dp[i][k][j] += dp[i - 1][k - 1][j - a[i]];
}
}
}
}
fact[0] = 1.0;
for (int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
}
long double ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= p; j++) {
ans += dp[n][i][j] * fact[n - i] * i * fact[i - 1];
}
}
return ans / fact[n];
}
void readData() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> p;
sort(a + 1, a + n + 1);
}
int main() {
readData();
cout.precision(10);
cout << fixed << solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
class TaskB {
public:
void solve(std::istream& cin, std::ostream& cout) {
long long n, m, y;
long long ans = 0;
cin >> n >> m >> y >> y;
vector<long long> v1(n), v2(m);
vector<long long> used(m, 2e18);
if (m == 0) {
cout << n;
return;
}
for (long long i = 0; i < n; i++) cin >> v1[i];
for (long long i = 0; i < m; i++) cin >> v2[i];
for (long long i = 0; i < n; i++) {
long long it = lower_bound(v2.begin(), v2.end(), v1[i]) - v2.begin();
long long len1 = 1e18, len2 = 1e18;
if (it != v2.size()) len1 = abs(v1[i] - v2[it]);
if (it != 0) len2 = abs(v1[i] - v2[it - 1]);
if (len1 == len2)
if (used[it - 1] == 2e18 || used[it - 1] == len2)
used[it - 1] = len2;
else if (used[it] == 2e18 || used[it] == len1)
used[it] = len1;
else if (used[it - 1] >= len2)
used[it - 1] = len2;
else if (used[it] >= len1)
used[it] = len1;
else
ans++;
else if (len1 > len2)
if (used[it - 1] == 2e18 || used[it - 1] >= len2) {
ans += (used[it - 1] != 2e18 && used[it - 1] > len2);
used[it - 1] = len2;
} else
ans++;
else if (used[it] == 2e18 || used[it] >= len1) {
ans += (used[it] != 2e18 && len1 < used[it]);
used[it] = len1;
} else
ans++;
}
cout << ans;
}
};
int main() {
TaskB solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| 2 |
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[100010],cnt[2];
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
bool check(){
cnt[0]=cnt[1]=0;
for(int i=1;i<=n;i++)
cnt[a[i]&1]++;
if(cnt[0]&1)
return 1;
if(cnt[1]>1)
return 0;
if(a[1]==1)
return 0;
int d=(a[1]-=(a[1]&1));
for(int i=2;i<=n;i++){
if(a[i]==1)
return 0;
d=gcd(d,a[i]-=(a[i]&1));
}
for(int i=1;i<=n;i++)
a[i]/=d;
return !check();
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
puts(check()?"First":"Second");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool f[10005] = {false};
int a, b, c;
int main() {
scanf("%d%d%d", &a, &b, &c);
f[0] = true;
for (int i = 1; i <= c; i++) {
if (i >= a) f[i] |= f[i - a];
if (i >= b) f[i] |= f[i - b];
}
if (f[c])
puts("Yes");
else
puts("No");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int M = 100000 + 10;
long long int v[M];
int c[M];
long long int dp[M];
int flag[M];
long long int vc[M];
int main() {
time_t t_start, t_end;
t_start = clock();
int n, p;
cin >> n >> p;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int q = 0; q < p; q++) {
long long int a, b;
cin >> a >> b;
for (int i = 0; i < M; i++) {
dp[i] = 0;
flag[i] = 0;
vc[i] = 0;
}
long long int maxv1 = 0;
int maxc1 = 0;
long long int maxv2 = 0;
int maxc2 = 0;
for (int i = 1; i <= n; i++) {
dp[i] = v[i] * b;
if (flag[c[i]] == 1) {
long long int val1 = vc[c[i]] + v[i] * a;
dp[i] = max(dp[i], val1);
}
if (maxc1 == c[i]) {
long long int val2 = maxv2 + v[i] * b;
dp[i] = max(dp[i], val2);
} else {
long long int val2 = maxv1 + v[i] * b;
dp[i] = max(dp[i], val2);
}
if (flag[c[i]] == 0) {
flag[c[i]] = 1;
vc[c[i]] = dp[i];
} else {
vc[c[i]] = max(vc[c[i]], dp[i]);
}
if (maxc1 == c[i]) {
maxv1 = max(maxv1, vc[c[i]]);
} else {
if (maxc2 == c[i]) {
maxv2 = max(maxv2, vc[c[i]]);
if (maxv2 > maxv1) {
long long int tmp = maxv1;
maxv1 = maxv2;
maxv2 = tmp;
int idx = maxc1;
maxc1 = maxc2;
maxc2 = idx;
}
} else {
if (vc[c[i]] > maxv1) {
maxv2 = maxv1;
maxc2 = maxc1;
maxv1 = vc[c[i]];
maxc1 = c[i];
} else {
if (vc[c[i]] > maxv2) {
maxv2 = vc[c[i]];
maxc2 = c[i];
}
}
}
}
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
t_end = clock();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int c[100][100];
int coun = 0;
void rec(vector<char> a[], int i, int j, int n, int m) {
char r = 'G';
if (a[i][j] == r) coun++;
c[i][j] = 1;
if (i > 0 && c[i - 1][j] == 0 && (a[i - 1][j] != '#')) rec(a, i - 1, j, n, m);
if (i < n - 1 && c[i + 1][j] == 0 && a[i + 1][j] != '#')
rec(a, i + 1, j, n, m);
if (j > 0 && c[i][j - 1] == 0 && a[i][j - 1] != '#') rec(a, i, j - 1, n, m);
if (j < m - 1 && c[i][j + 1] == 0 && a[i][j + 1] != '#')
rec(a, i, j + 1, n, m);
return;
}
int main() {
int t;
cin >> t;
while (t--) {
char r = 'G';
int n, m, cg = 0, cb = 0, f = 0, g = 0;
cin >> n >> m;
vector<char> a[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char k;
cin >> k;
a[i].push_back(k);
if (k == 'B')
cb++;
else if (k == 'G')
cg++;
c[i][j] = 0;
}
}
if ((cb == 0 && cg == 0) || cg == 0)
cout << "Yes" << endl;
else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < a[i].size(); j++) {
if (a[i][j] == 'B') {
if (i > 0) {
if (a[i - 1][j] == r)
f = 1;
else if (a[i - 1][j] != 'B')
a[i - 1][j] = '#';
}
if (i < n - 1) {
if (a[i + 1][j] == r)
f = 1;
else if (a[i + 1][j] != 'B')
a[i + 1][j] = '#';
}
if (j > 0) {
if (a[i][j - 1] == r)
f = 1;
else if (a[i][j - 1] != 'B')
a[i][j - 1] = '#';
}
if (j < m - 1) {
if (a[i][j + 1] == r)
f = 1;
else if (a[i][j + 1] != 'B')
a[i][j + 1] = '#';
}
if (f == 1) break;
}
}
if (f == 1) break;
}
if (f == 1)
cout << "No" << endl;
else {
if (a[n - 1][m - 1] == '#')
cout << "No" << endl;
else {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) c[i][j] = 0;
}
coun = 0;
c[n - 1][m - 1] = 1;
rec(a, n - 1, m - 1, n, m);
if (coun == cg)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
}
}
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int block_size = 360;
const int maxn = 200010;
const long long mod = 1e9 + 7;
const int inf = 10000000;
const long double eps = 1e-9;
long long ans = 0;
int main() {
int n;
long long p;
scanf("%d", &n);
cin >> p;
vector<string> str;
for (long long i = 0; i < n; i++) {
string t;
cin >> t;
str.push_back(t);
}
reverse(str.begin(), str.end());
int a = 0;
for (long long i = 0; i < n; i++) {
if (str[i] == "halfplus") {
a++;
ans = ans * 2 + 1;
} else {
ans = ans * 2;
}
}
cout << (2 * ans - a) * p / 2;
return 0;
}
| 1 |
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }
#include <set>
using namespace std;
// x + y = (x ^ y) + (x & y) << 1;
// x = y, y = x ^ y
// y + (x ^ y) = (y ^ (x ^ y)) + (y & (x ^ y)) << 1
// = x + (y & ~x) << 1
// 19 127
// 108 127 127
// 19 127 127 127
int main(){
const int t = getInt();
REP(_,t) {
const int n = getInt();
const int d = getInt();
printf("%d\n", 127 * (n - 1) + (n % 2 == 0 ? 127 - d : d));
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[300001], idx[300002];
vector<pair<int, int> > ans;
int main() {
int n, qks;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
idx[arr[i]] = i;
}
qks = n / 2;
for (int i = n / 2 + 1; i <= n; i++) {
if (arr[i] == i) continue;
int ii = idx[i];
if (ii <= qks) {
ans.push_back({ii, n});
swap(arr[ii], arr[n]);
idx[arr[ii]] = ii;
idx[arr[n]] = n;
ans.push_back({1, n});
swap(arr[1], arr[n]);
idx[arr[1]] = 1;
idx[arr[n]] = n;
} else {
ans.push_back({1, ii});
swap(arr[1], arr[ii]);
idx[arr[1]] = 1;
idx[arr[ii]] = ii;
}
ans.push_back({1, i});
swap(arr[1], arr[i]);
idx[arr[1]] = 1;
idx[arr[i]] = i;
}
for (int i = 1; i <= qks; i++) {
if (arr[i] == i) continue;
int ii = idx[i];
if (ii != n) {
ans.push_back({ii, n});
swap(arr[ii], arr[n]);
idx[arr[ii]] = ii;
idx[arr[n]] = n;
}
ans.push_back({n, i});
swap(arr[n], arr[i]);
idx[arr[n]] = n;
idx[arr[i]] = i;
}
printf("%d\n", ans.size());
for (pair<int, int> v : ans) printf("%d %d\n", v.first, v.second);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int t[n + 1];
int r[n + 1];
int a[n + 1];
int x = 1;
for (int i = 0; i < n; i++) {
cin >> t[i + 1];
r[i + 1] = -1;
a[i + 1] = -1;
}
r[0] = 0;
a[0] = 0;
for (int i = 1; i <= n; i++) {
int room = r[t[i]];
if (a[room] == t[i]) {
a[room] = i;
r[i] = room;
} else {
a[x] = i;
r[i] = x;
x++;
}
}
cout << x;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void file() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const long long N = 2e5 + 7;
const long long MOD = 1e9 + 7;
long long n, m, nn, a[N * 10], d, b[N * 10];
int main() {
file();
cin >> nn;
for (int ii = 0; ii < nn; ++ii) {
cin >> n >> m >> d;
long long k = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < d; ++i) {
if (!b[a[i]]) ++k;
++b[a[i]];
}
long long mn = 1e9;
for (int i = d - 1; i < n; ++i) {
mn = min(mn, k);
b[a[i - d + 1]]--;
if (!b[a[i - d + 1]]) --k;
if (!b[a[i + 1]]) ++k;
b[a[i + 1]]++;
}
for (int i = 0; i <= n; ++i) {
b[a[i]] = 0;
}
cout << mn << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
const long long MAXL = 1e18;
const int N = 10005000;
const int MAXN = 1e9 + 7;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand(time(NULL));
unordered_map<long long, bool> used;
long long a, b, w, x, c;
cin >> a >> b >> w >> x >> c;
vector<long long> cnt(w + 10);
bool last = 0;
if (c <= a) {
cout << 0 << "\n";
return 0;
}
long long cycl = 0, minus_a = 0;
while (true) {
if (used[b]) {
break;
}
used[b] = true;
cycl++;
if (b >= x) {
b = b - x;
last = 0;
} else {
b = w - (x - b);
minus_a++;
cnt[cycl]++;
last = 1;
}
}
for (int i = 1; i < w + 10; ++i) {
cnt[i] = cnt[i - 1] + cnt[i];
}
long long l = 0, r = MAXL, mid;
while (l < r - 1) {
mid = l + (r - l) / 2;
long long temp_c = c - mid;
long long temp_a = a - ((mid / cycl) * minus_a + cnt[mid % cycl]);
if (temp_c <= temp_a) {
r = mid;
} else {
l = mid;
}
}
cout << r << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, a, b, ans[1000111];
int main() {
scanf("%lld%lld%lld", &n, &a, &b);
if (a < b) swap(a, b);
for (long long i = 0; a * i <= n; i++) {
if ((n - a * i) % b != 0) continue;
long long ac = i, bc = (n - a * i) / b;
for (long long i = 0; i < ac; i++) {
for (long long j = 0; j < a - 1; j++) {
ans[i * a + j + 1] = i * a + j + 1;
}
ans[i * a] = (i + 1) * a;
}
long long base = ac * a;
for (long long i = 0; i < bc; i++) {
for (long long j = 0; j < b - 1; j++) {
ans[base + i * b + j + 1] = base + i * b + j + 1;
}
ans[base + i * b] = base + (i + 1) * b;
}
for (long long i = 0; i < n; i++) printf("%lld ", ans[i]);
printf("\n");
return 0;
}
printf("-1\n");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int Set(int mask, int pos) { return mask = mask | (1 << pos); }
bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); }
int pos[300005], arr[300005];
vector<pair<int, int> > v;
int n;
void move(int i) {
v.push_back(make_pair(pos[arr[i]], pos[i]));
int t = pos[i], x = arr[i];
swap(arr[pos[i]], arr[i]);
pos[i] = i;
pos[x] = t;
}
void moveBackward(int i) {
v.push_back(make_pair(pos[i], n));
int t = pos[i], x = arr[n];
swap(arr[pos[i]], arr[n]);
pos[x] = pos[i];
pos[i] = n;
}
void moveForward(int i) {
v.push_back(make_pair(pos[i], 1));
int t = pos[i], x = arr[1];
swap(arr[pos[i]], arr[1]);
pos[x] = pos[i];
pos[i] = 1;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
pos[arr[i]] = i;
}
for (int i = 1; i <= n; i++) {
if (arr[i] == i) continue;
if (abs(pos[arr[i]] - pos[i]) >= n / 2) {
move(i);
} else {
if (pos[i] - 1 >= (n / 2)) {
moveForward(i);
if ((i - pos[i]) >= n / 2)
move(i);
else {
moveBackward(i);
move(i);
}
} else {
moveBackward(i);
if (pos[i] - i >= n / 2) {
move(i);
} else {
moveForward(i);
move(i);
}
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int mod = 1e9 + 7;
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first == b.first)
return a.second < b.second;
else
return a.first < b.first;
}
long long exp(long long a, long long b) {
long long ans = 1;
while (b != 0) {
if (b % 2) ans = ans * a;
a = a * a;
b /= 2;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long arr[10];
for (long long i = (long long)1; i < (long long)7; i++) {
cin >> arr[i];
}
long long ans = 0;
ans -= (arr[1] * arr[1] + arr[3] * arr[3] + arr[5] * arr[5]);
ans += (arr[1] + arr[2] + arr[3]) * (arr[1] + arr[2] + arr[3]);
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt;
char a[300005], b[10];
int main(void) {
scanf("%d%d", &n, &m);
scanf("%s", a + 1);
scanf("%d%s", &cnt, b);
a[cnt] = b[0];
int ret, ans = 0, flag;
for (int i = 1; i <= n; i++) {
ret = 0;
flag = 0;
while (a[i] == '.') {
i++;
ret++;
flag = 1;
}
if (flag) ans += ret - 1;
}
printf("%d\n", ans);
for (int i = 1; i < m; i++) {
scanf("%d%s", &cnt, b);
char ch = a[cnt];
a[cnt] = b[0];
if (b[0] == '.' && ch != '.') {
if (a[cnt - 1] == '.') ans++;
if (a[cnt + 1] == '.') ans++;
} else if (b[0] != '.' && ch == '.') {
if (a[cnt - 1] == '.') ans--;
if (a[cnt + 1] == '.') ans--;
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 111;
int a[MaxN];
int u[MaxN];
int main(void) {
int n;
while (scanf("%d", &n) != EOF) {
int i, j, q;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &q);
for (j = 0; j < q; j++) {
int p, x, y;
scanf("%d %d %d", &p, &x, &y);
if (p == 2) {
int t;
t = a[x - 1], a[x - 1] = a[y - 1], a[y - 1] = t;
} else {
int res = 0;
memset(u, 0, sizeof(u));
for (i = 0; i < n; i++) {
if (a[i] >= x && a[i] <= y && u[a[i] - 1] == 0) res++;
if (a[i] >= x && a[i] <= y) u[a[i]] = 1;
}
printf("%d\n", res);
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, dist[10007], k, spec = 0;
pair<int, int> data[10007];
int identify(int a, int b) {
if ((a == k || b == k) && spec == 1) {
if (abs(data[a].first - data[b].first) <= 1 ||
abs(data[a].second - data[b].second) <= 1)
return 1;
return 10000;
}
if (abs(data[a].first - data[b].first) +
abs(data[a].second - data[b].second) <
2)
return 0;
else if (abs(data[a].first - data[b].first) <= 2 ||
abs(data[a].second - data[b].second) <= 2)
return 1;
return 10000;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
cin >> data[i].first >> data[i].second;
}
sort(data + 1, data + 1 + k);
if (data[k].first != n || data[k].second != m) {
spec = 1;
k++;
data[k] = make_pair(n, m);
}
for (int i = 1; i <= k; i++) dist[i] = 1e4;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
process;
process.push(make_pair(0, 1));
dist[1] = 0;
while (!process.empty()) {
int val = process.top().first, cur = process.top().second;
process.pop();
if (dist[cur] == val) {
for (int i = 2; i <= k; i++) {
if (i == cur) continue;
int cl = identify(cur, i);
if (dist[i] > dist[cur] + cl) {
dist[i] = dist[cur] + cl;
process.push(make_pair(dist[i], i));
}
}
}
}
if (dist[k] >= 1e4) {
cout << -1;
} else
cout << dist[k];
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 0x3f3f3f3f;
const long double PI = 4 * atan((long double)1);
void setIO(string y) {
cin.tie(0);
ios_base::sync_with_stdio(false);
freopen((y + ".in").c_str(), "r", stdin);
freopen((y + ".out").c_str(), "w", stdout);
}
long long second(string &s, long long a, long long b) {
long long R = 0;
for (long long i = 0; i < (long long)s.size(); ++i) {
char c = s[i];
if (c - 48 == a) {
++R;
swap(a, b);
}
}
if (a != b && R % 2) --R;
return R;
}
void solve() {
string s;
cin >> s;
long long R = 0;
for (long long i = 0; i <= 9; ++i)
for (long long j = 0; j <= 9; ++j) R = max(R, second(s, i, j));
cout << (long long)s.size() - R << '\n';
}
int32_t main() {
long long t;
cin >> t;
while (t--) solve();
}
| 3 |
#include <bits/stdc++.h>
const int N = 300300;
using namespace std;
long long ans = 0;
int n, p, s[N], c[N], sz[N];
vector<int> v[N];
map<pair<int, int>, int> cnt;
bool used[N];
void solve(int x) {
int col = s[n] - s[max(0, p - sz[x])] + c[max(0, p - sz[x])];
if (sz[x] + sz[x] >= p) col--;
for (auto y : v[x])
if (sz[y] + sz[x] >= p && sz[y] + sz[x] - cnt[{x, y}] < p) col--;
ans += col;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> p;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
cnt[{x, y}]++;
cnt[{y, x}]++;
sz[x]++;
sz[y]++;
if (cnt[{x, y}] == 1) {
v[x].push_back(y);
v[y].push_back(x);
}
}
for (int i = 1; i <= n; i++) c[sz[i]]++;
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + c[i];
for (int i = 1; i <= n; i++) solve(i);
cout << ans / 2 << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int a[maxn];
int n;
long long ans;
int valid(int x) {
ans = 0;
int i, t, d;
for (i = 1; i <= n; i++) {
t = a[i] / x;
d = a[i] % x;
if (d == 0)
ans += t;
else if (d + t >= x - 1)
ans += t + 1;
else
return 0;
}
return 1;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
for (int i = 1; i <= a[1]; i++) {
if (valid(a[1] / i + 1))
break;
else if (valid(a[1] / i))
break;
}
printf("%I64d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int t, n, d, a, r, h, i;
int main() {
for (cin >> t; t--; i = 0, cout << r << endl)
for (cin >> n >> d >> r; ++i < n;)
cin >> a, h = min(d / i, a), d -= i * h, r += h;
}
| 1 |
#include <algorithm>
#include <cstdio>
typedef long long LL;
const int mo=1000000007;
LL f[100],a[100][200],b[100][200],cnt[100];
int main(){
f[0]=f[1]=1;
for (int i=2;i<90;i++) f[i]=f[i-1]+f[i-2];
cnt[1]=2;a[1][0]=1;b[1][0]=2;a[1][1]=1;b[1][1]=3;
for (int i=1;i<85;i++)
for (int j=0;j<cnt[i];j++){
LL x=b[i][j],y=a[i][j]+b[i][j];
for (;y<=f[i]+f[i+3];y+=b[i][j])
a[i+1][cnt[i+1]]=x,b[i+1][cnt[i+1]++]=y;
}
int Q;
scanf("%d\n",&Q);
for (;Q--;){
LL X,Y;int ans=0;
scanf("%lld%lld\n",&X,&Y);
if (X>Y) std::swap(X,Y);
int k=1;
for (;f[k+1]<=X && f[k+2]<=Y;k++);
if (!--k) ans=1ll*X*Y%mo;
else{
for (int i=0;i<cnt[k];i++){
LL x=a[k][i],y=b[k][i];
if (y<=X) ans=(ans+(Y-x)/y)%mo;
if (y<=Y && x<=X) ans=(ans+(X-x)/y)%mo;
}
}
printf("%d %d\n",k+1,ans);
}
}
| 0 |
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#define llint long long
#define mod 1000000007
using namespace std;
llint n;
vector<pair<llint, llint> > vec;
int main(void)
{
cin >> n;
llint x;
for(int i = 1; i <= n; i++){
cin >> x;
vec.push_back(make_pair(x, 1));
}
for(int i = 1; i <= n; i++){
cin >> x;
vec.push_back(make_pair(x, 2));
}
sort(vec.begin(), vec.end());
llint cnt = 0, cnt2 = 0, ans = 1;
for(int i = 0; i < vec.size(); i++){
if(vec[i].second == 1){
if(cnt2 == 0) cnt++;
else ans *= cnt2, ans %= mod, cnt2--;
}
else{
if(cnt == 0) cnt2++;
else ans *= cnt, ans %= mod, cnt--;
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#define N (1010)
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
namespace fastIO1{
inline char read(){
static const int IN_LEN=1000000;
static char buf[IN_LEN],*s,*t;
return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++);
}
template<class T>
inline void read(T &x){
static bool iosig;
static char c;
for(iosig=false,c=read();!isdigit(c);c=read()){
if(c=='-')iosig=true;
if(c==-1)return;
}
for(x=0;isdigit(c);c=read())x=((x+(x<<2))<<1)+(c^'0');
if(iosig)x=-x;
}
inline char readc(char &c){
for(c=read();!isalpha(c)&&!isdigit(c);c=read())
if(c==-1)return 0;
}
const int OUT_LEN = 10000000;
char obuf[OUT_LEN],*ooh=obuf;
inline void print(char c) {
if(ooh==obuf+OUT_LEN)fwrite(obuf,1,OUT_LEN,stdout),ooh=obuf;
*ooh++=c;
}
template<class T>
inline void print(T x){
static int buf[30],cnt;
if(x==0)print('0');
else{
if(x<0)print('-'),x=-x;
for(cnt=0;x;x/=10)buf[++cnt]=x%10+48;
while(cnt)print((char)buf[cnt--]);
}
}
inline void flush(){fwrite(obuf,1,ooh-obuf,stdout);}
}
namespace fastIO2{
template<class T>
inline void read(T &x){
static bool iosig;
static char c;
for(iosig=false,c=getchar();!isdigit(c);c=getchar()){
if(c=='-')iosig=true;
if(c==-1)return;
}
for(x=0;isdigit(c);c=getchar())x=((x+(x<<2))<<1)+(c^'0');
if(iosig)x=-x;
}
}
using namespace fastIO1;
#define mkp make_pair
int n,P,c[N],f[N],ans;
pair<int,int> a[N];
int getf(int x,int y){
int ans=0;
for(;ans<n*2&&ans*ans+y*y<x*x;ans++);
return ans;
}
int getg(int x,int y){
int ans=0;
for(;ans<n*2&&ans*ans+y*y<=x*x;ans++);
return ans;
}
int main(){
read(n),read(P);
for(int i=0;i<n;i++)a[i+1]=mkp(getf(n,i),-i);
for(int i=n;i<2*n;i++)a[i+1]=mkp(getg(n*2,i),1);
sort(a+1,a+2*n+1);
// for(int i=1;i<=2*n;i++)
// cout<<a[i].first<<" "<<a[i].second<<endl;
// puts("-------------------");
for(int i=0;i<n;i++)c[i]=getg(n*2,i);
for(int k=0;k<=n;k++){
memset(f,0,sizeof(f));
f[0]=1; int s1=0,s2=0;
for(int i=1;i<=2*n;i++)
if(a[i].second==1){
// cout<<a[i].first<<" "<<s1<<" faq"<<" "<<i<<endl;
for(int j=0;j<=s2;j++)
f[j]=(1ll*f[j]*(a[i].first-s1-j)%P+P)%P;
// cout<<j<<" "<<f[j]<<" "<<a[i].first-s1-j<<endl;
s1++;
}
else{
for(int j=s2;j>=0;j--){
f[j+1]=(0ll+f[j+1]+1ll*f[j]*(a[i].first-s1-j)%P+P)%P;
f[j]=(1ll*f[j]*(c[-a[i].second]-n-k-s2+j)%P+P)%P;
// cout<<j<<" "<<f[j]<<" "<<f[j+1]<<" "<<n+k+s2-j<<" "<<c[-a[i].second]<<endl;
}
s2++;
}
// cout<<k<<" "<<f[k]<<endl;
// puts("-------");
if(k&1)(ans+=P-f[k])%=P;else (ans+=f[k])%=P;
}
printf("%d\n",ans<0?ans+P:ans);
}
| 0 |
#include <bits/stdc++.h>
const long long MAXN = 80000 + 5;
std::vector<long long> G[MAXN];
long long n;
long long sz[MAXN], fa[MAXN];
inline void dfs1(long long v, long long faa = 0) {
sz[v] = 1;
fa[v] = faa;
for (auto x : G[v]) {
if (x == faa) continue;
dfs1(x, v);
sz[v] += sz[x];
}
}
long long ans = 0ll;
inline long long C(long long x) { return 1ll * x * (x - 1) / 2; }
signed main() {
scanf("%lld", &n);
for (register long long i = 1; i <= n - 1; ++i) {
long long u, v;
scanf("%lld%lld", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(1);
ans = C(n) * C(n);
for (register long long i = 1; i <= n; ++i) {
long long num2 = C(sz[i]), num1 = C(n);
for (auto x : G[i]) {
if (x == fa[i]) {
num1 -= C(n - sz[i]);
continue;
}
num2 -= C(sz[x]);
num1 -= C(sz[x]);
}
ans -= (2ll * num1 * num2 - num2 * num2);
}
std::cout << ans << std::endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<int(n); i++)
#define FOR(i,m,n) for(int i=int(m); i<int(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1 << 30;
const ll INFL = 1LL<<60;
const ll mod =1000000009;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll p;
cin >> n >> p;
ll c = 0, d = 0;
vector<ll> w(n), b(n);
vector<pair<ll, pair<ll, ll>>> v;
ll sum = 0;
ll a = 0;
REP(i, n) {
cin >> w[i] >> b[i];
v.push_back({ (100 - p)*w[i] + p * b[i],{w[i],b[i]} });
sum += b[i];
}
sort(ALL(v));
reverse(ALL(v));
int ans = 0;
REP(i, n) {
a += v[i].second.first;
sum -= v[i].second.second;
ans++;
if ((100 - p)*a >= p * sum)break;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int INFINITE = 0x3f3f3f3f;
template <class T>
inline void checkmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T lowbit(T n) {
return (n ^ (n - 1)) & n;
}
template <class T>
inline int countbit(T n) {
return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
typedef vector<int> VI;
typedef vector<long long> VI64;
typedef vector<VI> VII;
typedef vector<string> VS;
#pragma comment(linker, "/STACK:200000000")
void rotateClockwise(VII &b) {
int w = b.size();
int h = b[0].size();
VII bb;
bb.resize(h);
for (int i = (int)(0); i < (int)(h); i++) bb[i].resize(w);
for (int i = (int)(0); i < (int)(h); i++)
for (int j = (int)(0); j < (int)(w); j++) bb[i][j] = (b[j][i]);
b = bb;
}
void zoomInTwice(VII &b) {
int w = b.size();
int h = b[0].size();
VII bb;
bb.resize(2 * w);
for (int i = (int)(0); i < (int)(2 * w); i++) bb[i].resize(2 * h);
int r = 0;
for (int i = (int)(0); i < (int)(w); i++) {
int c = 0;
for (int j = (int)(0); j < (int)(h); j++) {
bb[r][c] = b[i][j];
bb[r][c + 1] = b[i][j];
bb[r + 1][c] = b[i][j];
bb[r + 1][c + 1] = b[i][j];
c += 2;
}
r += 2;
}
b = bb;
}
void solve() {
int w, h;
cin >> h >> w;
VS b;
b.resize(w);
for (int i = (int)(0); i < (int)(w); i++) cin >> b[i];
VII bi;
bi.resize(w);
for (int i = (int)(0); i < (int)(w); i++)
for (int j = (int)(0); j < (int)(h); j++) {
bi[i].push_back(b[i][j] == '*' ? 1 : 0);
}
::rotateClockwise(bi);
::zoomInTwice(bi);
int nw = bi.size();
int nh = bi[0].size();
for (int i = (int)(0); i < (int)(nw); i++)
for (int j = (int)(0); j < (int)(nh); j++) {
cout << (bi[i][j] == 1 ? '*' : '.');
if (j == nh - 1) cout << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
::solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9, PI = acos(-1);
const int mod = int(1e9) + 7, MX = int(2e5) + 10;
int P[MX], E[MX], n, root = 0;
vector<int> Adj[MX];
long long memo[MX][2];
void dfs(int pr) {
int sz = int(Adj[pr].size());
long long res = 0, mx = 0;
for (int ch : Adj[pr]) {
dfs(ch);
res += memo[ch][1];
}
for (int ch : Adj[pr]) mx = max(mx, res - memo[ch][1] + memo[ch][0]);
if (sz & 1)
memo[pr][1] = max(res, mx + E[pr]), memo[pr][0] = mx;
else
memo[pr][1] = res + E[pr], memo[pr][0] = res;
}
void solve() {
scanf("%d", &n);
for (int p, i = 1; i <= n; i++) {
scanf("%d %d", &p, &E[i]);
if (p + 1)
Adj[p].push_back(i);
else
root = i;
}
dfs(root);
long long ans = max(memo[1][1], memo[1][0]);
printf("%lld\n", ans);
}
int main() { solve(); }
| 2 |
// La solucion se puede representar con un automata
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define inf 1e9
typedef pair<int,int> par;
map<int,int> T;
int main(){
ios_base::sync_with_stdio(0);cin.tie(NULL);
int t;
map<int,int>::iterator it;
cin>>t;
while(t--){
int n,m,r,c;
string ans = "YES";
cin>>n>>m;
for(int i=0; i<m; i++){
cin>>r>>c;
it = T.find(c);
if(it == T.end())
T.insert(par(c,r));
else
it->second = 0;
}
T.insert(par(n+1,0));
int lastC = -1;
int lastType = 0;
for(it=T.begin(); it!=T.end() && ans == "YES"; it++){
if(lastType == 0)
lastType = it->second;
else{
if(lastType == 1){
if((it->second == 0) || (it->second == 1 && (((it->first) - lastC) % 2 == 0)) || (it->second == 2 && (((it->first) - lastC) % 2 == 1)))
ans = "NO";
}
else{
if((it->second == 0) || (it->second == 1 && (((it->first) - lastC) % 2 == 1)) || (it->second == 2 && (((it->first) - lastC) % 2 == 0)))
ans = "NO";
}
if(ans == "YES")
lastType = 0;
}
lastC = it->first;
}
cout<<ans<<endl;
T.clear();
}
}
| 6 |
#include <bits/stdc++.h>
const long long MAX_N = 2e5 + 10;
const long long MOD = 1e9 + 7;
using namespace std;
vector<int> g[MAX_N];
int rm[MAX_N][20];
vector<int> vers;
int a[MAX_N];
int b[MAX_N];
int t[MAX_N];
int pos[MAX_N];
int last[MAX_N];
int mark[MAX_N];
int n, m, q;
int ask(int l, int r) {
int len = r - l + 1;
int lg = log2(len);
return min(rm[l][lg], rm[r - (1 << lg) + 1][lg]);
}
void dfs(int v) {
if (vers.size() < n - 1)
t[v] = 1e9;
else if (n - 1)
t[v] = vers[vers.size() - (n - 1)];
vers.push_back(v);
mark[v]++;
for (auto x : g[v]) dfs(x);
vers.pop_back();
}
int main() {
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]), pos[b[i]] = i;
for (int i = 1; i <= m; ++i) cin >> a[i], a[i] = pos[a[i]];
for (int i = m; i > 0; --i) {
if (last[(a[i] % n) + 1]) g[last[(a[i] % n) + 1]].push_back(i);
last[a[i]] = i;
}
for (int i = m; i > 0; --i) {
if (!mark[i]) dfs(i);
}
for (int i = 1; i <= m; ++i) rm[i][0] = t[i];
for (int i = 1; i < 20; ++i)
for (int j = 1; j <= m - (1 << i) + 1; ++j)
rm[j][i] = min(rm[j][i - 1], rm[j + (1 << (i - 1))][i - 1]);
vector<int> ans;
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
if (ask(l, r) <= r)
ans.push_back(1);
else
ans.push_back(0);
}
for (auto x : ans) cout << x;
return 0;
}
| 2 |
#include <iostream>
#include <vector>
using namespace std;
void d(vector<vector<int>> v,int h){
for(int i=0;i<h;i++){
for(int j=0;j<v[i].size();j++){
cout<<v[i][j]<<" ";
}
cout<<"\n";
}
cout<<"\n";
}
int main (){
int h;
while(cin>>h && h){
vector<vector<int>> v(h,vector<int>(5));
for(int i=0;i<h;i++)for(int j=0;j<5;j++)cin>>v[i][j];
int ans=0;
while(1){
int flag=true;
for(int i=0;i<h;i++){
int chain;
for(int j=0;j<5;j++){
chain=1;
while(j+1<5 && v[i][j]!=-1 && v[i][j]==v[i][j+1]){
chain++;
j++;
}
if(chain>=3){
flag=false;
ans+=chain*v[i][j];
for(int k=0;k<chain;k++){
v[i][j-chain+k+1]=-1;
}
break;
}
}
}
//d(v,h);
for(int p=0;p<h;p++){
for(int i=0;i<5;i++){
for(int j=h-1;j>=1;j--){
if(v[j][i]==-1){
for(int k=j;k>=1;k--)swap(v[k][i],v[k-1][i]);
}
}
}
}
//d(v,h);
if(flag)break;
}
cout<<ans<<endl;
}
}
/*
5 8 105
8 5 105
1 2 24
99 98 24
12 13 26
1 22 23
1 13 201
13 16 112
2 24 50
1 82 61
1 84 125
1 99 999
99 1 999
98 99 999
1 99 11
99 1 12
0 0 0
*/
| 0 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
int main(){
while(true){
int N, M;
cin >> N >> M;
if(N == 0 && M == 0){ break; }
vector<pii> r(N);
for(int i = 0; i < N; ++i){
cin >> r[i].second >> r[i].first;
}
sort(r.begin(), r.end());
ll answer = 0;
for(int i = N - 1; i >= 0; --i){
if(r[i].second <= M){
M -= r[i].second;
}else{
answer += (r[i].second - M) * r[i].first;
M = 0;
}
}
cout << answer << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:536870912")
const int MOD = 1000000007;
const int INF = 1000000001;
const int MAXN = 17;
const long double EPS = 1e-6;
const int HASH_POW = 29;
const long double PI = acos(-1.0);
using namespace std;
void my_return(int code) { exit(code); }
set<string> Q;
void dfs(string v) {
Q.insert(v);
string tmp;
tmp = v;
if (tmp[0] == 'X' || tmp[1] == 'X') swap(tmp[0], tmp[1]);
if (Q.find(tmp) == Q.end()) dfs(tmp);
tmp = v;
if (tmp[0] == 'X' || tmp[2] == 'X') swap(tmp[0], tmp[2]);
if (Q.find(tmp) == Q.end()) dfs(tmp);
tmp = v;
if (tmp[1] == 'X' || tmp[3] == 'X') swap(tmp[1], tmp[3]);
if (Q.find(tmp) == Q.end()) dfs(tmp);
tmp = v;
if (tmp[2] == 'X' || tmp[3] == 'X') swap(tmp[2], tmp[3]);
if (Q.find(tmp) == Q.end()) dfs(tmp);
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
mt19937 mt_rand(time(0));
string s1(4, '0'), s2(4, '0');
scanf("%c%c\n", &s1[0], &s1[1]);
scanf("%c%c\n", &s1[2], &s1[3]);
dfs(s1);
scanf("%c%c\n", &s2[0], &s2[1]);
scanf("%c%c\n", &s2[2], &s2[3]);
if (Q.find(s2) != Q.end())
printf("YES\n");
else
printf("NO\n");
my_return(0);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double a[12][12];
int main() {
int n, t;
scanf("%d %d", &n, &t);
a[0][0] = t;
int ans = 0;
for (int i = 0; i < n; i++) {
int x = 0;
for (int j = 0; j <= i; j++) {
if (a[i][j] < 1) continue;
a[i + 1][x] += (a[i][j] - 1) / 2;
a[i + 1][++x] += (a[i][j] - 1) / 2;
ans++;
}
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int x, y;
vector<int> v[1000005];
int cnt = 0, tmp = 0;
bool u[1000005];
vector<int> g;
int q[1000005];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
x--, y--;
v[x].push_back(y);
v[y].push_back(x);
}
int l = 0, r = 0;
for (int i = 0; i < n; i++)
if (!u[i]) {
l = 0, r = 0;
u[i] = 1;
q[r] = i;
while (l <= r) {
int x = q[l];
for (int j = 0; j < v[x].size(); j++)
if (!u[v[x][j]]) {
u[v[x][j]] = 1;
r++;
q[r] = v[x][j];
}
l++;
}
g.push_back(min(r + 1, k));
}
int sum = 0;
for (int i = 0; i < g.size(); i++)
if (g[i] >= 2)
g[i] -= 2;
else
sum++, g[i]--;
sum -= 2;
for (int i = 0; i < g.size(); i++)
if (sum)
sum -= min(sum, g[i]);
else
break;
if (k == 1)
cout << sum;
else
cout << (sum + 1) / 2;
return 0;
}
| 4 |
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <list>
#define REP(i,a,b) for(i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
#define ll long long
#define ull unsigned ll
typedef long double ld;
#define SQR(X) ( (X)*(X) )
#define CUBE(X) ( (X)*(X)*(X) )
// #define int ll
using namespace std;
bool test = 0;
/* ?????????????????¬??¨ */
int main(){
int i,j,k,l;
int n;
while(cin >> n) {
if(n == 0) return 0;
map<int,int> m;
rep(i,n) {
int a; cin >> a;
if( m.count(a) == 0 ) m[a] = 1;
else m[a]++;
}
auto itr = m.begin();
for(; itr != m.end(); itr++) {
// cout << itr->first;
// cout << ":" << itr->second << endl;
if(itr->second > n/2) break;
}
if(itr == m.end() ) puts("NO COLOR");
else printf("%d\n",itr->first);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int SQ = 450;
const int MOD = 1e9 + 7;
long long fact[N], inv[N];
string s;
int pos[N];
int dp[SQ][N];
long long bpow(long long a, long long b) {
long long out = 1;
while (b) {
if (b & 1) out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return out;
}
long long c(long long a, long long b) {
if (a > b) return 0;
return fact[b] * inv[a] % MOD * inv[b - a] % MOD;
}
int main() {
ios::sync_with_stdio(false);
fact[0] = 1;
for (int i = 1; i < N; i++) fact[i] = fact[i - 1] * i % MOD;
inv[N - 1] = bpow(fact[N - 1], MOD - 2);
for (int i = N - 1; i > 0; i--) inv[i - 1] = inv[i] * i % MOD;
memset(pos, -1, sizeof pos);
int cnt = 0;
int q;
cin >> q;
cin >> s;
while (q--) {
int t;
cin >> t;
if (t == 2) {
int n;
cin >> n;
if (pos[s.size()] == -1) {
pos[s.size()] = cnt;
dp[cnt][s.size()] = 1;
long long k = 1;
for (int i = s.size() + 1; i < N; i++) {
k = k * 25 % MOD;
dp[cnt][i] =
(dp[cnt][i - 1] * 26LL % MOD + c(s.size() - 1, i - 1) * k % MOD) %
MOD;
}
cnt++;
}
cout << dp[pos[s.size()]][n] << endl;
} else
cin >> s;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long a = 0, b = getchar(), c = 1;
while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar();
while (isdigit(b)) a = a * 10 + b - '0', b = getchar();
return a * c;
}
const int N = 2005;
long long n, m, ab, ac = 1e15, a[N], b[N], c[N], d[N], e[1000005];
int main() {
n = read(), m = read();
for (int i = 0; i < n; i++) a[i] = read(), b[i] = read();
for (int i = 0; i < m; i++) c[i] = read(), d[i] = read();
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (c[j] >= a[i]) e[c[j] - a[i]] = max(e[c[j] - a[i]], d[j] - b[i] + 1);
for (int i = 1000001; i >= 0; i--) {
ab = max(ab, e[i]);
ac = min(ac, i + ab);
}
cout << ac;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e3 + 20;
const long long maxn = 1e6 + 50;
inline int read() {
int X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag) return X;
return ~(X - 1);
}
int n, m;
long long tree[maxn], ans[maxn];
vector<int> G[maxn], D[maxn], X[maxn];
void add(int x, int val) {
while (x <= n) {
tree[x] += val;
x = x + (x & -x);
}
}
long long sum(int x) {
long long ans = 0;
while (x) {
ans += tree[x];
x = x - (x & -x);
}
return ans;
}
void dfs(int x, int fa, int dep) {
for (int i = 0; i < D[x].size(); i++) {
add(min(D[x][i] + dep, n), X[x][i]);
}
ans[x] = sum(n) - sum(dep - 1);
for (int i = 0; i < G[x].size(); i++) {
if (G[x][i] == fa) continue;
dfs(G[x][i], x, dep + 1);
}
for (int i = 0; i < D[x].size(); i++) {
add(min(D[x][i] + dep, n), -X[x][i]);
}
}
int main() {
int x, y, z;
scanf("%d", &n);
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
D[x].push_back(y);
X[x].push_back(z);
}
dfs(1, 0, 1);
for (int i = 1; i <= n; i++) {
printf("%lld ", ans[i]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int kase;
cin >> kase;
while (kase--) {
string s1, s2;
cin >> s1 >> s2;
if (s1 < s2) {
cout << s1 << endl;
continue;
} else {
map<char, int> mp;
bool flag = 0;
for (int i = s1.size() - 1; i >= 0 && !flag; i--) {
for (char ch = 'A'; ch < s1[i] && !flag; ch++) {
if (mp.find(ch) != mp.end()) {
swap(s1[i], s1[mp[ch]]);
if (s1 < s2)
flag = 1;
else
swap(s1[i], s1[mp[ch]]);
}
}
if (mp.find(s1[i]) == mp.end()) mp[s1[i]] = i;
}
if (!flag) {
mp.clear();
for (int i = 0; i < s1.size() && !flag; i++) {
for (char ch = 'A'; ch < s1[i] && !flag; ch++) {
if (mp.find(ch) != mp.end()) {
swap(s1[i], s1[mp[ch]]);
if (s1 < s2)
flag = 1;
else
swap(s1[i], s1[mp[ch]]);
}
}
mp[s1[i]] = i;
}
}
if (s1 < s2)
cout << s1 << endl;
else
cout << "---" << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
int main() {
long long int n;
scanf("%lld", &n);
if (n % 2 == 0)
printf("%lld", n / 2);
else
printf("-%lld", (n + 1) / 2);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, p, x[20], y[20], a[20][20] = {0};
string w[100900];
int tot = 0, fal[100900] = {0}, ans = 0;
int r[100900], rr[100900];
struct cadongllas {
int x1, y1, x2, y2;
} f[100900];
int main() {
cin >> n >> m >> k >> p;
for (int i = 1; i <= k; i++) scanf("%d%d", &x[i], &y[i]), a[x[i]][y[i]] = 1;
for (int x1 = 1; x1 <= n; x1++)
for (int y1 = 1; y1 <= m; y1++)
for (int x2 = x1; x2 <= n; x2++)
for (int y2 = y1; y2 <= m; y2++) {
int now = 0;
for (int i = 1; i <= k; i++)
if (x1 <= x[i] && x[i] <= x2 && y1 <= y[i] && y[i] <= y2) now++;
if (now < p) continue;
tot++;
f[tot].x1 = x1, f[tot].x2 = x2;
f[tot].y1 = y1, f[tot].y2 = y2;
}
printf("%d\n", tot);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
for (int i(0); i < N; i++) scanf("%d", A + i);
}
char s[(1 << 19)], r[(1 << 19)];
int a, b, L, c, N, B, S, A[(1 << 19)];
bool cp(int a, int b) { return a > b; }
int main(void) {
scanf("%d", &N);
for (int i(0); i < 18; i++)
if ((N >> i) & 1) A[L++] = i + 1;
sort(A, A + L, cp);
for (int i(0); i < L; i++) printf("%d%c", A[i], i + 1 == L ? 10 : 32);
return 0;
}
| 1 |
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }
int main() {
const int INF = (1 << 29);
int n, m;
scanf("%d\n%d", &n, &m);
vector<vector<int>> mat(n, vector<int>(n, INF));
for(int i = 0; i < n; ++i) {
mat[i][i] = 0;
}
for(int i = 0; i < m; ++i) {
int a, b, c, d;
scanf("%d,%d,%d,%d", &a, &b, &c, &d);
--a; --b;
mat[a][b] = c;
mat[b][a] = d;
}
for(int k = 0; k < n; ++k) {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
chmin(mat[i][j], mat[i][k] + mat[k][j]);
}
}
}
int x1, x2, y1, y2;
scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2);
--x1; --x2;
printf("%d\n", y1 - (mat[x1][x2] + mat[x2][x1] + y2));
return EXIT_SUCCESS;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, x;
long long ans;
multiset<long long> q;
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;
q.insert(x);
}
while (q.size() > 1) {
if (q.size() % 2 == 0) {
q.insert(0);
}
long long x = *q.begin();
q.erase(q.begin());
long long y = *q.begin();
q.erase(q.begin());
long long z = *q.begin();
q.erase(q.begin());
ans += x + y + z;
q.insert(x + y + z);
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 222222;
vector<int> E[MAXN];
vector<int> E2[MAXN];
int color[MAXN];
int newNode[MAXN];
int currentNode = 2;
void dfs(int node, int dad = -1) {
for (int i = 0; i < E[node].size(); i++) {
int to = E[node][i];
if (to == dad) continue;
if (color[to] != color[node]) {
newNode[to] = currentNode++;
E2[newNode[node]].push_back(newNode[to]);
E2[newNode[to]].push_back(newNode[node]);
} else {
newNode[to] = newNode[node];
}
dfs(to, node);
}
}
int dist[MAXN];
pair<int, int> bfs(int source) {
queue<pair<int, int> > Q;
for (int i = 1; i < MAXN; i++) dist[i] = MAXN + 1000;
dist[source] = 0;
Q.push(make_pair(source, 0));
pair<int, int> ans;
while (!Q.empty()) {
ans = Q.front();
Q.pop();
for (int i = 0; i < E2[ans.first].size(); i++) {
int to = E2[ans.first][i];
if (dist[to] > MAXN) {
dist[to] = dist[ans.first] + 1;
Q.push(make_pair(to, dist[to]));
}
}
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> color[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
}
newNode[1] = 1;
dfs(1);
pair<int, int> best = bfs(1);
printf("%d\n", (bfs(best.first).second + 1) / 2);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, t;
long long a[100][100], b[100][100];
vector<long long> v;
vector<pair<long long, long long> > po;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) cin >> a[i][j];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) cin >> b[i][j];
}
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= m; j++) {
long long k = min(a[i][j], b[i][j]);
long long kk = max(a[i][j], b[i][j]);
a[i][j] = k;
b[i][j] = kk;
}
}
for (long long i = 1; i <= n; i++) {
for (long long j = 2; j <= m; j++) {
if (a[i][j] > a[i][j - 1])
continue;
else {
cout << "Impossible" << endl;
exit(0);
}
}
}
for (long long i = 1; i <= m; i++) {
for (long long j = 2; j <= n; j++) {
if (a[j][i] > a[j - 1][i])
continue;
else {
cout << "Impossible" << endl;
exit(0);
}
}
}
for (long long i = 1; i <= n; i++) {
for (long long j = 2; j <= m; j++) {
if (b[i][j] > b[i][j - 1])
continue;
else {
cout << "Impossible" << endl;
exit(0);
}
}
}
for (long long i = 1; i <= m; i++) {
for (long long j = 2; j <= n; j++) {
if (b[j][i] > b[j - 1][i])
continue;
else {
cout << "Impossible" << endl;
exit(0);
}
}
}
cout << "Possible" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char c[10][10];
int ans;
int main() {
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) cin >> c[i][j];
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
if (c[i][j] == 'W') break;
if (j == 8) ans++;
}
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
if (c[j][i] == 'W') break;
if (j == 8) ans++;
}
if (ans == 16) ans = 8;
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e9;
const long long N = 1e5 + 1;
const long long mod = 1e9 + 7;
const long double eps = 1E-7;
vector<int> g[N];
int n, m, x, y, mx, d[N];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> x >> y, g[x].push_back(y);
for (int i = 1; i <= m; i++) {
int cnt = 0;
sort(g[i].begin(), g[i].end());
reverse(g[i].begin(), g[i].end());
for (int j = 0; j < g[i].size(); j++)
cnt += g[i][j], d[j + 1] += max(0, cnt), mx = max(mx, d[j + 1]);
}
cout << mx << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int N, M, A[200000];
string s;
string c[200000], b[200000];
int main() {
std::ios::sync_with_stdio(false);
cin >> N;
cin >> s;
int x = 0, y = 0;
for (int i = 1; i <= N; i++) {
cin >> c[i];
if (c[i][0] == s[0] && c[i][1] == s[1] && c[i][2] == s[2]) x++;
if (c[i][5] == s[0] && c[i][6] == s[1] && c[i][7] == s[2]) y++;
}
if (x == y)
cout << "home";
else
cout << "contest";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, w;
long long a[100010], cnt[100010];
bool check(long long h) {
memset(cnt, 0, sizeof cnt);
long long tmpm = m;
for (int i = 1; i <= n; ++i) {
cnt[i] += cnt[i - 1];
long long cur = a[i] + cnt[i];
if (cur >= h) continue;
long long dif = h - cur;
if (dif > tmpm) return false;
int r = min(n, i + w - 1);
cnt[i] += dif;
cnt[r + 1] -= dif;
tmpm -= dif;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> w;
for (int i = 1; i <= n; ++i) cin >> a[i];
long long lo = 1ll, hi = 3000000000ll, ans;
while (lo <= hi) {
int mid = lo + hi >> 1;
if (check(mid)) {
ans = mid;
lo = mid + 1;
} else
hi = mid - 1;
}
cout << ans << '\n';
}
| 3 |
#include <bits/stdc++.h>
typedef struct nodo {
int dato;
struct nodo *anterior;
struct nodo *siguiente;
} nodo;
typedef struct pila {
nodo *primero;
nodo *ultimo;
} mazos;
void agregar(mazos *mazo, int valor);
void CrearMazo(mazos *mazo, int n);
void imprimir(mazos mazo[], int dim);
void eliminar(mazos *mazo, nodo *actual);
int main() {
int n, i, j, valor, turno = 1, ciel = 0, mayor, jiro = 0, ban = 1, cont,
indice;
nodo *direccion;
scanf("%d", &n);
int x[n];
mazos mazo[n];
CrearMazo(mazo, n);
for (i = 0; i < n; i++) {
scanf("%d", &x[i]);
for (j = 0; j < x[i]; j++) {
scanf("%d", &valor);
agregar(&mazo[i], valor);
}
}
for (i = 0; i < n; i++) {
if (x[i] % 2 == 0) {
while (mazo[i].primero != NULL) {
if (turno % 2 != 0) {
ciel += mazo[i].primero->dato;
eliminar(&mazo[i], mazo[i].primero);
} else {
jiro += mazo[i].ultimo->dato;
eliminar(&mazo[i], mazo[i].ultimo);
}
turno++;
}
} else {
while (mazo[i].primero != mazo[i].ultimo) {
if (turno % 2 != 0) {
ciel += mazo[i].primero->dato;
eliminar(&mazo[i], mazo[i].primero);
} else {
jiro += mazo[i].ultimo->dato;
eliminar(&mazo[i], mazo[i].ultimo);
}
turno++;
}
}
}
while (ban == 1) {
ban = 0;
indice = -1;
mayor = 0;
for (i = 0; i < n; i++) {
if (mazo[i].primero != NULL) {
if (turno % 2 == 0) {
if (mayor < mazo[i].ultimo->dato) {
mayor = mazo[i].ultimo->dato;
direccion = mazo[i].ultimo;
indice = i;
}
} else {
if (mayor < mazo[i].primero->dato) {
mayor = mazo[i].primero->dato;
direccion = mazo[i].primero;
indice = i;
}
}
}
}
if (turno % 2 != 0 && indice != -1) {
ciel += mayor;
eliminar(&mazo[indice], direccion);
ban = 1;
} else if (turno % 2 == 0 && indice != -1) {
jiro += mayor;
eliminar(&mazo[indice], direccion);
ban = 1;
}
turno++;
}
printf("%d %d", ciel, jiro);
return 0;
}
void agregar(mazos *mazo, int valor) {
nodo *nuevo = (nodo *)malloc(sizeof(nodo));
if (mazo->ultimo != NULL) {
nuevo->anterior = mazo->ultimo;
mazo->ultimo->siguiente = nuevo;
} else {
nuevo->anterior = NULL;
mazo->primero = nuevo;
}
mazo->ultimo = nuevo;
nuevo->siguiente = NULL;
nuevo->dato = valor;
}
void CrearMazo(mazos *mazo, int n) {
int i;
for (i = 0; i < n; i++) {
mazo[i].primero = NULL;
mazo[i].ultimo = NULL;
}
}
void imprimir(mazos mazo[], int dim) {
int i;
nodo *actual;
for (i = 0; i < dim; i++) {
actual = mazo[i].primero;
while (actual != NULL) {
printf("%d ", actual->dato);
actual = actual->siguiente;
}
}
}
void eliminar(mazos *mazo, nodo *actual) {
nodo *temp = (nodo *)malloc(sizeof(nodo));
if (mazo->ultimo == mazo->primero) {
mazo->primero = NULL;
mazo->ultimo = NULL;
temp = actual;
} else {
if (actual->siguiente == NULL) {
actual->anterior->siguiente = NULL;
mazo->ultimo = actual->anterior;
} else {
actual->siguiente->anterior = NULL;
mazo->primero = actual->siguiente;
}
temp = actual;
}
free(temp);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void affiche(int t[], int n) {
for (int i = 0; i < n; i++) cout << t[i] << " ";
cout << '\n';
}
void afficheV(vector<int> t) {
for (int i = 0; i < t.size(); i++) cout << t[i] << " ";
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
map<int, int> mp;
map<int, int>::iterator it;
int ans = 0, mx = 0, second = 0;
for (int i = 0; i < 5; i++) {
int x;
cin >> x;
second += x;
mp[x]++;
}
for (it = mp.begin(); it != mp.end(); it++) {
if (it->second == 1) continue;
int c = min(3, it->second);
int sum = c * it->first;
mx = max(mx, sum);
}
cout << second - mx;
}
| 1 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int data[10] = {-999,-1,0,1,-1,-999,1,-1,0,1};
int main(){
string str;
while(1){
cin >> str;
if(str=="#") break;
int pos[2] = {0,0};//left,right
int nextuse = 1;//0 left 1 right
int rst1 = 0,rst2 = 0;
pos[0] = data[str[0]-'0'];
FOR(i,1,str.size()){
int nextpos = data[str[i]-'0'];
bool isok = true;
if(nextuse==0){
if(pos[1]>nextpos) isok = false;
}else{
if(pos[0]<nextpos) isok = false;
}
if(isok){
pos[nextuse] = nextpos;
nextuse = 1 - nextuse;
}else{
pos[1-nextuse] = nextpos;
rst1++;
}
}
nextuse = 0;
pos[1] = data[str[0]-'0'];
FOR(i,1,str.size()){
int nextpos = data[str[i]-'0'];
bool isok = true;
if(nextuse==0){
if(pos[1]>nextpos) isok = false;
}else{
if(pos[0]<nextpos) isok = false;
}
if(isok){
pos[nextuse] = nextpos;
nextuse = 1 - nextuse;
}else{
pos[1-nextuse] = nextpos;
rst2++;
}
}
cout << min(rst1,rst2) << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long power(int a, int b) {
long long ret = 1;
long long tmp = a;
while (b > 0) {
if (b & 1) {
ret *= tmp;
ret %= MOD;
}
tmp *= tmp;
tmp %= MOD;
b >>= 1;
}
return ret;
}
int main() {
int a[] = {-1, 1, 2, 4, 6, 12, 16,
18, 30, 60, 88, 106, 126, 520,
606, 1278, 2202, 2280, 3216, 4252, 4422,
9688, 9940, 11212, 19936, 21700, 23208, 44496,
86242, 110502, 132048, 216090, 756838, 859432, 1257786,
1398268, 2976220, 3021376, 6972592, 13466916, 20996010};
int n;
cin >> n;
cout << (power(2, a[n]) - 1) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
vector<int> g[N];
int nd[N];
int n;
int func(int v) {
if (g[v].size() == 0) {
nd[v] = 1;
return 1;
}
for (int i = 0; i < g[v].size(); i++) {
func(g[v][i]);
nd[v] += nd[g[v][i]];
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
int a;
cin >> a;
g[a].push_back(i + 1);
}
func(1);
sort(nd, nd + n + 1);
for (int i = 1; i <= n; i++) {
cout << nd[i] << " ";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, b;
long long mx, r = 0;
cin >> m >> b;
mx = m * b;
for (long long i = 0; i <= mx; ++i) {
long long j = b;
if (i % m == 0)
j -= i / m;
else
j -= i / m + 1;
long long aux_r = (i + 1) * (j + 1) * (i + j) / 2;
if (aux_r > r) r = aux_r;
}
cout << r << '\n';
return 0;
}
| 2 |
#include<cstdio>
#include<vector>
#include<algorithm>
int main(){
for(int Q,L,q,x;scanf("%d%d",&Q,&L),Q;puts("end"))
for(std::vector<int>v;Q--;){
if(v.size()>L)v.erase(v.begin());
if(scanf("%d%d",&q,&x),!q)v.push_back(x);
if(q==1)v.erase(v.begin()+x-1);
if(q==2)printf("%d\n",v[x-1]);
if(q==3)v.erase(std::find(v.begin(),v.end(),x));
}
}
| 0 |
#include <bits/stdc++.h>
long long int gcd(long long int a, long long int b) {
return (b ? gcd(b, a % b) : a);
}
long long int P(long long int B, long long int power) {
long long int ans = 1;
while (power > 0) {
if (power % 2 == 1) {
ans = (ans * B) % 1000000007;
}
B = (B * B) % 1000000007;
power /= 2;
}
return ans;
}
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
long long int n, k;
cin >> n >> k;
string s;
cin >> s;
long long int temp = 0;
if (n == 1) {
if (k == 0) {
cout << s;
return 0;
}
cout << 0;
return 0;
}
while (k > 0) {
if (s[temp] != '1' && temp == 0) {
k--;
s[temp] = '1';
} else if (temp != 0) {
if (s[temp] != '0') {
k--;
s[temp] = '0';
}
}
temp++;
if (temp == n) {
break;
}
}
cout << s;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double vp, vd, t, f, c, accum = 0, time;
int res = 0;
cin >> vp >> vd >> t >> f >> c;
accum = t;
if (vd <= vp) {
cout << 0 << endl;
return 0;
}
while (accum * vp < c) {
time = vp * accum / (vd - vp);
if (vp * (accum + time) >= c) break;
res++;
accum += time * 2 + f;
}
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cur = 0;
string nextTok(string& s) {
string res = "";
while (s[cur] != '<') cur++;
cur++;
while (s[cur] != '>') res += s[cur++];
return res;
}
int main() {
string s, s1;
cin >> s;
while (!cin.eof()) {
cin >> s1;
s = s + s1;
}
vector<int> ans;
stack<int> rp;
int j = -1;
do {
string tok = nextTok(s);
if (tok == "td")
ans[j]++;
else if (tok == "table") {
ans.push_back(0);
rp.push(j);
j = ans.size() - 1;
} else if (tok == "/table") {
j = rp.top();
rp.pop();
}
} while (j != -1);
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int i, j, k;
int a[n];
for (i = 0; i < n; i++) cin >> a[i];
if (n == 1) {
cout << 0 << endl;
return 0;
} else if (n == 2) {
if (abs(a[0] - m) > abs(a[1] - m)) {
cout << abs(a[1] - m) << endl;
return 0;
} else {
cout << abs(a[0] - m) << endl;
return 0;
}
}
int minn1 = 10000000, minn2 = 10000000;
int maxx1 = -10000000, maxx2 = -10000000;
int left = 0, right = 0;
for (i = 0; i < n; i++) {
if (a[i] < minn1) {
minn2 = minn1;
minn1 = a[i];
} else if (a[i] < minn2) {
minn2 = a[i];
}
if (a[i] > maxx1) {
maxx2 = maxx1;
maxx1 = a[i];
} else if (a[i] > maxx2)
maxx2 = a[i];
if (a[i] > m)
right++;
else
left++;
}
if (left == 0) {
cout << abs(maxx2 - m) << endl;
return 0;
} else if (right == 0) {
cout << abs(minn2 - m) << endl;
return 0;
} else if (right == 1) {
swap(maxx1, minn1);
swap(maxx2, minn2);
int x = abs(maxx1 - m);
int y = abs(maxx2 - m) + 2 * abs(minn1 - m);
int z = abs(minn1 - m) + 2 * abs(maxx2 - m);
if (z < y) y = z;
if (x < y) y = x;
cout << y << endl;
return 0;
} else if (left == 1) {
int x = abs(maxx1 - m);
int y = abs(maxx2 - m) + 2 * abs(minn1 - m);
int z = abs(minn1 - m) + 2 * abs(maxx2 - m);
if (z < y) y = z;
if (x < y) y = x;
cout << y << endl;
return 0;
} else {
int x = abs(maxx1 - m) + 2 * abs(minn2 - m);
int y = abs(minn2 - m) + 2 * abs(maxx1 - m);
int x1 = abs(maxx2 - m) + 2 * abs(minn1 - m);
int y1 = abs(minn1 - m) + 2 * abs(maxx2 - m);
if (x > y) x = y;
if (x > x1) x = x1;
if (x > y1) x = y1;
cout << x << endl;
return 0;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
srand(130982);
int n;
int start;
int x;
cin >> n >> start >> x;
map<int, int> index_to_val;
vector<int> inds;
for (int i = 0; i < n / 50 + 3; i++) {
inds.push_back((start - 1 + 107 * i) % n + 1);
}
for (int i = 0; i < inds.size(); i++) {
cout << "? " << inds[i] << endl;
int val, scratch;
cin >> val >> scratch;
index_to_val[inds[i]] = val;
}
int max_b4_index = start;
int max_b4_value = index_to_val[start];
for (int i = 0; i < inds.size(); i++) {
if (index_to_val[inds[i]] > max_b4_value && index_to_val[inds[i]] < x) {
max_b4_index = inds[i];
max_b4_value = index_to_val[inds[i]];
}
}
int curr = max_b4_index;
int val = max_b4_value;
int queries = inds.size();
while (val < x) {
if (queries == 1999 || curr == -1) break;
cout << "? " << curr << endl;
cin >> val >> curr;
queries++;
}
if (val < x) val = -1;
cout << "! " << val << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void comeon() {
long long int weight, mass;
cin >> weight >> mass;
vector<long long int> weightbase;
while (mass) {
weightbase.push_back(mass % weight);
mass /= weight;
}
weightbase.push_back(0);
long long int size = weightbase.size();
for (int i = 0; i < size - 1; i++) {
if (abs(weightbase[i]) > 1) {
weightbase[i] -= weight;
weightbase[i + 1]++;
}
}
for (int i = 0; i < size; i++) {
if (abs(weightbase[i]) > 1) {
cout << "NO";
return;
}
}
cout << "YES";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int testcases = 1;
while (testcases--) {
comeon();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d;
cin >> n >> d;
long long arr[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long long ans[2 * n];
for (int i = 0; i < n; i++) {
ans[2 * i] = arr[i] - d;
ans[2 * i + 1] = arr[i] + d;
}
long long x = 1;
for (int i = 1; i < 2 * n; i++) {
if (ans[i] > ans[i - 1] && ans[i] <= ans[i + 1]) x++;
}
cout << x << endl;
}
| 1 |
#include <bits/stdc++.h>
const long long M = 1e9 + 7;
const double pi = 3.14159265358979323846;
using namespace std;
vector<long long> adj[100005];
long long vis[100005] = {0};
long long fact(long long x) {
if (x == 1 || x == 0)
return 1;
else
return ((x % M) * (fact(x - 1) % M)) % M;
}
long long factorial(long long n) {
const long long M = 1000000007;
long long f = 1;
for (long long i = 1; i <= n; i++) f = (f * i) % M;
return f;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
long long n, M;
cin >> n >> M;
char arr[n][M];
long long i1 = -1, i2 = -1;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < M; j++) {
cin >> arr[i][j];
if (arr[i][j] == 'S') {
i1 = i;
i2 = j;
}
}
}
string s;
cin >> s;
string temp = "DLRU";
long long cnt = 0;
do {
long long flag = 0;
long long a = i1, b = i2;
for (long long j = 0; j < s.size(); j++) {
long long p = (long long)s[j] - 48;
char ch = temp[p];
if (arr[a][b] == 'E') {
flag = 1;
break;
}
if (ch == 'U' && a - 1 >= 0 && arr[a - 1][b] != '#')
a -= 1;
else if (ch == 'D' && a + 1 < n && arr[a + 1][b] != '#')
a += 1;
else if (ch == 'L' && b - 1 >= 0 && arr[a][b - 1] != '#')
b -= 1;
else if (ch == 'R' && b + 1 < M && arr[a][b + 1] != '#')
b += 1;
else
break;
}
if (flag == 1) {
cnt++;
}
if (flag == 0 && arr[a][b] == 'E') cnt++;
} while (next_permutation(temp.begin(), temp.end()));
cout << (cnt) << "\n";
}
return 0;
}
| 2 |
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main() {
int n, m;
while(cin >> n >> m) {
if(n == 0 && m == 0) break;
vector<int> v(n+m);
rep(i,n) cin >> v[i];
rep(i,m) cin >> v[n+i];
sort(v.begin(), v.end());
v.insert(v.begin(), 0);
int ans = 0;
REP(i,1,v.size()) {
ans = max(ans, v[i] - v[i-1]);
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x;
double k;
string s;
vector<pair<string, int>> v;
map<string, int> mp;
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++) {
cin >> s >> x;
float ex = k * x;
if (ex >= 100) v.push_back(make_pair(s, ex)), mp[s] = 1;
}
for (int i = 0; i < m; i++) {
cin >> s;
if (mp[s] != 1) v.push_back(make_pair(s, 0));
}
sort(v.begin(), v.end());
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++)
cout << v[i].first << " " << v[i].second << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, s = 19, i, sum, j, l;
int main() {
cin >> n;
for (i = 1; i < n; i++) {
s += 9;
l = s;
while (l) {
sum += (l % 10);
l /= 10;
}
if (sum != 10) i--;
sum = 0;
}
cout << s;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
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;
}
const int mod = 1000000007;
const int N = 15;
void add(int& a, int b) {
a %= mod;
b %= mod;
a += b;
if (a >= mod) a -= mod;
}
int mul(int a, int b) { return int((1ll * a * b) % mod); }
struct MAT {
int a[N + 1][N + 1];
MAT() { memset((a), 0, sizeof(a)); }
MAT operator*(const MAT& ot) {
MAT ret;
for (int i = (0); i <= (N); i++)
for (int j = (0); j <= (N); j++)
for (int k = (0); k <= (N); k++)
add(ret.a[i][j], mul(a[i][k], ot.a[k][j]));
return ret;
}
};
MAT powmat(MAT a, long long b) {
MAT ret;
for (int i = (0); i <= (N); i++) ret.a[i][i] = 1;
while (b) {
if (b & 1) ret = ret * a;
b >>= 1;
a = a * a;
}
return ret;
}
int main() {
int n;
long long k;
gn(n);
gn(k);
MAT ans;
ans.a[0][0] = 1;
while (n--) {
long long A, B;
int c;
gn(A);
gn(B);
gn(c);
if (A > k) A = k;
if (B > k) B = k;
long long dis = B - A;
MAT b;
for (int i = (0); i <= (c); i++) {
b.a[i][i] = 1;
if (i - 1 >= 0) b.a[i - 1][i] = 1;
if (i + 1 <= c) b.a[i + 1][i] = 1;
}
ans = ans * powmat(b, dis);
}
cout << ans.a[0][0] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
template <typename _Tp>
void read(_Tp &x) {
char ch(getchar());
bool f(false);
while (!isdigit(ch)) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
template <typename _Tp>
inline _Tp max(_Tp a, _Tp b) {
return a < b ? b : a;
}
const int N = 100005;
int w[N], g[N], n, k;
long long sg[N], sw[N], A[N], B[N];
long long mb[N << 2], mc[N << 2], tag[N << 2];
inline void pushup(int x) {
mb[x] = max(mb[((x) << 1)], mb[((x) << 1 | 1)]),
mc[x] = max(mc[((x) << 1)], mc[((x) << 1 | 1)]);
}
void build(int l, int r, int cur) {
if (l == r) return mb[cur] = B[l], mc[cur] = B[l], void();
int mid = (l + r) >> 1;
build(l, mid, ((cur) << 1)), build(mid + 1, r, ((cur) << 1 | 1));
pushup(cur);
}
inline void upd(int cur, long long C) { tag[cur] += C, mc[cur] += C; }
inline void pushdown(int cur) {
if (tag[cur])
upd(((cur) << 1), tag[cur]), upd(((cur) << 1 | 1), tag[cur]), tag[cur] = 0;
}
void Update(int L, int R, int C, int l, int r, int cur) {
if (L <= l && r <= R) return upd(cur, C);
int mid = (l + r) >> 1;
pushdown(cur);
if (L <= mid) Update(L, R, C, l, mid, ((cur) << 1));
if (R > mid) Update(L, R, C, mid + 1, r, ((cur) << 1 | 1));
pushup(cur);
}
int _;
long long ask(int L, int R, long long mx, int l, int r, int cur) {
if (l == r) {
if (mb[cur] + k >= mx) _ = max(_, l);
return max(mx, mc[cur]);
}
int mid = (l + r) >> 1;
pushdown(cur);
if (L <= l && r <= R) {
long long tp = max(mx, mc[((cur) << 1)]);
mb[((cur) << 1 | 1)] + k >= tp ? ask(L, R, tp, mid + 1, r, ((cur) << 1 | 1))
: ask(L, R, mx, l, mid, ((cur) << 1));
return max(mx, mc[cur]);
}
if (L <= mid) mx = ask(L, R, mx, l, mid, ((cur) << 1));
if (R > mid) mx = ask(L, R, mx, mid + 1, r, ((cur) << 1 | 1));
return mx;
}
int st[N], top;
int main() {
read(n, k);
for (int i = 1; i < n; ++i) read(w[i]);
for (int i = 1; i <= n; ++i) read(g[i]);
for (int i = 1; i <= n; ++i)
sw[i] = sw[i - 1] - w[i], sg[i] = sg[i - 1] + g[i], A[i] = sg[i] + sw[i],
B[i] = sg[i] + sw[i - 1];
build(1, n, 1), st[0] = n + 1, st[top = 1] = n;
int ans = 0;
for (int l = n; l >= 1; --l) {
while (top && A[l - 1] <= A[st[top]]) {
if (top > 1) Update(st[top - 1], n, A[st[top - 1]] - A[st[top]], 1, n, 1);
--top;
}
if (top) Update(st[top], n, A[l - 1] - A[st[top]], 1, n, 1);
st[++top] = l - 1;
int L = 0, R = top, mid;
while (L < R)
mid = (L + R + 1) >> 1, A[l - 1] - A[st[mid]] > k ? L = mid : R = mid - 1;
_ = 0, ask(l, std::min(n, st[L]), -1e18, 1, n, 1),
ans = max(ans, _ - l + 1);
}
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, i, j, k, l, n, p, q;
string s1, s2;
cin >> a >> b;
if (a < b)
s1 = "Dasha", s2 = "Masha";
else {
c = a;
a = b;
b = c;
s2 = "Dasha", s1 = "Masha";
}
i = b;
j = a;
while (i % j) {
k = i % j;
i = j;
j = k;
}
c = a * b / j;
i = j = k = p = q = 0;
l = 1;
while (l * b <= c) {
k = l * b;
n = k / a;
if (k % a == 0) n--;
p += n * a - (l - 1) * b;
q += l * b - n * a;
l++;
}
if (p < q)
cout << s2;
else if (p > q)
cout << s1;
else
cout << "Equal";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, dsu[200069];
pair<long long, long long> ed[200069], as[200069];
long long fd(long long x) {
if (dsu[x] != x) {
dsu[x] = fd(dsu[x]);
}
return dsu[x];
}
int main() {
long long i, j, k, l, w, p, z = 0;
scanf("%lld%lld", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%lld%lld%lld", &k, &l, &w);
ed[i] = {k, l};
as[i] = {w, i};
}
sort(as + 1, as + m + 1);
for (i = 1; i <= n; i++) {
dsu[i] = i;
}
for (j = 0, i = 1; i <= m; i++) {
w = as[i].first;
p = as[i].second;
k = ed[p].first;
l = ed[p].second;
z -= fd(k) == fd(l);
if (i == m || w != as[i + 1].first) {
for (; j < i;) {
j++;
p = as[j].second;
k = ed[p].first;
l = ed[p].second;
z += fd(k) == fd(l);
dsu[fd(l)] = fd(k);
}
}
}
printf("%lld\n", z);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int num;
long long a;
int z;
} x[100010];
int n, k;
long long b;
bool cmp1(Node p, Node q) { return (p.a > q.a || p.a == q.a && p.num > q.num); }
bool cmp2(Node p, Node q) { return (p.z < q.z || p.z == q.z && p.num < q.num); }
int main() {
scanf("%d%d", &n, &k);
cin >> b;
for (int i = 1; i <= n; i++) {
x[i].num = i;
scanf("%d", &x[i].a);
x[i].z = 0;
}
sort(x + 1, x + n, cmp1);
long long bb = 0;
for (int i = 1; i < k; i++) {
bb += x[i].a;
x[i].z = 1;
}
if (bb + x[k].a <= b)
printf("%d\n", n);
else {
for (int i = 1; i < k; i++)
if (bb + x[k].a > b) x[i].z = 0;
for (int i = k; i <= n; i++)
if (bb + x[i].a <= b) x[i].z = 1;
sort(x + 1, x + n + 1, cmp2);
printf("%d\n", x[1].num);
}
return 0;
}
| 2 |
//By Vlgd
#include<bits/stdc++.h>
using namespace std;
const int K=10001;
int A[2000001],C[2000001];
int buck[K]={0};
int main(){
int n;scanf("%d",&n);
for (int i=0;i<n;i++){
scanf("%d",&A[i]);
buck[A[i]]++;
}
for (int i=1;i<=K;i++) buck[i]+=buck[i-1];
for (int i=n-1;i>=0;i--){
C[buck[A[i]]]=A[i];
buck[A[i]]--;
}
for (int i=1;i<=n;i++){
printf("%d",C[i]);
if (i!=n) printf(" ");
else printf("\n");
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template <typename T, typename... Args>
void DBG(T first, Args... args) {
cerr << first;
if (sizeof...(args)) cerr << ", ";
DBG(args...);
}
const long long N = 1e5 + 10;
vector<long long> pref(N);
long long sum(long long l, long long r) {
if (l < 0 || r < 0 || l > r) return 0;
return pref[r] - (l - 1 >= 0 ? pref[l - 1] : 0);
}
void solve() {
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
for (long long i = 0; i < n; i++) {
pref[i] = (i - 1 >= 0 ? pref[i - 1] : 0) + a[i];
}
long long s = 0;
long long max_sum = 0;
for (long long i = 0; i < n; i++) {
if (s + a[i] > 0) {
s += a[i];
} else {
s = 0;
}
max_sum = max(max_sum, s);
}
cout << 2 * max_sum - sum(0, n - 1) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long t = 1;
for (long long tt = 1; tt <= t; tt++) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
queue<int> x[20];
int a[1 << 8];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
for(int i = 0; i < M; i++) {
int c;
cin >> c;
a[c] = 1;
}
for(int i = 0; i < N; i++) {
x[0].push(i);
}
int ans = 0;
for(int i = 0; ; i++) {
if(x[i].size() == 1) break;
while(x[i].size()) {
int x1, x2;
x1 = x[i].front();
x[i].pop();
if(x[i].size() == 0) {
x[i + 1].push(x1);
break;
}
x2 = x[i].front();
x[i].pop();
if(!a[x1] && !a[x2]) ans++;
if(a[x1]) {
x[i + 1].push(x2);
}
else {
x[i + 1].push(x1);
}
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
if (a[i] != a[i - 1]) {
swap(a[i], a[i - 1]);
int cnt1 = n - 1, cnt2 = n - 1;
for (int j = 1; j < n; j++) {
if (a[j] <= a[j - 1]) cnt1--;
if (a[j] >= a[j - 1]) cnt2--;
}
if (cnt1 && cnt2) {
cout << i << " " << i + 1 << endl;
return 0;
}
swap(a[i], a[i - 1]);
}
}
cout << -1 << endl;
return 0;
}
| 2 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N=3000005;
const int mod=1000000007;
int ans,sum,s1,s2,s;
int x[10],y[10],fac[N],inv[N];
void add(int &x,int y){
(y>=mod)?y-=mod:0;
(y<0)?y+=mod:0;
x+=y;
(x>=mod)?x-=mod:0;
(x<0)?x+=mod:0;
}
int quickpow(int x,int y){
int s=1;
for (;y;y>>=1,x=1ll*x*x%mod)
if (y&1) s=1ll*s*x%mod;
return s;
}
void init(){
int i;
fac[0]=fac[1]=inv[0]=inv[1]=1;
for (i=2;i<=N-5;i++) fac[i]=1ll*fac[i-1]*i%mod;
inv[N-5]=quickpow(fac[N-5],mod-2);
for (i=N-6;i;i--) inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
int C(int x,int y){
if (x<y) return 0;
return 1ll*fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int calc(int x1,int y1,int x2,int y2){
int s=0,i,j;
//for (i=x1;i<=x2;i++)
// for (j=y1;j<=y2;j++) add(s,C(i+j,j));
s=(C(x2+1+y2+1,y2+1)+C(x1+y1,y1)-C(x1+y2+1,y2+1)-C(x2+1+y1,y1))%mod;
(s<0)?s+=mod:0;
return s;
}
int main(){
int i;
for (i=1;i<=6;i++) scanf("%d",&x[i]);
for (i=1;i<=6;i++) scanf("%d",&y[i]);
init();
for (i=x[3];i<=x[4];i++){
s1=calc(i-x[2],y[4]-y[2],i-x[1],y[4]-y[1]);
s2=calc(x[5]-i,y[5]-y[4]-1,x[6]-i,y[6]-y[4]-1);
s=1ll*s1*s2%mod;
add(sum,s);
add(ans,-1ll*s*(x[4]-i)%mod);
s1=calc(i-x[2],y[3]-1-y[2],i-x[1],y[3]-y[1]-1);
s2=calc(x[5]-i,y[5]-y[3],x[6]-i,y[6]-y[3]);
s=1ll*s1*s2%mod;
//add(sum,s);
add(ans,-1ll*s*(i-x[3])%mod);
}
for (i=y[3];i<=y[4];i++){
s1=calc(x[3]-1-x[2],i-y[2],x[3]-1-x[1],i-y[1]);
s2=calc(x[5]-x[3],y[5]-i,x[6]-x[3],y[6]-i);
s=1ll*s1*s2%mod;
//add(sum,s);
add(ans,-1ll*s*(i-y[3])%mod);
s1=calc(x[4]-x[2],i-y[2],x[4]-x[1],i-y[1]);
s2=calc(x[5]-1-x[4],y[5]-i,x[6]-x[4]-1,y[6]-i);
s=1ll*s1*s2%mod;
add(sum,s);
add(ans,-1ll*s*(y[4]-i)%mod);
}
add(ans,1ll*sum*(y[4]-y[3]+x[4]-x[3]+1)%mod);
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
gn(first);
gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
gn(first);
gn(second);
gn(z);
}
template <class T>
inline void print(T first) {
if (first < 0) {
putchar('-');
return print(-first);
}
if (first < 10) {
putchar('0' + first);
return;
}
print(first / 10);
putchar(first % 10 + '0');
}
template <class T>
inline void printsp(T first) {
print(first);
putchar(' ');
}
template <class T>
inline void println(T first) {
print(first);
putchar('\n');
}
template <class T, class U>
inline void print(T first, U second) {
printsp(first);
println(second);
}
template <class T, class U, class V>
inline void print(T first, U second, V z) {
printsp(first);
printsp(second);
println(z);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
char s[211111];
int vst[33];
int tmp[33] =
{
0,
},
tt[33] = {
0,
};
int main() {
int n;
gn(n);
for (int i = 0; i < 26; i++) vst[i] = 1;
char c;
int ans = 0, cc = 26;
while (n--) {
scanf(" %c %s", &c, s);
int len = strlen(s);
if (c == '.') {
for (int i = 0; i < len; i++) {
int u = s[i] - 'a';
if (vst[u]) vst[u] = 0, cc--;
}
continue;
}
if (c == '!') {
if (cc == 1) {
ans++;
continue;
}
int cnt = 0;
for (int i = 0; i < 26; i++) tt[i] = 0;
for (int i = 0; i < len; i++) {
int u = s[i] - 'a';
if (vst[u]) tt[u] = 1;
}
cc = 0;
for (int i = 0; i < 26; i++) vst[i] = tt[i], cc += vst[i];
continue;
}
if (c == '?') {
int u = s[0] - 'a';
if (vst[u]) {
if (cc == 1) continue;
cc--;
vst[u] = 0;
continue;
}
if (cc == 1) ans++;
}
}
println(ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int N, M, H, T;
vector<pair<int, int> > kan;
vector<int> adj[100000];
bool nahm[100000];
bool hat(int a, int b) {
for (typeof((adj[a]).begin()) it = (adj[a]).begin(); it != (adj[a]).end();
it++)
if (*it == b) return true;
return false;
}
int main() {
scanf("%d%d%d%d", &N, &M, &H, &T);
for (int i = 0; i < (M); i++) {
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
kan.push_back(pair<int, int>(a, b));
kan.push_back(pair<int, int>(b, a));
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 0; i < (N); i++) {
sort(adj[i].begin(), adj[i].end());
nahm[i] = false;
}
for (typeof((kan).begin()) it = (kan).begin(); it != (kan).end(); it++) {
int u = it->first, v = it->second;
if ((int)adj[u].size() <= H || (int)adj[v].size() <= T) continue;
bool ok;
if ((int)adj[u].size() >= H + T + 2 || (int)adj[v].size() >= H + T + 2)
ok = true;
else {
int gem = 0;
int ia = 0, ib = 0;
while (ia < (int)adj[u].size() && ib < (int)adj[v].size()) {
if (adj[u][ia] < adj[v][ib])
ia++;
else if (adj[u][ia] > adj[v][ib])
ib++;
else {
gem++;
ia++;
ib++;
}
}
ok = ((int)adj[u].size() + (int)adj[v].size() - gem >= H + T + 2);
}
if (ok) {
printf("YES\n%d %d\n", u + 1, v + 1);
nahm[u] = nahm[v] = true;
for (typeof((adj[u]).begin()) ai = (adj[u]).begin(); ai != (adj[u]).end();
ai++) {
if (!H) break;
if (!hat(v, *ai) && !nahm[*ai]) {
nahm[*ai] = true;
printf("%d", (*ai) + 1);
H--;
if (H) printf(" ");
}
}
for (typeof((adj[u]).begin()) ai = (adj[u]).begin(); ai != (adj[u]).end();
ai++) {
if (!H) break;
if (!nahm[*ai]) {
nahm[*ai] = true;
printf("%d", (*ai) + 1);
H--;
if (H) printf(" ");
}
}
printf("\n");
for (typeof((adj[v]).begin()) ai = (adj[v]).begin(); ai != (adj[v]).end();
ai++) {
if (!T) break;
if (!nahm[*ai]) {
nahm[*ai] = true;
printf("%d", (*ai) + 1);
T--;
if (T) printf(" ");
}
}
printf("\n");
return 0;
}
}
printf("NO\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 900;
struct edge {
int v, w, b;
double c;
};
vector<edge> adj[maxn];
int n, k;
int s, t;
double dis[maxn];
int bk[maxn], bki[maxn];
bool inq[maxn];
long long x[maxn], y[maxn];
inline void ae(int u, int v, int w, double c) {
edge nu, nv;
nu.v = v;
nu.w = w;
nu.c = c;
nu.b = adj[v].size();
nv.v = u;
nv.w = 0;
nv.c = -c;
nv.b = adj[u].size();
adj[u].push_back(nu);
adj[v].push_back(nv);
}
inline pair<int, double> spfa() {
for (int i = 0; i < maxn; i++) dis[i] = 1e30;
memset(inq, 0, sizeof(inq));
dis[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = 0;
for (int i = adj[u].size() - 1; i >= 0; i--) {
int v = adj[u][i].v;
int w = adj[u][i].w;
double c = adj[u][i].c;
if (dis[u] + c < dis[v] && w != 0) {
dis[v] = dis[u] + c;
bk[v] = u;
bki[v] = i;
if (!inq[v]) {
q.push(v);
inq[v] = 1;
}
}
}
}
if (dis[t] > 1e20) return make_pair(-1, 0.0);
int cur = t;
int flw = 0x3f3f3f3f;
while (cur != s) {
flw = min(flw, adj[bk[cur]][bki[cur]].w);
cur = bk[cur];
}
cur = t;
double cst = 0;
while (cur != s) {
adj[bk[cur]][bki[cur]].w -= flw;
adj[cur][adj[bk[cur]][bki[cur]].b].w += flw;
cst += flw * adj[bk[cur]][bki[cur]].c;
cur = bk[cur];
}
return make_pair(flw, cst);
}
inline pair<int, double> ek() {
pair<int, double> ans = make_pair(0ll, 0.0);
while (1) {
pair<int, double> cr = spfa();
if (cr.first <= 0) return ans;
ans.first += cr.first;
ans.second += cr.second;
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
}
s = n + n + 1;
t = n + n + 3;
for (int i = 1; i <= n; i++) {
ae(s, i, 2, 0.0);
ae(i + n, t, 1, 0.0);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) continue;
if (y[i] > y[j])
ae(i, j + n, 1,
sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])));
}
}
pair<int, double> ans = ek();
if (ans.first != n - 1)
cout << -1;
else
printf("%.15lf", ans.second);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int prime100[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
template <typename T>
inline bool isLeap(T y) {
return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false);
}
template <typename T>
inline T BIGMOD(T A, T B, T M = 1000000007) {
T ret = 1;
while (B) {
if (B & 1) ret = (ret * A) % M;
A = (A * A) % M;
B = B >> 1;
}
return ret;
}
template <typename T>
inline T BigMod(T A, T B, T M) {
T ret = 1;
while (B) {
if (B & 1) ret = (ret * A) % M;
A = (A * A) % M;
B = B >> 1;
}
return ret;
}
long long int MySqrt(long long int n) {
long long int p = sqrt(n);
if ((p + 1) * (p + 1) <= n)
return p + 1;
else if (p * p <= n)
return p;
else
return p - 1;
}
long long int MyPow(long long int x, long long int n) {
if (n == 0)
return 1;
else if (n % 2 == 0)
return MyPow(x * x, n / 2);
else
return x * MyPow(x * x, ((n - 1) / 2));
}
long long int modInverse(long long int n) {
return BIGMOD(n, (long long int)1000000007 - 2) % 1000000007;
}
int bye() { return 0; }
int main() {
int t;
cin >> t;
long long int second, a, b, c;
for (int i = 1; i <= t; i++) {
cin >> second >> a >> b >> c;
long long int x = second / c;
long long int y = x / a;
long long int z = y * b;
cout << x + z << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 1005;
int n, k;
long long int a[maxn], dp[maxn][maxn] = {0};
long long int answer(int x) {
dp[0][0] = 1;
int li = 0;
for (int i = 1; i <= n; i++) {
dp[i][0] = 1;
while (a[li] + x <= a[i]) li++;
if (li != 0) li--;
for (int j = 1; j <= i && j <= k; j++) {
dp[i][j] = dp[i - 1][j] + dp[li][j - 1];
dp[i][j] %= mod;
}
}
return dp[n][k];
}
int main() {
long long int ans = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
sort(a + 1, a + n + 1);
for (int i = 1; i * (k - 1) <= a[n]; i++) {
ans += answer(i);
ans %= mod;
}
printf("%I64d", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a[10][10];
int main(void) {
long long n, m, row = 0, col = 0;
bool x = true;
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] == 'S') {
x = false;
}
}
if (x == true) {
row++;
} else
x = true;
}
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (a[i][j] == 'S') {
x = false;
}
}
if (x == true) {
col++;
} else
x = true;
}
if (row == 0)
cout << col * n << endl;
else if (col == m && row == n)
cout << n * m << endl;
else
cout << col * n + row * (m - col) << endl;
return 0;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.