solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
struct node {
int x, y;
} p[MAXN];
vector<int> g[MAXN];
int n;
int rt[MAXN], cnt, ls[MAXN * 80], rs[MAXN * 80], sum[MAXN * 80];
void modify(int &x, int y, int l, int r, int pos) {
x = ++cnt;
if (l == r) {
sum[x] = 1;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
modify(ls[x], ls[y], l, mid, pos), rs[x] = rs[y];
else
modify(rs[x], rs[y], mid + 1, r, pos), ls[x] = ls[y];
sum[x] = sum[ls[x]] + sum[rs[x]];
}
int query(int p, int l, int r, int ql, int qr) {
if (!p) return 0;
if (ql <= l && qr >= r) return sum[p];
int mid = (l + r) >> 1, res = 0;
if (ql <= mid) res += query(ls[p], l, mid, ql, qr);
if (qr > mid) res += query(rs[p], mid + 1, r, ql, qr);
return res;
}
int main() {
ios::sync_with_stdio(0);
cin >> n;
vector<int> vx, vy;
for (int i = 1; i <= n; ++i) {
cin >> p[i].x >> p[i].y;
vx.push_back(p[i].x);
vy.push_back(p[i].y);
}
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
vy.erase(unique(vy.begin(), vy.end()), vy.end());
int X = vx.size(), Y = vy.size();
for (int i = 1; i <= n; ++i) {
p[i].x = lower_bound(vx.begin(), vx.end(), p[i].x) - vx.begin() + 1;
p[i].y = lower_bound(vy.begin(), vy.end(), p[i].y) - vy.begin() + 1;
g[p[i].y].push_back(p[i].x);
}
for (int i = Y; i >= 1; --i) {
rt[i] = rt[i + 1];
for (int j = 0; j < (int)g[i].size(); ++j) {
modify(rt[i], rt[i], 1, X, g[i][j]);
}
}
long long ans = 0;
for (int i = 1; i <= Y; ++i) {
g[i].push_back(0);
sort(g[i].begin(), g[i].end());
for (int j = 1; j < (int)g[i].size(); ++j) {
ans += (long long)query(rt[i], 1, X, g[i][j - 1] + 1, g[i][j]) *
(long long)query(rt[i], 1, X, g[i][j], X);
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long BigMod(long long B, long long P, long long M) {
long long R = 1;
while (P > 0) {
if (P % 2 == 1) {
R = (R * B) % M;
}
P /= 2;
B = (B * B) % M;
}
return R;
}
int main() {
long long n, m, k, k1, k2, first, second;
cin >> n >> m;
cin >> k1 >> k2;
vector<long long> g, b;
for (int i = 0; i < n; i++) {
cin >> k;
b.push_back(k);
}
for (int i = 0; i < m; i++) {
cin >> k;
g.push_back(k);
}
int c = 0;
sort(b.begin(), b.end());
sort(g.rbegin(), g.rend());
first = b[k1 - 1];
second = g[k2 - 1];
if (second > first)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
bool comp(pair<ii, int> a, pair<ii, int> b) {
return a.first.first + a.first.second > b.first.first + b.first.second;
}
int main() {
int n; cin >> n;
pair<ii, int> arr[n];
for(int i = 0; i < n; ++i) {
int w, s, v; cin >> w >> s >> v;
arr[i] = pair<ii, int>(ii(w, s), v);
}
sort(arr, arr + n, comp);
vector<long long> dp(20004);
for(int i = n-1; i >= 0; --i) {
for(int j = min(arr[i].first.second, 20001 - arr[i].first.first); j >= 0; --j) {
dp[j + arr[i].first.first] = max(dp[ j + arr[i].first.first] , dp[j] + arr[i].second);
}
}
long long ans = 0;
for(int i = 0; i <= 20001; ++i) {
ans = max(ans, dp[i]);
}
cout << ans;
} | 0 |
#include "bits/stdc++.h"
using namespace std;
int main() {
int N;
cin >> N;
if (N <= 2) cout << (N == 1) + 1 << endl;
else cout << 0 << endl;
}
| 0 |
#include <bits/stdc++.h>
int b[100001];
int main() {
int n, k, i, a;
scanf("%d %d", &n, &k);
for (i = 1; i <= n; i++) {
scanf("%d", &a);
b[(i % k)] += a;
}
int min = 0xFFFFFFF, tmp;
for (i = 0; i < k; i++) {
if (b[i] < min) {
min = b[i];
if (i == 0)
tmp = k;
else
tmp = i;
} else if (b[i] == min)
if (i < tmp) tmp = i;
}
printf("%d", tmp);
return 0;
}
| 2 |
#include<iostream>
#include<algorithm>
using namespace std;
int N;
int A[15][15];
int main()
{
cin>>N;
for(int i=0;i<N;i++)
{
int a;cin>>a;
for(int j=0;j<a;j++)
{
int x,y;cin>>x>>y;
x--;
A[i][x]=y==1?1:-1;
}
}
int ans=0;
for(int i=1;i<1<<N;i++)
{
int c[15]={};
bool flag=true;
for(int j=0;j<N;j++)
{
if(!(i>>j&1))continue;
for(int k=0;k<N;k++)
{
if(A[j][k]==0)continue;
if(c[k]==0)c[k]=A[j][k];
else if(c[k]!=A[j][k])
{
flag=false;
}
}
}
for(int j=0;j<N;j++)
{
if((i>>j&1)&&c[j]==-1||!(i>>j&1)&&c[j]==1)flag=false;
}
if(flag)ans=max(ans,__builtin_popcount(i));
}
cout<<ans<<endl;
}
| 0 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);
using namespace std;
namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;
template<class T> inline int num(const map<T, int> &m, T k){
auto it = m.find(k);
if(it == m.end()) return 0;
return it->second;
}
int main(){
cin.tie(0); cin.sync_with_stdio(0);
int n, m, X, Y;
cin >> n >> m >> X >> Y;
vi v(n + m); rep(i, n + m) cin >> v[i];
ll ans = 0;
set<int> lens;
map<int,int> cnt;
map<pi,int> cnt2;
set<tuple<int,int,int>> vis;
rep(i, n){
if(i < m){
int a = v[i] - v[i + n], b = v[i + n];
lens.insert(a);
lens.insert(b);
++cnt2[pi(min(a, b), max(a, b))];
}
else{
++cnt[v[i]];
lens.insert(v[i]);
}
}
if(X == 0){
vector<pi> vs;
for(auto i : cnt) if(i.second >= 4) vs.emplace_back(i.first, i.second);
rep(i, vs.size()){
if(vs[i].second >= 12) ans++;
if(vs[i].second >= 8) ans += (int)vs.size() - i - 1;
for(int j = i + 1; j < vs.size(); j++){
if(vs[j].second >= 8) ans++;
ans += (int)vs.size() - j - 1;
}
}
cout << ans << endl;
return 0;
}
for(auto i : cnt2) for(int j : lens){
vi l = {i.first.first, i.first.second, j};
sort(all(l));
if(vis.count(tie(l[0], l[1], l[2]))) continue;
if(l[0] == l[1] && l[1] == l[2]){
int x = min(X, num(cnt2, pi(l[0], l[1])));
int y = num(cnt, l[0]);
if(x == X && 2 * x + y >= 12) vis.emplace(l[0], l[1], l[2]);
}
else if(l[0] == l[1]){
int x0 = min(X, num(cnt2, pi(l[0], l[1])));
int x1 = min(X, num(cnt2, pi(l[0], l[2])));
bool ok = 0;
rep(i, x0+1){
if(X - i > x1) continue;
int a = 2 * i + X - i, c = X - i;
if(a > 8 || c > 4) continue;
if(a + num(cnt, l[0]) >= 8 &&
c + num(cnt, l[2]) >= 4){
ok = 1;
break;
}
}
if(ok) vis.emplace(l[0], l[1], l[2]);
}
else if(l[1] == l[2]){
int x0 = min(X, num(cnt2, pi(l[0], l[1])));
int x1 = min(X, num(cnt2, pi(l[1], l[2])));
bool ok = 0;
rep(i, x0+1){
if(X - i > x1) continue;
int a = i, c = i + 2*(X - i);
if(a > 4 || c > 8) continue;
if(a + num(cnt, l[0]) >= 4 &&
c + num(cnt, l[2]) >= 8){
ok = 1;
break;
}
}
if(ok) vis.emplace(l[0], l[1], l[2]);
}
else{
int x0 = min(X, num(cnt2, pi(l[0], l[1])));
int x1 = min(X, num(cnt2, pi(l[1], l[2])));
int x2 = min(X, num(cnt2, pi(l[0], l[2])));
bool ok = 0;
rep(i, x0+1) rep(j, x1+1){
if(X - i - j > x2 || i + j > X) continue;
int a = i + (X - i - j), b = i + j, c = j + (X - i - j);
if(a > 4 || b > 4 || c > 4) continue;
if(a + num(cnt, l[0]) >= 4 &&
b + num(cnt, l[1]) >= 4 &&
c + num(cnt, l[2]) >= 4){
ok = 1;
break;
}
}
if(ok) vis.emplace(l[0], l[1], l[2]);
}
}
cout << vis.size() << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, x, t;
cin >> t;
while (t--) {
cin >> n >> x;
cout << 2 * x << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct Item {
long long sum;
long long row;
long long column;
};
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
vector<Item> titems;
for (int i = 1; i <= n; i++) {
int number;
cin >> number;
long long sum = 0;
vector<long long> items;
for (int j = 1; j <= number; j++) {
long long inp;
cin >> inp;
sum += inp;
items.push_back(inp);
}
for (int j = 0; j < (int)items.size(); j++) {
long long s = sum - items[j];
Item _item;
_item.row = i;
_item.column = j + 1;
_item.sum = s;
titems.push_back(_item);
}
}
sort(titems.begin(), titems.end(),
[](Item &lhs, Item &rhs) { return lhs.sum < rhs.sum; });
for (int i = 1; i < (int)titems.size(); i++) {
if (titems[i].sum == titems[i - 1].sum &&
titems[i].row != titems[i - 1].row) {
cout << "YES" << endl;
cout << titems[i - 1].row << " " << titems[i - 1].column << endl;
cout << titems[i].row << " " << titems[i].column << endl;
goto pause;
}
}
cout << "NO" << endl;
pause:
int pause;
cin >> pause;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e1;
long long n, m, ans, dp[1 << MAXN][MAXN];
bool edge[MAXN][MAXN];
vector<long long> adj[MAXN];
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (long long u, v, i = 1; i <= m; i++) {
cin >> v >> u;
v--, u--;
adj[v].push_back(u);
adj[u].push_back(v);
edge[v][u] = edge[u][v] = true;
}
for (long long i = 1; i < (1 << n); i++) {
long long mx = 0, cnt = 0;
for (long long j = 0; j < n; j++)
if ((1 << j) & i) mx = j, cnt++;
if (cnt == 1) {
dp[i][mx] = 1;
continue;
}
for (long long j = 0; j < n; j++)
if ((1 << j) & i and j != mx)
for (long long u : adj[j]) dp[i][j] += dp[i ^ (1 << j)][u];
if (cnt < 3) continue;
for (long long j = 0; j < n; j++)
if (edge[j][mx]) ans += dp[i][j];
}
cout << (ans >> 1);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N = 100005;
set<int> g[N];
int p[N];
int num = 0;
int find(int x){
return x == p[x] ? x : p[x]=find(p[x]);
}
void merge(int u, int v){
u = find(u), v = find(v);
if (u == v) return;
if (g[u].size() < g[v].size()) swap(u,v);
//printf("merge %d to %d\n",v,u);
set<ii> que;
for (auto &x: g[v]){
if (x == u) continue;
//printf("%d connected to %d\n",x,v);
g[x].erase(v);
if (g[x].count(u)){
que.insert({x,u});
}
g[u].insert(x);
g[x].insert(u);
}
g[u].erase(v);
g[v].clear();
p[v] = u;
for (auto cur : que){
merge(cur.first,cur.second);
}
}
int main(){
int n;
scanf("%d",&n);
iota(p,p+N,0);
for (int i = 1; i < n; i++){
int a,b;
scanf("%d%d",&a,&b);
g[a].insert(b);
g[b].insert(a);
}
for (int i = 1; i < n; i++){
int a,b;
scanf("%d%d",&a,&b);
a = find(a); b = find(b);
if (g[a].count(b)) merge(a,b);
else{
g[a].insert(b);
g[b].insert(a);
}
}
int c = find(1);
for (int i = 2; i <= n; i++){
if (c != find(i)){
printf("NO");
return 0;
}
}
printf("YES");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int cal() {
int l = 0;
int r = 0;
int h = 0;
int c;
for (int i = 0; i < n; i++) {
if (s[i] == '(') l++;
if (s[i] == ')') r++;
if (s[i] == '#') h++;
}
if (r >= l) return 0;
if (l < r + h) return 0;
c = l - r;
l = 0;
r = 0;
int th = 0;
int cc = c;
for (int i = 0; i < n; i++) {
if (s[i] == '(') l++;
if (s[i] == ')') r++;
if (s[i] == '#') {
th++;
if (th == h) {
r = r + cc;
} else {
r++;
cc--;
}
}
if (r > l) return 0;
}
if (r != l) return 0;
for (int i = 1; i <= h; i++) {
if (i == h) {
cout << c << endl;
} else {
cout << 1 << endl;
c--;
}
}
return 1;
}
int main() {
cin >> s;
n = s.length();
if (cal() == 0) cout << -1 << endl;
return 0;
}
| 1 |
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int s,n,t,p,m;
string w,ti;
int can[11000];
const int halfday = 720;
const int week = 10080;
int main(){
while(cin >> s >> n >> t >> w >> ti >> p >> m,w!="None"){
for(int i=0;i<week;i++)can[i] = false;
int loop = (w == "All")?7:1;
if(ti == "All"){
for(int i=0;i<loop;i++){
for(int j=0;j<2*halfday;j++)can[i*2*halfday+j] = true;
}
}else if(ti == "Day"){
for(int i=0;i<loop;i++){
for(int j=0;j<halfday;j++){
can[i*2*halfday + j + halfday/2] = true;
}
}
}else{
for(int i=0;i<loop;i++){
for(int j=0;j<halfday/2;j++){
can[i*2*halfday + j] = true;
can[i*2*halfday + j + 3*halfday/2] = true;
}
}
}
int res = 0;
//int lim = (w=="All")?2*halfday:week;
for(int start = 0;start < 2*halfday; start++){
int now = start;
int cnt = 0;
for(int i=0;i<m;i++){
if(can[now%week] && can[(now+s)%week])cnt++;
now += t;
}
res = max(res,cnt);
}
printf("%.10lf\n",1-pow(1-1.0/p,n*res));
}
} | 0 |
#include<stdio.h>
#include<string.h>
int main()
{
char a[51], b[51];
scanf("%s %s", a,b);
int i,l=strlen(b);
for(i=0; i<l; i++)
printf("%c%c", a[i], b[i]);
if(strlen(a)==l+1)
printf("%c", a[i]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cout << fixed;
cout.precision(10);
int t = 1;
for (int i = 1; i <= t; ++i) {
solve();
}
return 0;
}
vector<int> a;
vector<int> used;
void go(int v) {
used[v] = 1;
if (v == a.size()) return;
int to = v + a[v];
if (!used[to]) {
go(to);
}
}
void solve() {
int n;
cin >> n;
used.resize(n, 0);
int t;
cin >> t;
--t;
a.resize(n - 1);
for (int i = 0; i < n - 1; ++i) {
cin >> a[i];
}
go(0);
if (used[t]) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
| 1 |
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
double N,P=0;
double x,y;
double p=0,P1=0;
x=0;y=0;
while(1)
{
scanf("%lf,%lf",&N,&P1);
if((int)N==0&&(int)P1==0)break;
x+=N*sin(P*M_PI/180);y+=N*cos(P*M_PI/180);
P+=P1;
}
int X,Y;
X=(int)x;Y=(int)y;
printf("%d\n",X);printf("%d\n",Y);
return 0;
} | 0 |
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
vector<T> compressed(vector<T> v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v;
}
int main() {
int n; cin >> n;
vector<int> a(n);
for (int &ai: a) cin >> ai;
int i = 0; for (int x: compressed(a)) cout << (i++ ? " " : "") << x;
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
const int prime[25] = {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};
const int modnum[5] = {223092870, 58642669, 8965109, 27433619, 716539};
int n, d, m, len;
long long a[maxn];
int bit[17], capa[17];
vector<int> f[5][17];
long long c;
char s[40];
int num[40];
int calc() {
int res = 0;
for (int i = 0; i < len; ++i) res = res * (d + 1) + num[i];
return res;
}
int dp(int m, int v) {
if (f[m][d][v]) return f[m][d][v];
for (int i = 0; i < len; ++i)
if (num[i] == d) {
f[m][d][v] = 1;
for (int j = 0; j < d; ++j) {
num[i] = j;
f[m][d][v] = ((long long)dp(m, calc()) * f[m][d][v]) % modnum[m];
}
num[i] = d;
return f[m][d][v];
}
int p = 0;
for (int i = 0; i < len; ++i) p = p * d + num[i];
return f[m][d][v] = p < n ? a[p] % modnum[m] : 1;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%I64d", &a[i]);
for (d = 2; d <= 16; ++d) {
bit[d] = 0;
capa[d] = 1;
for (int i = 1; i <= n; i *= d) ++bit[d], capa[d] *= d + 1;
for (int i = 0; i < 5; ++i) f[i][d].resize(capa[d], 0);
}
scanf("%d", &m);
while (m--) {
scanf("%d%s%I64d", &d, s, &c);
len = strlen(s);
bool noans = false;
char *ns = s;
if (len > bit[d]) {
for (int i = 0; i < len - bit[d]; ++i)
if (s[i] != '0' && s[i] != '?') noans = true;
ns += len - bit[d];
len = bit[d];
}
for (int i = 0; i < len; ++i)
if (ns[i] == '?')
num[i] = d;
else if (ns[i] >= '0' && ns[i] <= '9')
num[i] = ns[i] - '0';
else
num[i] = ns[i] - 'A' + 10;
int ans = -1;
if (!noans)
for (int i = 0, pp = 0; i < 5 && ans == -1; ++i) {
int res = dp(i, calc()) % modnum[i] + c % modnum[i];
while (pp < 25 && modnum[i] % prime[pp] == 0) {
if (res % prime[pp] == 0) {
ans = prime[pp];
break;
}
++pp;
}
}
printf("%d\n", ans);
}
return 0;
}
| 5 |
#include <iostream>
int main()
{
std::string S;
std::cin >> S;
S.erase(S.end() - 8, S.end());
std::cout << S << std::endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 140140;
struct Node {
long long x[10];
char nxt[10];
bool need;
} a[MX * 10];
int b[MX], mx, n, m, i, t, l, r, xfr, y;
long long j, res, cur[10], scur[10];
void init(int i, int l, int r) {
for (int j = 0; j < 10; j++) a[i].nxt[j] = j;
if (l == r) {
b[l] = i;
return;
}
init(i * 2, l, (l + r) / 2);
init(i * 2 + 1, (l + r) / 2 + 1, r);
}
void recount(int i, int l, int r) {
if (l == r) return;
recount(i * 2, l, (l + r) / 2);
recount(i * 2 + 1, (l + r) / 2 + 1, r);
for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j];
}
void psh(int i, int L, int R) {
if (L != R)
for (int k = i * 2; k <= i * 2 + 1; k++) {
for (int j = 0; j < 10; j++) cur[j] = 0;
for (int j = 0; j < 10; j++) cur[a[i].nxt[j]] += a[k].x[j];
for (int j = 0; j < 10; j++) {
a[k].x[j] = cur[j];
a[k].nxt[j] = a[i].nxt[a[k].nxt[j]];
a[k].need |= (a[k].nxt[j] != j);
}
}
a[i].need = false;
for (int j = 0; j < 10; j++) a[i].nxt[j] = j;
}
void modify(int i, int L, int R, int l, int r) {
if (a[i].need) psh(i, L, R);
if (L == l && R == r) {
if (a[i].x[xfr] > 0 && xfr != y) {
a[i].x[y] += a[i].x[xfr];
a[i].x[xfr] = 0;
for (int j = 0; j < 10; j++)
if (a[i].nxt[j] == xfr) {
a[i].nxt[j] = y;
a[i].need |= (L < R && j != y);
}
}
return;
}
int h = (L + R) / 2;
if (l <= h) modify(i * 2, L, h, l, min(h, r));
if (r > h) modify(i * 2 + 1, h + 1, R, max(h + 1, l), r);
for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j];
}
void fsum(int i, int L, int R, int l, int r) {
if (a[i].need > 0) psh(i, L, R);
if (L == l && R == r) {
for (int j = 0; j < 10; j++) scur[j] += a[i].x[j];
return;
}
int h = (L + R) / 2;
if (l <= h) fsum(i * 2, L, h, l, min(h, r));
if (r > h) fsum(i * 2 + 1, h + 1, R, max(h + 1, l), r);
for (int j = 0; j < 10; j++) a[i].x[j] = a[i * 2].x[j] + a[i * 2 + 1].x[j];
}
int main() {
scanf("%d%d", &n, &m);
init(1, 1, n);
for (i = 1; i <= n; i++) {
scanf("%d", &y);
for (j = 1; y > 0; y /= 10, j *= 10) a[b[i]].x[y % 10] += j;
}
recount(1, 1, n);
for (i = 0; i < m; i++) {
scanf("%d%d%d", &t, &l, &r);
if (t == 1) {
scanf("%d%d", &xfr, &y);
modify(1, 1, n, l, r);
} else {
for (int j = 0; j < 10; j++) scur[j] = 0;
fsum(1, 1, n, l, r);
for (int j = res = 0; j < 10; j++) res += j * scur[j];
printf("%lld\n", res);
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
__attribute__((constructor)) static void fast_cin() {
ios_base::sync_with_stdio(false);
cin.tie(0);
}
using ll = int64_t;
using ull = uint64_t;
using mp = pair<ll, ll>;
using mt = tuple<ll, ll, ll>;
template <typename T, typename R>
istream &operator>>(istream &fin, pair<T, R> &p) {
fin >> p.first >> p.second;
return fin;
}
template <typename T, typename R>
ostream &operator<<(ostream &fout, const pair<T, R> &p) {
fout << "{" << p.first << ", " << p.second << "} ";
return fout;
}
template <typename T>
ostream &operator<<(ostream &fout, const vector<T> &vec) {
for (size_t i = 0; i < vec.size(); i++) fout << vec[i] << ' ';
return fout;
}
template <typename T>
istream &operator>>(istream &fin, vector<T> &vec) {
for (size_t i = 0; i < vec.size(); i++) fin >> vec[i];
return fin;
}
vector<string> split(const string &s, char c) {
vector<string> v;
ull st = 0, fn = 0, lvl = 0;
for (; fn < s.size(); fn++) {
if (s[fn] == c && lvl == 0) {
v.push_back(s.substr(st, fn - st));
st = fn + 1;
}
if (s[fn] == '(') {
lvl++;
}
if (s[fn] == ')') {
lvl--;
}
}
v.push_back(s.substr(st, fn - st));
return v;
}
void pr_var(vector<string>::iterator it) {
(void)it;
cerr << endl;
}
template <typename T, typename... Args>
void pr_var(vector<string>::iterator it, T a, Args... args) {
cerr << *it << "=" << a << " ";
pr_var(++it, args...);
}
ll div_ceil(ll n, ll d) {
ll res = n / d;
if (res >= 0) res += n % d ? 1 : 0;
return res;
}
ll gcd(ll a, ll b) {
a = llabs(a);
b = llabs(b);
while (a && b) a > b ? a %= b : b %= a;
return a + b;
}
ll lcm(ll a, ll b) {
ll c = gcd(a, b);
return c == 0 ? 0 : llabs(a / c * b);
}
const double PI = acos(-1.0);
constexpr double EPS = 1e-6;
constexpr ll INF = numeric_limits<ll>::max();
constexpr ll MOD = 1e9 + 7;
int main() {
ll n, h, a, b, k;
cin >> n >> h >> a >> b >> k;
for (ll i = 0; i < k; i++) {
ll ta, fa, tb, fb;
cin >> ta >> fa >> tb >> fb;
ll res = 0;
if (ta == tb) {
res += llabs(fa - fb);
} else {
ll d = fa;
if (!(fa >= a && fa <= b)) {
if (llabs(fa - a) < llabs(fa - b)) {
d = a;
res += llabs(fa - a);
} else {
d = b;
res += llabs(fa - b);
}
}
res += llabs(ta - tb);
res += llabs(d - fb);
}
cout << res << endl;
}
return 0;
}
| 1 |
#include<iostream>
#include<vector>
using namespace std;
typedef long long int lli;
template<typename Integer>
Integer GCD(Integer a, Integer b){
if(!b)return a;
else return GCD(b,a%b);
}
template<typename Integer>
inline pair<Integer, pair<Integer, Integer> > ExtendedGCD(Integer a, Integer b){
pair<Integer, pair<Integer, Integer> > ret,tmp;
if( b==0 ) return make_pair( a, make_pair( 1, 0 ) );
tmp = ExtendedGCD(b,a%b);
ret.first = tmp.first;
ret.second.first = tmp.second.second;
ret.second.second = tmp.second.first - (a/b)*tmp.second.second;
return ret;
}
template<typename Integer>
inline bool SolveModEquation(Integer a, Integer b, Integer n){
pair<Integer, pair<Integer, Integer> > t;
t = ExtendedGCD( a, n );
if( b % t.first == 0 ){
Integer ini_x = ( t.second.first * ( b / t.first ) ) % n;
for(int i = 0; i < t.first; ++i){
Integer I = ( ini_x + i * ( n / t.first ) ) % n;
if(I<0)I+=n;
Integer J = (b - a*I) / n;
if( I >= 0 && J >= 0 && a * I + n * J == b )
return true;
}
}
return false;
}
int main(){
while(true){
int ableCount = 0;
lli n,a,b;
cin >> n >> a >> b;
if( n==0 && a==0 && b==0 )break;
for(lli k = 1; k <= n; k++){
if( SolveModEquation(a,k,b) )
++ableCount;
}
cout << n-ableCount << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<int> V(N);
for (int &X : V) cin >> X;
int Ans = min(V[0], V[N - 1]);
int Cnt = 0;
while (Cnt < N - 2) {
if (V[Cnt + 1] > V[Cnt + 2]) {
Ans = min(Ans, V[Cnt + 1]);
Cnt++;
} else {
Ans = min(Ans, V[Cnt + 2]);
Cnt += 2;
}
}
cout << Ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[2010] = {0}, b[2010] = {0}, n, x, y, sum = 0;
void init() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
a[x + y]++, b[1000 - x + y]++;
}
}
void work() {
for (int i = 0; i <= 2000; i++) {
if (a[i]) sum += a[i] * (a[i] - 1);
if (b[i]) sum += b[i] * (b[i] - 1);
}
sum >>= 1;
printf("%d\n", sum);
}
int main() {
init();
work();
return 0;
}
| 2 |
#include <iostream>
#include <string>
using namespace std;
#define ref(i,x,y)for(int i=x;i<=y;++i)
const int mod=1e9+7;
const int N=410;
int n;
int a[N],b[N],f[N/3][N][N][2],s[N][N],pa[N],pb[N],mk[N];
int mul(int a,int b)
{
return (1LL*a*b%mod+mod)%mod;
}
void inc(int&a,int b)
{
a=(a+b)%mod;
}
int main()
{
cin>>n;
ref(i,1,n)cin>>a[i],pa[a[i]]=i;
ref(i,1,n)cin>>b[i],pb[b[i]]=i;
ref(i,0,n)
{
ref(j,1,n)mk[j]=0;
int tot=0;
ref(j,1,i){mk[a[j]]++;if(mk[a[j]]==1)tot++;}
s[i][0]=tot;
ref(j,1,n){
mk[b[j]]++;
if(mk[b[j]]==1)tot++;
s[i][j]=tot;
}
}
f[0][0][0][0]=1;
ref(t,0,n/3)ref(i,0,n)ref(j,0,n)
{
if(f[t][i][j][0]&&i<n){
int F=f[t][i][j][0];
if(pb[a[i+1]]>j){
int tt=t-(s[i][j]-t*2);
if(tt>0)inc(f[t][i+1][j][0],mul(tt,F));
}
if(pb[a[i+1]]<=j)inc(f[t][i+1][j][0],F);
}
if(t<n/3||i==n)
inc(f[t][i][j][1],f[t][i][j][0]);
if(f[t][i][j][1]&&j<n){
int F=f[t][i][j][1];
if(pa[b[j+1]]>i){
int tt=t-(s[i][j]-t*2);
if(tt>0)inc(f[t][i][j+1][1],mul(tt,F));
}
if(pa[b[j+1]]<=i)inc(f[t][i][j+1][1],F);
if(t<n/3&&i<n&&pb[a[i+1]]>j+1&&pa[b[j+1]]>i+1)
inc(f[t+1][i+1][j+1][0],F);
}
}
int ans=f[n/3][n][n][0]+f[n/3][n][n][1];
for(int i=0;i<n;i+=3)ans=mul(ans,mul(i+1,i+2));
cout<<ans<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
int sum = 0;
for(int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
sort(a.begin(), a.end());
if(sum % 2 == 0) {
cout << sum / 2 << endl;
}
else {
for(int i = 0; i < n; i++) {
if(a[i] % 2 == 1) {
cout << (sum - a[i]) / 2 << endl;
return 0;
}
}
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N; cin>>N;
vector<int> par(N,-1),lch(N),rch(N);
for(int i=0;i<N;i++){
int v; cin>>v;
cin>>lch[v]>>rch[v];
if(lch[v]!=-1) par[lch[v]]=v;
if(rch[v]!=-1) par[rch[v]]=v;
}
function<void(int)> preorder=[&](int v)
{
printf(" %d",v);
if(lch[v]!=-1) preorder(lch[v]);
if(rch[v]!=-1) preorder(rch[v]);
};
function<void(int)> inorder=[&](int v)
{
if(lch[v]!=-1) inorder(lch[v]);
printf(" %d",v);
if(rch[v]!=-1) inorder(rch[v]);
};
function<void(int)> postorder=[&](int v)
{
if(lch[v]!=-1) postorder(lch[v]);
if(rch[v]!=-1) postorder(rch[v]);
printf(" %d",v);
};
int root=-1;
for(int i=0;i<N;i++) if(par[i]==-1) root=i;
puts("Preorder"); preorder(root); puts("");
puts("Inorder"); inorder(root); puts("");
puts("Postorder"); postorder(root); puts("");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
const int SQRTN = 1003;
const int LOGN = 22;
const double PI = acos(-1);
const int INF = 1e9;
int MOD = 1000000007;
const int FMOD = 998244353;
const double eps = 1e-9;
void solve() {
string s;
cin >> s;
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
sort(s.begin(), s.end());
long long m = s.length();
long long k = m - 1;
string ans;
ans.resize(n);
int cnt = 0;
map<char, long long> mp;
for (int i = 0; i < m; i++) mp[s[i]]++;
while (cnt != n) {
int s1 = 0;
vector<long long> v1;
for (int i = 0; i < n; i++) {
if (arr[i] == -1) continue;
if (arr[i] == 0) {
arr[i] = -1;
v1.push_back(i);
cnt++;
}
}
int sz = v1.size();
while (k >= 0 && mp[s[k]] < sz) {
k--;
}
mp[s[k]] = 0;
for (int i = 0; i < sz; i++) {
ans[v1[i]] = s[k];
k--;
}
for (int i = 0; i < n; i++) {
if (arr[i] == -1) continue;
for (int j = 0; j < sz; j++) {
arr[i] -= abs(i - v1[j]);
}
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
solve();
}
return (0);
}
| 4 |
#include <bits/stdc++.h>
char arr[200010];
int main(void) {
int n, t, start = -1;
scanf("%d %d", &n, &t);
scanf("%s", arr + 1);
for (int i = 1; i <= n; i++) {
if (arr[i] == '.') {
start = i;
break;
}
}
if (start == -1) {
for (int i = 1; i <= n; i++) printf("%c", arr[i]);
printf("\n");
return 0;
}
int finish = -1;
for (int i = start + 1; i <= n; i++) {
if (arr[i] >= '5') {
finish = i;
break;
}
}
if (finish == -1) {
for (int i = start + 1; i <= n; i++)
if (arr[i] != '0') finish = i;
for (int i = 1; i <= finish; i++) printf("%c", arr[i]);
printf("\n");
return 0;
}
arr[0] = '0';
while (t--) {
if (arr[finish - 1] == '.') {
arr[finish - 2]++;
for (int i = finish - 2; i >= 1; i--) {
if (arr[i] > '9') {
arr[i] = '0';
arr[i - 1]++;
}
}
break;
} else
arr[finish - 1]++;
for (int i = finish - 1; i > start; i--) {
if (arr[i] > '9') {
arr[i] = '0';
if (arr[i - 1] == '.') {
arr[i - 2]++;
for (int j = i - 2; j >= 1; j--) {
if (arr[j] > '9') {
arr[j] = '0';
arr[j - 1]++;
}
}
break;
} else
arr[i - 1]++;
} else
break;
}
if (arr[finish - 1] < '5' || t == 0) break;
finish--;
}
if (start == finish - 1) {
int i;
if (arr[0] == '0')
i = 1;
else
i = 0;
for (; i < start; i++) {
printf("%c", arr[i]);
}
printf("\n");
return 0;
}
int new_finish = -1;
for (int i = start + 1; i < finish; i++)
if (arr[i] != '0') new_finish = i;
if (new_finish == -1) {
int i;
if (arr[0] == '0')
i = 1;
else
i = 0;
for (; i < start; i++) {
printf("%c", arr[i]);
}
printf("\n");
return 0;
}
int i;
if (arr[0] == '0')
i = 1;
else
i = 0;
for (; i <= new_finish; i++) printf("%c", arr[i]);
printf("\n");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void FAST() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long gcd(long long x, long long y) {
if (x == 0) return y;
return gcd(y % x, x);
}
long long powM(long long x, long long y, long long m) {
long long ans = 1, r = 1;
x %= m;
while (r > 0 && r <= y) {
if (r & y) {
ans *= x;
ans %= m;
}
r <<= 1;
x *= x;
x %= m;
}
return ans;
}
map<long long, long long> factorize(long long n) {
map<long long, long long> ans;
for (long long i = 2; i * i <= n; i++) {
while (n % i == 0) {
ans[i]++;
n /= i;
}
}
if (n > 1) {
ans[n]++;
n = 1;
}
return ans;
}
int main() {
FAST();
long long t;
cin >> t;
while (t--) {
int n, i;
cin >> n;
int arr[n];
for (long long i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n);
for (i = 0; i < n - 1; i++) {
if ((arr[i + 1] - arr[i]) > 1) break;
}
if (i == n - 1)
cout << "Yes" << '\n';
else
cout << "No" << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
const int inf = 1000111222;
const int max_n = 211;
struct point {
int x, y;
};
long long get_sqr(const point& p1, const point& p2, const point& p3) {
long long res = 0;
res += (p2.x - p1.x) * 1LL * (p1.y + p2.y);
res += (p3.x - p2.x) * 1LL * (p2.y + p3.y);
res += (p1.x - p3.x) * 1LL * (p3.y + p1.y);
return abs(res);
}
point p[max_n];
int dp[max_n][max_n], n, k;
long long all_sqr[max_n][max_n][max_n], sums[max_n][max_n];
bool check(long long sz) {
for (int l = 2; l < n; ++l) {
for (int st = 0; st + l < n; ++st) {
int fn = st + l;
if (sums[st][fn] < sz) {
dp[st][fn] = 0;
continue;
}
int ans = 0;
long long curr_sqr = 0;
int curr_st = 0;
for (int i = st + 1; i < fn; ++i) {
if (curr_sqr < sz) {
curr_st = i;
curr_sqr += all_sqr[fn][curr_st - 1][i];
}
if (all_sqr[st][fn][i] >= sz) {
ans = max(ans, dp[st][i] + dp[i][fn] + 1);
}
}
if (curr_sqr >= sz) {
ans = max(ans, 1 + dp[curr_st][fn]);
}
int pr = curr_st - 1;
assert(curr_st != fn && ans);
for (int fnn = fn - 1; fnn > st; --fnn) {
curr_sqr += all_sqr[fnn][curr_st][fnn + 1];
long long nxt_sqr = all_sqr[fnn][pr][curr_st];
while (curr_sqr - nxt_sqr >= sz && curr_st != st) {
curr_sqr -= nxt_sqr;
curr_st = pr;
--pr;
nxt_sqr = all_sqr[fnn][pr][curr_st];
}
ans = max(ans, dp[curr_st][fnn] + 1);
if (curr_st == st) {
break;
}
}
dp[st][fn] = ans;
if (ans >= k + 1) {
return true;
}
}
}
return false;
}
long long get_ans() {
long long l = 1, r = 1LL * inf * inf;
while (l + 1 < r) {
long long mid = (l + r) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
return l;
}
void init() {
for (int i = 0; i < n; ++i) {
const point& p1 = p[i];
for (int q = 0; q < n; ++q) {
const point& p2 = p[q];
for (int w = 0; w < n; ++w) {
all_sqr[i][q][w] = get_sqr(p1, p2, p[w]);
}
}
int pr, cr = (i + 1) % n;
for (;;) {
pr = cr;
if (++cr == n) cr -= n;
if (cr == i) break;
sums[i][cr] = sums[i][pr] + all_sqr[i][pr][cr];
}
}
}
void shift(int l) { rotate(p, p + l, p + n); }
int main() {
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> p[i].x >> p[i].y;
}
long long l = 0;
for (int i = 0; i < 3; ++i) {
shift(rand() % (n - 1) + 1);
init();
l = max(l, get_ans());
}
cout << l << "\n";
}
| 5 |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"
int main() {
int id = 0;
while (1) {
int N; cin >> N;
if (!N)break;
if (id)cout << endl;
id++;
vector<vector<vector<int>>>field(5, vector<vector<int>>(5, vector<int>(5)));
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
string st; cin >> st;
for (int k = 0; k < 5; ++k) {
field[i][j][k] = st[k] == '1';
}
}
}
int A; cin >> A;
vector<int>as, bs;
for (int i = 0; i < A; ++i) {
int a; cin >> a;
as.push_back(a);
}
int B; cin >> B;
for (int i = 0; i < B; ++i) {
int b; cin >> b;
bs.push_back(b);
}
vector<vector<vector<int>>>nfield(field);
while (N--) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < 5; ++k) {
int num = 0;
for (int a = i-1; a <= i+1; ++a) {
for (int b = j-1; b <= j+1; ++b) {
for (int c = k-1; c <= k+1; ++c) {
if (a >= 0 && a < 5 && b >= 0 && b < 5 && c >= 0 && c < 5) {
if(a!=i||b!=j||c!=k)num += field[a][b][c];
}
}
}
}
if (!field[i][j][k]) {
nfield[i][j][k] = (find(as.begin(), as.end(), num) != as.end());
}
else {
nfield[i][j][k] = (find(bs.begin(), bs.end(), num) != bs.end());
}
}
}
}
field = nfield;
}
cout << "Case " << id << ":" << endl;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
for (int k = 0; k < 5; ++k) {
cout << field[i][j][k];
}
cout << endl;
}
if(i!=4)cout << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e5;
bool checkstring(string a, long long n_a, string b, long long n_b) {
for (long long i = 0; i < min(n_b, n_a); ++i)
if (a[i] != b[i]) return false;
return true;
}
void solve() {
int n;
char a[100];
cin >> a;
int small = 0, large = 0;
for (int i = 0; i < strlen(a); ++i) {
if (int(a[i]) >= 97 and int(a[i]) <= 122)
small++;
else
large++;
}
if (small == large)
for (int i = 0; i < strlen(a); ++i) {
if (int(a[i]) >= 65 and int(a[i]) <= 90) a[i] += 32;
}
else if (small == min(large, small))
for (int i = 0; i < strlen(a); ++i) {
if (int(a[i]) >= 97 and int(a[i]) <= 122) a[i] -= 32;
}
else
for (int i = 0; i < strlen(a); ++i) {
if (int(a[i]) >= 65 and int(a[i]) <= 90) a[i] += 32;
}
cout << a << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
return 0;
}
| 1 |
#include <algorithm>
#include <cstring>
#include <cstdio>
int val[100005], app[25], seq[25];
int dp[100005], pop[100005], sum[100005];
int main()
{
// freopen("E.in", "r", stdin);
int n, m = 0, ans = 0;
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
val[u] ^= w;
val[v] ^= w;
}
for (int i = 0; i < n; i++)
app[val[i]]++;
for (int i = 1; i < 16; i++)
{
ans += app[i] >> 1;
if (app[i] & 1)
seq[m++] = i;
}
for (int i = 1; i < 1 << m; i++)
{
pop[i] = pop[i ^ (i & -i)] + 1;
for (int j = 0; j < m; j++)
{
if (i & (1 << j))
sum[i] ^= seq[j];
}
}
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int i = 0; i + 1 < 1 << m; i++)
{
int rem = (1 << m) - 1 ^ i, low = rem & -rem;
for (int j = rem ^ low; ; j = j - 1 & (rem ^ low))
{
int add = j | low;
if (!sum[add])
dp[i | add] = std::min(dp[i | add], dp[i] + pop[j]);
if (!j)
break;
}
}
printf("%d\n", ans + dp[(1 << m) - 1]);
return 0;
}
| 0 |
/*
Do you like it heavy
Do you love it mean
Do you want it dirty
We don't play it clean
Standing in the spotlight
Ceiling to the floor
I can see you rockin'
Let me hear you roar
Turn it up
Tear it down
Hammering through
Can you feel the sound
Any day, Any night
Midnight madness
Dynamite
If you want it
Come and get it
Do you want it
Come and get it
Can you feel the power
Blinded by the light
Keep on goin' crazy
Burnin' up the night
Shouting out together
Boiling like the sun
Metal lives forever
Blazing on and on
Turn it up
Tear it down
Hammering through
Can you feel the sound
Any day, Any night
Midnight madness
Dynamite
If you want it
Come and get it
Do you want it
Come and get it
Do you like it heavy
Do you love it loud
If you want it
Come and get it
Do you want it
Come and get it
*/
#pragma GCC optimize("Ofast")
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define left asdgashgrketwjklrej
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd
#define prev asdgSHJsfgsdfhdsh
#define hash asdgasdgasdgdfrywewery
#define eps 1e-12
#define M_PI 3.141592653589793
#define bsize 1024
#define ldouble long double
using namespace std;
const long long bs = 1000000007;
const int N = 100031;
int tests,n,already_there[N],ar[N];
int main() {
//freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);
cin>>tests;
for (;tests;--tests){
cin>>n;
bool has_negative=0;
for (int i=0;i<=100;i++){
already_there[i]=0;
}
for (int i=1;i<=n;i++){
cin>>ar[i];
if (ar[i]>0)
already_there[ar[i]]=1;
if (ar[i]<0)
has_negative=1;
}
for (int i=1;i<=200;i++){
if (already_there[i]==0){
ar[n+1]=i;
++n;
}
}
if (has_negative){
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
cout<<n<<endl;
for (int i=1;i<=n;i++){
if (i>1)
cout<<" ";
cout<<ar[i];
}
cout<<endl;
}
//cin.get(); cin.get();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[101010];
int f[101010];
bool visit[101010];
int main() {
int q, n, maxb, t;
scanf("%d %d %d %d", &q, &n, &maxb, &t);
t = min(t, maxb);
while (q--) {
memset(f, 0, sizeof(f));
maxb = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
maxb = max(maxb, a[i]);
visit[a[i]] = 1;
}
for (int i = 1; i <= t; i++)
for (int j = 1; j <= n; j++)
for (int k = a[j]; k <= maxb && f[k] == f[a[j] - 1]; k++) f[k]++;
printf("%d\n", f[maxb]);
}
return 0;
}
| 4 |
#include <iostream>
#include <vector>
#include <deque>
#define MAXN 200005
#define mod 1000000007
using namespace std;
int n, dad[MAXN], crtDp, idxDp[MAXN], lvlCount[MAXN];
int pw2[MAXN];
vector<int> G[MAXN];
deque<int> dp[MAXN][3];
void precalc() {
pw2[0] = 1;
for (int i = 1; i < MAXN; ++i)
pw2[i] = (pw2[i - 1] << 1) % mod;
}
void DFS(int u, int lvl) {
++lvlCount[lvl];
if (G[u].empty()) {
idxDp[u] = (++crtDp);
dp[idxDp[u]][0].push_back(1);
dp[idxDp[u]][1].push_back(1);
dp[idxDp[u]][2].push_back(0);
return;
}
int bestSon = G[u][0];
for (auto x: G[u]) {
DFS(x, lvl + 1);
if (dp[idxDp[x]][0].size() > dp[idxDp[bestSon]][0].size())
bestSon = x;
}
idxDp[u] = idxDp[bestSon];
int idxu = idxDp[u];
int lvlMax = 0;
for (auto x: G[u]) {
if (x == bestSon)
continue;
int idx = idxDp[x];
int lvl = (int) dp[idx][0].size();
lvlMax = max(lvlMax, lvl);
for (int i = 0; i < lvl; ++i) {
int x0 = dp[idxu][0][i];
int x1 = dp[idxu][1][i];
int x2 = dp[idxu][2][i];
dp[idxu][0][i] = (1LL * x0 * dp[idx][0][i]) % mod;
dp[idxu][1][i] = (1LL * x1 * dp[idx][0][i] + 1LL * x0 * dp[idx][1][i]) % mod;
dp[idxu][2][i] = (1LL * x1 * dp[idx][1][i] % mod + 1LL * x2 * (dp[idx][0][i] + dp[idx][1][i])) % mod;
}
}
for (int i = 0; i < lvlMax; ++i) {
dp[idxu][0][i] = (dp[idxu][0][i] + dp[idxu][2][i]) % mod;
dp[idxu][2][i] = 0;
}
dp[idxu][0].push_front(1);
dp[idxu][1].push_front(1);
dp[idxu][2].push_front(0);
}
int main() {
ios::sync_with_stdio(0);
precalc();
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> dad[i];
G[dad[i]].push_back(i);
}
DFS(0, 0);
int sol = 0;
int idx = idxDp[0];
int lvl = (int) dp[idx][1].size();
for (int i = 0; i < lvl; ++i)
sol = (sol + 1LL * dp[idx][1][i] * pw2[n + 1 - lvlCount[i]]) % mod;
cout << sol << "\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[100100], t[100100];
long long ans, a[100100], b[100100];
int main() {
scanf("%d %s %s", &n, s, t);
for (int i = 0; i < n; i++) a[i] = s[i], b[i] = t[i];
for (int i = 0; i < n; i += 2) {
ans += a[i] - b[i];
ans -= a[i + 1] - b[i + 1];
}
if (ans) return puts("-1"), 0;
vector<int> vec, tmp;
for (int i = 0; i < n - 1; i++) {
ans += abs(a[i] - b[i]);
if (vec.size() >= 100000) {
a[i + 1] += b[i] - a[i];
continue;
}
tmp.clear();
while (a[i] < b[i]) tmp.push_back(i + 1), a[i]++, a[i + 1]++;
while (a[i] > b[i]) tmp.push_back(-i - 1), a[i]--, a[i + 1]--;
for (int j = i + 1;; j++) {
if (j >= n) assert(0);
if ('0' <= a[j] && a[j] <= '9') break;
while (a[j] < '0') tmp.push_back(j + 1), a[j]++, a[j + 1]++, ans++;
while (a[j] > '9') tmp.push_back(-j - 1), a[j]--, a[j + 1]--, ans++;
}
for (int k = tmp.size() - 1; k >= 0; k--) vec.push_back(tmp[k]);
}
if (vec.size() >= 100000) vec.resize(100000);
printf("%lld\n", ans);
for (int i = 0; i < vec.size(); i++)
printf("%d %d\n", abs(vec[i]), vec[i] / abs(vec[i]));
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, l;
scanf("%d%d", &n, &l);
int a[100];
for (int i = 0; i < n; ++i) scanf("%d", a + i);
int ans = 0;
for (int i = l; i <= 100; ++i) {
int c = 0;
for (int k = 0; k < n; ++k) c += a[k] / i;
ans = max(ans, c * i);
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long md=998244353;
const int maxn=300100;
long long fac[maxn],inv[maxn];
long long powd(long long x,long long y){
long long res=1;
while(y){
if(y&1) res=res*x%md;
x=x*x%md;
y>>=1;
}
return res;
}
void csh(){
fac[0]=1;
for(int i=1;i<=300000;i++)
fac[i]=fac[i-1]*i%md;
inv[300000]=powd(fac[300000],md-2);
for(int i=300000;i>=1;i--)
inv[i-1]=inv[i]*i%md;
}
long long C(int x,int y){
return fac[x]*inv[y]%md*inv[x-y]%md;
}
int main(){
csh();
long long N, A, B, K;
cin >> N >> A >> B >> K;
long long ans = 0;
for (int i=0; i<N+1; i++){
if (((K - i*A) % B != 0) || (K - i*A < 0) || ((K-i*A)/B > N)) continue;
ans = (ans + C(N,i) * C(N,(K-i*A)/B)) % md;
}
cout << ans << endl;
}
| 0 |
#include <iostream>
using i64 = long long;
const int N = 300000 + 7;
int n;
i64 a[N], b[N], s[2];
i64 l, r, m, p;
int main() {
// freopen("code.in", "r", stdin);
// freopen("code.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%lld", a + i),
b[i] = b[i - 1] + (i & 1 ? 1 : -1) * a[i],
s[i & 1] += a[i];
if (n % 2 == 0) return printf("%lld %lld", std::max(s[0], s[1]), std::min(s[0], s[1])), 0;
r = s[0] + s[1];
while (l < r) {
m = l + r + 1 >> 1, p = 0;
for (int i = 1; i < n; i += 2)
if (b[i] + p >= m) p = std::max(p, a[i + 1] - b[i]);
b[n] + p >= m ? (l = m) : (r = m - 1);
}
m = std::max(s[0] + l, s[1]);
return printf("%lld %lld", m, s[0] + s[1] - m), 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int ni() {
int a;
scanf("%d", &a);
return a;
}
const int N = 3e5 + 1;
const int PATHSZ = 3e5 + 1;
unsigned long long modpow(unsigned long long a, unsigned long long b,
unsigned long long mod) {
if (b == 0) return 1;
if (mod == 0) {
if (b % 2 == 0) return modpow(a * a, b / 2, mod);
return modpow(a, b - 1, mod) * a;
} else {
if (b % 2 == 0)
return modpow((((a * a) % (mod) + (mod)) % (mod)), b / 2, mod);
return (((modpow(a, b - 1, mod) * a) % (mod) + (mod)) % (mod));
}
}
const unsigned long long HMOD = 1e9 + 7;
struct dhash {
unsigned long long x, y;
dhash() : x(0), y(0) {}
dhash(unsigned long long x, unsigned long long y) : x(x), y(y) {}
dhash operator+(const dhash &h) const {
return dhash((x + h.x) % HMOD, y + h.y);
}
dhash operator-(const dhash &h) const {
return dhash((x - h.x + HMOD) % HMOD, y - h.y);
}
dhash operator*(const dhash &h) const {
return dhash((x * h.x) % HMOD, y * h.y);
}
bool operator<(const dhash &h) const {
return x < h.x || x == h.x && y < h.y;
}
};
dhash pows[PATHSZ], ipows[PATHSZ];
dhash p(33, 57);
dhash ip(modpow(33, (1 << 63) - 1, 0), modpow(57, HMOD - 2, HMOD));
vector<pair<int, char>> g[N];
dhash Hash[N];
set<dhash> Hashes;
int sz[N];
int h[N];
int par[N];
int Char[N];
int deleted[N];
vector<int> onh[N];
void precalc(int v, int p, int d) {
par[v] = p;
sz[v] = 1;
h[v] = d;
onh[d].push_back(v);
if (p != -1) {
Hash[v] = Hash[p] + dhash(Char[v], Char[v]) * pows[d];
}
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
Char[u] = c;
precalc(u, v, d + 1);
sz[v] += sz[u];
}
}
map<dhash, int> cnt;
void add(int v, int p, int basedeg, dhash base) {
dhash vhash = base + pows[basedeg + 1] * dhash(Char[v], Char[v]);
cnt[vhash]++;
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
add(u, v, basedeg + 1, vhash);
}
}
void rem(int v, int p, int basedeg, dhash base) {
dhash vhash = base + pows[basedeg + 1] * dhash(Char[v], Char[v]);
cnt[vhash]--;
if (cnt[vhash] == 0) {
cnt.erase(vhash);
}
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
rem(u, v, basedeg + 1, vhash);
}
}
void go(int v, int p, bool heavy) {
int big = -1;
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
if (big == -1 || sz[big] < sz[u]) big = u;
}
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
if (u == big) continue;
go(u, v, false);
}
if (big != -1) {
go(big, v, true);
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
if (u == big) continue;
for (auto &k2 : g[u]) {
int uu = k2.first;
if (uu == u) continue;
if (uu == v) continue;
add(uu, u, h[big], Hash[big]);
}
}
int s = 0;
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
s += sz[u];
}
deleted[h[big]] += s - cnt.size();
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
if (u == big) continue;
for (auto &k2 : g[u]) {
int uu = k2.first;
if (uu == u) continue;
if (uu == v) continue;
rem(uu, u, h[big], Hash[big]);
add(uu, u, h[u], Hash[u]);
}
}
}
if (heavy) {
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
cnt[Hash[u]]++;
}
} else {
for (auto &k : g[v]) {
int u = k.first;
char c = k.second;
if (u == p) continue;
for (auto &k2 : g[u]) {
int uu = k2.first;
if (uu == u) continue;
if (uu == v) continue;
rem(uu, u, h[u], Hash[u]);
}
}
}
}
int main(int argc, char *argv[]) {
ios::sync_with_stdio(0);
pows[0] = dhash(1, 1);
ipows[0] = dhash(1, 1);
for (int(i) = (1); (i) < (PATHSZ); (i)++) {
pows[i] = pows[i - 1] * p;
ipows[i] = ipows[i - 1] * ip;
}
int n;
cin >> n;
for (int(i) = (0); (i) < (n - 1); (i)++) {
int a, b;
string c;
cin >> a >> b >> c;
g[a].push_back(make_pair(b, c[0]));
}
precalc(1, -1, 0);
go(1, -1, 1);
int p = 1;
for (int(i) = (2); (i) < (PATHSZ); (i)++)
if (deleted[i] > deleted[p]) p = i;
cout << n - deleted[p] << endl;
cout << p << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
vector<long long int> v;
unordered_map<long long int, long long int> m;
for (long long int i = 0; i < n; i++) {
long long int a;
cin >> a;
m[a]++;
v.push_back(a);
}
long long int t = 0, f = 0;
for (long long int i = 0; i < n; i++) {
if (t < m[v[i]]) {
t = m[v[i]];
f = v[i];
}
}
long long int l = 0, k = 0;
vector<vector<long long int>> v1;
for (long long int i = 0; i < n; i++) {
if (v[i] == f) {
l = i;
break;
}
}
for (long long int i = l + 1; i < n; i++) {
if (v[i] < f) {
vector<long long int> a;
a.push_back(1);
a.push_back(i + 1);
a.push_back(i);
v1.push_back(a);
k++;
} else if (v[i] > f) {
vector<long long int> a;
a.push_back(2);
a.push_back(i + 1);
a.push_back(i);
v1.push_back(a);
k++;
}
}
for (long long int i = l - 1; i >= 0; i--) {
if (v[i] < f) {
vector<long long int> a;
a.push_back(1);
a.push_back(i + 1);
a.push_back(i + 2);
v1.push_back(a);
k++;
} else if (v[i] > f) {
vector<long long int> a;
a.push_back(2);
a.push_back(i + 1);
a.push_back(i + 2);
v1.push_back(a);
k++;
}
}
cout << k << endl;
for (long long int i = 0; i < k; i++) {
cout << v1[i][0] << " " << v1[i][1] << " " << v1[i][2] << endl;
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
vector<pair<int, int>> A(N);
for(int i = 0; i < N; i++){
cin >> A[i].first;
A[i].second = i;
}
sort(A.begin(), A.end());
long long yi = 0;
for(int i = 0; i < N; i++){
if((A[i].second ^ i) & 1) ++yi;
}
cout << (yi >> 1) << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
template <typename T>
class IntegerIterator
: public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
explicit IntegerIterator(T value) : value(value) {}
IntegerIterator& operator++() {
++value;
return *this;
}
IntegerIterator operator++(int) {
IntegerIterator copy = *this;
++value;
return copy;
}
IntegerIterator& operator--() {
--value;
return *this;
}
IntegerIterator operator--(int) {
IntegerIterator copy = *this;
--value;
return copy;
}
T operator*() const { return value; }
bool operator==(IntegerIterator rhs) const { return value == rhs.value; }
bool operator!=(IntegerIterator rhs) const { return !(*this == rhs); }
private:
T value;
};
template <typename T>
class IntegerRange {
public:
IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }
private:
T begin_;
T end_;
};
template <typename T>
class ReversedIntegerRange {
public:
ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; }
std::reverse_iterator<IntegerIterator<T>> begin() const {
return std::reverse_iterator<IntegerIterator<T>>(
IntegerIterator<T>(begin_));
}
std::reverse_iterator<IntegerIterator<T>> end() const {
return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
}
private:
T begin_;
T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
return IntegerRange<T>(from, to);
}
template <typename T>
IntegerRange<T> inclusiveRange(T to) {
return IntegerRange<T>(0, to + 1);
}
template <typename T>
IntegerRange<T> inclusiveRange(T from, T to) {
return IntegerRange<T>(from, to + 1);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
return ReversedIntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from) {
return ReversedIntegerRange<T>(from + 1, 0);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from, T to) {
return ReversedIntegerRange<T>(from + 1, to);
}
template <typename T>
T divideCeil(T a, T b) {
;
if (b < 0) {
a = -a;
b = -b;
}
return a / b + (a % b > 0);
}
template <typename T>
T divideFloor(T a, T b) {
;
if (b < 0) {
a = -a;
b = -b;
}
return a / b - (a % b < 0);
}
template <typename T>
T divideTowardsZero(T a, T b) {
;
return a / b;
}
template <typename T>
T divideAwayFromZero(T a, T b) {
;
bool changeSign = false;
if (a < 0) {
changeSign = !changeSign;
a = -a;
}
if (b < 0) {
changeSign = !changeSign;
b = -b;
}
T res = (a + b - 1) / b;
if (changeSign) {
res *= -1;
}
return res;
}
using namespace std;
array<array<int, 16 * 16>, 1 << 16> dp;
class TaskC {
public:
void solve(std::istream& in, std::ostream& out) {
int n;
in >> n;
struct Card {
char c;
int r, b;
};
vector<Card> v(n);
for (int i : range(n)) {
in >> v[i].c >> v[i].r >> v[i].b;
}
memset(&dp, 127, sizeof dp);
dp[0][0] = 0;
vector<int> cntRed(1 << n);
vector<int> cntBlue(1 << n);
for (int i : range(1 << n)) {
for (int j : range(n)) {
if (i & (1 << j)) {
if (v[j].c == 'R') {
++cntRed[i];
} else {
++cntBlue[i];
}
}
}
}
int ansR = 0;
int ansB = 0;
for (int i : range(n)) {
if (v[i].r >= n) {
ansR += v[i].r - n + 1;
v[i].r = n - 1;
}
if (v[i].b >= n) {
ansB += v[i].b - n + 1;
v[i].b = n - 1;
}
}
int inf = 2139062143;
for (int i : range(1 << n)) {
for (int j : range(n)) {
if (i & (1 << j)) {
continue;
}
for (int r : range(16 * 16)) {
if (dp[i][r] == inf) {
continue;
}
int needRed = max(v[j].r - cntRed[i], 0);
int needBlue = max(v[j].b - cntBlue[i], 0);
if (r + needRed < 16 * 16) {
dp[i | (1 << j)][r + needRed] =
min(dp[i | (1 << j)][r + needRed], dp[i][r] + needBlue);
}
}
}
}
int ou = inf;
for (int r : range(16 * 16)) {
if (dp[(1 << n) - 1][r] == inf) {
continue;
}
ou = min(ou, n + max(ansR + r, ansB + dp[(1 << n) - 1][r]));
}
out << ou << "\n";
}
};
int main() {
std::ios_base::sync_with_stdio(false);
TaskC solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
in.tie(nullptr);
out << std::fixed;
out.precision(20);
solver.solve(in, out);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const double INF = 1e18;
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
} p[N];
int max1[N], max2[N];
double deltA[N], deltB[N];
double sqr(double x) { return x * x; }
double dist(const Point &a, const Point &b) {
return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
int main() {
double x1, x2, y1, y2, xx, yy;
int n;
cin >> x1 >> y1 >> x2 >> y2 >> xx >> yy;
cin >> n;
for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
Point A(x1, y1), B(x2, y2), T(xx, yy);
double sum = 0;
for (int i = 1; i <= n; i++) {
deltA[i] = dist(T, p[i]) - dist(A, p[i]);
deltB[i] = dist(T, p[i]) - dist(B, p[i]);
sum += 2 * dist(p[i], T);
}
double ans = INF;
int now = 1;
for (int i = 2; i <= n; i++) {
if (deltA[i] > deltA[now]) now = i;
}
ans = min(ans, sum - deltA[now]);
now = 1;
for (int i = 2; i <= n; i++) {
if (deltB[i] > deltB[now]) now = i;
}
ans = min(ans, sum - deltB[now]);
if (n >= 2) {
deltA[0] = -INF;
deltA[n + 1] = -INF;
max1[0] = 0;
max2[n + 1] = n + 1;
for (int i = 1; i <= n; i++)
if (deltA[i] > deltA[max1[i - 1]])
max1[i] = i;
else
max1[i] = max1[i - 1];
for (int i = n; i >= 1; i--)
if (deltA[i] > deltA[max2[i + 1]])
max2[i] = i;
else
max2[i] = max2[i + 1];
for (int i = 1; i <= n; i++) {
int now = max2[i + 1];
if (deltA[max1[i - 1]] > deltA[max2[i + 1]]) now = max1[i - 1];
ans = min(ans, sum - deltB[i] - deltA[now]);
}
}
printf("%.9f\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<string> rjesenja;
int prime[1000100];
int main() {
long long n, a, b;
cin >> n;
for (int i = 2; i < 1000000; i++) {
if (!prime[i]) {
for (int j = i * 2; j <= 1000000; j += i) prime[j] = 1;
}
}
for (int i = 0; i < n; i++) {
cin >> a;
b = sqrt(a);
if (b * b != a || a == 1)
rjesenja.push_back("NO");
else if (!prime[b])
rjesenja.push_back("YES");
else
rjesenja.push_back("NO");
}
for (int i = 0; i < n; i++) cout << rjesenja[i] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n], k = 0;
while (n != 0) {
int temp = n, i = 0, j = 1;
while (temp) {
if (temp % 10 != 0) {
i += j;
}
j *= 10;
temp /= 10;
}
n = n - i;
arr[k++] = i;
}
sort(arr, arr + k);
cout << k << "\n";
for (int i = 0; i < k; i++) cout << arr[i] << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t = 0, c = 0;
char ch[105][105];
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> ch[i][j];
for (int i = 1; i <= n; i++) {
c = 0;
for (int j = 1; j <= n; j++) {
if (ch[i][j] == 'C') c++;
}
t += (c * (c - 1)) / 2;
}
for (int j = 1; j <= n; j++) {
c = 0;
for (int i = 1; i <= n; i++) {
if (ch[i][j] == 'C') c++;
}
t += (c * (c - 1)) / 2;
}
cout << t;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const* argv[]) {
vector<int> v;
int n;
char c;
while (true) {
scanf("%d", &n);
v.push_back(n);
if (scanf("%c", &c) == EOF || c != ',') break;
}
sort(v.begin(), v.end());
bool first = true;
int minR = v[0];
int maxR = v[0];
for (int i = 1; i < v.size(); ++i) {
if (v[i] == v[i - 1]) continue;
if (v[i] == v[i - 1] + 1)
maxR++;
else {
if (!first) printf(",");
first = false;
if (minR != maxR)
printf("%d-%d", minR, maxR);
else
printf("%d", minR);
minR = maxR = v[i];
}
}
if (!first) printf(",");
if (minR != maxR)
printf("%d-%d", minR, maxR);
else
printf("%d", minR);
puts("");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp = 0; begin != end; ++begin) {
if (sp)
putchar(' ');
else
sp = true;
printf(fmt, *begin);
}
putchar('\n');
}
struct UnionFindTimestamp {
int n;
vector<vector<pair<int, int> > > U;
int count_link;
UnionFindTimestamp(int n_ = 0) : n(n_), U(n + 2), count_link(0) {
for (int i = 0, i_len = (n); i < i_len; ++i) U[i].emplace_back(0, -1);
U[n].emplace_back(0, n);
}
int root(int x, int t) const {
while (1) {
if (t < U[x].back().first || U[x].back().second < 0) return x;
x = U[x].back().second;
}
}
bool link(int x, int y) {
x = root(x, count_link);
y = root(y, count_link);
count_link++;
if (x == y) return false;
if (U[x].back().second < U[y].back().second) swap(x, y);
U[y].emplace_back(count_link, U[x].back().second + U[y].back().second);
U[x].emplace_back(count_link, y);
U[n].emplace_back(count_link, U[n].back().second - 1);
U[n + 1].emplace_back(y, x);
return true;
}
bool same(int x, int y, int t) const {
return x == y || root(x, t) == root(y, t);
}
int count(int t) const {
int k = upper_bound(U[n].begin(), U[n].end(), pair<int, int>(t, n)) -
U[n].begin() - 1;
return U[n][k].second;
}
int size(int x, int t) const {
while (1) {
if (t < U[x].back().first || U[x].back().second < 0) {
int k = upper_bound(U[x].begin(), U[x].end(), pair<int, int>(t, n)) -
U[x].begin() - 1;
return -U[x][k].second;
}
x = U[x].back().second;
}
}
bool rollback() {
if (count_link == 0) return false;
count_link--;
if (U[n + 1].empty()) return false;
pair<int, int> p = U[n + 1].back();
if (count_link >= U[p.first].back().first) return false;
U[p.first].pop_back();
U[p.second].pop_back();
U[n].pop_back();
U[n + 1].pop_back();
return true;
}
};
const int MAGIC = 334;
int N, M, K;
vector<int> G[100111], R[100111];
;
int Q;
struct Query {
int l, r, id;
bool operator<(const Query &y) const {
return l / MAGIC != y.l / MAGIC ? l < y.l : r < y.r;
}
} query[100111];
int ans[100111];
struct UnionFind {
int n, cc, *u;
UnionFind() : n(0), cc(0), u(NULL) {}
UnionFind(int n_) : n(n_), cc(n_) {
u = new int[n_];
memset(u, -1, sizeof(int) * n);
}
UnionFind(const UnionFind &y) : n(y.n), cc(y.cc) {
u = new int[y.n];
memcpy(u, y.u, sizeof(int) * n);
}
~UnionFind() {
delete[] u;
u = NULL;
n = cc = 0;
}
friend void swap(UnionFind &x, UnionFind &y) {
swap(x.n, y.n);
swap(x.cc, y.cc);
swap(x.u, y.u);
}
UnionFind &operator=(UnionFind y) {
swap(*this, y);
return *this;
}
int root(int x) {
int y = x, t;
while (u[y] >= 0) y = u[y];
while (x != y) {
t = u[x];
u[x] = y;
x = t;
}
return y;
}
bool link(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (u[y] < u[x]) swap(x, y);
u[x] += u[y];
u[y] = x;
cc--;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -u[root(x)]; }
int count() { return cc; }
};
void naive(int l, int r, int i) {
UnionFind U(r - l);
for (int j = l; j < r; j++) {
for (__typeof((G[j]).begin()) e = (G[j]).begin(), e_end = (G[j]).end();
e != e_end; ++e)
if (*e < r) U.link(j - l, *e - l);
}
ans[i] = U.count();
}
int main() {
scanf("%d%d%d", &N, &K, &M);
for (int i = 0, i_len = (M); i < i_len; ++i) {
int x, y;
scanf("%d%d", &x, &y);
x--;
y--;
if (y < x) swap(x, y);
G[x].push_back(y);
R[y].push_back(x);
}
scanf("%d", &Q);
int cur = 0;
for (int i = 0, i_len = (Q); i < i_len; ++i) {
int l, r;
scanf("%d%d", &l, &r);
l--;
if (r - l < 2 * MAGIC) {
naive(l, r, i);
} else {
query[cur].l = l;
query[cur].r = r;
query[cur].id = i;
cur++;
}
}
sort(query, query + cur);
for (int i = 0; i < cur;) {
int j = i;
int sep = (query[i].l / MAGIC + 1) * MAGIC;
int pos = sep;
UnionFindTimestamp U(N);
while (j < cur && query[j].l < sep) {
int bak = query[j].r - 1;
while (pos < query[j].r) {
for (__typeof((R[pos]).begin()) e = (R[pos]).begin(),
e_end = (R[pos]).end();
e != e_end; ++e)
if (sep <= *e) U.link(*e, pos);
pos++;
}
int cnt = 0;
for (int top = sep - 1; top >= query[j].l; top--) {
for (__typeof((G[top]).begin()) e = (G[top]).begin(),
e_end = (G[top]).end();
e != e_end; ++e)
if (*e <= bak) {
cnt++;
U.link(top, *e);
}
}
ans[query[j].id] = U.count(U.count_link) - query[j].l - (N - query[j].r);
for (int t = 0, t_len = (cnt); t < t_len; ++t) U.rollback();
j++;
}
i = j;
}
for (int i = 0, i_len = (Q); i < i_len; ++i) printf("%d\n", ans[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int p[100009], A[100009];
vector<int> tree[100009];
long long ans = 0;
void go(int now) {
if (A[now] != -1) {
int dif = A[now] - A[p[now]];
if (dif < 0) {
puts("-1");
exit(0);
}
ans += dif;
} else {
if (tree[now].size() == 0) return;
int par = A[p[now]], mn = 1e9;
for (auto& it : tree[now]) mn = min(mn, A[it] - par);
if (mn < 0) {
puts("-1");
exit(0);
}
A[now] = par + mn;
ans += mn;
}
for (auto& it : tree[now]) go(it);
}
int main() {
int N;
scanf("%d", &N);
p[1] = 0;
for (int i = 2; i <= N; i++) {
scanf("%d", &p[i]);
tree[p[i]].push_back(i);
}
for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
go(1);
printf("%lld", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 10;
int n, nxt[MAX_N][26 + 10], f[MAX_N], ans;
vector<pair<int, string>> adj[MAX_N];
string s;
void dfs(int v, int par = 0, int cur = 0) {
int tmp;
string ss;
for (auto u : adj[v]) {
if (u.first != par) {
ss = u.second;
tmp = cur;
for (int i = 0; i < ss.size(); i++) {
tmp = nxt[tmp][ss[i] - 'a'];
if (tmp == s.size()) {
ans++;
}
}
dfs(u.first, v, tmp);
}
}
}
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
int a;
string b;
cin >> a >> b;
adj[i].push_back({a, b});
adj[a].push_back({i, b});
}
cin >> s;
int cur = 0;
for (int i = 1; i < s.size(); i++) {
while (cur && s[cur] != s[i]) {
cur = f[cur];
}
cur += (s[cur] == s[i]);
f[i + 1] = cur;
}
for (int i = 0; i <= s.size(); i++) {
for (int c = 0; c < 26; c++) {
if (i != s.size() && s[i] - 'a' == c) {
nxt[i][c] = i + 1;
} else {
nxt[i][c] = nxt[f[i]][c];
}
}
}
dfs(1);
cout << ans;
}
| 5 |
#include<iostream>
int main(){
int n;
std::cin>>n;
for(int i=0;i<10;++i)for(int j=0;j<10;++j)
if(i*j==n){
puts("Yes");
return 0;
}
puts("No");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 8e6 + 5;
int a[N], tot, n;
bool hs[N], vis[N];
void dfs(int x) {
if (vis[x]) return;
vis[x] = true;
if (hs[x]) dfs(tot ^ x);
for (int i = 0; i < n; i++) {
if (x & (1 << i)) dfs(x ^ (1 << i));
}
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) scanf("%d", &a[i]), hs[a[i]] = true;
tot = (1 << n) - 1;
int ans = 0;
for (int i = 1; i <= m; i++) {
if (!vis[a[i]]) {
ans++;
vis[a[i]] = true;
dfs(tot ^ a[i]);
}
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int n;
char s[maxn + 1];
int Check(void) {
int x = 0;
for (int i = 0; i < n; ++i) x = (10 * x + s[i] - '0') % 7;
return x;
}
void Find(int x, int j) {
int i = find(s, s + n, '0' + x) - s;
swap(s[i], s[j]);
}
int main(void) {
scanf("%s", s);
n = strlen(s);
Find(1, 0);
Find(6, 1);
Find(8, 2);
Find(9, 3);
do {
if (Check() == 0) {
puts(s);
return 0;
}
} while (next_permutation(s, s + 4));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,M;cin>>N>>M;
int count=0;
for(int i=0;i<M;i++){int a;cin>>a;count=max(count,a);}
cout<<max(0,count*2-N-1)<<endl;
} | 0 |
#include <bits/stdc++.h>
const long long inf = 9223372036854775807ll;
const int iinf = 2147483647;
const int limit = 1048576;
using namespace std;
bool sync_with_stdio(bool sync = false);
int main() {
int n, T;
scanf("%d%d", &n, &T);
vector<int> a(n + 1);
for (int i = (int)1; i < (int)n + 1; i++) scanf("%d", &a[i]);
vector<vector<int> > lis(301, vector<int>(301, 0));
for (int i = (int)1; i < (int)301; i++) {
vector<int> dp(301, 0);
for (int j = (int)1; j < (int)n + 1; j++) {
if (a[j] < i) continue;
int t1 = dp[a[j]] + 1;
for (int k = (int)a[j]; k < (int)301; k++) {
dp[k] = max(dp[k], t1);
lis[i][k] = max(lis[i][k], dp[k]);
}
}
}
vector<int> ans(301, 0);
for (int i = (int)1; i < (int)min(1000, T) + 1; i++) {
vector<int> new_ans(301, 0);
for (int j = (int)1; j < (int)301; j++)
for (int k = (int)1; k < (int)j + 1; k++)
new_ans[j] = max(new_ans[j], ans[k] + lis[k][j]);
swap(new_ans, ans);
}
if (T > 2000) {
vector<int> new_ans(301, 0);
for (int j = (int)1; j < (int)301; j++) {
new_ans[j] = max(new_ans[j], ans[j] + (T - 2000) * lis[j][j]);
for (int k = (int)1; k < (int)j; k++)
new_ans[j] =
max(new_ans[j], ans[k] + (T - 2001) * lis[k][k] + lis[k][j]);
}
swap(ans, new_ans);
}
for (int i = (int)1; i < (int)min(1000, T - 1000) + 1; i++) {
vector<int> new_ans(301, 0);
for (int j = (int)1; j < (int)301; j++)
for (int k = (int)1; k < (int)j + 1; k++)
new_ans[j] = max(new_ans[j], ans[k] + lis[k][j]);
swap(new_ans, ans);
}
printf("%d", ans[300]);
return 0;
}
| 2 |
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#define size sz
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN=200005;
int n,len;
int color[MAXN],head[MAXN],size[MAXN],num[MAXN];
ll ans[MAXN];
struct node{
int to,next;
} e[MAXN * 2];
void add(int u,int v){
e[++len].to = v;
e[len].next = head[u];
head[u] = len;
}
ll f(ll n){
return 1ll * n * (n + 1) / 2;
}
void dfs(int u,int fa){
int c = color[u];
int lst,d;
size[u] = 1;
ll tmp = num[c];
for(int i = head[u];i != -1;i = e[i].next){
int v = e[i].to;
if(v == fa)
continue;
lst = num[c];
dfs(v,u);
d = num[c] - lst;
ans[c] -= f(size[v] - d);
size[u] += size[v];
}
num[c] = tmp + size[u];
}
int main(){
memset(head,-1,sizeof(head));
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",&color[i]);
ans[i] = f(n);
}
for(int i = 1;i < n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(1,0);
for(int i = 1;i <= n;i++){
ans[i] -= f(n - num[i]);
printf("%lld\n",ans[i]);
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll check(vector<pair<ll,ll>> vt){
ll n = vt.size();
for(int i=0;i<n;i++) vt[i].first=min(vt[i].first,n);
vector<vector<ll>> vs(n+1);
for(int i=0;i<n;i++) vs[vt[i].first].emplace_back(vt[i].second);
priority_queue<ll> que;
ll ans=0;
for(int i=n;i>0;i--){
for(auto u:vs[i]){
que.push(u);
}
if(!que.empty()){
ans+=que.top();
que.pop();
}
}
//cout << "ans:" << ans << endl;
return ans;
}
ll solve(){
int n;
cin >> n;
ll k,l,r;
ll ans = 0;
vector<pair<ll,ll>> right,left;
ll m;
for(int i=0;i<n;i++){
cin >> k >> l >> r;
/*m=min(l,r);
l-=m;
r-=m;
ans+=m;
if(l>0){
left.emplace_back(k,l);
}else{
right.emplace_back(n-k,r);
}*/
if(l>r){
ans+=r;
left.emplace_back(k,l-r);
}else{
ans+=l;
right.emplace_back(n-k,r-l);
}
}
//cout << "befor:" << ans << endl;
ans+=check(left);
ans+=check(right);
return ans;
}
int main(){
ll t;
cin >> t;
vector<ll> ans;
for(ll i=0;i<t;i++){
//solve();
ans.push_back(solve());
}
//cout << "ans" << endl;
for(ll i=0;i<t;i++){
cout << ans[i] << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 3;
const long long INFF = (long long)5e18 + 7;
vector<int> edge[N];
int nd;
int id[N], used[N], siz[N];
void sol(int n) {
set<pair<int, int>> st;
for (int i = 1; i <= n; i++) {
siz[i] = (int)edge[i].size();
st.insert({siz[i], i});
}
vector<int> ans;
while (!st.empty()) {
int x = st.begin()->second;
st.erase(st.begin());
used[x] = 1;
ans.push_back(x);
for (int v : edge[x]) {
if (used[v]) continue;
used[v] = 1;
st.erase({siz[v], v});
for (int u : edge[v]) {
if (used[u]) continue;
st.erase({siz[u], u});
siz[u]--;
st.insert({siz[u], u});
}
}
}
nd = sqrt(n);
if (nd * nd < n) nd++;
if ((int)ans.size() < nd) return;
printf("1\n");
for (int i = 0; i < nd; i++) printf("%d%c", ans[i], " \n"[i == nd - 1]);
exit(0);
}
int dep[N], prt[N];
map<int, pair<int, int>> mp[N];
void ot(int x, int y) {
printf("2\n%d\n", dep[x] - dep[y] + 1);
int cur = x;
vector<int> ans;
while (cur != y) {
ans.push_back(cur);
cur = prt[cur];
}
ans.push_back(y);
for (int i = 0; i < (int)ans.size(); i++)
printf("%d%c", ans[i], " \n"[i == (int)ans.size() - 1]);
exit(0);
}
void ot2(int x, int y, int p1, int p2) {
printf("2\n%d\n", dep[x] + dep[y] - 2 * dep[p1] + 2);
vector<int> ans, ans2;
ans.push_back(p2);
int cur = x;
while (cur != p1) {
ans.push_back(cur);
cur = prt[cur];
}
ans.push_back(p1);
cur = y;
while (cur != p1) {
ans2.push_back(cur);
cur = prt[cur];
}
reverse(ans2.begin(), ans2.end());
for (int x : ans2) ans.push_back(x);
for (int i = 0; i < (int)ans.size(); i++)
printf("%d%c", ans[i], " \n"[i == (int)ans.size() - 1]);
exit(0);
}
void dfs(int u, int pa, int dpt) {
dep[u] = dpt;
prt[u] = pa;
for (int v : edge[u]) {
if (v == pa) continue;
if (dep[v]) {
continue;
} else {
dfs(v, u, dpt + 1);
mp[id[v]].erase(u);
if (mp[id[u]].size() < mp[id[v]].size()) swap(id[u], id[v]);
for (auto x : mp[id[v]]) {
if (mp[id[u]].find(x.first) != mp[id[u]].end()) {
if (x.second.first + mp[id[u]][x.first].first -
2 * (dpt - dep[x.first]) >=
nd) {
ot2(x.second.second, mp[id[u]][x.first].second, u, x.first);
}
mp[id[u]][x.first] = max(mp[id[u]][x.first], x.second);
} else {
mp[id[u]][x.first] = x.second;
}
}
}
}
for (int v : edge[u]) {
if (dep[v] > dpt) continue;
if (dpt - dep[v] + 1 >= nd) {
ot(u, v);
}
mp[id[u]][v] = max(mp[id[u]][v], make_pair(dpt - dep[v] + 1, u));
}
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
sol(n);
for (int i = 1; i <= n; i++) id[i] = i;
dfs(1, 0, 1);
assert(0);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void read(int &k) {
k = 0;
char x = getchar();
while (x < '0' || x > '9') x = getchar();
while (x >= '0' && x <= '9') {
k = k * 10 - 48 + x;
x = getchar();
}
}
int n, a[200010], b[200010], now, ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
now = 1;
ans = 0;
for (int i = 1; i <= n; i++) {
while (now <= n) {
if (a[i] == b[now]) break;
now++;
}
if (now > n) {
ans = n - i + 1;
break;
}
}
printf("%d\n", ans);
return 0;
}
| 1 |
/*
AuThOr GaRyMr
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
bool same(char c1,char c2){
return (c1=='?'||c2=='?'||c1==c2);
}
string a,b,c;
int A,B,C;
const int M=20000;
bool ab[1000000],ac[1000000],bc[1000000];
int main(){
fastio;
cin>>a>>b>>c;
A=a.length();
B=b.length();
C=c.length();
rep(i,A) rep(j,B) if(!same(a[i],b[j])) ab[i-j+M]=1;
rep(i,A) rep(j,C) if(!same(a[i],c[j])) ac[i-j+M]=1;
rep(i,B) rep(j,C) if(!same(b[i],c[j])) bc[i-j+M]=1;
int res=INF;
rb(i,-5000,5000){
rb(j,-5000,5000){
if(!ab[M+i]&&!ac[M+j]&&!bc[j-i+M]){
int L = min(0, min(i, j));
int R = max(A, max(B + i, C + j));
res = min(res, R - L);
}
}
}
cout<<res<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int g[105][105] = {};
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a - 1][b - 1] = c;
}
int start = 0;
int visited[105] = {1};
vector<int> ring;
ring.push_back(start);
int s = start;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[j]) continue;
if (g[start][j] || g[j][start]) {
start = j;
visited[j] = 1;
ring.push_back(start);
}
}
}
ring.push_back(s);
int ans1 = 0;
for (int i = 1; i < ring.size(); i++) {
int a = ring[i - 1];
int b = ring[i];
if (g[a][b] == 0) {
if (g[b][a] == 0)
while (1)
;
ans1 += g[b][a];
}
}
int ans2 = 0;
for (int i = 1; i < ring.size(); i++) {
int a = ring[i - 1];
int b = ring[i];
if (g[b][a] == 0) {
if (g[a][b] == 0)
while (1)
;
ans2 += g[a][b];
}
}
cout << min(ans1, ans2) << endl;
return 0;
}
| 1 |
#include<stdio.h>
#include<algorithm>
using namespace std;
int x[200];
int y[200];
int main(){
int a;
while(scanf("%d",&a),a){
for(int i=1;i<a;i++){
int b,c;scanf("%d%d",&b,&c);
x[i]=x[b];
y[i]=y[b];
switch(c){
case 0:x[i]--;break;
case 1:y[i]--;break;
case 2:x[i]++;break;
case 3:y[i]++;break;
}
}
int Xmin=99999;
int Xmax=-99999;
int Ymin=99999;
int Ymax=-99999;
for(int i=0;i<a;i++){
Xmin=min(Xmin,x[i]);
Xmax=max(Xmax,x[i]);
Ymin=min(Ymin,y[i]);
Ymax=max(Ymax,y[i]);
}
printf("%d %d\n",Xmax-Xmin+1,Ymax-Ymin+1);
}
} | 0 |
#include<stdio.h>
int main() {
long long int a, b, n, m, c, d ,sa=0,sb=0;
scanf("%lld %lld", &c, &d);
for (n = 1; c >= n; n++) {
scanf("%lld",&a );
sa += a;
}
for (m = 1; d >= m; m++) {
scanf("%lld", &b);
sb += b;
}
printf("%lld\n", sa*sb);
return 0;
}
| 0 |
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<algorithm>
typedef signed long long int int64;
inline unsigned getint() {
register char ch;
while(!isdigit(ch=getchar()));
register unsigned x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
inline int64 min(const int64 &a,const int64 &b) {
return a<b?a:b;
}
const int64 inf=0x7ffffffffffffff;
const int N=200001;
int n;
class FenwickTree {
private:
int64 val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
FenwickTree() {
std::fill(&val[0],&val[N],inf);
}
void modify(int p,const int64 &x) {
while(p<=n) {
if(x<val[p]) {
val[p]=x;
} else
p+=lowbit(p);
}
}
int64 query(int p) const {
int64 ret=inf;
while(p) {
ret=min(ret,val[p]);
p-=lowbit(p);
}
return ret;
}
};
FenwickTree ta;
class RevFenwickTree {
private:
int64 val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
RevFenwickTree() {
std::fill(&val[0],&val[N],inf);
}
void modify(int p,const int64 &x) {
while(p) {
if(x<val[p]) {
val[p]=x;
}
p-=lowbit(p);
}
}
int64 query(int p) const {
int64 ret=inf;
while(p<=n) {
ret=min(ret,val[p]);
p+=lowbit(p);
}
return ret;
}
};
RevFenwickTree tb;
int64 f[N];
inline void modify(const int &p,const int64 x) {
if(x<f[p]) {
f[p]=x;
ta.modify(p,x-p);
tb.modify(p,x+p);
}
}
int main() {
n=getint();
int q=getint(),a=getint(),b=getint();
std::fill(&f[0],&f[N],inf);
modify(a,0);
int64 sum=0;
while(q--) {
a=b;
b=getint();
sum+=abs(a-b);
int64 t1=ta.query(b)+b,t2=tb.query(b)-b;
modify(a,min(t1,t2)-abs(a-b));
}
int64 tmp=inf;
for(register int i=1;i<=n;i++) {
tmp=min(tmp,f[i]);
}
printf("%lld\n",tmp+sum);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct segment {
int l, r;
};
segment s[1010];
int n, ans[1010], m, pi[1010], d[1010];
bool operator<(const segment &a, const segment &b) { return a.r < b.r; }
int main() {
int i, j;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> s[i].l >> s[i].r;
if (s[i].l > s[i].r) swap(s[i].l, s[i].r);
}
sort(s + 1, s + n + 1);
s[0].r = s[0].l = -2147483647;
pi[0] = 0;
for (i = 1; i <= n; i++) {
j = i - 1;
while (pi[j] == m) {
if (s[j].r < s[i].l) {
d[i] = -1;
break;
}
j--;
}
if (d[i] == -1)
d[i] = d[i - 1] + 1, ans[++m] = s[i].r, pi[i] = m;
else
pi[i] = m, d[i] = d[i - 1];
}
printf("%d\n", d[n]);
for (i = 1; i <= m; i++) printf("%d ", ans[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
int N;
cin>>N;
if(N%10==3) cout<<"bon";
else if(N%10==0 || N%10==1 || N%10==6 || N%10==8) cout<<"pon";
else cout<<"hon";
} | 0 |
#include<iostream>
#include<algorithm>
using namespace std;
long long power(long long x, long long n){
long long ans = 1;
while(n){
if(n&1) ans *= x;
n >>= 1;
x *= x;
}
return ans;
}
long long Eulers_totient_function(int n){//1からnまでの自然数のnうちと互いに素なものの個数を求める
int count = 1;
for(int i = 2; i * i <= n; i++){
int con = 0;
while(n%i==0){
n/= i;
con++;
}
if(con) count *= (power(i,con)-power(i,con-1));
}
if(n != 1)count *= (power(n,1)-power(n,0));
return count;
}
signed main(){
int n;
cin>>n;
cout<<Eulers_totient_function(n)<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dirs[4][2]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void solve() {
int N, M, K;
cin >> N >> M >> K;
bool lake[N][M];
for (int n = (0); n < (N); ++n)
for (int m = (0); m < (M); ++m) {
char c;
cin >> c;
lake[n][m] = c == '.';
}
int id[N][M];
for (int n = (0); n < (N); ++n)
for (int m = (0); m < (M); ++m) id[n][m] = -1;
int cid = -1;
vector<pair<int, int> > sizes;
for (int n = (0); n < (N); ++n)
for (int m = (0); m < (M); ++m) {
if (id[n][m] == -1 && lake[n][m]) {
vector<pair<int, int> > active(1, {n, m});
id[n][m] = ++cid;
sizes.push_back({1, cid});
bool ocean = false;
while (!active.empty()) {
int y = active.back().first;
int x = active.back().second;
active.pop_back();
ocean = ocean || y == 0 || x == 0 || y == N - 1 || x == M - 1;
for (int dir = 0; dir < 4; ++dir) {
int nx = x + dirs[dir][0];
int ny = y + dirs[dir][1];
if (nx >= 0 && nx < M && ny >= 0 && ny < N && lake[ny][nx] &&
id[ny][nx] == -1) {
id[ny][nx] = cid;
active.push_back({ny, nx});
sizes.back().first++;
}
}
}
if (ocean) {
sizes.pop_back();
}
}
}
sort(sizes.begin(), sizes.end());
int res = 0;
bool dry[cid + 1];
fill_n(dry, cid + 1, false);
for (int i = 0; i < sizes.size() - K; ++i) {
res += sizes[i].first;
dry[sizes[i].second] = true;
}
cout << res << endl;
for (int n = (0); n < (N); ++n) {
for (int m = (0); m < (M); ++m)
cout << (id[n][m] == -1 || dry[id[n][m]] ? '*' : '.');
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(12) << fixed;
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int arr[101] = {0};
int main() {
int c = 0;
while (~scanf("%d", &c)) {
for (int i = 0; i < c; ++i) {
scanf("%d", &arr[i]);
}
sort(arr, arr + c);
if (c == 1) {
printf("%d\n", arr[0]);
} else {
printf("%d\n", arr[c / 2]);
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 1;
vector<int> gx[maxn + 7];
vector<int> gy[maxn + 7];
long long g[maxn + 7];
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
g[i] = maxn * x + y;
gx[x].push_back(y);
gy[y].push_back(x);
}
for (int i = 0; i <= maxn; i++) {
sort(gx[i].begin(), gx[i].end());
sort(gy[i].begin(), gy[i].end());
}
sort(g, g + n);
long long ans = 0;
for (int i = 0; i < n; i++) {
int x, y;
x = (int)(g[i] / maxn);
y = (int)(g[i] % maxn);
int idx, idy;
idx = lower_bound(gy[y].begin(), gy[y].end(), x + 1) - gy[y].begin();
idy = lower_bound(gx[x].begin(), gx[x].end(), y + 1) - gx[x].begin();
while (idx < gy[y].size() && idy < gx[x].size()) {
int nowx = gy[y][idx];
int nowy = gx[x][idy];
if (nowx - x == nowy - y) {
int pos = lower_bound(g, g + n, nowx * maxn + (long long)nowy) - g;
if (g[pos] == nowx * maxn + (long long)nowy) ans++;
idx++;
idy++;
} else if (nowx - x > nowy - y)
idy = lower_bound(gx[x].begin(), gx[x].end(), y + nowx - x) -
gx[x].begin();
else
idx = lower_bound(gy[y].begin(), gy[y].end(), x + nowy - y) -
gy[y].begin();
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int BIT = 17;
int n, m;
int P[N];
int pot2[33];
set<int> S[18];
set<int>::iterator it;
long long sol;
inline int find(int bit, int pos) {
if (S[bit].empty()) return 0;
it = S[bit].lower_bound(pos);
if (it == S[bit].begin()) return 0;
it--;
return *it + 1;
}
inline int find2(int bit, int pos) {
if (S[bit].empty()) return n - 1;
it = S[bit].lower_bound(pos);
if (it == S[bit].end()) return n - 1;
return *it - 1;
}
inline void load() {
pot2[0] = 1;
for (int i = 1; i <= BIT; i++) pot2[i] = (pot2[i - 1] << 1);
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &P[i]);
x = P[i];
for (int j = BIT; j >= 0; j--) {
if (x & (1 << j)) {
sol += (long long)pot2[j] * (i - find(j, i) + 1);
} else
S[j].insert(i);
}
}
}
inline void print() {
printf("SADRZAJ NA MJESTU == %d\n", 1);
for (set<int>::iterator it = S[0].begin(); it != S[0].end(); it++) {
printf("%d ", *it);
}
printf("\n");
system("pause");
}
inline void solve() {
for (; m; m--) {
int pos, val;
scanf("%d %d", &pos, &val);
pos--;
for (int j = BIT; j >= 0; j--) {
if (val & (1 << j)) {
if ((P[pos] & (1 << j)) == 0) {
S[j].erase(pos);
sol += (long long)pot2[j] * (pos - find(j, pos) + 1) *
(find2(j, pos) - pos + 1);
}
} else {
if ((P[pos] & (1 << j))) {
int gore = pos - find(j, pos) + 1;
int dole = find2(j, pos) - pos;
sol -= (long long)pot2[j] * dole * gore;
sol -= (long long)pot2[j] * gore;
S[j].insert(pos);
}
}
}
P[pos] = val;
cout << sol << endl;
}
}
int main(void) {
load();
solve();
return 0;
}
| 5 |
#include<cstdio>
#include<algorithm>
using namespace std;
int price(int a,int b,int c)
{
int na=a/200,nb=b/300,nc=c/500;
int pa=1520*(na/5)+380*(na%5);
int pb=1870*(nb/4)+550*(nb%4);
int pc=2244*(nc/3)+850*(nc%3);
return pa+pb+pc;
}
int main()
{
int g;
while(scanf("%d",&g),g){
int pmin=10000000;
for(int c=0;c<=g/500*500;c+=500){
for(int b=0;b+c<=g;b+=300){
int a=g-(b+c);
if(a%200==0){
pmin=min(pmin,price(a,b,c));
}
}
}
printf("%d\n",pmin);
}
return 0;
} | 0 |
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
int tx[] = {0,1,0,-1};
int ty[] = {-1,0,1,0};
static const double EPS = 1e-8;
class State {
public:
string cost;
int pos;
bool operator<(const State& s) const{
return cost < s.cost;
}
bool operator>(const State& s) const{
return cost > s.cost;
}
State() : cost(""),pos(0) {}
State(const string& _c,int _p) : cost(_c),pos(_p) {}
};
int main(){
int total_nodes;
int total_arrows;
int star_node_idx;
int gold_node_idx;
while(~scanf("%d %d %d %d",
&total_nodes,
&total_arrows,
&star_node_idx,
&gold_node_idx)){
if(total_nodes == 0
&& total_arrows == 0
&& star_node_idx == 0
&& gold_node_idx == 0) break;
string dp[total_nodes][401];
vector<string> nodes[total_nodes][total_nodes];
for(int i=0;i<total_nodes;i++){
for(int j=0;j<=400;j++){
dp[i][j] = string(1000,'~');
}
}
for(int arrow_idx = 0; arrow_idx < total_arrows; arrow_idx++){
int from,to;
string word;
cin >> from >> to >> word;
nodes[from][to].push_back(word);
}
priority_queue<State,vector<State>,greater<State> > que;
que.push(State("",star_node_idx));
dp[star_node_idx][0] = "";
string res(1000,'~');
while(!que.empty()){
State s = que.top();
que.pop();
if(s.cost >= res) continue;
for(int to=0;to<total_nodes;to++){
for(int str_idx = 0; str_idx < nodes[s.pos][to].size(); str_idx++){
string next_cost = s.cost + nodes[s.pos][to][str_idx];
if(next_cost >= res) continue;
if(next_cost.size() >= 400) continue;
if(next_cost < dp[to][next_cost.size()]){
dp[to][next_cost.size()] = next_cost;
if(to == gold_node_idx
&& res > next_cost){
res = next_cost;
}
que.push(State(next_cost,to));
}
}
}
}
if(res.size() > total_nodes * 6){
cout << "NO" << endl;
}
else{
cout << res << endl;
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
getchar();
char c;
while ((c = getchar()) != EOF) putchar(c == '1' ? '2' : c);
}
| 4 |
#include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
int field[92][92] = { };
int vx[4] = { 1, -1, 0, 0 };
int vy[4] = { 0, 0, 1, -1 };
int ans=0,ANSW=0;
int dfs(int X, int Y, int n){
if (field[X][Y]==0)return n;
field[X][Y] = 0;
for (int i = 0; i < 4; i++){
ans = max(ans, dfs(X + vx[i], Y + vy[i], n + 1));
}
field[X][Y] = 1;
return ans;
}
int main(){
while (1){
ans = 0, ANSW = 0;
int l, m;
scanf("%d%d", &l, &m);
if (l == 0 && m == 0)return 0;
for (int i = 0; i < l; i++){
for (int j = 0; j < m; j++){
cin>> field[i + 1][j + 1];
}
}
for (int i = 0; i < l; i++){
for (int j = 0; j < m; j++){
if (field[i + 1][j + 1])ANSW = max(ANSW, dfs(i + 1, j + 1, 0));
}
}
printf("%d\n", ANSW);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;
int ans[101][101];
vector<int> g[10001];
bool used[10001];
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int main(){
int n,ai,bi;
cin>>n;
int m=2*n*n-2*n;
rep(i,m){
cin>>ai>>bi;
--ai,--bi;
g[ai].pb(bi);
g[bi].pb(ai);
}
int cur=0;
rep(i,m){
if(g[i].size()==2){
cur=i;break;
}
}
int tx=0,ty=0,id=0;
while(1){
ans[ty][tx]=cur+1;
used[cur]=true;
ty=ty+dy[id],tx=tx+dx[id];
rep(i,g[cur].size()){
if(g[g[cur][i]].size()==2&&!used[g[cur][i]]){
cur=g[cur][i];
++id;
break;
}
else if(g[g[cur][i]].size()==3&&!used[g[cur][i]]){
cur=g[cur][i];break;
}
}
if(tx==0&&ty==0) break;
}
FOR(i,1,n-1)FOR(j,1,n-1){
int t1=ans[i-1][j]-1,t2=ans[i][j-1]-1;
map<int,int> mp;
rep(k,g[t1].size()) ++mp[g[t1][k]];
rep(k,g[t2].size()) ++mp[g[t2][k]];
for(auto it:mp)if(it.second==2&&!used[it.first]){
ans[i][j]=it.first+1;used[it.first]=true;
break;
}
}
rep(i,n){
rep(j,n) cout<<ans[i][j]<<" ";
cout<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename V>
ostream &operator<<(ostream &out, const pair<T, V> x) {
out << "{" << x.first << " : " << x.second << "}";
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const set<T> x) {
for (auto &it : x) {
out << it << " ";
}
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const multiset<T> x) {
for (auto &it : x) {
out << it << " ";
}
return out;
}
template <typename T, typename V>
ostream &operator<<(ostream &out, const map<T, V> x) {
for (auto &it : x) {
out << "[" << it.first << "]"
<< " = " << it.second << "\n";
}
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> x) {
for (int i = 0; i < x.size() - 1; ++i) {
out << x[i] << " ";
}
out << x.back();
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<vector<T> > x) {
for (int i = 0; i < x.size() - 1; ++i) {
out << "[" << i << "]"
<< " = {" << x[i] << "}\n";
}
out << "[" << x.size() - 1 << "]"
<< " = {" << x.back() << "}\n";
return out;
}
const long long N = 1e6 + 5;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
long long cnt[N][26];
void solve() {
long long n, k;
string s;
cin >> n >> k >> s;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < 26; ++j) cnt[i][j] = 0;
}
for (long long i = 0; i < n; ++i) {
long long md = i % k;
if (md >= (k / 2)) {
md = k - md - 1;
}
cnt[md][s[i] - 'a']++;
}
long long bk = k;
k = k / 2;
if (bk % 2 == 0) --k;
long long ans = 0;
for (long long i = 0; i <= k; ++i) {
long long mx = 0, al = 0;
for (long long j = 0; j < 26; ++j) {
mx = max(mx, cnt[i][j]);
al += cnt[i][j];
}
ans += al - mx;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vc;
int g, s = 0, b = 0;
bool check(int md, int n) {
if (md >= vc.size() - 1) {
g = 0;
b = 0;
s = 0;
return false;
}
g = 0;
s = 0;
b = 0;
g = vc[0];
int i = 1;
while (i <= md && s <= g) {
s += vc[i];
i++;
}
while (i <= md) {
b += vc[i];
i++;
}
if (g < b && g < s && (g + s + b) <= (n / 2)) {
return true;
} else {
g = 0;
b = 0;
s = 0;
return false;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.precision(6);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
map<int, int> mp;
vc.clear();
for (int i = 0; i < n; i++) {
int q;
cin >> q;
mp[q]++;
}
for (auto i = mp.begin(); i != mp.end(); i++) {
vc.push_back(i->second);
}
reverse(vc.begin(), vc.end());
int k = n;
while (vc.size() > 0 && k > (n / 2)) {
k -= vc.back();
vc.pop_back();
}
if (vc.size() < 3) {
cout << "0 0 0" << endl;
continue;
}
g = 0;
s = 0;
b = 0;
g = vc[0];
int c;
for (int i = 1; i < vc.size(); i++) {
s += vc[i];
if (s > g) {
c = i + 1;
break;
}
}
for (int i = c; i < vc.size(); i++) {
b += vc[i];
}
if (g < b && g < s) {
cout << g << " " << s << " " << b << endl;
} else {
cout << "0 0 0" << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, m, i, j, cont = 0, exito = 0;
int asteriscos[3][2];
scanf("%d %d", &n, &m);
char mat[n][m];
char a[m];
for (i = 0; i < n; i++) {
scanf("%s", a);
for (j = 0; j < m; j++) {
mat[i][j] = a[j];
if (mat[i][j] == '*') {
asteriscos[cont][0] = i;
asteriscos[cont][1] = j;
cont++;
}
}
}
for (i = 0; i < 2; i++) {
for (j = i + 1; j < 3; j++) {
if ((asteriscos[i][0] != asteriscos[j][0] &&
asteriscos[i][1] != asteriscos[j][1]) &&
exito == 0) {
if (mat[asteriscos[i][0]][asteriscos[j][1]] != '*') {
printf("%d %d\n", asteriscos[i][0] + 1, asteriscos[j][1] + 1);
exito = 1;
} else if (mat[asteriscos[j][0]][asteriscos[i][1]] != '*') {
printf("%d %d\n", asteriscos[j][0] + 1, asteriscos[i][1] + 1);
exito = 1;
}
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct Tpoint {
double x, y;
Tpoint() {}
Tpoint(double _x, double _y) {
x = _x;
y = _y;
}
inline void read() { scanf("%lf%lf", &x, &y); }
inline void show() { printf("%lf %lf\n", x, y); }
inline double norm() { return sqrt(((x) * (x)) + ((y) * (y))); }
};
inline Tpoint operator+(const Tpoint &a, const Tpoint &b) {
return Tpoint(a.x + b.x, a.y + b.y);
}
inline Tpoint operator-(const Tpoint &a, const Tpoint &b) {
return Tpoint(a.x - b.x, a.y - b.y);
}
inline Tpoint operator*(const Tpoint &a, const double &b) {
return Tpoint(a.x * b, a.y * b);
}
inline Tpoint operator/(const Tpoint &a, const double &b) {
return Tpoint(a.x / b, a.y / b);
}
inline double det(const Tpoint &a, const Tpoint &b) {
return a.x * b.y - a.y * b.x;
}
inline double dot(const Tpoint &a, const Tpoint &b) {
return a.x * b.x + a.y * b.y;
}
int Cnt[(1 << 10) + 5], f[(1 << 10) + 5][(1 << 10) + 5], n, m, K,
Ind[(1 << 10) + 5];
bool Adj[15][15];
inline int dfs(int tree, int leaf) {
if (Cnt[leaf] < 2) return 0;
if (Cnt[leaf] == 2 && tree == leaf) {
int u = Ind[((leaf) & (-(leaf)))];
int v = Ind[((leaf ^ (1 << u)) & (-(leaf ^ (1 << u))))];
return Adj[u][v];
}
int &res = f[tree][leaf];
if (res != -1) return res;
res = 0;
int u = Ind[((leaf) & (-(leaf)))];
for (int fa = 0; fa < n; ++fa)
if (((1 << fa) & tree) && !((1 << fa) & leaf) && Adj[fa][u])
res += dfs(tree ^ (1 << u), leaf ^ (1 << u)) +
dfs(tree ^ (1 << u), leaf ^ (1 << u) ^ (1 << fa));
return res;
}
int main() {
scanf("%d%d%d", &n, &m, &K);
memset(Adj, false, sizeof(Adj));
for (int i = 0; i < m; ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a;
--b;
Adj[a][b] = Adj[b][a] = true;
}
for (int i = 0; i < n; ++i) Ind[1 << i] = i;
for (int i = 1; i < 1 << n; ++i) Cnt[i] = Cnt[i / 2] + (i & 1);
int Ans = 0;
memset(f, -1, sizeof(f));
for (int leaf = 0; leaf < 1 << n; ++leaf)
if (Cnt[leaf] == K) Ans += dfs((1 << n) - 1, leaf);
printf("%d\n", Ans);
return 0;
}
| 5 |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#define mkp make_pair
typedef long long ll;
using namespace std;
ll L,A,B,M;
ll d[20];
ll MOD=1000000007;
typedef vector<ll> vec;//ll?
typedef vector<vec> mat;
mat mul(mat &A,mat &B){
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();i++){
for(int k=0;k<B.size();k++){
for(int j=0;j<B[0].size();j++){
C[i][j]=(C[i][j]+A[i][k]*B[k][j]%MOD)%MOD;
}
}
}
return C;
}
mat pow(mat A,ll n){
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++){
B[i][i]=1;
}
while(n>0){
if(n&1) B=mul(B,A);
A=mul(A,A);
n=(n>>1);
}
return B;
}
int digit(ll val){
if(val==0) return 0;
else return (1+digit(val/10));
}
int main(){
cin>>L>>A>>B>>M;
MOD=M;
ll ans=0;
ll s=A;
ll ten[20]={};
ten[0]=1;
for(int i=1;i<=18;i++) ten[i]=ten[i-1]*10%MOD;
ll start=0;
while(start<L){
ll ng=L;
ll ok=start;
ll nowdigit=digit(A+B*start);
while(ng-ok>1){
ll mid=(ng+ok)/2;
if(digit(A+B*mid)==nowdigit) ok=mid;
else ng=mid;
}
mat C(3,vec(3));
C[0][0]=ten[nowdigit];
C[0][1]=1;
C[0][2]=0;
C[1][0]=0;
C[1][1]=1;
C[1][2]=B%MOD;
C[2][0]=0;
C[2][1]=0;
C[2][2]=1;
C=pow(C,(ok-start+1));
mat D(3,vec(1));
D[0][0]=ans;
D[1][0]=(A+B*start)%MOD;
D[2][0]=1;
D=mul(C,D);
ans=D[0][0];
ans=(ans+MOD)%MOD;
start=ok+1;
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[70][70];
int main() {
long i, j, n, t, x[50000], y[50000];
int ri, rj, p;
bool ch;
cin >> n >> t;
for (i = 0; i < t; i++) cin >> x[i] >> y[i];
if (n <= 3) {
for (i = 0; i < t; i++)
if ((x[i] == 0) && (y[i] == 0))
cout << n << "\n";
else
cout << 0 << "\n";
} else {
a[0][0] = n % 4;
a[0][1] = n / 4;
rj = 2;
ch = true;
while (ch) {
ch = false;
for (j = 0; j < rj; j++)
for (i = 0; i <= j; i++)
if (a[i][j] > 3) {
rj = (j + 1 == rj) ? rj + 1 : rj;
p = a[i][j];
ch = true;
a[i][j + 1] += p / 4;
if (i == 1) a[0][j] += 2 * (p / 4);
if (i > 1) a[i - 1][j] += p / 4;
if (i == j - 1) {
a[i + 1][j] += 2 * (p / 4);
a[i][j - 1] += 2 * (p / 4);
if (i == 0) a[0][0] += 2 * (p / 4);
}
if (i < j - 1) {
a[i + 1][j] += p / 4;
a[i][j - 1] += p / 4;
}
a[i][j] = p % 4;
}
}
for (i = 0; i < t; i++)
if ((abs(x[i]) > 69) || (abs(y[i]) > 69))
cout << 0 << "\n";
else if (abs(x[i]) > abs(y[i]))
cout << a[abs(y[i])][abs(x[i])] << "\n";
else
cout << a[abs(x[i])][abs(y[i])] << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int ans[2003];
int c = 1001;
vector<int> mvs;
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
if (!ans[x - n + c]) {
ans[x - n + c] = 1;
q.push(x - n + c);
mvs.push_back(x - n);
}
}
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = 0; i < mvs.size(); i++) {
int y = x + mvs[i];
if (y > 2002 || y < 0 || ans[y] != 0) {
continue;
}
ans[y] = ans[x] + 1;
q.push(y);
}
}
if (ans[c] == 0) {
printf("-1\n");
} else {
printf("%d\n", ans[c]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int, int>> G[262144];
int d1[26][262144];
pair<int, int> d2[2][262144];
int vis[2][262144];
long long mod = 998244353ll;
long long qpow(long long x, long long y) {
long long a = x, ans = 1;
while (y) {
if (y & 1) ans = (ans * a) % mod;
y >>= 1;
a = (a * a) % mod;
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(make_pair(v, 0));
G[v].push_back(make_pair(u, 1));
}
queue<pair<int, int>> q;
q.push(make_pair(0, 1));
for (int i = 1; i <= n; i++)
for (int j = 0; j <= 25; j++) d1[j][i] = -1;
d1[0][1] = 0;
while (!q.empty()) {
pair<int, int> top = q.front();
q.pop();
int flips = top.first, u = top.second;
for (auto i : G[u]) {
int v = i.first, f = i.second;
int flip = ((flips & 1) ^ f);
if (flips + flip > 25) continue;
if (d1[flips + flip][v] == -1) {
d1[flips + flip][v] = d1[flips][u] + 1;
q.push(make_pair(flips + flip, v));
}
}
}
int ans = 2100000000;
for (int j = 0; j <= 25; j++)
if (d1[j][n] != -1) {
ans = min(ans, (1 << j) - 1 + d1[j][n]);
}
if (ans < 2100000000) {
cout << ans << endl;
return 0;
}
priority_queue<pair<pair<int, int>, pair<int, int>>> q2;
q2.push(make_pair(make_pair(0, 0), make_pair(0, 1)));
for (int i = 1; i <= n; i++)
for (int j = 0; j <= 1; j++) d2[j][i] = make_pair(2100000000, 2100000000);
d2[0][1] = make_pair(0, 0);
while (!q2.empty()) {
pair<pair<int, int>, pair<int, int>> top = q2.top();
q2.pop();
int flipsbit = top.second.first, u = top.second.second;
if (vis[flipsbit][u]) continue;
vis[flipsbit][u] = 1;
int flips = d2[flipsbit][u].first, dist = d2[flipsbit][u].second;
for (auto i : G[u]) {
int v = i.first, f = i.second;
int flip = (flipsbit ^ f);
int vflipsbit = ((flips + flip) & 1);
pair<int, int> dv = make_pair(flips + flip, dist + 1);
if (d2[vflipsbit][v] > dv) {
d2[vflipsbit][v] = dv;
q2.push(make_pair(make_pair(-dv.first, -dv.second),
make_pair(vflipsbit, v)));
}
}
}
int flips = d2[0][n].first, dist = d2[0][n].second;
if (d2[1][n].first < d2[0][n].first) {
flips = d2[1][n].first, dist = d2[1][n].second;
}
long long ans2 = (qpow(2, flips) + dist - 1) % mod;
cout << ans2 << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
bool f[241514];
int main(){
ll n,x;
cin>>n>>x;
if(x==1||x==2*n-1)cout<<"No\n";
else{
cout<<"Yes\n";
f[x]=f[x-1]=f[x+1]=1;
ll cnt=n-2;
ll now=1;
while(cnt){
if(!f[now]){
cout<<now<<endl;
cnt--;
}
now++;
}
cout<<x-1<<endl<<x<<endl<<x+1<<endl;
cnt=n-2;
while(cnt){
if(!f[now]){
cout<<now<<endl;
cnt--;
}
now++;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0, 0, 0};
const long long inf = 100000007;
const double eps = 0.000000009;
long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long s, k;
cin >> s >> k;
vector<long long> a;
a.push_back(0);
a.push_back(1);
long long sum = 1;
long long tt = 1e9 + 5;
for (int i = 2; i <= k; i++) {
a.push_back(pow(2, i - 2));
if (pow(2, i - 2) > tt) break;
}
while (a[a.size() - 1] < tt) {
long long x = 0;
for (int i = 0; i < k; i++) {
x += a[a.size() - 1 - i];
}
a.push_back(x);
}
vector<long long>::iterator it;
vector<long long> sol;
while (s) {
it = lower_bound(a.begin(), a.end(), s);
if (*it > s) it--;
s -= *it;
sol.push_back(*it);
}
if (sol.size() == 1) {
cout << 2 << endl << 0 << " " << sol[0] << endl;
} else {
cout << sol.size() << endl;
for (int i = 0; i < sol.size(); i++) cout << sol[i] << " ";
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
vector<long long> Sieve(long long n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (long long i = 2; i * i <= n; i++) {
if (prime[i]) {
for (long long j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
vector<long long> primes;
for (long long i = 2; i <= n; i++) {
if (prime[i]) primes.push_back(i);
}
return primes;
}
set<long long> primeFactors(long long n) {
set<long long> factors;
while (n % 2 == 0) {
n /= 2;
factors.insert(2);
}
for (long long i = 3; i <= sqrt(n); i += 2) {
while (n % i == 0) {
factors.insert(i);
n /= i;
}
}
if (n > 2) factors.insert(n);
return factors;
}
int32_t main() {
c_p_c();
long long t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long a[3];
long long l = s.length();
a[0] = a[1] = a[2] = -l;
long long ans = l;
for (long long i = 0; i < s.length(); i++) {
a[s[i] - '1'] = i;
ans = min(ans, i - min(a[0], min(a[1], a[2])));
}
if (ans == l) {
cout << 0 << '\n';
} else {
cout << ans + 1 << '\n';
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool vis[101];
set<int> g[101];
void dfs(int p) {
vis[p] = 1;
for (int i : g[p]) {
if (!vis[i]) {
dfs(i);
}
}
}
void run() {
int n, m;
cin >> n >> m;
bool on0 = 1;
set<int> l[m];
for (int w = 0; w < n; w++) {
int k;
cin >> k;
if (k > 0) {
on0 = 0;
}
for (int i = 0; i < k; i++) {
int x;
cin >> x;
x--;
l[x].insert(w);
}
}
if (on0) {
cout << n << endl;
return;
}
for (set<int> i : l) {
for (int j : i) {
for (int k : i) {
g[j].insert(k);
g[k].insert(j);
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
vis[i] = 0;
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i);
ans++;
}
}
cout << ans - 1 << endl;
}
int main() {
run();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int f(int hp, int atk, int def) {
int ur = atk - def;
if (ur <= 0) {
return 1000000000;
}
return hp / ur + (hp % ur != 0);
}
int main() {
int hpy, hpm, atkm, atky, defm, defy;
cin >> hpy >> atky >> defy;
cin >> hpm >> atkm >> defm;
int zhp, zatk, zdef;
cin >> zhp >> zatk >> zdef;
int ans = 1000000000;
for (int hpi = hpy; hpi <= 1000; hpi++) {
for (int atki = atky; atki <= 200; atki++) {
for (int defi = defy; defi <= 200; defi++) {
if (f(hpm, atki, defm) < f(hpi, atkm, defi)) {
if (ans >
zhp * (hpi - hpy) + zatk * (atki - atky) + zdef * (defi - defy)) {
ans =
zhp * (hpi - hpy) + zatk * (atki - atky) + zdef * (defi - defy);
}
}
}
}
}
cout << ans;
return 0;
}
| 1 |
#include<math.h>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<stdio.h>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include <iostream>
#include <limits.h>
using namespace std;
#define ull unsigned long long
#define pp pair<ull,ull>
const ull MAXN = 4233;
#define vecu vector<ull>
#define vevi vector<int>
#define vecp vector<pp >
const ull HASH_P1 = 4003;
const ull HASH_P2 = 4007;
const ull HASH_MOD1 = 1000247;
const ull HASH_MOD2 = 1000000037;
#define hash1(x,b) (((ull)x * HASH_P1 + b)%HASH_MOD1)
#define hash2(x,b) (((ull)x * HASH_P2 + b)%HASH_MOD2)
#define next_hash(tmp,b) (make_pair(hash1(tmp.first,b),hash2(tmp.second,b)))
#define add_hash(tmp,b) (make_pair((tmp.first + hash_hex[idx(b)].first) % HASH_MOD1,(tmp.second + hash_hex[idx(b)].second) % HASH_MOD2))
#define sub_hash(tmpa,tmpb) (make_pair((tmpa.first + HASH_MOD1 - tmpb.first) % HASH_MOD1 , (tmpa.second + HASH_MOD2 - tmpb.second)%HASH_MOD2 ) )
#define idx(x) (x-'a')
char str1[MAXN];
char str2[MAXN];
int str1_len,str2_len;
pp hash_hex[MAXN];
pp str1_hash[MAXN];
pp str2_hash[MAXN];
vecu hash_table[HASH_MOD1];
inline bool find_key(ull idx,ull key){
int len = hash_table[idx].size();
for(int i=0;i<len;++i){
if(hash_table[idx][i] == key)return true;
}return false;
}
inline void hash_insert(ull idx,ull key){
hash_table[idx].push_back(key);
}
inline void hash_clear(ull idx){
hash_table[idx].clear();
}
inline bool check(int length){
hash_insert(str1_hash[length-1].first,str1_hash[length-1].second);
for(int i=length;i<str1_len;++i){
pp tmp = sub_hash(str1_hash[i],str1_hash[i-length]);
hash_insert(tmp.first,tmp.second);
}
if(find_key(str2_hash[length-1].first,str2_hash[length-1].second))return true;
for(int i=length;i<str2_len;++i){
pp tmp = sub_hash(str2_hash[i],str2_hash[i-length]);
// hash_insert(tmp.first,tmp.second);
if(find_key(tmp.first,tmp.second))return true;
}
hash_clear(str1_hash[length-1].first);
for(int i=length;i<str1_len;++i){
pp tmp = sub_hash(str1_hash[i],str1_hash[i-length]);
hash_clear(tmp.first);
}
return false;
}
void init(){
str1_len = strlen(str1);
str2_len = strlen(str2);
str1_hash[0] = hash_hex[idx(str1[0])];
str2_hash[0] = hash_hex[idx(str2[0])];
for(int i=1;i<str1_len;++i)str1_hash[i] = add_hash(str1_hash[i-1],str1[i]);
for(int i=1;i<str2_len;++i)str2_hash[i] = add_hash(str2_hash[i-1],str2[i]);
int limit = min(str1_len,str2_len);
int ans = 0;
for(int i=limit;i;i--){
if(check(i)){
ans = i;
break;
}
}
cout<<ans<<"\n";
}
int main(){
hash_hex[0] = make_pair(1,1);
for(int i=1;i<233;++i) hash_hex[i] = next_hash(hash_hex[i-1],0);
// while(gets(str1)&&gets(str2))init();
while(~scanf("%s %s",str1,str2))init();
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
//type
typedef long long ll;
#define pii pair<int, int>
#define vi vector<int>
//x * y * 1.0 can cause overflow
//constant
#define inf (int)(1e9+7)
#define mod (ll)(1e9+7)
#define eps 1e-10
//omission
#define eb emplace_back
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define all(v) v.begin(), v.end()
#define ios cin.tie(0); ios::sync_with_stdio(false)
#define FIX(a) cout << fixed << setprecision(a)
//manip
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end(), v.end());
#define fill(x, y) memset(x, y, sizeof(x))
#define ceil(a, b) a / b + !!(a % b)
template<class T> T power(T a, T b)
{return b ? power(a * a % inf, b / 2) * (b % 2 ? a : 1) % inf : 1;}
#define LB(v, x) (int)(lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (int)(upper_bound(v.begin(), v.end(), x) - v.begin())
template<typename T> T gcd(T a, T b) {if (b == 0) return a; return gcd(b, a % b);}
template<typename T> T lcm(T a, T b) {return a / gcd(max(a, b), min(a, b)) * b;} //先に割る!!
#define outa(a, n) rep(i, n) cout << a[i] << " "; cout << endl
#define out(a) cout << a << endl
//loop
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep3(i, st, n) for (int i = st; i < n; ++i)
//algorithm
//double pointer, l start, how many adds, can be 0 -> init r = l, sum = 0
//bfs, not x, y, i, j
//not !(i % 2), i % 2 == 0
//minimal required terms
//cast caution
//look constraints always
int main() {
int n; cin >> n;
string s; cin >> s;
int ans = 1;
rep3(i, 1, SZ(s)) {
if (s[i] == s[i - 1] && s[i] == 'x') {
break;
}
ans++;
}
out(ans);
}
| 0 |
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
class people{
public:
int t,w;
people(void){}
people(int a,int b){t = a; w = b;}
const bool operator<(const people a)const{return t>a.t;}
};
int main(){
int n;
int m,a,b;
for(;;){
cin >> n;
if(!n)break;
priority_queue<people> in,out;
for(int i=0;i<n;i++){
cin >> m >> a >> b;
in.push(people(a,m));
out.push(people(b,m));
}
int now=0;
while(in.size() || out.size()){
people p1,p2;
if(in.empty()){
p1 = out.top();out.pop();
now -= p1.w;
}else if(out.empty()){
p1 = in.top();in.pop();
now += p1.w;
}else{
p1 = out.top();
p2 = in.top();
if(p1.t>p2.t){
in.pop();
now += p2.w;
}else{
out.pop();
now -= p1.w;
}
}
if(now>150)break;
}
if(now>150)cout << "NG\n";
else cout << "OK\n";
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
typename T::value_type arr_sum(const T& v, int n) {
typename T::value_type sum = 0;
for (int i = (0); i < (n); ++i) sum += v[i];
return sum;
}
struct Sync_stdio {
Sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
} _sync_stdio;
int main() {
string s1, s2;
cin >> s1 >> s2;
if (s1.size() != s2.size()) {
cout << "NO";
return 0;
}
cout << (((count(s1.begin(), s1.end(), '0') == s1.size()) ^
(count(s2.begin(), s2.end(), '0') == s2.size()))
? "NO"
: "YES");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
long long now = 0;
for (long long i = 1; i <= n; ++i) {
if (i == 1)
printf("2\n");
else
printf("%lld\n", i * (i + 1) * (i + 1) - (i - 1));
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[52][52][52][102], n, m, C[102][102], p, ans;
int main() {
cin >> n >> m >> p;
for (int i = 0; i < 102; i++) C[i][0] = 1;
for (int i = 1; i < 102; i++)
for (int j = 1; j <= i; j++)
C[i][j] = min(C[i - 1][j - 1] + C[i - 1][j], 102);
n /= 2;
for (int i = 1; i <= n; i++) a[1][i][i][1] = 1;
for (int i = 1; i <= m && i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++)
for (int l = 1; l <= p; l++) {
int e = a[i][j][k][l];
ans = (ans + (long long)e * (m - i)) % 1000000007;
for (int q = 1; q <= n - k; q++) {
int w = l * C[j + q - 1][j - 1];
if (w > p) continue;
a[i + 1][q][k + q][w] = (a[i + 1][q][k + q][w] + e) % 1000000007;
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
using namespace std;
vector<string> vs;
bool used[1010][100];
int search(int x,int y){
// 0->E,1->º,2->¶,3->ã
int sit=0;
int cx=x;
int cy=y;
int over=1;
int under=1;
int high=0;
int dx=1;
int dy=0;
int miny=10000000;
int maxy=-1000000;
while(1){
used[cy][cx]=true;
int nx=cx+dx;
int ny=cy+dy;
if(nx>=0&&ny>=0&&ny<vs.size()&&nx<vs[ny].size()
&&vs[ny][nx]=='*'){
cx=nx;
cy=ny;
miny=min(miny,ny);
maxy=max(maxy,ny);
if(sit==0)
over++;
else if(sit==2)
under++;
if(sit==3&&ny==y&&nx==x)
break;
}
// Ú®ûüÌÏX
else{
if(sit==0){
for(int i = -1; i <=1; i++){
int nnx=cx+i;
int nny=cy+1;
int nnnx=nnx+i;
int nnny=nny+1;
if(nnx>=0&&nny<vs.size()&&nny>=0&&nnx<vs[nny].size()
&&vs[nny][nnx]=='*'
&&nnnx>=0&&nnny<vs.size()&&nnny>=0&&nnnx<vs[nnny].size()
&&vs[nnny][nnnx]=='*'
){
dy=1;
dx=i;
break;
}
}
sit=1;
}
else if(sit==1){
// ¶ÖÚ®
dx=-1;
dy=0;
sit=2;
}
else if(sit==2){
// ãÖÚ®
for(int i = -1; i <=1; i++){
int nnx=cx+i;
int nny=cy-1;
int nnnx=nnx+i;
int nnny=nny-1;
if(nnx>=0&&nny<vs.size()&&nny>=0&&nnx<vs[nny].size()
&&vs[nny][nnx]=='*'
&&nnnx>=0&&nnny<vs.size()&&nnny>=0&&nnnx<vs[nnny].size()
&&vs[nnny][nnnx]=='*'
){
dy=-1;
dx=i;
break;
}
}
sit=3;
}
}
// if(sit==0){
// int nx=cx+dx;
// int ny=cy+dy;
// if(nx>=0&&ny>=0&&ny<vs.size()&&nx<vs[ny].size()
// &&vs[ny][nx]=='*'){
// cx=nx;
// cy=ny;
// miny=min(miny,ny);
// maxy=max(maxy,ny);
// over++;
// }
// // ºÖÚ®·éBÚ®ûüàT·
// else{
// for(int i = -1; i <=1; i++){
// int nnx=cx+i;
// int nny=cy+1;
// int nnnx=nnx+i;
// int nnny=nny+1;
// if(nnx>=0&&nny<vs.size()&&nny>=0&&nnx<vs[nny].size()
// &&vs[nny][nnx]=='*'
// &&nnnx>=0&&nnny<vs.size()&&nnny>=0&&nnnx<vs[nnny].size()
// &&vs[nnny][nnnx]=='*'
// ){
// dy=1;
// dx=i;
// break;
// }
// }
// sit=1;
// }
// }
// else if(sit==1){
// int nx=cx+dx;
// int ny=cy+dy;
// if(nx>=0&&ny<vs.size()&&ny>=0&&nx<vs[ny].size()
// &&vs[ny][nx]=='*'){
// cx=nx;
// cy=ny;
// miny=min(miny,ny);
// maxy=max(maxy,ny);
// }
// else{
// // ¶ÖÚ®
// dx=-1;
// dy=0;
// sit=2;
// }
// }
// else if(sit==2){
// int nx=cx+dx;
// int ny=cy+dy;
// if(nx>=0&&ny<vs.size()&&ny>=0&&nx<vs[ny].size()
// &&vs[ny][nx]=='*'){
// under++;
// cx=nx;
// cy=ny;
// miny=min(miny,ny);
// maxy=max(maxy,ny);
// }
// else{
// // ãÖÚ®
// for(int i = -1; i <=1; i++){
// int nnx=cx+i;
// int nny=cy-1;
// int nnnx=nnx+i;
// int nnny=nny-1;
// if(nnx>=0&&nny<vs.size()&&nny>=0&&nnx<vs[nny].size()
// &&vs[nny][nnx]=='*'
// &&nnnx>=0&&nnny<vs.size()&&nnny>=0&&nnnx<vs[nnny].size()
// &&vs[nnny][nnnx]=='*'
// ){
// dy=-1;
// dx=i;
// break;
// }
// }
// sit=3;
// }
// }
// else{
// int nx=cx+dx;
// int ny=cy+dy;
// cx=nx;
// cy=ny;
// if(ny==y&&nx==x)
// break;
// }
}
int res=(over+under)*(abs(miny-maxy)+1)/2;
return res;
}
int main(){
int n;
bool b=false;
while(cin>>n&&n!=0){
if(b){
cout<<"----------"<<endl;
}
b=true;
memset(used,0,sizeof(used));
int maxLength=0;
string str;
getline(cin,str);
for(int i = 0; i < n; i++){
getline(cin,str);
vs.push_back(str);
maxLength=max(maxLength,(int)str.size());
}
for(int i = 0; i < n; i++){
int diff=maxLength-vs[i].size();
for(int j = 0; j < diff; j++){
vs[i]+=' ';
}
}
map<int,int> mii;
int res=0;
for(int i = 0; i < vs.size(); i++){
for(int j= 0;j<vs[i].size();j++){
if(vs[i][j]=='*'&&!used[i][j]){
//cout<<i<<" "<<j<<endl;
//cout<<search(j,i)<<endl;
mii[search(j,i)]++;
}
}
}
for(map<int,int>::iterator it=mii.begin();it!=mii.end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
vs.clear();
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k,i=1,g,h;
cin>>n>>k;
set<int>p;
while(n--)
{
p.insert(i);
i++;
}
while(k--)
{
cin>>g;
while(g--)
{
cin>>h;
if(p.count(h)==1)
p.erase(h);
}
}
cout<<p.size();
}
| 0 |