solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int main(){
int N;
while(cin>>N,N){
vector<int> card[2];
int ba=-1,in;
bool latte[201]={},turn=0;
for(int i=0;i<N;i++){
cin>>in;
latte[in]=true;
}
for(int i=1;i<=2*N;i++){
card[!latte[i]].push_back(i);
}
sort(card[0].begin(),card[0].end()); sort(card[1].begin(),card[1].end());
while(card[0].size() && card[1].size()){
vector<int>::iterator itr =
upper_bound(card[turn].begin(),card[turn].end(),ba);
if(itr==card[turn].end()) ba=-1;
else{
ba = *itr;
card[turn].erase(itr);
}
turn = !turn;
}
printf("%d\n%d\n",card[1].size(),card[0].size());
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int y, k, n;
cin >> y >> k >> n;
bool ok = false;
for (int i = k; i <= n; i += k) {
if (i - y > 0) {
cout << i - y << " ";
ok = true;
}
}
if (!ok) cout << -1 << endl;
return 0;
}
| 1 |
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;
#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }
const int MAX = 500;
const int INF = 1<<29;
map<string,int> ma;
int ndp[MAX][MAX];
int ddp[MAX][MAX];
int dp[MAX][MAX];
int N, M;
int main() {
//freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin );
while (cin >> N) {
if (N == 0) break;
ma.clear();
for (int i = 0; i < MAX; ++i) for (int j = 0; j < MAX; ++j) ndp[i][j] = ddp[i][j] = dp[i][j] = INF;
for (int i = 0; i < MAX; ++i) ndp[i][i] = ddp[i][i] = dp[i][i] = 0;
vector<string> alt;
int it = 0;
for (int i = 0; i < N; ++i) {
string str; cin >> str;
for (int j = 0; j < str.size(); ++j) if (str[j] == '-') str[j] = ' ';
istringstream sin(str);
string sub1, sub2;
sin >> sub1 >> sub2;
if (!ma.count(sub1)) ma[sub1] = it++, alt.PB(sub1);
if (!ma.count(sub2)) ma[sub2] = it++, alt.PB(sub2);
ndp[ma[sub1]][ma[sub2]] = 1;
ndp[ma[sub2]][ma[sub1]] = 1;
ddp[ma[sub1]][ma[sub2]] = 1;
}
int n = it;
for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
chmin(ndp[i][j], ndp[i][k] + ndp[k][j]);
chmin(ddp[i][j], ddp[i][k] + ddp[k][j]);
}
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
chmin(dp[i][j], ddp[i][j]);
if (ndp[i][j] == 2 && ddp[i][j] >= INF && ddp[j][i] >= INF) dp[i][j] = 0;
}
for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
chmin(dp[i][j], dp[i][k] + dp[k][j]);
}
// COUT(ma);
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
//cout << alt[i] << ", " << alt[j] << " : " << ndp[i][j] << ", " << ddp[i][j] << ", " << dp[i][j] << endl;
}
cout << ma.size() << endl;
cin >> M;
for (int i = 0; i < M; ++i) {
string str; cin >> str;
for (int j = 0; j < str.size(); ++j) if (str[j] == '-') str[j] = ' ';
istringstream sin(str);
string sub1, sub2;
sin >> sub1 >> sub2;
if (!ma.count(sub1)) { puts("NO"); continue; }
if (!ma.count(sub2)) { puts("NO"); continue; }
int u = ma[sub1], v = ma[sub2];
if (ndp[u][v] >= INF) puts("NO");
else {
if (ndp[u][v] % 2 == 0) puts("NO");
else {
if (dp[u][v] < INF) puts("YES");
else puts("NO");
}
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
x *= f;
}
int tb[1010], t[1010], a[1010], usd[1010], n, opt[1010], opn;
char ans[1010][1010];
int main() {
read(n);
for (int i = 1; i <= n; ++i) read(a[i]), t[a[i]] = i;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) ans[i][j] = '.';
int nw = n;
for (int i = n; i >= 1; --i)
if (!usd[i] && a[i] != i && a[i] != n) {
int x = i;
opn = 0;
while (!usd[x]) opt[++opn] = x, usd[x] = 1, x = a[x];
sort(opt + 1, opt + 1 + opn);
for (int x = t[opt[opn]], i = 1; i < opn; x = t[x], ++i)
if (a[x] != n) {
tb[x] = nw--;
if (a[x] > x)
ans[tb[x]][x] = '/', ans[tb[x]][a[x]] = '/';
else
ans[tb[x]][x] = '\\', ans[tb[x]][a[x]] = '\\';
}
x = opt[opn];
if (a[x] != n) {
tb[x] = nw--;
ans[tb[x]][a[x]] = '\\';
ans[tb[x]][n] = '\\';
ans[tb[t[x]]][n] = '/';
}
}
cout << ((nw == n) ? (n) : (n - 1)) << endl;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) putchar(ans[i][j]);
puts("");
}
return 0;
}
| 5 |
#include<bits/stdc++.h>
#define ll long long
#define N 10010
#define mr make_pair
using namespace std;
int n,g[2],s[N][2],w[N];
string dp[N];
char ch[N];
string que(int l,int r){string no="";for (int i=0;i<2*n;i++)if (w[i]>=l&&w[i]<=r)no+=ch[i];return no;}
int main(){
cin>>n;
for (int i=0;i<2*n;i++){
ch[i]=getchar();while (ch[i]!='a'&&ch[i]!='b')ch[i]=getchar();
g[ch[i]-'a']++;s[g[ch[i]-'a']][ch[i]-'a']=i;w[i]=g[ch[i]-'a'];
}
for (int i=n;i>=1;i--){
dp[i]=dp[i+1];
if (s[i][0]<s[i][1]){
int x=i+1;
while (x<=n&&s[x][0]<s[i][1])x++;
dp[i]=max(dp[i],"ab"+dp[x]);
}else{
int x=i+1,y=s[i][0];
while (x<=n&&s[x][1]<y)y=s[x][0],x++;
dp[i]=max(dp[i],que(i,x-1)+dp[x]);
}
}
cout<<dp[1]<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct DSU {
int nbElem;
vector<int> repr, weight;
DSU(int ta) : nbElem(ta), repr(ta), weight(ta, 1) {
iota(repr.begin(), repr.end(), 0);
}
int get(int x) {
if (repr[x] != x) repr[x] = get(repr[x]);
return repr[x];
}
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) return false;
if (weight[a] < weight[b]) swap(a, b);
repr[b] = a;
weight[a] += weight[b];
return true;
}
};
struct Query {
int pos, val;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int nbRow, nbCol, nbQuery, nbVal;
cin >> nbRow >> nbCol >> nbQuery;
int nbPos = nbRow * nbCol;
nbVal = 0;
vector<Query> queries(nbQuery);
for (auto &query : queries) {
int row, col;
cin >> row >> col >> query.val;
--row;
--col;
query.pos = row * nbCol + col;
assert(query.pos < nbPos);
nbVal = max(nbVal, query.val + 1);
}
vector<int> grid(nbRow * nbCol, 0);
vector<vector<pii>> add(nbVal);
vector<vector<pii>> rem(nbVal);
for (int iQuery = 0; iQuery < nbQuery; ++iQuery) {
const auto &query = queries[iQuery];
if (grid[query.pos] == query.val) {
continue;
}
rem[grid[query.pos]].push_back({iQuery, query.pos});
grid[query.pos] = query.val;
add[query.val].push_back({iQuery, query.pos});
}
for (int iPos = 0; iPos < nbPos; ++iPos) {
rem[grid[iPos]].push_back({nbQuery, iPos});
}
vector<vector<pii>> setComp(nbQuery + 1);
auto process = [&](int val, vector<pii> infos, bool isRem) {
DSU uf(nbPos);
int nbComp = 0;
vector<bool> isOn(nbPos, false);
for (auto &info : infos) {
int iQuery = info.first, pos = info.second;
if (isRem) {
setComp[iQuery].push_back({val, nbComp});
}
++nbComp;
int row = pos / nbCol, col = pos % nbCol;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
bool corr = (dx != 0) ^ (dy != 0);
if (!corr) continue;
int newRow = row + dx, newCol = col + dy;
if (newRow < 0 || newRow >= nbRow) continue;
if (newCol < 0 || newCol >= nbCol) continue;
int newPos = newRow * nbCol + newCol;
if (isOn[newPos] && uf.unite(pos, newPos)) {
--nbComp;
}
}
}
isOn[pos] = true;
assert(nbComp >= 1);
if (!isRem) {
setComp[iQuery].push_back({val, nbComp});
}
}
};
for (int iVal = 0; iVal < nbVal; ++iVal) {
process(iVal, add[iVal], false);
reverse(rem[iVal].begin(), rem[iVal].end());
process(iVal, rem[iVal], true);
}
vector<int> stateVal(nbVal, 0);
stateVal[0] = 1;
int sumState = 1;
for (int iQuery = 0; iQuery < nbQuery; ++iQuery) {
for (auto &todo : setComp[iQuery]) {
int val = todo.first, toSet = todo.second;
int diff = toSet - stateVal[val];
stateVal[val] += diff;
sumState += diff;
}
cout << sumState << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long int n, m, h;
cin >> n >> m >> h;
long long int a[m + 1];
long long int tot = 0;
for (long long int i = 1; i <= m; i++) {
cin >> a[i];
tot += a[i];
}
if (tot < n) {
cout << -1 << "\n";
return;
}
long double n1 = 1;
long double d1 = 1;
for (long long int i = 0; i <= n - 2; i++) {
n1 *= (tot - a[h] - i);
}
for (long long int i = 1; i <= n - 1; i++) {
d1 *= (tot - i);
}
long double p1 = (n1 / d1);
long double ans = 1 - p1;
cout << fixed << setprecision(18);
;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t, q;
q = 1;
for (t = 1; t <= q; t++) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Segment {
int l, r, id;
friend inline bool operator<(const Segment &a, const Segment &b) {
if (a.l != b.l) return a.l > b.l;
if (a.r != b.r) return a.r < b.r;
return a.id < b.id;
}
} t[N];
namespace BIT {
int c[N];
inline void add(int x, int y) {
for (; x <= N; x += x & (-x)) c[x] += y;
}
inline int ask(int x) {
int res = 0;
for (; x; x -= x & (-x)) res += c[x];
return res;
}
} // namespace BIT
using BIT::add;
using BIT::ask;
int n, m, cnt, pos[N], ans[N];
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1, l, r; i <= n; i++) {
cin >> l >> r;
t[++cnt] = (Segment){l, r, 0};
}
for (int i = 1, tot; i <= m; i++) {
cin >> tot;
for (int j = 1; j <= tot; j++) cin >> pos[j];
for (int j = 1; j <= tot; j++)
if (pos[j] > 1 && pos[j - 1] + 1 < pos[j])
t[++cnt] = (Segment){pos[j - 1] + 1, pos[j] - 1, i};
t[++cnt] = (Segment){pos[tot] + 1, N - 5, i};
}
sort(t + 1, t + cnt + 1);
for (int i = 1; i <= cnt; i++)
if (t[i].id)
ans[t[i].id] += ask(t[i].r);
else
add(t[i].r, 1);
for (int i = 1; i <= m; i++) cout << n - ans[i] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
int cnt1 = n * a, cnt2, cnt3 = 1000000000;
if (n % m == 0) {
cnt2 = (n / m) * b;
} else {
cnt2 = ((n / m) + 1) * b;
cnt3 = (n / m) * b + (n % m) * a;
}
cout << min(cnt1, min(cnt2, cnt3)) << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long int ara[999999];
int main() {
ios_base::sync_with_stdio(0);
long int n, t, i, c = 0, s = 0;
cin >> n >> t;
for (i = 1; i <= n; i++) cin >> ara[i];
for (i = 1; i <= n; i++) {
if (c < t) {
c = c + (86400 - ara[i]);
s++;
}
}
cout << s;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int n, m, k, a[1100000], pw2[1100000], sum[1100000];
int main() {
pw2[0] = 1;
for (int i = (1); i <= (1010000); i++)
pw2[i] = (long long)pw2[i - 1] * 2 % MOD;
cin >> n >> m >> k;
for (int i = (1); i <= (m); i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x + 1 == y) continue;
if (y - x != k + 1) {
puts("0");
return 0;
}
a[y] = 1;
}
for (int i = (1); i <= (n); i++) sum[i] = sum[i - 1] + a[i];
for (int x = (1); x <= (n); x++) {
if (x - k - 2 >= 0)
if (a[x] && sum[x - k - 1]) {
puts("0");
return 0;
}
}
int ans = 1;
for (int x = (n); x >= (1); x--) {
if (x - k - 1 <= 0) break;
int p = sum[x - 1] - sum[x - k - 1];
if (p + a[x] == sum[n]) {
int num = min(k, x - k - 2);
num -= p;
ans = (ans + (long long)pw2[num]) % MOD;
if (a[x]) ans = (ans - 1 + MOD) % MOD;
}
if (a[x]) break;
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int sum = 1;
while (n > (sum * 5)) {
n = n - sum * 5;
sum = sum * 2;
}
n = n - 1;
long long int k = n / sum;
switch (k) {
case 0:
cout << "Sheldon";
break;
case 1:
cout << "Leonard";
break;
case 2:
cout << "Penny";
break;
case 3:
cout << "Rajesh";
break;
case 4:
cout << "Howard";
break;
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<string, int> p;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
string s;
cin >> s;
long long ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'B') ans = ans + pow(2, i);
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void chkmin(T &a, T b) {
if (a > b) a = b;
}
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') ch = getchar(), f = -1;
while (isdigit(ch)) s = s * 10 + ch - '0', ch = getchar();
return ~f ? s : -s;
}
const int maxn = 3e5 + 20;
vector<int> ed[maxn];
int h[maxn];
int n, m, sg[maxn], rd[maxn];
int rk[maxn], tp;
inline void get_topx() {
queue<int> q;
for (int i = (1), _end_ = (n); i <= _end_; i++)
if (!rd[i]) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
rk[++tp] = u;
for (int v : ed[u])
if (!(--rd[v])) q.push(v);
}
}
inline void init() {
n = read();
m = read();
for (int i = (1), _end_ = (n); i <= _end_; i++) h[i] = read();
for (int i = (1), _end_ = (m); i <= _end_; i++) {
int u = read(), v = read();
ed[u].push_back(v);
rd[v]++;
}
get_topx();
}
int vis[maxn];
int sum[maxn];
inline void doing() {
for (int i = (n), _end_ = (1); i >= _end_; i--) {
int x = rk[i];
for (int y : ed[x]) vis[sg[y]]++;
while (vis[sg[x]]) ++sg[x];
for (int y : ed[x]) vis[sg[y]]--;
}
for (int i = (1), _end_ = (n); i <= _end_; i++) sum[sg[i]] ^= h[i];
for (int i = (n), _end_ = (0); i >= _end_; i--)
if (sum[i]) {
puts("WIN");
for (int j = (1), _end_ = (n); j <= _end_; j++)
if (sg[j] == i && (h[j] ^ sum[i]) < h[j]) {
h[j] = h[j] ^ sum[i];
for (int k : ed[j])
if (sum[sg[k]]) h[k] ^= sum[sg[k]], sum[sg[k]] = 0;
break;
}
for (int j = (1), _end_ = (n); j <= _end_; j++) printf("%d ", h[j]);
return;
}
puts("LOSE");
}
int main() {
init();
doing();
return 0;
}
| 5 |
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
int n;
cin >> n;
cout << n << ":";
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
n /= i;
cout << " " << i;
}
}
if (n != 1) cout << " " << n;
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
void Enter() { cin >> n >> a >> b; }
void Solve() {
int kq = 0;
for (int i = 1; i < n; i++) {
kq = max(kq, min(a / i, b / (n - i)));
}
cout << kq;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Enter();
Solve();
}
| 2 |
#include<cstdio>
#define MAX 100
int A[MAX][MAX] = {0};
int main()
{
int n,u,k,v;
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d%d", &u, &k);
for(int j=0; j<k; j++)
{
scanf("%d", &v);
A[u-1][v-1] = 1;
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(j) printf(" ");
printf("%d", A[i][j]);
}
printf("\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, sum, jum, ans;
string s;
map<long long, long long> m, k;
map<long long, bool> b;
vector<long long> v;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s;
sum = 0;
jum = 0;
for (int j = 0; j < s.length(); j++) {
if (s[j] == ')') {
if (jum) {
jum--;
} else {
sum++;
}
} else {
jum++;
}
}
if (sum && jum) {
continue;
} else if (jum) {
if (b[jum] == false) {
b[jum] = true;
v.push_back(jum);
}
k[jum]++;
} else if (sum) {
m[sum]++;
} else {
ans++;
}
}
ans *= ans;
for (int i = 0; i < v.size(); i++) {
ans += m[v[i]] * k[v[i]];
}
cout << ans << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
int n;
cin >> n;
vector<pair<int, int> > arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].first >> arr[i].second;
}
if (n % 2) {
cout << "no" << endl;
return 0;
}
vector<int> l1, l2, l3, l4;
for (int i = 0; i < n - 1; i++) {
l1.push_back(arr[i].first);
l2.push_back(arr[i].second);
l3.push_back(arr[i + 1].first);
l4.push_back(arr[i + 1].second);
}
l1.push_back(arr[n - 1].first);
l2.push_back(arr[n - 1].second);
l3.push_back(arr[0].first);
l4.push_back(arr[0].second);
int flag = 1;
for (int i = 0; i < n / 2; i++) {
int j = i + n / 2;
int pay1 = l1[i] - l3[i];
int payda1 = l2[i] - l4[i];
int par1 = 1;
if (pay1 < 0) {
par1 *= -1;
pay1 *= -1;
}
if (payda1 < 0) {
par1 *= -1;
payda1 *= -1;
}
int pay2 = l1[j] - l3[j];
int payda2 = l2[j] - l4[j];
int par2 = 1;
if (pay2 < 0) {
par2 *= -1;
pay2 *= -1;
}
if (payda2 < 0) {
par2 *= -1;
payda2 *= -1;
}
if (pay1 == 0 || payda1 == 0) par1 = 0;
if (pay2 == 0 || payda2 == 0) par2 = 0;
if (pay1 != pay2 || payda1 != payda2 || par1 != par2) flag = 0;
}
if (flag) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <int D, typename T>
struct dense : public vector<dense<D - 1, T>> {
template <typename... Args>
dense(int n = 0, Args... args)
: vector<dense<D - 1, T>>(n, dense<D - 1, T>(args...)) {}
};
template <typename T>
struct dense<1, T> : public vector<T> {
dense(int n = 0, const T &val = T()) : vector<T>(n, val) {}
};
inline void update(int64_t &A, int64_t B) {
if (A > B) A = B;
}
int64_t dp[2][500][500][2];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
dense<3, pair<int, int>> robot(n, m);
vector<int> d(k), e(k);
dense<2, bool> good(k, 4);
vector<int> dx = {0, 1, 1, 0}, dy = {0, -1, 0, 1};
for (int i = 0; i < k; i++) {
int x, y, t;
cin >> x >> y >> d[i] >> t >> e[i];
for (int zi = 0; zi < 4; zi++) {
int nx = x + dx[zi] * d[i], ny = y + dy[zi] * d[i];
if (zi + t <= nx + ny && (zi + t) % 4 == (nx + ny) % 4) {
robot[nx][ny].push_back({i, zi});
good[i][zi] = 1;
}
}
}
int64_t ans = LLONG_MAX;
for (int X = 0; X < 2; X++)
for (int Y = 0; Y < m; Y++)
for (int W = 0; W < max(n, m); W++)
for (int vert = 0; vert < 2; vert++) dp[X][Y][W][vert] = LLONG_MAX;
dp[0][0][0][0] = dp[0][0][0][1] = 0;
for (int X = 0; X < n; X++) {
int cur = X & 1;
int nxt = 1 - cur;
for (int Y = 0; Y < m; Y++) {
for (int W = 0; W < max(n, m); W++) {
for (int vert = 0; vert < 2; vert++) {
if (W > (vert ? X : Y) || dp[cur][Y][W][vert] == LLONG_MAX) continue;
if (Y < m - 1) {
int right = (vert ? 0 : W) + 1;
int64_t smash = 0;
for (auto &[id, zi] : robot[X][Y + 1]) {
if (zi == 2 && right >= d[id] && good[id][1]) continue;
if (zi == 3 && right >= d[id] && good[id][0]) continue;
smash += e[id];
}
update(dp[cur][Y + 1][right][0], dp[cur][Y][W][vert] + smash);
update(dp[cur][Y + 1][0][1], dp[cur][Y][W][vert] + smash);
}
if (X < n - 1) {
int down = (vert ? W : 0) + 1;
int64_t smash = 0;
for (auto &[id, zi] : robot[X + 1][Y]) {
if (zi == 2 && down >= d[id] && good[id][0]) continue;
smash += e[id];
}
update(dp[nxt][Y][down][1], dp[cur][Y][W][vert] + smash);
update(dp[nxt][Y][0][0], dp[cur][Y][W][vert] + smash);
}
}
}
}
if (X == n - 1) {
for (int W = 0; W < max(n, m); W++)
for (int vert = 0; vert < 2; vert++)
update(ans, dp[cur][m - 1][W][vert]);
break;
}
for (int Y = 0; Y < m; Y++)
for (int W = 0; W < max(n, m); W++)
for (int vert = 0; vert < 2; vert++) dp[cur][Y][W][vert] = LLONG_MAX;
}
cout << ans;
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
string str , ans[6003]; int L;
string solve(int id){
if(id == 2 * L) return string();
if(ans[id].size()) return ans[id];
if(str[id] == 'a'){
int sum = 0 , pos = id;
while(pos < 2 * L && sum <= 0) sum += str[pos++] == 'a' ? -1 : 1;
if(pos == 2 * L){
queue < int > stk; int r = -1;
for(int i = id ; i < 2 * L ; ++i)
if(str[i] == 'a') stk.push(i);
else{if(stk.front() > r){r = i; ans[id] += "ab";} stk.pop();}
}
else ans[id] = solve(pos - 1);
}
else{
int sum = 1 , pos = id + 1;
while(pos < 2 * L && sum > 0) sum += str[pos++] == 'a' ? -1 : 1;
string temp = solve(pos); int num = (pos - id) / 2;
for(int i = 0 ; i <= num ; ++i){
int cnta = 0 , cntb = 0; string now;
for(int j = id ; j < pos ; ++j)
if(str[j] == 'a' && ++cnta > i) now += 'a';
else if(str[j] == 'b' && ++cntb > i) now += 'b';
now += temp; if(ans[id] < now) ans[id] = now;
}
}
return ans[id];
}
int main(){cin >> L >> str; solve(0); cout << ans[0]; return 0;} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, S;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> S;
if (S < 2 * N) {
cout << "NO";
return 0;
}
cout << "YES\n";
for (int i = 1; i < N; i++) {
cout << 2 << ' ';
}
cout << S - 2 * (N - 1) << '\n';
cout << 1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int n;
long long a[N];
long long num[N];
bool mark[N];
long long ans;
long long get(long long x) {
long long res = 0;
for (long long i = 2; i * i <= x; i++) {
while (x % i == 0) {
res++;
x /= i;
}
}
if (x > 1) res++;
return res;
}
void dfs(int u) {
if (!mark[u]) ans++;
if (num[u] == 1) {
mark[u] = true;
return;
}
long long temp = a[u];
while (1) {
int j = -1;
for (int i = u + 1; i <= n; i++) {
if (temp % a[i]) continue;
if (mark[i]) continue;
if (j == -1 || num[j] < num[i]) j = i;
}
if (j == -1) break;
temp /= a[j];
mark[j] = true;
if (num[j] > 1) ans++;
}
if (!mark[u]) ans += num[u];
mark[u] = true;
}
int main() {
ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) num[i] = get(a[i]);
int cnt = 0;
for (int i = 1; i <= n; i++) {
if (!mark[i]) cnt++;
dfs(i);
}
if (cnt > 1) ans++;
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
constexpr int dx[] = {1, -1, 0, 0};
constexpr int dy[] = {0, 0, 1, -1};
constexpr int MAX_H = 60;
constexpr int MAX_W = 10;
int w, h;
int num;
vector<vector<int>> cnt;
vector<int> on, xl, xr;
bool visited[MAX_H][MAX_W];
int label[MAX_H][MAX_W];
inline bool out(int x, int y) {
return x < 0 || y < 0 || x >= w || y >= h;
}
void calc_unique_label(vector<string> &field) {
const int h = field.size();
const int w = field[0].size();
num = 0;
memset(label, -1, sizeof(label));
for(int i = h - 1; i >= 0; --i) {
for(int j = 0; j < w; ++j) {
const char &c = field[i][j];
if(label[i][j] == -1 && c != '.') {
queue<pair<int, int>> que;
que.push({j, i});
label[i][j] = num;
while(!que.empty()) {
const int x = que.front().first;
const int y = que.front().second;
que.pop();
for(int d = 0; d < 4; ++d) {
const int nx = x + dx[d];
const int ny = y + dy[d];
if(out(nx, ny)) continue;
if(label[ny][nx] == -1 && field[ny][nx] == c) {
label[ny][nx] = num;
que.push({nx, ny});
}
}
}
++num;
}
}
}
}
void calc(int x, int y) {
const int &l = label[y][x];
visited[y][x] = true;
++cnt[l][x];
for(int d = 0; d < 4; ++d) {
const int nx = x + dx[d], ny = y + dy[d];
if(out(nx, ny)) {
if(d == 2) {
chmin(xl[l], x);
chmax(xr[l], x);
}
continue;
}
if(out(nx, ny)) continue;
if(label[ny][nx] != -1 && !visited[ny][nx]) {
calc(nx, ny);
}
if(label[ny][nx] != l && label[ny][nx] != -1 && d == 2) {
chmin(xl[l], x);
chmax(xr[l], x);
on[l] = label[ny][nx];
}
}
}
bool dfs(int idx) {
for(int i = 0; i < num; ++i) {
if(on[i] == idx) {
if(!dfs(i)) return false;
for(int x = 0; x < w; ++x) {
cnt[idx][x] += cnt[i][x];
}
}
}
int sum = 0;
int weight_sum = 0;
for(int x = 0; x < w; ++x) {
sum += cnt[idx][x];
weight_sum += cnt[idx][x] * x;
}
const double g = (double)weight_sum / sum;
return xl[idx] - 0.5 < g && g < xr[idx] + 0.5;
}
int main() {
while(cin >> w >> h && w) {
vector<string> field(h);
for(auto &e : field) cin >> e;
calc_unique_label(field);
cnt.assign(num, vector<int>(w, 0));
on.assign(num, -1);
xl.assign(num, w + 1);
xr.assign(num, 0);
memset(visited, false, sizeof(visited));
for(int x = 0; x < w; ++x) {
if(label[h - 1][x] == 0) {
calc(x, h - 1);
break;
}
}
cout << (dfs(0) ? "STABLE" : "UNSTABLE") << endl;
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int n;
double x[100],y[100];
double distance(double p){
double sum=0;
for(int i=0;i<n;i++){
double w=abs(x[i]-y[i]);
sum+=pow(w,p);
}
return pow(sum,1.0/p);
}
int main(){
cout<<fixed<<setprecision(12);
cin>>n;
for(int i=0;i<n;i++){
cin>>x[i];
}
for(int i=0;i<n;i++){
cin>>y[i];
}
cout<<distance(1)<<endl;
cout<<distance(2)<<endl;
cout<<distance(3)<<endl;
double mx=0;
for(int i=0;i<n;i++){
mx=max(mx,abs(x[i]-y[i]));
}
cout<<mx<<endl;
}
| 0 |
#include<iostream>
#include<vector>
#include<set>
using namespace std;
typedef vector<int> vec;
int main(){
int n,x,s;
while(cin>>n,n){
vec tab[31];
set<int> have[60];
set<int>::iterator ite;
int now=0;
while(n!=now++){
cin>>x;
for(int i=0;i<x;i++){
cin>>s;
tab[s].push_back(now);
}
}
for(int i=1;i<=n;i++)
have[i].insert(i);
for(int i=1;i<=30;i++){
vec box;
for(int j=0;j<(int)tab[i].size();j++){
box.push_back(tab[i][j]);
ite=have[tab[i][j]].begin();
while(ite!=have[tab[i][j]].end()){
box.push_back(*ite);
ite++;
}
}
for(int j=0;j<(int)tab[i].size();j++){
for(int k=0;k<box.size();k++)
have[tab[i][j]].insert(box[k]);
if((int)have[tab[i][j]].size()==n){
cout<<i<<endl;
goto end;
}
}
}
cout<<"-1"<<endl;
end:;
}
return 0;
} | 0 |
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<complex>
#include<algorithm>
#include<vector>
int main()
{
int n;
long long a[100000], sum{};
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%lld", a + i);
}
std::sort(a, a + n);
if (n & 1)
{
int les = std::min(a[n / 2] - a[n / 2 - 1], a[n / 2 + 1] - a[n / 2]);
for (int i = 0; i < n; ++i)
{
sum += std::abs(a[i] - a[n / 2]) * 2;
}
sum -= les;
}
else
{
for (int i = 0; i < n; ++i)
{
sum += std::abs(a[i] - a[n / 2]) * 2;
}
sum -= a[n / 2] - a[n / 2 - 1];
}
printf("%lld\n", sum);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long N;
cin>>N;
int x=pow(N,0.5);
for(int i=x;i>0;i--){
if(N%i==0){
cout<<i+(N/i)-2<<endl;
break;
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
long long l[100001];
long long hv[11];
long long ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> l[i];
ans += l[i] * 2;
}
bool hvw = false;
long long pw = 0;
char c = 0;
for (int i = 1; i <= n; i++) {
cin >> c;
if (c == 'G') hv[10] = 1e18;
if (c == 'G') hv[4] += l[i] * 2;
if (c == 'W') hv[6] = 1e18;
if (c == 'W') hv[2] += l[i] * 2;
pw = pw - l[i];
for (int j = 2; j <= 10; j++) {
long long cur = min(-pw, hv[j]);
hv[j] -= cur;
pw += cur;
ans += cur * j;
if (pw >= 0) break;
}
}
cout << ans / 2 << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string in;
cin >> in;
vector<int> left;
vector<int> rigth;
for (int i = 0; i < in.size(); i++) {
if (in[i] == 'l')
left.push_back(i + 1);
else
rigth.push_back(i + 1);
}
for (int i = 0; i < rigth.size(); i++) {
printf("%i\n", rigth[i]);
}
for (int i = left.size() - 1; i >= 0; i--) {
printf("%i\n", left[i]);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
o << "{";
for (const T& v : vc) o << v << ",";
o << "}";
return o;
}
using ll = long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
string s;
cin >> s;
string t = s;
reverse(t.begin(), t.end());
cout << s + t << endl;
}
| 1 |
#include <iostream>
#include <cstdio>
using namespace std;
int n, a, mx, s;
int main() {
cin>>n;
while(n--) {
cin>>a;
s += a;
mx = max(mx, a);
}
cout<<s - mx/2;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int PD(int x1, int y1, int x2, int y2, int x3, int y3);
int main() {
int x1, x2, y1, y2, x3, y3;
while (scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3) != EOF) {
if (PD(x1, y1, x2, y2, x3, y3))
printf("RIGHT\n");
else if (PD(x1 + 1, y1, x2, y2, x3, y3) || PD(x1, y1 + 1, x2, y2, x3, y3) ||
PD(x1, y1, x2 + 1, y2, x3, y3) || PD(x1, y1, x2, y2 + 1, x3, y3) ||
PD(x1, y1, x2, y2, x3 + 1, y3) || PD(x1, y1, x2, y2, x3, y3 + 1) ||
PD(x1 - 1, y1, x2, y2, x3, y3) || PD(x1, y1 - 1, x2, y2, x3, y3) ||
PD(x1, y1, x2 - 1, y2, x3, y3) || PD(x1, y1, x2, y2 - 1, x3, y3) ||
PD(x1, y1, x2, y2, x3 - 1, y3) || PD(x1, y1, x2, y2, x3, y3 - 1))
printf("ALMOST\n");
else
printf("NEITHER\n");
}
return 0;
}
int PD(int x1, int y1, int x2, int y2, int x3, int y3) {
int a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6;
a1 = x1 - x2, b1 = y1 - y2;
a2 = x1 - x3, b2 = y1 - y3;
a3 = x2 - x1, b3 = y2 - y1;
a4 = x2 - x3, b4 = y2 - y3;
a5 = x3 - x1, b5 = y3 - y1;
a6 = x3 - x2, b6 = y3 - y2;
if (a1 * b4 == b1 * a4) return 0;
if (a1 * a2 + b1 * b2 == 0 || a3 * a4 + b3 * b4 == 0 ||
a5 * a6 + b5 * b6 == 0)
return 1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int inf = 0x20202020;
const int N = 1000005;
int n, m;
int r[N], c[N], wb[N + 100], tsa[N + 100], sa[N], b[N], rk[N], lcp[N], slen[N];
long long sum[N];
int bel[N];
bool strhead[N];
long long ans;
bool cmp(int *r, int i, int j, int l) {
return (r[i] == r[j] && r[i + l] == r[j + l]);
}
void da(int *t) {
int *x = wb, *y = tsa;
for (int i = 0; i <= m; i++) b[i] = 0;
for (int i = 1; i <= n; i++) b[x[i] = t[i]]++;
for (int i = 1; i <= m; i++) b[i] += b[i - 1];
for (int i = 1; i <= n; i++) sa[b[x[i]]--] = i;
for (int j = 1, p = 1; p < n; j *= 2, m = p) {
p = j;
for (int i = 1; i <= j; i++) y[i] = n - i + 1;
for (int i = 1; i <= n; i++)
if (sa[i] > j) y[++p] = sa[i] - j;
for (int i = 0; i <= m; i++) b[i] = 0;
for (int i = 1; i <= n; i++) b[x[i]]++;
for (int i = 1; i <= m; i++) b[i] += b[i - 1];
for (int i = n; i >= 1; i--) sa[b[x[y[i]]]--] = y[i];
t = x;
x = y;
y = t;
x[sa[1]] = 1;
p = 1;
for (int i = 2; i <= n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p : (++p);
}
for (int i = 1; i <= n; i++) rk[i] = x[i];
}
void calclcp(int *r) {
int k = 0, j;
for (int i = 1; i <= n; i++) {
j = sa[rk[i] - 1];
if (j == 0) continue;
for (k ? k-- : 0; r[j + k] == r[i + k]; k++)
;
lcp[rk[i]] = k;
}
lcp[1] = 0;
}
void workon(int le, int ri, int len) {
le--;
ans = max(ans, (long long)len * (sum[ri] - sum[le - 1]));
}
stack<pair<int, int> > st;
void lcpinterval() {
int le;
pair<int, int> tmp;
st.push(make_pair(0, 0));
for (int i = 2; i <= n; i++) {
le = i;
while (lcp[i] < st.top().second) {
tmp = st.top();
st.pop();
workon(tmp.first, i - 1, tmp.second);
le = tmp.first;
}
if (lcp[i] > st.top().second) {
tmp.first = le;
tmp.second = lcp[i];
st.push(tmp);
}
}
while (st.top().second != 0) {
tmp = st.top();
st.pop();
workon(tmp.first, n, tmp.second);
}
}
int main() {
memset(slen, 0, sizeof(slen));
memset(strhead, 0, sizeof(strhead));
int num;
scanf("%d", &num);
n = 0;
char ch;
for (int i = 1; i <= num; i++) {
while (scanf("%c", &ch), ch == '\n' || ch == '\r' || ch == ' ')
;
strhead[n + 1] = true;
while (1) {
slen[i]++;
r[++n] = ch - 'a' + 1;
bel[n] = i;
scanf("%c", &ch);
if (ch == '\n' || ch == '\r' || ch == ' ') break;
}
r[++n] = 26 + i;
bel[n] = i;
}
for (int i = 1; i <= num; i++) scanf("%d", &c[i]);
m = 26 + num;
da(r);
calclcp(r);
ans = 0;
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + c[bel[sa[i]]];
lcpinterval();
lcp[n + 1] = 0;
for (int i = 1; i <= n; i++) {
if (r[sa[i]] > 26) break;
if (!strhead[sa[i]]) continue;
int tmplen = slen[bel[sa[i]]];
if (lcp[i] < tmplen && lcp[i + 1] < tmplen) {
ans = max(ans, (long long)tmplen * (long long)c[bel[sa[i]]]);
}
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
char strMas[10010][102];
int results[101][2510], sums[10010], len[10010];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", &strMas[i]);
sums[i] = 0;
len[i] = strlen(strMas[i]);
for (int j = 0; strMas[i][j]; j++) sums[i] += strMas[i][j] - 'a';
}
for (int i = 0; i < 101; i++)
for (int j = 0; j <= 2500; j++) results[i][j] = 0;
results[0][0] = 1;
for (int i = 1; i <= 100; i++)
for (int j = 0; j <= 25 * i; j++)
for (int k = 0; k <= 25; k++) {
if (k <= j) {
results[i][j] += results[i - 1][j - k];
results[i][j] %= 1000000007;
}
}
for (int i = 0; i < n; i++) {
printf("%d\n", (results[len[i]][sums[i]] + 1000000006) % 1000000007);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
int arr[205];
while (t--) {
int n, k;
cin >> n >> k;
for (int i = 0; i < 2 * n; i++) arr[i + 1] = -1;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
arr[x] = y;
arr[y] = x;
}
vector<int> v;
for (int i = 0; i < 2 * n; i++) {
if (arr[i + 1] == -1) v.push_back(i + 1);
}
for (int j = 0; j < n - k; j++) {
arr[v[j]] = v[j + n - k];
arr[v[j + n - k]] = v[j];
}
long long ans = 0;
for (int i = 1; i <= 2 * n; i++) {
if (arr[i] > i) {
for (int j = i + 1; j < arr[i]; j++) {
if ((arr[j] < i) || (arr[j] > arr[i])) ++ans;
}
}
}
cout << ans / 2 << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[5000];
int gg[5000], g[200][200], ng[200][200];
int f[30][200][200];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (a[j] >= a[i]) {
gg[j] = 1;
for (int k = 1; k <= j - 1; k++)
if (a[k] >= a[i] && a[k] <= a[j]) gg[j] = max(gg[j], gg[k] + 1);
} else
gg[j] = 0;
for (int j = 1; j <= n; j++) f[0][i][j] = gg[j];
}
for (int k = 1; k <= 25; k++) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int t = 1; t <= n; t++)
if (f[k - 1][i][j] != 0 && f[k - 1][j][t] != 0)
f[k][i][t] = max(f[k][i][t], f[k - 1][i][j] + f[k - 1][j][t]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = f[0][i][j];
int frst = 0;
for (int k = 25; k >= 0; k--) {
if ((m & (1 << k)) != 0) {
if (frst == 0) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = f[k][i][j];
frst = 1;
} else {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) ng[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int t = 1; t <= n; t++)
if (g[i][j] != 0 && f[k][j][t] != 0)
ng[i][t] = max(ng[i][t], g[i][j] + f[k][j][t]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) g[i][j] = ng[i][j];
}
}
}
int mx = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (g[i][j] > mx) mx = g[i][j];
cout << mx << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x1fffffffffffffff;
const double INFD = 1e20;
const double EPS = 1e-9;
const double PI = atan(1) * 4;
const long long M = 1000000007;
long long scan() {
long long r;
scanf("%lld", &r);
return r;
}
void scanstr(char *buf) { scanf("%s", buf); }
long long solve(vector<long long> &A) {
long long n = A.size();
vector<vector<long long> > f(n, vector<long long>(n));
vector<long long> prev(n, -1);
map<long long, long long> v2lastocc;
for (long long i = 0; i < n; ++i) {
prev[i] = v2lastocc[A[i]] - 1;
v2lastocc[A[i]] = i + 1;
}
for (long long w = 0; w < n; ++w) {
for (long long i = 0; i < n - w; ++i) {
long long j = i + w;
f[i][j] = (i == j) ? 0 : f[i][j - 1];
for (long long k = prev[j]; k >= i; k = prev[k]) {
f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j - 1] + 1);
}
}
}
return n - 1 - f[0][n - 1];
}
int main() {
long long tn = scan();
for (long long ti = 0; ti < tn; ++ti) {
long long n = scan();
vector<long long> A(n);
for (long long i = 0; i < n; ++i) A[i] = scan();
long long ans = solve(A);
cout << ans << endl;
}
return 0;
}
| 3 |
#include<stdio.h>
int main(){
int n,a,b;
scanf("%d%d",&a,&b);
printf("%d",a-b+1);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void IO(T &x) {
char c = getchar();
T sgn = 1;
while (c < '0' || c > '9') {
if (c == '-') sgn = -1;
c = getchar();
}
x = 0;
while (c >= '0' && c <= '9') {
x = (T)(x << 1) + (x << 3) + c - '0';
c = getchar();
}
x *= sgn;
}
template <class T>
T POW(T b, T push) {
T res = 1;
while (push > 0) {
if (push & 1) res *= b;
push >>= (1ll), b *= b;
}
return res;
}
template <class T>
T bMOD(T a, T push, T m) {
if (push == 0) return (T)1;
if (!(push & 1)) {
T temp = bMOD(a, push / 2, m);
return ((temp % m) * (temp % m)) % m;
}
return ((a % m) * (bMOD(a, push - 1, m) % m)) % m;
}
template <class T>
T inMOD(T a, T m) {
return bMOD(a, m - 2, m);
}
template <class T>
bool isPrime(T n) {
for (T i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
template <class T>
string toString(T n) {
stringstream second;
string num;
second << n;
second >> num;
return num;
}
template <class T>
T sq(T n) {
return (n * n);
}
template <class T>
T gcd(T a, T b) {
return (b == 0) ? a : gcd(b, a % b);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd(a, b)) * b;
}
bool iseq(double a, double b) { return fabs(a - b) < 1e-9; }
template <class T>
T toDeg(T x) {
return (180.0 * x) / ((T)acos(-1.0));
}
template <class T>
T toReg(T x) {
return (x * (T)acos(-1.0)) / (180.0);
}
template <class T>
double _distance(T x1, T y1, T x2, T y2) {
return 1.0 * sqrt(sq(x1 - x2) + sq(y1 - y2));
}
int kx[] = {-2, -2, +2, +2, +1, -1, +1, -1};
int ky[] = {+1, -1, +1, -1, -2, -2, +2, +2};
int dx[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy[] = {+1, -1, +0, +0, -1, +1, +1, -1};
int main() {
int n;
cin >> n;
int ans = bMOD(8, n, 10);
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(void){
int n;
while (cin>>n) {
if (n==0) {
break;
} else {
char a;
std::vector<vector<char>> vec(n,vector<char>(n));
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cin>>a;
vec.at(i).at(j)=a;
}
}
int countVer=0,countSide=0,countDiaLeft=0,countDiaRight=0,maxCount=0;
for (int l=0;l<n;l++) {
for (int m=0;m<n;m++) {
if (vec.at(l).at(m)=='1') {
int tmp1=l;
int tmp2=m;
//横探索
while (tmp2!=n&&vec.at(tmp1).at(tmp2)=='1') {
countSide++;
tmp2++;
}
tmp1=l;
tmp2=m;
//縦探索
while (tmp1!=n&&vec.at(tmp1).at(tmp2)=='1') {
countVer++;
tmp1++;
}
tmp1=l;
tmp2=m;
//左斜め探索
while (tmp1!=n&&tmp2!=-1&&vec.at(tmp1).at(tmp2)=='1') {
countDiaLeft++;
tmp1++;
tmp2--;
}
//右斜め探索
tmp1=l;
tmp2=m;
while (tmp1!=n&&tmp2!=n&&vec.at(tmp1).at(tmp2)=='1') {
countDiaRight++;
tmp1++;
tmp2++;
}
int ans=max(countVer,max(countSide,max(countDiaLeft,countDiaRight)));
if (ans>maxCount) {
maxCount=ans;
}
countVer=0,countSide=0,countDiaLeft=0,countDiaRight=0;
}
}
}
cout<<maxCount<<endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
inline long long toll(string s) {
long long v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
int dcmp(double x, double y) {
return fabs(x - y) <= 1e-9 ? 0 : x < y ? -1 : 1;
}
int cnt[10];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] <= '1') {
continue;
}
if (s[i] == '2' || s[i] == '3' || s[i] == '5' || s[i] == '7') {
cnt[s[i] - '0']++;
}
if (s[i] == '4') {
cnt[2] += 2;
cnt[3]++;
}
if (s[i] == '6') {
cnt[5]++;
cnt[3]++;
}
if (s[i] == '8') {
cnt[7]++;
cnt[2] += 3;
}
if (s[i] == '9') {
cnt[7]++;
cnt[3] += 2;
cnt[2]++;
}
}
for (int i = 9; i >= 0; i--) {
for (int j = 0; j < cnt[i]; j++) {
cout << i;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
vector<int> G[maxn];
set<int> S;
bool dfs(int x) {
vector<int> v;
bool ret = 0;
if (G[x].empty() || G[x][0] != 1) ret = 1;
for (set<int>::iterator it = S.begin(); it != S.end(); it++) {
vector<int>::iterator vit = lower_bound(G[x].begin(), G[x].end(), *it);
if (vit != G[x].end() && *vit == *it) continue;
v.push_back(*it);
}
int sz = v.size();
for (int i = 0; i < sz; i++) S.erase(v[i]);
for (int i = 0; i < sz; i++) ret |= dfs(v[i]);
return ret;
}
int main(void) {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 2; i <= n; i++) S.insert(i);
for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());
int ma = n - 1 - G[1].size(), mi = 0;
for (int i = 2; i <= n; i++) {
if (S.find(i) == S.end()) continue;
S.erase(i);
if (!dfs(i)) return puts("impossible");
mi++;
}
puts(k >= mi && k <= ma ? "possible" : "impossible");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int dp[5001][501];
int main() {
for (int j = 0; j <= 500; j++) {
dp[0][j] = 0;
}
int n, k;
cin >> n >> k;
int card_count[100005] = {0};
for (int i = 0; i < n * k; i++) {
int z;
cin >> z;
card_count[z]++;
}
int favorite_count[100005] = {0};
for (int i = 1; i <= n; i++) {
int z;
cin >> z;
favorite_count[z]++;
}
int h[k + 1];
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
h[i] = x;
}
h[0] = 0;
for (int i = 1; i <= k; i++) {
dp[i][1] = h[i];
}
for (int i = k + 1; i <= n * k; i++) {
dp[i][1] = h[k];
}
for (int i = 1; i <= 500; i++) {
dp[1][i] = h[1];
}
for (int i = 2; i <= n * k; i++) {
for (int j = 2; j <= n; j++) {
for (int l = 0; l <= min(k, i); l++) {
dp[i][j] = max(dp[i - l][j - 1] + h[l], dp[i][j]);
}
}
}
int happiness = 0;
for (int i = 1; i <= 100000; i++) {
happiness += dp[card_count[i]][favorite_count[i]];
}
cout << happiness;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)1000000007;
long long gcd(long long a, long long b) { return (!b ? a : gcd(b, a % b)); }
int gcd(int a, int b) { return (!b ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int pc(long long x) { return __builtin_popcount(x); }
int lsf(long long x) { return x ^ (x & (x - 1)); }
void prv(vector<int> vec) {
for (auto v : vec) cout << v << " ";
}
long long exGcd(long long a, long long b, long long &x, long long &y) {
if (a < 0) {
long long r = exGcd(-a, b, x, y);
x *= -1;
return r;
}
if (b < 0) {
long long r = exGcd(a, -b, x, y);
y *= -1;
return r;
}
if (!b) {
x = 1, y = 0;
return a;
}
long long prevX, prevY;
long long gcd = exGcd(b, a % b, prevX, prevY);
x = prevY;
y = prevX - a / b * prevY;
return gcd;
}
bool cmp(const pair<char, int> &a, const pair<char, int> &b) {
return (a != b ? a < b : a.second < b.second);
}
bool sameParity(long long a, long long b) { return (a % 2 == b % 2); }
vector<int> ModMulInvRange(int p) {
vector<int> inv(p - 1, 1);
for (int i = 2; i < p; ++i) inv[i] = (p - (p / i) * inv[p % i] % p) % p;
return inv;
}
long long pow(long long a, long long k, long long m) {
if (!k) return 1;
long long res = pow(a, k >> 1, m);
res = (res * res) % m;
if (k & 1) res = (res * a) % m;
return res % m;
}
long long phi(long long m) {
int count = 0;
long long p, res = 1, nn = m;
while (!(nn % 2)) ++count, nn >>= 1;
if (count) res *= (1 << (count - 1));
for (long long i = 3; i * i <= m; i += 2) {
p = 1;
while (!(nn % i)) nn /= i, p *= i;
if (p != 1) res *= (p / i * (i - 1));
}
if (nn != 1) res *= (nn - 1);
return res;
}
long long fact(long long n, long long m) {
long long res = 1;
for (int i = 2; i <= n; ++i) res *= i, res %= m;
return res;
}
long long modMulInv(long long a, long long m) { return pow(a, m - 2, m); }
long long nCr(long long n, long long r, long long m) {
return (fact(n, m) * modMulInv((fact(n - r, m) * fact(r, m)) % m, MOD)) % m;
}
void tst(bool bol) { cout << (bol ? "YES" : "NO") << '\n'; }
void tst(bool bol1, bool bol2) {
cout << (bol1 && bol2 ? "YES" : "NO") << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, n, val, val1, val2, v, v1, v2, a, b, aa, bb, c, d;
bool ok, ok1, ok2, done;
vector<int> vec;
cin >> t;
while (t--) {
cin >> n >> a >> b >> c >> d;
int x = a - b, y = a + b;
int s = n * x, l = n * y;
int x1 = c - d, x2 = c + d;
if (x1 > l || x2 < s)
cout << "No";
else
cout << "Yes";
cout << '\n';
}
}
| 1 |
#include<iostream>
int main()
{
while (true)
{
int n;
std::cin >> n;
if (n == 0)
{
break;
}
int result = 0;
int five_pow = 5;
while (true)
{
const int fives = n / five_pow;
if (fives == 0)
{
break;
}
result += fives;
five_pow *= 5;
}
std::cout << result << std::endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define MOD 1000000007
typedef vector<int> vi;
typedef long long int ll;
int main()
{
int N;
ll T;
cin >> N >> T;
vi D(N);
vi DD(N);
rep(i, 0, N) cin >> D[i];
rep(i, 0, N) DD[i] = D[i];
sort(D.begin(), D.end());
sort(DD.begin(), DD.end(), greater<int>());
ll ans = 1;
rep(i, 0, N)
{
auto ite = upper_bound(D.begin(), D.end(), DD[i]+T);
ll com = N - distance(D.begin(), ite);
com = i - com + 1;
ans = (ans * com) % MOD;
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long test;
cin >> test;
while (test--) {
long long n, k;
cin >> n >> k;
long long i, j;
long long a[n];
long long maxim = 1e9;
for (i = 0; i < n; i++) {
cin >> a[i];
maxim = min(maxim, a[i]);
}
long long xx = -1e9;
long long yy = 1e9;
for (i = 0; i < n; i++) {
xx = max(xx, a[i] - k);
}
for (i = 0; i < n; i++) {
yy = min(yy, a[i] + k);
}
if (xx > yy) {
cout << -1 << '\n';
} else {
cout << yy << '\n';
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
vector<vector<char>> grid;
vector<vector<int>> sum;
vector<vector<int>> mx;
int n, m;
int get(int x1, int y1, int x2, int y2) {
return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
}
bool check(int t) {
vector<vector<int>> pSum = vector<vector<int>>(n + 2, vector<int>(m + 2));
;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mx[i][j] >= t) {
int x1 = i - (t);
int x2 = i + (t);
int y1 = j - (t);
int y2 = j + (t);
pSum[x1][y1]++;
pSum[x1][y2 + 1]--;
pSum[x2 + 1][y1]--;
pSum[x2 + 1][y2 + 1]++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
pSum[i][j] =
pSum[i][j] + pSum[i - 1][j] + pSum[i][j - 1] - pSum[i - 1][j - 1];
if (!pSum[i][j] && grid[i][j] == 'X') return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
grid = vector<vector<char>>(n + 1, vector<char>(m + 1));
mx = sum = vector<vector<int>>(n + 1, vector<int>(m + 1));
;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
sum[i][j] = (grid[i][j] == 'X') + sum[i - 1][j] + sum[i][j - 1] -
sum[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int lo = -1, hi = min({n - i, i - 1, j - 1, m - j});
while (lo < hi) {
int md = lo + (hi - lo + 1) / 2;
if (get(i - md, j - md, i + md, j + md) == (md * 2 + 1) * (md * 2 + 1))
lo = md;
else
hi = md - 1;
}
mx[i][j] = lo;
}
}
int lo = 0, hi = min(n, m);
while (lo < hi) {
int md = lo + (hi - lo + 1) / 2;
if (check(md))
lo = md;
else
hi = md - 1;
}
cout << lo << '\n';
for (int i = 1; i <= n; i++, cout << '\n')
for (int j = 1; j <= m; j++) {
if (mx[i][j] >= lo)
cout << 'X';
else
cout << '.';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double INF = 1e6;
const double PI = atan2(0, -1);
const int maxn = 550;
inline int sgn(double x) {
if (fabs(x) < EPS) return 0;
return x < 0 ? -1 : 1;
}
inline int iseq(double x, double y) { return sgn(x - y) == 0; }
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
double Angle() { return atan2(y, x); }
};
inline Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }
inline Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
inline Point operator*(double x, Point A) { return Point(x * A.x, x * A.y); }
inline double operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; }
inline double operator%(Point A, Point B) { return A.x * B.y - A.y * B.x; }
inline double Length2(Point A) { return A * A; }
inline double Length(Point A) { return sqrt(A * A); }
inline double Distance2(Point A, Point B) { return Length2(A - B); }
inline double Distance(Point A, Point B) { return Length(A - B); }
inline bool operator==(Point A, Point B) { return iseq(Distance(A, B), 0); }
inline bool operator<(Point A, Point B) {
return iseq(A.x, B.x) ? sgn(A.y - B.y) == -1 : sgn(A.x - B.x) == -1;
}
struct Line {
Point P, Q;
Line() {}
Line(Point A, Point B) {
P = A;
Q = B;
}
Point Direction() { return Q - P; }
};
inline int IsOnLine(Point P, Line l) { return sgn((l.P - P) % (l.Q - P)) == 0; }
inline int IsOnSegment(Point P, Line l) {
if (P == l.P || P == l.Q) return 1;
return sgn((l.P - P) % (l.Q - P)) == 0 && sgn((l.P - P) * (l.Q - P)) <= 0;
}
inline bool IsSegmentOverlap(Line A, Line B) {
if (iseq(A.Direction() % B.Direction(), 0))
return IsOnSegment(A.P, B) + IsOnSegment(A.Q, B) + IsOnSegment(B.P, A) +
IsOnSegment(B.Q, A) >=
2;
else
return false;
}
inline bool IsSegmentIntersected(Line A, Line B) {
if (IsOnSegment(A.P, B) || IsOnSegment(A.Q, B)) return true;
if (IsOnSegment(B.P, A) || IsOnSegment(B.Q, A)) return true;
int sgn1 = sgn((A.P - B.P) % B.Direction()),
sgn2 = sgn((A.Q - B.P) % B.Direction());
int sgn3 = sgn((B.P - A.P) % A.Direction()),
sgn4 = sgn((B.Q - A.P) % A.Direction());
return sgn1 * sgn2 == -1 && sgn3 * sgn4 == -1;
}
inline Point getLineIntersection(Line A, Line B) {
Point v = A.Direction(), w = B.Direction();
Point u = B.P - A.P;
return A.P + ((w % u) / (w % v)) * v;
}
int n;
vector<Point> Rect[maxn];
double area1, area2;
inline double getPolygonArea(const vector<Point> &A) {
double ans = 0;
for (auto j = 1u; j + 1 < A.size(); ++j)
ans += (A[j] - A[0]) % (A[j] - A[0]) * 0.5;
return ans;
}
inline bool IsInPolygon(Point P, const vector<Point> &A) {
double area1 = getPolygonArea(A), area2 = 0.0;
for (auto j = 0u; j + 1 < A.size(); ++j)
area2 += (A[j] - P) % (A[j + 1] - P) * 0.5;
return iseq(area1, area2);
}
inline bool IsPolygonOverlap(vector<Point> P[], int i, int j) {
int sgn = 1;
for (auto k = 0u; k < P[i].size(); ++k)
if (!IsInPolygon(P[i][k], P[j])) {
sgn = 0;
break;
}
if (!sgn) return false;
if (iseq(getPolygonArea(P[i]), getPolygonArea(P[j]))) return i < j;
return true;
}
struct info {
Point P;
double ang;
int delta;
info() {}
info(Point x, double y, int z) {
P = x;
ang = y;
delta = z;
}
};
inline int operator<(const info &A, const info &B) {
if (iseq(A.ang, B.ang))
return A.delta > B.delta;
else
return A.ang < B.ang;
}
inline double getPosition(Point P, Line l) {
return ((P - l.P) * l.Direction()) / (l.Direction() * l.Direction());
return (P.x - l.P.x) / (l.Q.x - l.P.x);
}
double ans[maxn];
double getAreaUnion(vector<Point> P[], int n, double *ans) {
for (int i = 1; i <= n; ++i) ans[i] = 0.0;
static vector<pair<double, int>> PointList;
double aans = 0;
for (int i = 1; i <= n; ++i)
for (auto j = 0u; j + 1 < P[i].size(); ++j) {
Line A(P[i][j], P[i][j + 1]);
PointList.clear();
for (int k = 1; k <= n; ++k)
if (i != k)
for (auto w = 0u; w + 1 < P[k].size(); ++w) {
Line B(P[k][w], P[k][w + 1]);
int p1 = sgn((B.P - A.P) % A.Direction());
int p2 = sgn((B.Q - A.P) % A.Direction());
if (!p1 && !p2) {
if (i < k && sgn(A.Direction() * B.Direction()) == 1) {
PointList.push_back(
make_pair(min(1.0, max(getPosition(B.P, A), 0.0)), 1));
PointList.push_back(
make_pair(min(1.0, max(getPosition(B.Q, A), 0.0)), -1));
}
} else {
Point tp = getLineIntersection(A, B);
if (p1 >= 0 && p2 < 0)
PointList.push_back(
make_pair(min(1.0, max(getPosition(tp, A), 0.0)), 1));
if (p1 < 0 && p2 >= 0)
PointList.push_back(
make_pair(min(1.0, max(getPosition(tp, A), 0.0)), -1));
}
}
PointList.push_back(make_pair(0.0, 1));
PointList.push_back(make_pair(1.0, -1));
sort(PointList.begin(), PointList.end());
double S0 = A.P % A.Q * 0.5;
double lst = 0;
int now = 0;
for (auto l = 0u, r = 0u; l < PointList.size();) {
if (now == 1) aans += S0 * (PointList[l].first - lst);
lst = PointList[l].first;
for (r = l + 1; r < PointList.size() && iseq(PointList[r].first, lst);
++r)
;
for (auto w = l; w < r; ++w) now += PointList[w].second;
l = r;
}
}
return aans;
return ans[1];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= 4; ++j) {
Point P;
scanf("%lf%lf", &P.x, &P.y);
Rect[i].push_back(P);
}
if (sgn((Rect[i][1] - Rect[i][0]) % (Rect[i][2] - Rect[i][0])) == -1)
reverse(Rect[i].begin(), Rect[i].end());
area1 += (Rect[i][1] - Rect[i][0]) % (Rect[i][3] - Rect[i][0]);
Rect[i].push_back(Rect[i][0]);
}
area2 = getAreaUnion(Rect, n, ans);
printf("%.10lf\n", area1 / area2);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class TYPE>
TYPE gcd(TYPE x, TYPE y) {
if (y)
return gcd(y, x % y);
else
return x;
}
template <class TYPE>
TYPE lcm(TYPE x, TYPE y) {
TYPE t = gcd(x, y);
return t ? (x / t * y) : 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, d[10] = {0};
string s, ans;
cin >> n >> s;
ans = s;
for (int i = 0; i < n; i++) d[s[i] - '0']++;
int cur = 0, last_val;
for (int i = 0; i < n; i++) {
while (d[cur] == 0) {
cur++;
}
if (s[i] - '0' == cur) {
d[cur]--;
ans[i] = '1';
last_val = cur;
} else
ans[i] = '2';
}
bool check = true;
for (int i = 0; i < n; i++) {
if (ans[i] == '2') {
if (s[i] - '0' < cur) {
check = false;
break;
} else
cur = s[i] - '0';
}
}
if (check)
cout << ans << endl;
else
cout << '-' << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, c, i, j;
cin >> r >> c;
char k[r + 2][c + 2];
for (i = 0; i <= r + 1; i++)
for (j = 0; j <= c + 1; j++) k[i][j] = '.';
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++) cin >> k[i][j];
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++) {
if (k[i][j] == 'S') {
if (k[i + 1][j] == 'W' || k[i][j + 1] == 'W' || k[i - 1][j] == 'W' ||
k[i][j - 1] == 'W') {
cout << "NO" << endl;
return 0;
}
} else if (k[i][j] == '.')
k[i][j] = 'D';
}
cout << "YES" << endl;
for (i = 1; i <= r; i++) {
for (j = 1; j <= c; j++) cout << k[i][j];
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, pair<int, string> > > crew;
for (int i = 0; i < n; i++) {
string name, priority;
cin >> name >> priority;
int p;
if (priority == "rat")
p = 1;
else if (priority == "woman" || priority == "child")
p = 3;
else if (priority == "man")
p = 4;
else if (priority == "captain")
p = 5;
crew.push_back(make_pair(p, make_pair(i, name)));
}
sort(crew.begin(), crew.end());
for (int i = 0; i < n; i++) cout << crew[i].second.second << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
void vow(char a) {
if (a >= 'A' && a <= 'Z') a = a + 32;
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'y') {
} else {
printf(".%c", a);
}
}
int main(void) {
char x[100];
scanf("%s", x);
int l = strlen(x);
for (int i = 0; i < l; i++) {
vow((x[i]));
}
return 0;
}
| 1 |
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
#include<complex>
#include<random>
#include<ctime>
#include<set>
using namespace std;
const long long int mod=1000000007;
const long long int INF=99999999999999999;
int main() {
cout << fixed << setprecision(18);
long double d,res=0;
cin>>d;
if(d>=1.0&&sqrt(d*d-(int)d*(int)d)<=1)res=(int)d+1.0;
res=max(res,d*sqrt(2));
cout<<res<<endl;
}
| 0 |
#include<iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
long long N,ans;
int main(){
cin>>N;
int A[N][N],B[N][N];
rep(i,N*N){cin>>A[i/N][i%N];B[i/N][i%N]=A[i/N][i%N];}
rep(k,N)rep(i,N)rep(j,N)B[i][j]=min(B[i][j],B[i][k]+B[k][j]);
rep(i,N*N)if(A[i/N][i%N]!=B[i/N][i%N]){cout<<-1<<endl;return 0;}
rep(k,N)rep(i,N)rep(j,N){
if(i==k||k==j||i==j)continue;
if(A[i][k]+A[k][j]==A[i][j])A[i][j]=1000000001;
}
rep(i,N*N)if(A[i/N][i%N]!=1000000001)ans+=A[i/N][i%N];
cout<<ans/2<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct e {
int diff, ind;
} st[1000010];
int n, a[1000010], rez[1000010];
bool srt(e aa, e bb) { return (aa.diff > bb.diff); }
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) {
st[i].ind = i;
st[i].diff = n - a[i] - a[(i + 1) % n];
}
sort(st, st + n, srt);
for (int i = 0; i < n; i++) {
}
for (int i = 0; i < n; i++) rez[st[i].ind] = i;
for (int i = 0; i < n; i++) printf("%d ", rez[i]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e9;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
long long t, n, sum, lft, rght;
vector<long long> a;
bool ok, upper, lower, digits;
char c;
string second;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> lft >> rght;
cout << "YES"
<< "\n";
for (long long i = lft; i <= rght; i += 2) {
cout << i << " " << i + 1 << "\n";
}
return 0;
}
| 2 |
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
vector<string> M;
int H,W;
int c_to_int[26];
char int_to_c[7];
int num;
vector<vector<vector<char> > > E;
bool pre_check(){
E.clear();
E.resize(H,vector<vector<char> > (W,vector<char>()));
for(int k=0;k<num;k++){
char now=int_to_c[k];
int minx=100,maxx=0,miny=100,maxy=0;
for(int y=0;y<H;y++){
for(int x=0;x<W;x++){
if(M[y][x]==now){
maxx=max(maxx,x);minx=min(minx,x);
maxy=max(maxy,y);miny=min(miny,y);
}
}
}
for(int y=miny;y<=maxy;y++){
for(int x=minx;x<=maxx;x++){
if(M[y][x]=='.') return false;
E[y][x].push_back(now);
}
}
}
return true;
}
bool check(vector<int> perm){
for(int y=0;y<H;y++){
for(int x=0;x<W;x++){
if(M[y][x]=='.') continue;
int top=c_to_int[M[y][x]];
for(int i=0;i<E[y][x].size();i++){
if(E[y][x].size()<2) continue;
int now=c_to_int[E[y][x][i]];
if(perm[top]>perm[now]) return false;
}
}
}
return true;
}
bool solve(){
cin>>H>>W;
M.clear();
set<char> S;
num=0;
for(int i=0;i<H;i++){
string s; cin>>s; M.push_back(s);
for(int k=0;k<s.size();k++){
if(!S.count(s[k]) && s[k]!='.'){
S.insert(s[k]);
c_to_int[s[k]]=num;
int_to_c[num]=s[k];
num++;
}
}
}
if(!pre_check()) return false;
if(num<2) return true;
vector<int> perm;
for(int i=0;i<num;i++) perm.push_back(i);
sort(perm.begin(),perm.end());
while(next_permutation(perm.begin(),perm.end())){
if(check(perm)) return true;
}
return false;
}
int main()
{
int n;cin>>n;
for(int i=0;i<n;i++){
if(solve()) cout<<"SAFE\n";
else cout<<"SUSPICIOUS\n";
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 70;
int n, m, a[N], b[N];
map<double, pair<long long, long long> > mp;
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < m; i++) scanf("%d", &b[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
double y = 1.0 * (a[i] + b[j]) / 2.0;
long long msk1 = 0, msk2 = 0;
if (mp.count(y) != 0) continue;
for (int k1 = 0; k1 < n; k1++)
for (int k2 = 0; k2 < m; k2++)
if (1.0 * (a[k1] + b[k2]) / 2.0 == y) {
msk1 = (msk1 | (1LL << k1));
msk2 = (msk2 | (1LL << k2));
}
mp[y] = {msk1, msk2};
}
}
int ans = 0;
for (auto x : mp)
for (auto y : mp) {
long long msk1 = (x.second.first | y.second.first);
long long msk2 = (x.second.second | y.second.second);
ans = max(ans, __builtin_popcountll(msk1) + __builtin_popcountll(msk2));
}
printf("%d", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, d, a[100000], diff;
long sum = 0;
cin >> n >> d;
for (long i = 0; i < n; i++) cin >> a[i];
for (long i = 1; i < n; i++) {
if (a[i - 1] >= a[i]) {
diff = a[i - 1] - a[i];
a[i] = a[i] + (diff / d + 1) * d;
sum = sum + diff / d + 1;
}
}
cout << sum;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, t[10000005], sum, h0;
int a[10000005], b[10000005], inv[10000005];
const int Mxdt = 1000000;
inline char gc() {
static char buf[Mxdt], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Mxdt, stdin), p1 == p2)
? EOF
: *p1++;
}
inline int read() {
int res = 0, bj = 0;
char ch = getchar();
while (ch < '0' || ch > '9') bj |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9')
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return bj ? -res : res;
}
void Geth(int n, int m) {
int aa, bb, cc, dd;
a[1] = 1, b[1] = mod - n + 1, a[0] = 1, b[0] = 0;
for (int i = 2; i <= m || i <= n; ++i)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
int x = 1ll * m * (n - 1) % mod,
y = 1ll * inv[m] % mod * (n - 2) % mod * inv[n - 1] % mod;
for (int i = 1; i < m; ++i) {
aa = 1ll * x * inv[m - i] % mod, bb = 1ll * (m - i) * y % mod,
cc = 1ll * i * inv[m] % mod, dd = 1;
a[i + 1] = ((a[i] * (1ll - bb + mod) - 1ll * cc * a[i - 1]) % mod + mod) *
aa % mod;
b[i + 1] =
((b[i] * (1ll - bb + mod) - 1ll * cc * b[i - 1] - dd) % mod + mod) *
aa % mod;
}
h0 = mod - b[m];
}
int main() {
n = read(), inv[0] = inv[1] = 1;
for (int i = 1; i <= n; ++i) sum += (t[i] = read());
Geth(n, sum);
int ans = 0;
for (int i = 1; i <= n; ++i) ans = (ans + 1ll * a[t[i]] * h0 + b[t[i]]) % mod;
cout << 1ll * inv[n] * (ans - (n - 1ll) * h0 % mod + mod) % mod;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a;cin>>a;
cout<<"Christmas ";
for(int i=24;i>=a;i--){
cout<<"Eve ";
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> primes;
void sieve(long long int n) {
vector<bool> check(n + 1, true);
check[0] = false;
check[1] = false;
for (long long int i = 2; i <= n; ++i) {
if (check[i]) {
if (i * i * 1ll <= n)
for (long long int j = i * i; j <= n; j += i) check[j] = false;
}
}
for (long long int i = 0; i < n + 1; ++i) {
if (check[i]) primes.push_back(i);
}
}
vector<int> v[105];
bool visited[105];
int maxi = 0, budget = 0;
;
vector<int> stackprototype;
void dfs(int source) {
visited[source] = true;
for (int i = 0; i < v[source].size(); ++i) {
if (visited[v[source][i]] == false) {
dfs(v[source][i]);
}
}
if (v[source].size() == 1) {
stackprototype.push_back(source);
}
return;
}
bool isPrime(long long int n) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int test = 1;
while (test--) {
int n, x = 0, y = 1023, w;
char ch;
for (cin >> n; n--;) {
cin >> ch >> w;
if (ch == '^') {
x ^= w;
y ^= w;
} else if (ch == '&') {
x &= w;
y &= w;
} else {
x |= w;
y |= w;
}
}
cout << "3" << endl;
cout << "^ " << (x & ~y & 1023) << "\n& " << (x | y) << "\n| " << (x & y);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, q, dp[26][1505];
string dat;
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> dat;
for (int c = 0; c < 26; ++c) {
for (int i = 0; i < n; ++i) {
int gap = 0;
for (int j = i; j < n; ++j) {
if (dat[j] - 'a' != c) {
++gap;
}
dp[c][gap] = max(dp[c][gap], j - i + 1);
}
}
for (int i = 1; i <= n; ++i) {
dp[c][i] = max(dp[c][i], dp[c][i - 1]);
}
}
cin >> q;
for (int i = 1; i <= q; ++i) {
char c;
int cnt;
cin >> cnt >> c;
cout << dp[c - 'a'][cnt] << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a[120];
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
cin >> a;
vector<int> count_B;
for (int i = 0; i < n; i++) {
while (i < n && a[i] == 'W') i++;
if (i >= n) break;
int count = 0;
while (i < n && a[i] == 'B') {
count++;
i++;
}
count_B.push_back(count);
}
cout << count_B.size() << endl;
if (count_B.size() > 0) {
for (int i = 0; i < count_B.size(); i++) cout << count_B[i] << ' ';
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &_p) {
return os << "(" << _p.first << "," << _p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &_V) {
bool f = true;
os << "[";
for (auto v : _V) {
os << (f ? "" : ",") << v;
f = false;
}
return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &_S) {
bool f = true;
os << "(";
for (auto s : _S) {
os << (f ? "" : ",") << s;
f = false;
}
return os << ")";
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &_M) {
return os << set<pair<T, U>>(_M.begin(), _M.end());
}
const signed long long INF = 1000000100;
const long double EPS = 1e-9;
const int MAXN = 50050;
const int MAXW = 1 << 21;
const int S = 600;
int N, QS;
int A[MAXN];
int first[MAXN];
int second[MAXN];
int F[MAXW];
struct trie {
vector<pair<int *, int>> S;
int *P, *Q;
int result;
trie() : P(new int[MAXW]), Q(new int[MAXW]) { clear(); }
void clear() {
memset(P, 107, sizeof(int) * MAXW);
memset(Q, -1, sizeof(int) * MAXW);
result = 0;
}
void insert(int k, int t) {
int v = 1;
update(&P[v], min(P[v], t));
update(&Q[v], max(Q[v], t));
for (int(i) = (19); (i) >= (0); (i)--) {
if (((1 << i) & k) != 0) {
v = 2 * v + 1;
} else {
v = 2 * v;
}
update(&P[v], min(P[v], t));
update(&Q[v], max(Q[v], t));
}
}
int query(int k, int p, int q) {
int v = 1;
int r = 0;
for (int(i) = (19); (i) >= (0); (i)--) {
int e = (((1 << i) & k) != 0);
if (!(q < P[2 * v + (!e)] or p > Q[2 * v + (!e)])) {
v = 2 * v + (!e);
r |= (1 << i);
} else {
v = 2 * v + e;
}
}
return r;
}
void update_result(int k, int p, int q) {
update(&result, max(result, query(k, p, q)));
}
void rollback() {
while (!S.empty()) {
*(S.back().first) = S.back().second;
S.pop_back();
}
}
void begin() { S.clear(); }
void update(int *Z, int v) {
S.push_back({Z, *Z});
*Z = v;
}
};
void read_data() {
scanf("%d %d", &N, &QS);
for (int(i) = (1); (i) <= (N); (i)++) scanf("%d", A + i);
for (int(i) = (1); (i) <= (QS); (i)++) scanf("%d %d", first + i, second + i);
}
void insert(trie &T0, trie &T1, int i) {
T0.insert(F[A[i]], A[i]);
T1.insert(F[A[i] - 1], A[i]);
T0.update_result(F[A[i] - 1], A[i], INF);
T1.update_result(F[A[i]], 0, A[i]);
}
void solve() {
map<int, vector<pair<int, int>>> M;
map<pair<int, int>, int> R;
for (int(i) = (1); (i) <= (QS); (i)++)
M[first[i] / S].push_back(make_pair(first[i], second[i]));
trie P, Q;
for (auto &m : M) {
vector<pair<int, int>> &queries = m.second;
sort(queries.begin(), queries.end(),
[](pair<int, int> a, pair<int, int> b) {
return make_pair(a.second, a.first) < make_pair(b.second, b.first);
});
int l = -INF;
for (auto q : queries) l = max(l, q.first);
P.clear();
Q.clear();
int r = l - 1;
for (auto q : queries)
if (q.second - q.first <= S + 3) {
P.begin();
Q.begin();
for (int(i) = (q.first); (i) <= (q.second); (i)++) insert(P, Q, i);
R[q] = max(P.result, Q.result);
P.rollback();
Q.rollback();
}
for (auto q : queries)
if (q.second - q.first > S + 3) {
int temp_l = q.first;
int new_r = q.second;
for (int(i) = (r + 1); (i) <= (new_r); (i)++)
if (q.first <= i and i <= q.second) insert(P, Q, i);
P.begin();
Q.begin();
for (int(i) = (l - 1); (i) >= (temp_l); (i)--)
if (q.first <= i and i <= q.second) insert(P, Q, i);
R[q] = max(P.result, Q.result);
P.rollback();
Q.rollback();
r = new_r;
}
}
for (int(i) = (1); (i) <= (QS); (i)++)
printf("%d\n", R[make_pair(first[i], second[i])]);
}
int main() {
for (int(i) = (1); (i) < (MAXW); (i)++) F[i] = (F[i - 1] ^ i);
read_data();
solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long ans = n * n;
ans = (ans * (ans + 1)) / 2;
long long k = ans / n;
k /= n / 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
cout << (n / 2 * i + 1 + j) << " " << k - (n / 2 * i + 1 + j) << " ";
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <int MOD>
struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
ModInt(signed long long sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) {
if ((x += that.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(ModInt that) {
if ((x += MOD - that.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator*=(ModInt that) {
x = (unsigned long long)x * that.x % MOD;
return *this;
}
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const {
signed a = x, b = MOD, u = 1, v = 0;
while (b) {
signed t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
if (u < 0) u += Mod;
ModInt res;
res.x = (unsigned)u;
return res;
}
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const {
ModInt t;
t.x = x == 0 ? 0 : Mod - x;
return t;
}
};
template <int MOD>
ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1;
while (k) {
if (k & 1) r *= a;
a *= a;
k >>= 1;
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<ModInt<998244353>> fact(n + 1, ModInt<998244353>(1));
for (int i = 1; i <= n; ++i) {
fact[i] = ModInt<998244353>(i) * fact[i - 1];
}
vector<pair<int, int>> s(n);
vector<int> fa(n + 1, 0), fb(n + 1, 0);
for (int i = 0; i < n; ++i) {
cin >> s[i].first >> s[i].second;
fa[s[i].first]++;
fb[s[i].second]++;
}
ModInt<998244353> spa = ModInt<998244353>(1), spb = ModInt<998244353>(1);
for (int i = 1; i <= n; ++i) {
spa *= fact[fa[i]];
spb *= fact[fb[i]];
}
sort(s.begin(), s.end());
ModInt<998244353> spab = ModInt<998244353>(1);
for (int i = 0; i < n - 1; ++i) {
if (s[i].second > s[i + 1].second) spab = ModInt<998244353>(0);
}
for (int i = 0, j = 0; i < n; i = j) {
j = i + 1;
while (j < n && s[j] == s[i]) j++;
spab *= fact[j - i];
}
ModInt<998244353> ans = fact[n] - spa - spb + spab;
cout << ans.get();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 17) + 10;
int ans, anstag;
vector<int> son[maxn];
int h[maxn], tag[maxn];
inline void dfs(int now, int f) {
tag[now] = 0;
if (27982 > 6886) {
if (25900 > 1900) {
bool EOYODPGAKG;
EOYODPGAKG = 27137;
if (EOYODPGAKG) {
if (24801 > 26594) {
long long NQREIAMCNP;
long long SEHVUHYVHI;
}
if (3407 > 24017) {
long long PVRQOANPZD;
short WIXHWXOGPJ;
long long QKBASCBXEG;
}
short XKYBMGEDPW;
}
long long SNCNNSUWDA;
short PWQKXAUHNO;
bool GPYQWWWTSG;
GPYQWWWTSG = 17374;
}
if (9417 > 29377) {
bool HKGXEQVLLN;
HKGXEQVLLN = 7235;
if (HKGXEQVLLN) {
short TDDMSZZPQD;
TDDMSZZPQD = 26895;
int CIUOPOTFYA;
CIUOPOTFYA = 20025;
if (CIUOPOTFYA) {
bool TADEZSHRWR;
}
if (7261 > 11862) {
double YQJBAJFPWO;
bool TVKVWICGAU;
short UXYDPJSQBJ;
}
}
int MZKOBCIGDX;
MZKOBCIGDX = 10607;
}
if (29568 > 24573) {
int NISQLLGRHG;
short NRHKYGSCEZ;
short FOSTPJCMWT;
FOSTPJCMWT = 29544;
if (FOSTPJCMWT) {
bool WVCNDDLVGJ;
WVCNDDLVGJ = 18587;
}
}
}
for (int i = 0; i < son[now].size(); i++) {
if (son[now][i] == f) swap(son[now][i], son[now][son[now].size() - 1]);
}
if (f == 0) son[now].push_back(0);
if (son[now].size() == 1) {
h[now] = 0, tag[now] = 0;
} else if (son[now].size() == 2) {
dfs(son[now][0], now);
ans = now;
if (tag[son[now][0]] || h[son[now][0]])
anstag = 0;
else
h[now] = 1, tag[now] = 1;
} else if (son[now].size() == 3) {
dfs(son[now][0], now);
dfs(son[now][1], now);
if ((tag[son[now][0]] && tag[son[now][1]]) ||
h[son[now][0]] != h[son[now][1]])
anstag = 0;
else if (tag[son[now][0]] || tag[son[now][1]])
tag[now] = 1;
else
tag[now] = 0;
h[now] = h[son[now][0]] + 1;
} else if (son[now].size() == 4) {
dfs(son[now][0], now);
dfs(son[now][1], now);
dfs(son[now][2], now);
ans = now;
sort(son[now].begin(), son[now].begin() + 3, [&](int a, int b) {
return h[a] < h[b];
if (16828 > 23103) {
int LLKJGQFCXI;
LLKJGQFCXI = 29481;
short AILHYAISQI;
}
});
double EKIQRSVPMK;
EKIQRSVPMK = 29197;
if (EKIQRSVPMK) {
int QNACQBXVCV;
QNACQBXVCV = 32602;
double DXFPJFQRXH;
DXFPJFQRXH = 18214;
if (4856 > 15182) {
if (32702 > 4477) {
long long XAWLDMQMBR;
long long XNHITREJZG;
}
if (5042 > 29694) {
short LHNWHUCKZH;
long long FWKHQSMYFI;
}
}
}
if (tag[son[now][0]] || tag[son[now][1]] || tag[son[now][2]])
anstag = 0;
else if (h[son[now][0]] != h[son[now][1]] ||
h[son[now][1]] != h[son[now][2]] - 1)
anstag = 0;
else
tag[now] = 1, h[now] = h[son[now][2]] + 1;
} else
anstag = 0;
int FSVOCAETHA;
if (f == 0) son[now].pop_back();
}
int n, m;
int wsze[maxn], sze[maxn];
inline void dfs2(int now, int f) {
sze[now] = 1;
int ENLJAEJZXP;
ENLJAEJZXP = 27957;
for (auto i : son[now]) {
if (i != f)
dfs2(i, now), sze[now] += sze[i], wsze[now] = max(wsze[now], sze[i]);
}
wsze[now] = max(wsze[now], n - sze[now]);
bool KOERFVAQLO;
}
int main() {
scanf("%d", &m);
if (5278 > 29799) {
if (13197 > 15311) {
if (12222 > 29528) {
double HHOREWKCUX;
HHOREWKCUX = 21741;
if (16487 > 5136) {
short DYCCCRLORU;
double KGOWZCQDMV;
short WGYEQUTXMY;
}
}
}
short SREPNDQKIO;
SREPNDQKIO = 27898;
}
if (m == 1)
return puts("1\n1"), 0;
else if (m == 2)
return puts("2\n1 2"), 0;
n = (1 << m) - 2;
for (int i = 1; i < n; i++) {
int u, v;
if (12120 > 2889) {
long long SADHIJZUGU;
SADHIJZUGU = 6225;
int QMIJHWSKCU;
QMIJHWSKCU = 26003;
if (QMIJHWSKCU) {
if (31447 > 6188) {
double BEVWCUHCLF;
short ELCICEFXUI;
short PJIRIWKYUR;
}
if (21064 > 164) {
double QVFJHUGINM;
double XHRHRLRYGM;
}
}
}
scanf("%d%d", &u, &v);
if (29085 > 28297) {
double WOFAFYTQEN;
WOFAFYTQEN = 20708;
if (4055 > 31082) {
bool PPPGWLADCR;
PPPGWLADCR = 18932;
if (PPPGWLADCR) {
double BZULELCFWY;
int EWNQYOJUZE;
}
bool HOFDVDSLDP;
HOFDVDSLDP = 13132;
double DTJPELHRPZ;
DTJPELHRPZ = 21167;
if (20575 > 14520) {
int TUJHYPRKXZ;
long long AVAKALRWYB;
}
}
if (30463 > 12884) {
if (4541 > 10807) {
double ULYSZPZUHY;
double NKHNETXLZK;
long long LGPPLVEMQP;
}
if (26277 > 20795) {
long long CKRUXNACRP;
}
}
}
son[u].push_back(v);
bool HPRHUSFJYO;
HPRHUSFJYO = 32765;
son[v].push_back(u);
bool OPRIZNNKJH;
OPRIZNNKJH = 22495;
}
dfs2(1, 0);
long long HZPXBQOUEF;
int _min = n + 1;
if (31932 > 21913) {
int YWSZODSDHO;
YWSZODSDHO = 3044;
if (YWSZODSDHO) {
short TOVFUYXDOU;
TOVFUYXDOU = 31893;
if (TOVFUYXDOU) {
if (21650 > 26668) {
long long XIFKPWLZYP;
short VJBTJEOKHV;
}
int YMLNOMNBYQ;
YMLNOMNBYQ = 31953;
if (7185 > 22597) {
double OAVOUWCGDP;
}
}
bool PNDCQGPWKK;
PNDCQGPWKK = 3022;
long long KGEEOMSMFO;
KGEEOMSMFO = 12602;
}
double ONHDFIVPDO;
ONHDFIVPDO = 10543;
if (ONHDFIVPDO) {
long long THAXMISHKZ;
long long QTHMUHLANU;
QTHMUHLANU = 15522;
if (QTHMUHLANU) {
bool QLNOJLLJJK;
if (25998 > 8513) {
double HPXWMGMXYF;
int TUPMPALBJI;
}
}
bool XZZZZPDOAG;
XZZZZPDOAG = 7646;
if (XZZZZPDOAG) {
short NYHNPIDRRE;
}
if (13545 > 29893) {
if (3099 > 9743) {
long long CNVJWZDTME;
long long DOMIWCMZAF;
}
bool UYXAHDWGNY;
UYXAHDWGNY = 30404;
if (UYXAHDWGNY) {
bool XPPMEJQKCC;
short CREWZRNCFL;
}
}
if (25252 > 23903) {
int RQERASDDCF;
if (29315 > 6639) {
int RQELPKZLQU;
bool TRGUWMPVAB;
int BEVQIPVRUR;
}
}
}
short WYZLIVRGOR;
WYZLIVRGOR = 21589;
}
vector<int> answ;
int ISOQDCHNJW;
ISOQDCHNJW = 13263;
if (ISOQDCHNJW) {
long long DAMPMZGJDC;
DAMPMZGJDC = 264;
if (DAMPMZGJDC) {
if (15432 > 28484) {
double LJLRFNKQWF;
LJLRFNKQWF = 10187;
}
}
long long RLNILRNDXT;
double LRTRUQQVYA;
}
for (int i = 1; i <= n; i++) _min = min(wsze[i], _min);
short YUPIXKRVFG;
YUPIXKRVFG = 14751;
if (YUPIXKRVFG) {
int JVFLWBYPSK;
int ZMQRFFYTOD;
}
for (int i = 1; i <= n; i++) {
if (wsze[i] == _min) {
anstag = 1;
dfs(i, 0);
if (8163 > 19288) {
if (25658 > 31892) {
long long ZIJMLIXIWL;
short CWDRWAQWSE;
int IVGJTMYMJL;
}
short DYSQTCEPNB;
}
if (tag[i] == 1 && h[i] == m - 1 && anstag) {
answ.push_back(ans);
}
}
}
printf("%d\n", answ.size());
if (16937 > 17794) {
int SKWZXBBZHD;
SKWZXBBZHD = 27169;
if (SKWZXBBZHD) {
if (27080 > 18326) {
double XGLJSMQMUX;
XGLJSMQMUX = 20028;
if (XGLJSMQMUX) {
long long SCDGTNCWOY;
int KYFFHQIBEE;
}
}
if (7784 > 11854) {
int PLAWWBUFKC;
bool TLSCHJDTOS;
}
}
}
for (auto i : answ) printf("%d ", i);
double EQEASEEHYP;
return 0;
if (11376 > 21871) {
double DFIGOZIQHQ;
DFIGOZIQHQ = 19221;
double DWMFNHAORG;
bool TFIAXPPIFJ;
TFIAXPPIFJ = 9793;
if (TFIAXPPIFJ) {
if (25129 > 31765) {
int HLYKJWVLUC;
long long ZDHBAHSBSR;
ZDHBAHSBSR = 20259;
}
double YPALWFIVSJ;
YPALWFIVSJ = 2017;
if (YPALWFIVSJ) {
if (26605 > 10076) {
double UFJVQONBIT;
int IRVTMIZXNG;
}
int DXRXMQFHLA;
DXRXMQFHLA = 21297;
}
long long XXHBIKVPRO;
XXHBIKVPRO = 20695;
long long GUNREIEUAK;
GUNREIEUAK = 23650;
}
bool LQSFCCHDEK;
LQSFCCHDEK = 16535;
if (LQSFCCHDEK) {
double QPHHVLZVET;
QPHHVLZVET = 17259;
if (QPHHVLZVET) {
bool AIHKWJDAQG;
AIHKWJDAQG = 32683;
if (AIHKWJDAQG) {
short RNDYRBPEVF;
int HPNYRPTROB;
}
short RMZCZUOHOH;
if (28814 > 15817) {
int PNEZGUADMU;
}
if (5684 > 14584) {
double EMUCOAUSED;
short KPOHRCFRPY;
}
int SFJHCLFPBU;
}
if (18574 > 3684) {
bool WGSHVHELRK;
WGSHVHELRK = 4905;
if (WGSHVHELRK) {
double VKJOQHKWNN;
bool WWURNIHEXD;
double ROSVJISAUH;
}
if (27915 > 31915) {
bool VAJRFUSWLM;
double QOOKGGOICU;
int ZEWPEEGFXI;
}
short BOQLFBGWFJ;
BOQLFBGWFJ = 29276;
}
if (15156 > 11920) {
short NLAAKUZXXT;
NLAAKUZXXT = 27081;
if (NLAAKUZXXT) {
short CQKPSTKGDW;
int QUSSSTXPLL;
}
if (5045 > 3589) {
long long AWCPFBHBRU;
}
}
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
multiset<int> a[35];
long long int s[35];
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int t;
cin >> t;
int cnt = 0;
for (int z = 1; z <= t; z++) {
char c;
cin >> c;
int x;
cin >> x;
int b = 64 - __builtin_clzll(x) - 1 + 1;
if (c == '+') {
a[b].insert(x);
s[b] += x;
cnt++;
} else {
a[b].erase(a[b].find(x));
s[b] -= x;
cnt--;
}
long long int sum = 0;
int res = 0;
for (int i = 1; i <= 31; i++) {
int mi = *a[i].begin();
if (mi > 2 * sum) {
res++;
}
sum += s[i];
}
cout << cnt - res << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct dri {
string nam;
int win[60];
int poi;
};
map<int, string> is;
map<string, int> si;
int df[60] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1};
int n, t, sum;
dri ans[1200];
string s;
bool c1(dri x, dri y) {
if (x.poi != y.poi) return x.poi > y.poi;
for (int i = 1; i <= 50; i++)
if (x.win[i] != y.win[i]) return x.win[i] > y.win[i];
return 1;
}
bool c2(dri x, dri y) {
if (x.win[1] != y.win[1]) return x.win[1] > y.win[1];
if (x.poi != y.poi) return x.poi > y.poi;
for (int i = 2; i <= 50; i++)
if (x.win[i] != y.win[i]) return x.win[i] > y.win[i];
return 1;
}
int main() {
cin >> t;
int i, j, k, l;
for (i = 1; i <= t; i++) {
cin >> n;
for (j = 1; j <= n; j++) {
cin >> s;
if (si[s] == 0) {
sum++;
si[s] = sum;
is[sum] = s;
ans[sum].nam = s;
memset(ans[sum].win, 0, sizeof(ans[sum].win));
ans[sum].poi = 0;
}
k = si[s];
if (j <= 10) {
ans[k].poi += df[j];
}
ans[k].win[j]++;
}
}
j = 1;
for (i = 2; i <= sum; i++)
if (c1(ans[i], ans[j])) j = i;
cout << ans[j].nam << endl;
j = 1;
for (i = 2; i <= sum; i++)
if (c2(ans[i], ans[j])) j = i;
cout << ans[j].nam << endl;
return 0;
}
| 2 |
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <map>
#include <sstream>
using namespace std;
using Weight = int;
using Capacity = int;
struct Edge {
int src, dst; Weight weight; Capacity cap;
Edge(int s, int d, Weight w, Capacity c) : src(s), dst(d), weight(w), cap(c) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
struct Dinic {
int n, s, t;
vector<int> level, prog, que;
vector<vector<Capacity>> cap, flow;
vector<vector<int>> g;
Capacity inf;
Dinic(const Graph &graph)
: n(graph.size()),
cap(n, vector<Capacity>(n)), flow(n, vector<Capacity>(n)),
g(n, vector<int>()), inf(numeric_limits<Capacity>::max() / 8) {
for(int i = 0; i < n; i++) {
for(auto &e : graph[i]) {
int u = e.src, v = e.dst;
Capacity c = e.cap;
cap[u][v] += c; cap[v][u] += c; flow[v][u] += c;
g[u].push_back(v); g[v].push_back(u);
}
}
}
inline Capacity residue(int u, int v) { return cap[u][v] - flow[u][v]; }
Capacity solve(int s_, int t_) {
this->t = t_, this->s = s_;
que.resize(n + 1);
Capacity res = 0;
while(levelize()) { prog.assign(n, 0); res += augment(s, inf); }
return res;
}
bool levelize() {
int l = 0, r = 0;
level.assign(n, -1); level[s] = 0; que[r++] = s;
while(l != r) {
int v = que[l++]; if(v == t) break;
for(const int &d : g[v]) if(level[d] == -1 && residue(v, d) != 0) {
level[d] = level[v] + 1; que[r++] = d;
}
}
return level[t] != -1;
}
Capacity augment(int v, Capacity lim) {
Capacity res = 0;
if(v == t) return lim;
for(int &i = prog[v]; i < (int)g[v].size(); i++) {
const int &d = g[v][i];
if(residue(v, d) == 0 || level[v] >= level[d]) continue;
const Capacity aug = augment(d, min(lim, residue(v, d)));
flow[v][d] += aug; flow[d][v] -= aug;
res += aug; lim -= aug;
if(lim == 0) break;
}
return res;
}
};
typedef pair<int, int> Pii;
vector<int> f(string s) {
replace(s.begin(), s.end(), '+', ' ');
//cout << s << endl;
vector<int> ret(51);
stringstream ss(s);
while(ss >> s) {
stringstream ss2(s);
if(s.find('x') == string::npos) {
ss2 >> ret[0];
}
else {
char c;
int a = 1, L = 1;
if(s[0] != 'x') ss2 >> a;
if(s.find('^') != string::npos) {
ss2 >> c >> c >> L; // x^L
}
ret[L] = a;
}
}
return ret;
}
int u[500];
int v[500];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
while(cin >> N >> M, N) {
map<Pii, vector<int> > poly;
for(int i = 0; i < M; i++) {
string p;
cin >> u[i] >> v[i] >> p;
u[i]--, v[i]--;
poly[Pii(u[i], v[i])] = f(p);
}
vector<int> ans(51);
for(int i = 50; i >= 0; i--) {
Graph G(N);
for(auto&& p : poly) {
int a = p.first.first, b = p.first.second, c = p.second[i];
G[a].emplace_back(a, b, 0, c);
G[b].emplace_back(b, a, 0, c);
}
Dinic dinic(G);
int f = dinic.solve(0, N - 1);
ans[i] = f;
for(int j = 0; j < M; j++) {
int a = u[j], b = v[j];
if(dinic.flow[a][b] != 0 && dinic.cap[a][b] != dinic.flow[a][b]) {
for(int l = 0; l <= i; l++) {
poly[Pii(a, b)][l] = 1e6;
}
}
}
}
bool flag = false;
for(int i = 50; i >= 0; i--) {
if(ans[i] == 0) continue;
if(flag) cout << "+";
flag = true;
if(i == 0 || ans[i] != 1) cout << ans[i];
if(i != 0) cout << "x";
if(i >= 2) cout << "^" << i;
}
if(!flag) cout << 0;
cout << "\n";
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 13;
int q, tree[4 * maxN], lzy[4 * maxN], n, m, a[maxN], b[maxN];
struct seg_tree {
void fix(int id, int val) {
lzy[id] += val;
tree[id] += val;
}
void relax(int id) {
fix(id << 1, lzy[id]);
fix(id << 1 ^ 1, lzy[id]);
lzy[id] = 0;
}
void update(int L, int R, int val, int l = 0, int r = maxN, int id = 1) {
if (r <= L || R <= l) return;
if (L <= l && r <= R) {
fix(id, val);
return;
}
relax(id);
int mid = l + r >> 1;
update(L, R, val, l, mid, id << 1);
update(L, R, val, mid, r, id << 1 ^ 1);
tree[id] = min(tree[id << 1], tree[id << 1 ^ 1]);
}
int get(int l = 0, int r = maxN, int id = 1) {
if (r - l < 2) return l;
relax(id);
int mid = l + r >> 1;
if (tree[id << 1 ^ 1] < 0) return get(mid, r, id << 1 ^ 1);
return get(l, mid, id << 1);
}
} seg;
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
seg.update(0, a[i] + 1, -1);
}
for (int i = 0; i < m; i++) {
cin >> b[i];
seg.update(0, b[i] + 1, 1);
}
cin >> q;
while (q--) {
int t, i, x;
cin >> t >> i >> x;
i--;
if (t == 1) {
seg.update(0, a[i] + 1, 1);
a[i] = x;
seg.update(0, a[i] + 1, -1);
} else {
seg.update(0, b[i] + 1, -1);
b[i] = x;
seg.update(0, b[i] + 1, 1);
}
int tmp = seg.get();
if (!tmp)
cout << -1 << '\n';
else
cout << tmp << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, m, l;
cin >> n >> d >> m >> l;
long long pos, eq, ans;
pos = (n - 1) * m + l;
eq = pos % d;
eq = d - eq;
ans = pos + eq;
for (int i = 1; i <= n; i++) {
pos = (i - 1) * m + l;
eq = pos % d;
eq = d - eq;
if (eq < m - l) {
ans = pos + eq;
break;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, -1, 0, 1};
template <class T>
void inputVector(vector<T>& v, long long n) {
v.resize(n);
for (long long i = 0; i < v.size(); i++) cin >> v[i];
}
signed main() {
long long N;
cin >> N;
vector<long long> a;
inputVector(a, N);
reverse((a).begin(), (a).end());
long long sum[2] = {0, 0};
sum[0] = a[0];
long long prevDecide = 0;
for (long long i = (1); i <= (N - 1); i++) {
sum[1 - prevDecide] += a[i];
long long nowDecide;
if (sum[1 - prevDecide] >= sum[prevDecide])
nowDecide = 1 - prevDecide;
else
nowDecide = prevDecide;
prevDecide = nowDecide;
}
if (prevDecide)
cout << sum[0] << " " << sum[1] << endl;
else
cout << sum[1] << " " << sum[0] << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
std::ios::sync_with_stdio(false);
int n, a, b;
cin >> n >> a >> b;
int used[105];
fill(used, used + 105, 0);
for (int i = 0; i < a; i++) {
int q;
cin >> q;
used[q] = 1;
}
for (int i = 0; i < b; i++) {
int q;
cin >> q;
used[q] = 2;
}
for (int i = 1; i <= n; i++) {
cout << used[i] << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
char matrice[105][105];
bool lie[105] = {0};
bool hang[105] = {0};
scanf("%d", &n);
for (int i = 0; i < n; i++) {
getchar();
for (int j = 0; j < n; j++) scanf("%c", &matrice[i][j]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrice[i][j] == '.') {
hang[i] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrice[j][i] == '.') {
lie[i] = 1;
}
}
}
int flag = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (lie[i] == 0 && hang[j] == 0) flag = 1;
}
}
if (flag == 1) {
cout << "-1";
return 0;
} else {
for (int i = 0; i < n; i++) {
if (lie[i] == 0) flag = 2;
}
for (int i = 0; i < n; i++) {
if (hang[i] == 0) flag = 3;
}
}
if (flag == 2 || flag == 0) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (matrice[i][j] == '.') {
cout << i + 1 << " " << j + 1 << endl;
break;
}
}
return 0;
}
if (flag == 3 || flag == 0) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (matrice[j][i] == '.') {
cout << j + 1 << " " << i + 1 << endl;
break;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 9;
const long long MAX_SIZE = 1e7 + 1;
long long _t = 1, _T, csn;
long long max(long long a, long long b) {
if (a < b) return b;
return a;
}
long long min(long long a, long long b) {
if (a > b) return b;
return a;
}
long long mmi(long long a, long long m) {
long long m0 = m;
long long y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m;
long long t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
long long nxor(long long n) {
if (n % 4 == 0) return n;
if (n % 4 == 1) return 1;
if (n % 4 == 2) return n + 1;
return 0;
}
long long sum(long long n) { return ((n * (n + 1)) / 2); }
long long modsum(long long n, long long m) {
long long ans = n % m;
ans *= (n + 1) % m;
ans %= m;
ans *= mmi(2, m);
return ans % m;
}
long long leap(long long y) {
if (y % 400 == 0)
return 1;
else if (y % 100 == 0)
return 0;
else if (y % 4 == 0)
return 1;
else
return 0;
}
void solve();
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
bool multicases = 1;
if (multicases) cin >> _t;
_T = _t;
while (_t--) solve();
return 0;
}
void solve() {
csn = _T - _t;
long long a, b, c;
cin >> a >> b >> c;
;
if (a >= c) {
cout << -1 << " " << b << "\n";
return;
}
if (a * b >= c) {
cout << 1 << " ";
if (a * b == c)
cout << -1 << "\n";
else
cout << b << "\n";
return;
}
if (a * b < c) {
cout << 1 << " " << -1 << "\n";
return;
}
return;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
while (cin >> n) {
if (n % 4 == 1 || n % 4 == 2)
cout << 1 << endl;
else
cout << 0 << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[50005], ans[50005], nm[50005];
double b[50005], deg, deg0;
bool cmp(int x, int y) { return b[x] > b[y]; }
double work(int a, int n, int b) {
double l = n / cos(deg) / 2;
double A = a + l, B = b + l;
double C = sqrt(A * A + B * B - 2 * A * B * cos(deg0));
double degA = acos((B * B + C * C - A * A) / B / C / 2),
degB = acos((A * A + C * C - B * B) / A / C / 2);
double h1 = a * sin(degB), h2 = b * sin(degA);
double h = h1 - h2;
if (h < 0) h = -h;
return C * h;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
deg = 3.1415926535 / n;
deg0 = 3.1415926535 - deg * 2;
for (int i = 1; i <= n; i++)
if (i == n)
b[i] = work(n - a[n], n, a[1]);
else
b[i] = work(n - a[i], n, a[i + 1]);
for (int i = 1; i <= n; i++) nm[i] = i;
sort(nm + 1, nm + 1 + n, cmp);
int l = 0, r = n - 1;
for (int i = 1; i <= n; i++) {
int x = n - a[nm[i]], y;
if (nm[i] == n)
y = a[1];
else
y = a[nm[i] + 1];
if (x < y) {
ans[nm[i]] = r;
r--;
} else {
ans[nm[i]] = l;
l++;
}
}
for (int i = 1; i < n; i++) printf("%d ", ans[i]);
printf("%d\n", ans[n]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int dp[maxn][maxn][30][2];
char ans[maxn][maxn];
int n, m;
struct node {
int to, val;
node(int a, int b) {
to = a;
val = b;
}
};
vector<node> edge[maxn];
int dfs(int a, int b, int v, int t) {
if (dp[a][b][v][t]) return dp[a][b][v][t];
int win = 0;
if (t == 0) {
win = 'B';
int siz = edge[a].size();
for (int i = 0; win != 'A' && i < siz; i++) {
int to = edge[a][i].to;
int val = edge[a][i].val;
if (val >= v) {
if (dfs(to, b, val, 1) == 'A') win = 'A';
}
}
}
if (t == 1) {
win = 'A';
int siz = edge[b].size();
for (int i = 0; win != 'B' && i < siz; i++) {
int to = edge[b][i].to;
int val = edge[b][i].val;
if (val >= v) {
if (dfs(a, to, val, 0) == 'B') win = 'B';
}
}
}
dp[a][b][v][t] = win;
return win;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v, c;
char ch[10];
scanf("%d%d%s", &u, &v, ch);
edge[--u].push_back(node(--v, ch[0] - 'a' + 1));
}
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans[i][j] = dfs(i, j, 0, 0);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%c", ans[i][j]);
}
printf("\n");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, p, q, i, x, k = 1;
cin >> n >> x;
int a[n], b[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
cin >> b[i];
}
sort(a, a + n);
sort(b, b + n, greater<int>());
for (i = 0; i < n; i++) {
if (a[i] + b[i] > x) k = 0;
}
if (k == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[10000];
scanf("%s", a);
int l = strlen(a), i, k = 0, h = 0;
for (i = 0; i < l - 1; i++) {
if (a[i] == a[i + 1])
h++;
else
k++, h = 0;
if (h == 5) k++, h = 0;
}
printf("%d\n", k + 1);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool isvalid(long long x, long long y) {
if (max(x, y) <= 3 && min(x, y) >= 1) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
long long i, j, k, m, n, d, t, q, p, c;
long long a[4][4];
long long x[] = {-1, 0, 0, 0, 1};
long long y[] = {0, -1, 0, 1, 0};
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++) a[i][j] = 1;
for (i = 1; i <= 3; i++)
for (j = 1; j <= 3; j++) {
cin >> d;
for (k = 0; k <= 4; k++) {
if (isvalid(i + x[k], j + y[k]))
a[i + x[k]][j + y[k]] = a[i + x[k]][j + y[k]] + d;
}
}
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
cout << a[i][j] % 2;
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const int maxn = 2e5 + 10, maxm = 3e5 + 10, lg = 21, mod = 1e9 + 7, inf = 1e18;
int n, m, q, par[maxn][lg + 1], bus[maxn][lg + 1], A[maxn], B[maxn], h[maxn],
st[maxn], ft[maxn], tt, mn[maxn];
vector<int> g[maxn], bb[maxn];
void pfs(int v = 1, int p = 0) {
st[v] = tt++;
par[v][0] = p;
for (int i = 1; i <= lg; i++) par[v][i] = par[par[v][i - 1]][i - 1];
h[v] = h[p] + 1;
for (auto u : g[v])
if (u != p) pfs(u, v);
ft[v] = tt;
}
int lca(int v, int u) {
if (h[v] < h[u]) swap(v, u);
for (int i = 0, x = h[v] - h[u]; i <= lg; i++)
if (x >> i & 1) v = par[v][i];
if (v == u) return v;
for (int i = lg; i >= 0; i--)
if (par[v][i] != par[u][i]) v = par[v][i], u = par[u][i];
return par[v][0];
}
void pff(int v = 1, int p = 0) {
mn[v] = v;
for (auto i : bb[v]) {
int u = lca(v, A[i] ^ B[i] ^ v);
if (h[u] < h[mn[v]]) mn[v] = u;
}
for (auto u : g[v])
if (u != p) {
pff(u, v);
if (h[mn[u]] < h[mn[v]]) mn[v] = mn[u];
}
}
void dfs(int v = 1, int p = 0) {
bus[v][0] = mn[v];
for (int i = 1; i <= lg; i++) bus[v][i] = bus[bus[v][i - 1]][i - 1];
for (auto u : g[v])
if (u != p) dfs(u, v);
}
vector<pair<int, int> > seg[maxn << 2];
void add(int i, pair<int, int> p, int s = 0, int t = n, int v = 1) {
seg[v].push_back(p);
if (t - s == 1) return;
(i < ((s + t) >> 1) ? add(i, p, s, ((s + t) >> 1), (v << 1))
: add(i, p, ((s + t) >> 1), t, ((v << 1) ^ 1)));
}
bool get(int l, int r, int l2, int r2, int s = 0, int t = n, int v = 1) {
if (l >= t || r <= s) return 0;
if (l <= s && r >= t) {
auto lb = lower_bound(seg[v].begin(), seg[v].end(), make_pair(l2, 0));
if (lb == seg[v].end()) return 0;
return ((lb->second) <= r2);
}
return get(l, r, l2, r2, s, ((s + t) >> 1), (v << 1)) |
get(l, r, l2, r2, ((s + t) >> 1), t, ((v << 1) ^ 1));
}
pair<int, int> bet(int v, int u) {
if (h[bus[v][lg]] > h[u]) return {-1, -1};
int ret = 0;
for (int i = lg; i >= 0; i--)
if (h[bus[v][i]] > h[u]) v = bus[v][i], ret += (1 << i);
return {ret + 1, v};
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
g[p].push_back(i);
}
pfs();
cin >> m;
for (int i = 0; i < m; i++) {
cin >> A[i] >> B[i];
bb[A[i]].push_back(i), bb[B[i]].push_back(i);
add(st[A[i]], make_pair(st[B[i]], ft[B[i]]));
add(st[B[i]], make_pair(st[A[i]], ft[A[i]]));
}
for (int i = 0; i < 4 * maxn; i++) {
sort(seg[i].begin(), seg[i].end());
for (int j = (int)(seg[i].size()) - 1, nw = mod; j >= 0; j--) {
nw = min(nw, seg[i][j].second);
seg[i][j].second = nw;
}
}
pff(), dfs();
cin >> q;
for (int i = 1; i <= q; i++) {
int v, u;
cin >> v >> u;
int w = lca(v, u);
if (w == v) {
cout << bet(u, v).first << '\n';
continue;
}
if (w == u) {
cout << bet(v, u).first << '\n';
continue;
}
pair<int, int> a = bet(u, w), b = bet(v, w);
if (a.first == -1 || b.first == -1) {
cout << "-1\n";
continue;
}
cout << a.first + b.first -
get(st[a.second], ft[a.second], st[b.second], ft[b.second])
<< '\n';
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int isPrime(int n) {
int i;
for (i = 2; i * 1ll * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n, i, j, fl = 0;
scanf("%d", &n);
if (isPrime(n)) {
puts("1");
return 0;
}
if (n == 2) {
puts("1");
} else if (n == 3) {
puts("1");
} else if ((n % 2 == 0) || ((n % 2 == 1) && (isPrime(n - 2)))) {
puts("2");
} else {
puts("3");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 87;
const long long inf = 1e18;
struct edge {
int u, v, w;
int go(int x) { return u ^ v ^ x; }
} e[N];
vector<int> g[N];
int n, m, q;
void dijk(int s, long long d[], int pr[], vector<int>& tp) {
tp.clear();
fill_n(d, n + 1, inf);
d[s] = 0;
pr[s] = 0;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
greater<pair<long long, long long>>>
pq;
pq.push({0, s});
while (pq.size()) {
long long w, u;
tie(w, u) = pq.top();
pq.pop();
if (w != d[u]) continue;
tp.push_back(u);
for (int i : g[u]) {
int v = e[i].go(u);
long long nw = w + e[i].w;
if (d[v] > nw) {
d[v] = nw;
pq.push({nw, v});
pr[v] = i;
}
}
}
}
long long ds[N], dt[N];
int pr[N];
int on[N];
vector<int> pe, pv;
long long ohne[N];
long long opt;
int lt[N], rt[N];
vector<long long> add[N], del[N];
void bld() {
vector<int> tss, tdd;
dijk(1, ds, pr, tss);
memset(pr, 0, sizeof pr);
dijk(n, dt, pr, tdd);
opt = ds[n];
pv = pe = {0};
for (int u = 1; pr[u]; u = e[pr[u]].go(u)) {
int i = pr[u];
on[i] = pe.size();
pe.push_back(i);
pv.push_back(u);
}
pv.push_back(n);
for (int u : tss) {
if (u == 1) {
lt[u] = 0;
continue;
}
lt[u] = N;
for (int i : g[u]) {
int v = e[i].go(u);
if (ds[v] + e[i].w != ds[u]) continue;
if (on[i]) {
lt[u] = min(lt[u], on[i]);
} else {
lt[u] = min(lt[u], lt[v]);
}
}
}
for (int u : tdd) {
if (u == n) {
rt[u] = pe.size();
continue;
}
rt[u] = -N;
for (int i : g[u]) {
int v = e[i].go(u);
if (dt[v] + e[i].w != dt[u]) continue;
if (on[i]) {
rt[u] = max(rt[u], on[i]);
} else {
rt[u] = max(rt[u], rt[v]);
}
}
}
auto upd = [&](int u, int v, int w) {
if (ds[u] >= inf || dt[v] >= inf) return;
long long d = ds[u] + w + dt[v];
int l = lt[u] + 1, r = rt[v] - 1;
if (l <= r) {
assert(l > 0 && r < pe.size());
add[l].emplace_back(d);
del[r + 1].emplace_back(d);
}
};
for (int i = 1; i <= m; ++i)
if (!on[i]) {
int u = e[i].u, v = e[i].v, w = e[i].w;
upd(u, v, w);
upd(v, u, w);
}
multiset<long long> ms;
for (int i = 1; i < pe.size(); ++i) {
for (long long x : add[i]) ms.insert(x);
for (long long x : del[i]) ms.erase(ms.lower_bound(x));
ohne[i] = ms.size() ? *begin(ms) : inf;
}
}
long long qry(int t, long long x) {
int u = e[t].u, v = e[t].v, w = e[t].w;
if (!on[t]) return min({opt, ds[u] + x + dt[v], ds[v] + x + dt[u]});
return min(ohne[on[t]], opt - w + x);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= m; ++i) {
cin >> e[i].u >> e[i].v >> e[i].w;
g[e[i].u].emplace_back(i);
g[e[i].v].emplace_back(i);
}
bld();
while (q--) {
int t, x;
cin >> t >> x;
cout << qry(t, x) << '\n';
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int INF = 1e9 + 7;
vector<vector<int>> par;
struct sp_table {
vector<vector<pair<int, int>>> rmq;
sp_table(vector<int>& v) {
int n = v.size();
rmq = vector<vector<pair<int, int>>>(
n, vector<pair<int, int>>(log2(n) + 1, {-1, -1}));
for (int i = 0; i < n; i++) rmq[i][0] = {v[i], i};
for (int k = 1, pow = 2; k <= log2(n); k++, pow *= 2)
for (int i = 0; i + pow - 1 < n; i++)
rmq[i][k] = min(rmq[i][k - 1], rmq[i + pow / 2][k - 1]);
}
int _min(int a, int b) {
if (b == a) return rmq[a][0].second;
int d = log2(b - a + 1);
return min(rmq[a][d], rmq[b - (1 << d) + 1][d]).second;
}
};
long long cur_sum;
long long mx_sum;
vector<int> cur_ans;
vector<int> mx_ans;
long long solve(sp_table& spt, vector<int>& m, int st, int fin) {
if (st > fin) return 0;
int mn = spt._min(st, fin);
long long sm1 = solve(spt, m, st, mn - 1);
long long sm2 = solve(spt, m, mn + 1, fin);
if (sm1 + (long long)m[mn] * (fin - mn + 1) >
sm2 + (long long)m[mn] * (mn - st + 1)) {
for (int i = mn; i <= fin; i++) {
mx_ans[i] = m[mn];
sm1 += m[mn];
}
return sm1;
} else {
for (int i = st; i <= mn; i++) {
mx_ans[i] = m[mn];
sm2 += m[mn];
}
return sm2;
}
}
int main() {
int n;
cin >> n;
vector<int> m(n);
for (int i = 0; i < n; i++) cin >> m[i];
sp_table spt(m);
mx_ans = vector<int>(n);
cur_ans = vector<int>(n);
solve(spt, m, 0, n - 1);
for (int num : mx_ans) cout << num << ' ';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL<<61;
static const int MAX_N = 100005;
int n, m, c, d[MAX_N], cumu[MAX_N], visit[MAX_N];
vector<pii> G[MAX_N], res;
void dijkstra() {
fill(d, d + MAX_N, INF);
priority_queue<pii, vector<pii>, greater<pii> > q;
d[1] = 0;
q.push(pii(0, 1));
while (q.size()) {
pii p = q.top();
q.pop();
int v = p.second;
if (d[v] < p.first) continue;
for (int i = 0; i < G[v].size(); ++i) {
pii e = G[v][i];
int to = e.first, cost = e.second;
if (d[to] < cost + d[v]) continue;
d[to] = cost + d[v];
q.push(pii(d[to], to));
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> c;
for (int i = 0; i < m; ++i) {
int a , b, d;
cin >> a >> b >> d;
G[a].PB(pii(b, d));
G[b].PB(pii(a, d));
}
dijkstra();
for (int i = 1; i <= n; ++i) {
res.PB(pii(d[i], i));
}
sort(res.begin(), res.end());
for (int i = res.size() - 1; 0 <= i; --i) {
int v = res[i].second;
visit[v] = true;
cumu[i] += cumu[i + 1];
for (int j = 0; j < G[v].size(); ++j) {
int to = G[v][j].first;
if (visit[to]) continue;
cumu[i] += G[v][j].second;
}
}
int ans = INF;
for (int i = 0; i < res.size(); ++i) {
int temp = res[i].first * c, v = res[i].second;
temp += cumu[i + 1];
ans = min(ans, temp);
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int const N = 60, inf = 1e9;
char g[N][N + 1];
int n, m;
void solve() {
scanf("%d%d", &n, &m);
for (int i = 0; i < (int)(n); ++i) scanf("%s", g[i]);
int an = inf;
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j)
if (g[i][j] == 'A') {
int z = 2;
if (i > 0 && i + 1 < n) ++z;
if (j > 0 && j + 1 < m) ++z;
an = min(an, z);
}
for (int i = 0; i < (int)(n); ++i) {
int z = 0;
for (int j = 0; j < (int)(m); ++j) z += g[i][j] == 'A';
if (z == m) an = min(an, 1 + (i > 0 && i + 1 < n));
}
for (int j = 0; j < (int)(m); ++j) {
int z = 0;
for (int i = 0; i < (int)(n); ++i) z += g[i][j] == 'A';
if (z == n) an = min(an, 1 + (j > 0 && j + 1 < m));
}
int z = 0;
for (int i = 0; i < (int)(n); ++i)
for (int j = 0; j < (int)(m); ++j) z += g[i][j] == 'A';
if (z == n * m) an = 0;
if (an == inf)
printf("MORTAL\n");
else
printf("%d\n", an);
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int n, a[maxn], freq[101];
int solve(int d, int v) {
unordered_map<int, int> presum;
presum[0] = -1;
int ans = 0;
int sum = 0;
int minsum = 0, maxsum = 0;
for (int i = 0; i < n; i++) {
if (a[i] == d) sum++;
if (a[i] == v) sum--;
if (sum >= minsum && sum <= maxsum) {
ans = max(ans, i - presum[sum]);
} else
presum[sum] = i;
minsum = min(minsum, sum);
maxsum = max(maxsum, sum);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ans = 0;
cin >> n;
int d = 1;
for (int i = 0; i < n; i++) {
cin >> a[i];
freq[a[i]] += 1;
if (freq[d] < freq[a[i]]) d = a[i];
}
for (int i = 1; i <= 100; i++) {
if (d == i) continue;
ans = max(ans, solve(d, i));
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
const long long N = 5e2 + 5;
long long n, a[N][N], b[N][N], d[N][N], vis[N], x, sum;
vector<long long> v, ans;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n, v.resize(n);
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++) cin >> b[i][j], d[i][j] = 1e9;
for (long long i = 0; i < n; i++) cin >> v[i];
reverse(v.begin(), v.end());
for (long long i = 0; i < n; i++) {
x = v[i], vis[x] = 1, sum = 0;
for (long long j = 1; j <= n; j++)
if (vis[j])
a[j][x] = b[j][x], a[x][j] = b[x][j], d[j][x] = min(d[j][x], a[j][x]),
d[x][j] = min(d[x][j], a[x][j]);
for (long long j = 1; j <= n; j++)
if (vis[j])
for (long long k = 1; k <= n; k++)
if (vis[k])
d[x][j] = min(d[x][j], d[x][k] + d[k][j]),
d[j][x] = min(d[j][x], d[j][k] + d[k][x]);
for (long long j = 1; j <= n; j++)
if (vis[j])
for (long long k = 1; k <= n; k++)
if (vis[k]) d[j][k] = min(d[j][k], d[j][x] + d[x][k]);
for (long long j = 1; j <= n; j++)
if (vis[j])
for (long long k = 1; k <= n; k++)
if (vis[k]) sum += d[j][k];
ans.push_back(sum);
}
reverse(ans.begin(), ans.end());
for (auto &i : ans) cout << i << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long inf = 1e18;
int __FAST_IO__ = []() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
return 0;
}();
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
vector<int> v(N);
for (int i = 0; i < (N); ++i) cin >> v[i];
int ans = 1e9;
for (int i = 0; i < (5); ++i)
for (int j = 0; j < (5); ++j) {
int tot = i + j * 2;
vector<int> mo(tot + 1, -1);
for (int x = 0; x < (i + 1); ++x)
for (int y = 0; y < (j + 1); ++y) {
mo[x + y * 2] = x + y;
}
int ans2 = 0;
for (int k = 0; k < (N); ++k) {
bool flag = false;
for (int x = (tot); x >= 0; --x) {
if (mo[x] >= 0 && v[k] >= x && (v[k] - x) % 3 == 0) {
ans2 = max(ans2, (v[k] - x) / 3);
flag = true;
break;
}
}
if (!flag) {
ans2 = 1e9;
break;
}
}
ans = min(ans, ans2 + i + j);
}
printf("%d\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int di[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dj[8] = {0, 0, -1, 1, 0, 0, 0, 0};
int A[100000];
int main() {
int n;
cin >> n;
int k;
cin >> k;
for (int i = 0; i < (n); ++i) {
A[i] = i;
}
reverse(A, A + k + 1);
for (int i = 0; i < (n); ++i) {
printf("%d%c", A[i] + 1, i == n - 1 ? '\n' : ' ');
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char a1[N], a2[N], b1[N], b2[N], t1[N], t2[N];
int main() {
int n;
int a;
char hasSoultuion = true;
cin >> n;
for (int i = 0; i < n - 1; ++i) {
cin >> a;
a1[i] = a & 1, a2[i] = (a >> 1) & 1;
}
for (int i = 0; i < n; ++i) t1[i] = t2[i] = 2;
for (int i = 0; i < n - 1; ++i) {
cin >> a;
b1[i] = a & 1, b2[i] = (a >> 1) & 1;
if (a1[i] == 0 && b1[i] == 1) {
hasSoultuion = false;
break;
}
if (a1[i] == 0 && b1[i] == 0)
t1[i] = t1[i + 1] = 0;
else if (a1[i] == 1 && b1[i] == 1)
t1[i] = t1[i + 1] = 1;
if (a2[i] == 0 && b2[i] == 1) {
hasSoultuion = false;
break;
}
if (a2[i] == 0 && b2[i] == 0)
t2[i] = t2[i + 1] = 0;
else if (a2[i] == 1 && b2[i] == 1)
t2[i] = t2[i + 1] = 1;
}
for (int i = 0; i < n - 1; ++i) {
if (t1[i] != 2 && t1[i + 1] == 2) t1[i + 1] = 1 ^ t1[i];
if (t2[i] != 2 && t2[i + 1] == 2) t2[i + 1] = 1 ^ t2[i];
}
for (int i = n - 1; i >= 1; --i) {
if (t1[i] != 2 && t1[i - 1] == 2) t1[i - 1] = 1 ^ t1[i];
if (t2[i] != 2 && t2[i - 1] == 2) t2[i - 1] = 1 ^ t2[i];
}
char l1 = 1, l2 = 1;
for (int i = 0; i < n; ++i) {
if (t1[i] == 2) t1[i] = l1;
l1 ^= 1;
if (t2[i] == 2) t2[i] = l2;
l2 ^= 1;
}
for (int i = 0; i < n - 1; ++i) {
if (((t1[i] | t1[i + 1]) == a1[i]) && ((t2[i] | t2[i + 1]) == a2[i]) &&
((t1[i] & t1[i + 1]) == b1[i]) && ((t2[i] & t2[i + 1]) == b2[i])) {
} else {
}
}
if (hasSoultuion) {
cout << "YES" << endl;
for (int i = 0; i < n; ++i) {
cout << (int)t1[i] + (int)t2[i] * 2 << " ";
}
} else {
cout << "NO" << endl;
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> adj[n + 1];
set<int> s;
vector<int> a;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
a.push_back(k);
if (adj[k].size() > 0) {
if (adj[k].back() == i) {
adj[k].pop_back();
adj[k].push_back(i + 1);
}
else adj[k].push_back(i + 1);
}
else {
adj[k].push_back(i + 1);
}
s.insert(k);
}
if (s.size() == 1) {
cout << 0 << endl;
continue;
}
else {
int ans = 2000000;
for (int i = 1; i <= n; i++) {
if (adj[i].size() >= 1) {
int temp = adj[i].size() - 1;
if (adj[i][temp] != n) temp++;
if (i != a[0]) temp++;
ans = min(ans, temp);
}
}
cout << ans << endl;
}
}
return 0;
} | 3 |
#include<iostream>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k == 1) cout << 0;
else cout << n - k;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 4e3 + 10, MOD = 1e9 + 7, INF = 1e15, SQ = 370, LOG = 30;
long long n, m, x, y, x2, y2, z, t, now, d, cur, ptr, answer, q, p, ans,
dp[N][N], c[N][N], sum[N];
int main() {
cin >> n;
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == i || j == 1)
dp[i][j] = 1;
else
dp[i][j] = (dp[i - 1][j] * j + dp[i - 1][j - 1]) % MOD;
sum[i] += dp[i][j];
sum[i] %= MOD;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i)
c[j][i] = 1;
else
c[j][i] = (c[j - 1][i - 1] + c[j][i - 1]) % MOD;
}
}
ans++;
for (int i = 1; i < n; i++) ans += (sum[i] * c[i][n]) % MOD, ans %= MOD;
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 5;
const long long mod = 1000000007;
int n;
long long a[maxn], s[maxn], mi[maxn];
long long ans;
long long mult(long long a, long long b) {
if (b == 1) return a;
long long temp = mult(a, b / 2);
temp = (temp + temp) % mod;
if (b % 2 == 1) temp = (temp + a) % mod;
return temp;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%I64d", a + i);
sort(a, a + n);
mi[0] = 1;
for (int i = 0; i < n; i++) {
if (i == 0)
s[i] = a[i];
else
s[i] = s[i - 1] + a[i];
if (i != 0) mi[i] = (2 * mi[i - 1]) % mod;
}
for (int k = 1; k < n; k++) {
ans = (ans + mult(mi[k - 1], s[n - 1] - s[n - 1 - k] - s[k - 1])) % mod;
}
ans = (ans + mod) % mod;
cout << ans << endl;
return 0;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.