solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<bits/stdc++.h>
using namespace std;
void fail(){
cout<<"No"<<endl;
exit(0);
}
int a[100001];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,m=0,s=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i],m=max(m,a[i]);
for(int i=1;i<=n;i++)
if(a[i]<m-1)
fail();
else if(m-1==a[i])
s++;
if(s==0){
if(m==n-1||m<=n/2) cout<<"Yes"<<endl;
else fail();
return 0;
}
if(s>=m) fail();
if((m-s)*2>n-s)
fail();
else
cout<<"Yes"<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long double min(long double a, long double b) {
if (a < b) return a;
return b;
}
inline long double max(long double a, long double b) {
if (a < b) return b;
return a;
}
vector<string> split(string s, string c) {
unsigned long pos2 = s.find(c);
unsigned long pos1 = 0;
vector<string> res;
while (pos2 != s.size()) {
res.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.size()) res.push_back(s.substr(pos1));
return res;
}
int n;
int arr[100100];
int dp[100100][2];
int solve() {
dp[0][0] = 0;
dp[0][1] = 0;
dp[0][arr[0]] = 1;
for (int(i) = (1); (i) < (n); ++(i)) {
if (arr[i] == 0) {
dp[i][1] = dp[i - 1][1];
dp[i][0] = dp[i - 1][1] + 1;
} else {
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][0] + 1;
}
}
int res = max(dp[n - 1][0], dp[n - 1][1]);
return min(n, res + 2);
int len = 1;
int count = 1;
for (int(i) = (1); (i) < (n); ++(i)) {
if (arr[i] == arr[i - 1])
count++;
else {
len = max(len, count);
count = 1;
}
}
len = max(len, count);
if (len >= 3)
return res + 2;
else if (len == 2)
return res + 1;
return res;
}
string s;
int main() {
cin >> n;
cin >> s;
for (int(i) = (0); (i) < (n); ++(i)) arr[i] = s[i] - '0';
cout << solve() << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int menor[100010], maior[100010];
int main() {
int n, m;
int i;
int rig, lef;
scanf("%d %d", &n, &m);
if (m == 0) {
printf("%d\n", n - 1);
return 0;
}
for (i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
if (b < a) {
int temp = a;
a = b;
b = temp;
}
menor[i] = a;
maior[i] = b;
}
sort(menor, menor + m);
sort(maior, maior + m);
rig = maior[0] - 1;
lef = menor[m - 1];
if (rig == 0 || lef == n + 1) {
printf("0\n");
return 0;
}
if (lef > rig) {
printf("0\n");
return 0;
}
printf("%d\n", rig - lef + 1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > ans;
struct node {
int first, second;
bool operator<(const node& a) const {
return first > a.first || (first == a.first && second < a.second);
}
};
multiset<node> s;
int main() {
int i, j, k, m, n;
while (~scanf("%d%d", &n, &m)) {
s.clear();
for (i = 0; i < n; i++) {
scanf("%d", &j);
node z;
z.first = j, z.second = i + 1;
if (j) s.insert(z);
}
if (s.size() == 1) {
puts("No");
continue;
}
ans.clear();
vector<node> q;
multiset<node>::iterator it;
while (!s.empty() && s.size() >= 2) {
it = s.begin();
node e = *it;
if (e.first > s.size() - 1) break;
q.clear();
for (i = 0, it++; i < e.first; i++, it++) {
q.push_back(*it);
ans.push_back(make_pair(e.second, it->second));
}
s.erase(s.begin());
for (i--; i >= 0; i--) {
s.erase(q[i]);
q[i].first--;
if (q[i].first > 0) s.insert(q[i]);
}
}
if (!s.empty())
puts("No");
else {
printf("Yes\n%d\n", ans.size());
for (i = 0; i < ans.size(); i++)
printf("%d %d\n", ans[i].first, ans[i].second);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int N = 2097155;
char s[N], t[N];
int n, cnt[N], all;
unsigned long long a[N], b[N];
int main() {
scanf("%d%s%s", &n, s, t);
all = (1 << n);
for (register int i = 1; i <= all - 1; i++) cnt[i] = cnt[i >> 1] + (i & 1);
for (register int i = 0; i <= all - 1; i++)
a[i] = (1LL * s[i] - '0') << (cnt[i] * 2), b[i] = (1LL * t[i] - '0')
<< (cnt[i] * 2);
for (register int j = 0; j <= n - 1; j++)
for (register int i = 0; i <= all - 1; i++)
a[i] += ((i >> j) & 1) ? a[i ^ (1 << j)] : 0,
b[i] += ((i >> j) & 1) ? b[i ^ (1 << j)] : 0;
for (register int i = 0; i <= all - 1; i++) a[i] *= b[i];
for (register int j = 0; j <= n - 1; j++)
for (register int i = 0; i <= all - 1; i++)
a[i] -= ((i >> j) & 1) ? a[i ^ (1 << j)] : 0;
for (register int i = 0; i <= all - 1; i++)
printf("%c", '0' + ((a[i] >> (cnt[i] * 2)) & 3));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const double PI = acos(-1);
using ll = long long;
struct Node {
double x;
int id;
} b[N * 2];
struct Pair {
int x, y;
} c[N];
double x[N], y[N], dis[N], s[N];
int t[N * 2], n;
ll kk;
inline int lowbit(int x) { return x & -x; }
void add(int x, int v) {
while (x <= n * 2) t[x] += v, x += lowbit(x);
}
int query(int x) {
int ret = 0;
while (x) ret += t[x], x -= lowbit(x);
return ret;
}
bool check(double r) {
double s1 = -1, s2 = -1, tt = -1;
ll sum = 0;
int idx = 0;
for (int i = 1; i <= n; i++)
if (dis[i] > r) {
tt = acos(r / dis[i]);
s1 = s[i] - tt, s2 = s[i] + tt;
if (s1 < 0) s1 += PI * 2;
if (s2 > PI * 2) s2 -= PI * 2;
if (s1 > s2) swap(s1, s2);
b[++idx] = {s1, -i}, b[++idx] = {s2, i};
}
if (idx) {
for (int i = 1; i <= n; i++) c[i] = {1, 0};
sort(b + 1, b + idx + 1, [](Node a, Node b) { return a.x < b.x; });
for (int i = 1; i <= idx; i++) {
if (b[i].id < 0)
c[-b[i].id].x = i;
else
c[b[i].id].y = i;
}
sort(c + 1, c + n + 1, [](Pair a, Pair b) { return a.x < b.x; });
memset(t, 0, sizeof(t));
for (int i = n; i > 0; --i) {
if (!c[i].y) continue;
sum += query(c[i].y);
add(c[i].x, 1);
add(c[i].y, -1);
}
}
sum = n * (n - 1ll) / 2 - sum;
return sum >= kk;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> kk;
for (int i = 1; i <= n; i++) {
int t1, t2;
cin >> t1 >> t2;
x[i] = t1;
y[i] = t2;
dis[i] = sqrt(x[i] * x[i] + y[i] * y[i]);
s[i] = atan2(y[i], x[i]);
if (s[i] < 0) s[i] += PI * 2;
}
double l = 0, r = 20000;
while (r - l > 1e-10) {
double mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
printf("%.10lf\n", l);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, ans = 0, i;
cin >> n;
long long int a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
ans = abs(a[0]);
for (i = 0; i < n - 1; i++) {
ans = ans + abs(a[i] - a[i + 1]);
}
cout << ans;
}
| 2 |
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
const static ull P = 107;
signed main(void)
{
string s1, s2;
cin >> s1 >> s2;
int len1 = s1.length();
int len2 = s2.length();
ull p[30] = {1ULL}; for (int i = 1; i < 30; ++i)p[i] = p[i - 1] * P;
for (int len = min(len1, len2); len >= 1; --len)
{
unordered_set<ull> us1;
unordered_set<ull> us2;
register ull tmp1 = 0ULL;
for (int i = 0; i < len1; ++i)
{
tmp1 += p[s1[i] - 'a'];
if (i >= len)
tmp1 -= p[s1[i - len] - 'a'];
if (i >= len - 1)
us1.insert(tmp1);
}
register ull tmp2 = 0ULL;
for (int i = 0; i < len2; ++i)
{
tmp2 += p[s2[i] - 'a'];
if (i >= len)
tmp2 -= p[s2[i - len] - 'a'];
if (i >= len - 1)
us2.insert(tmp2);
}
for (auto i : us1)
if (us2.count(i))
return cout << len << endl, 0;
}
return cout << 0 << endl, 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int kMaxn = 50 + 5;
const int kInf = 0x1f1f1f1f;
int s[kMaxn], z[kMaxn], f[kMaxn];
int a[kMaxn];
bool vis[kMaxn];
int Min;
void Count(int i, int pos, int m, char c) {
if (c >= '0' && c <= '9') s[i] = min(min(s[i], pos), min(s[i], m - pos));
if (c >= 'a' && c <= 'z') z[i] = min(min(z[i], pos), min(z[i], m - pos));
if (c == '#' || c == '*' || c == '&')
f[i] = min(min(f[i], pos), min(f[i], m - pos));
}
void Dfs(int n, int deep) {
if (deep == 3 + 1) {
int sum = s[a[1]] + z[a[2]] + f[a[3]];
Min = min(Min, sum);
return;
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
a[deep] = i;
vis[i] = true;
Dfs(n, deep + 1);
vis[i] = false;
}
}
}
int main() {
int n, m;
cin >> n >> m;
memset(s, kInf, sizeof(s));
memset(z, kInf, sizeof(z));
memset(f, kInf, sizeof(f));
memset(vis, false, sizeof(vis));
Min = kInf;
for (int i = 1; i <= n; i++) {
string ss;
getchar();
cin >> ss;
for (int j = 0; j < m; j++) Count(i, j, m, ss[j]);
}
Dfs(n, 1);
cout << Min << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int W;
cin >> W >> W;
string a, b(W + 2, '#');
for (cout << b << endl; cin >> a; cout << '#' << a << "#\n");
cout << b;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int hh, mm;
cin >> hh >> mm;
cout << 24 * 60 - hh * 60 - mm << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int x, y;
int row[100003], col[100003];
bool counted[100003];
bool flag = false;
int counter, rows, cols, counter2;
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
scanf("%d %d", &x, &y);
row[x] = 1;
col[y] = 1;
}
for (int i = 2; i < n; i++) {
if (n % 2 == 0 || i != (n / 2 + 1)) {
if (row[i] == 0) counter++;
if (col[i] == 0) counter++;
}
}
if (n % 2 != 0) {
if ((col[n / 2 + 1] != 0 && row[n / 2 + 1] == 0) ||
(col[n / 2 + 1] == 0 && row[n / 2 + 1] != 0) ||
(col[n / 2 + 1] == 0 && row[n / 2 + 1] == 0))
counter++;
}
cout << max(counter, 0);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7;
char arr[51][51];
long long vis[51][51];
long long flag;
long long n, m, k;
long long dx[] = {0, 0, 1, -1};
long long dy[] = {1, -1, 0, 0};
long long isValid(long long x, long long y) {
if (x < 0 || y < 0 || x >= n || y >= m || vis[x][y] != 0 || arr[x][y] == '*')
return 0;
return 1;
}
long long sc;
void dfs(long long x, long long y, long long cnt) {
vis[x][y] = cnt;
sc++;
if (x == 0 || y == 0 || x == n - 1 || y == m - 1) flag = 1;
for (long long i = 0; i < 4; i++) {
long long nx = x + dx[i];
long long ny = y + dy[i];
if (isValid(nx, ny)) {
dfs(nx, ny, cnt);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n >> m >> k;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) cin >> arr[i][j], vis[i][j] = 0;
}
long long x = 1;
vector<pair<long long, long long>> v;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (vis[i][j] == 0 && arr[i][j] == '.') {
flag = 0;
sc = 0;
dfs(i, j, x);
if (flag == 0) v.push_back({sc, x});
x = x + 1;
}
}
}
sort(v.begin(), v.end());
long long ans = 0;
long long ff = v.size();
ff = ff - k;
for (long long i = 0; i < ff; i++) {
long long key = v[i].second;
ans = ans + v[i].first;
for (long long l = 0; l < n; l++) {
for (long long j = 0; j < m; j++) {
if (vis[l][j] == key) arr[l][j] = '*';
}
}
}
cout << ans << "\n";
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) cout << arr[i][j];
cout << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string str;
cin >> str;
map<char, int> cnt;
for (auto i : str) ++cnt[i];
for (auto i : cnt) {
for (int j = 0; j < i.second; ++j) cout << i.first;
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long NMAX = 110;
int N, K, S;
vector<long long> V;
long long Fib[NMAX], Sum[NMAX];
int main() {
scanf("%I64d %i", &S, &K);
Fib[1] = Sum[1] = N = 1;
for (int i = 2;; ++i) {
Fib[i] = Sum[i - 1] - Sum[max(0, i - K - 1)];
if (Fib[i] > 1LL * S) break;
N++;
Sum[i] = Sum[i - 1] + Fib[i];
}
for (int i = N; i; --i)
if (Fib[i] <= S) V.push_back(Fib[i]), S -= Fib[i];
while (V.size() < 2) V.push_back(0);
printf("%i\n", V.size());
for (long long i = 0; i < V.size(); ++i) printf("%i ", V[i]);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long a00, a01, a10, a11;
int main() {
scanf("%lld%lld%lld%lld", &a00, &a01, &a10, &a11);
long long n = 1;
while (n <= 1000000) {
if (a00 + a01 + a10 + a11 == n * (n - 1) / 2)
break;
else
n++;
}
if (n > 1000000) {
printf("Impossible");
return 0;
}
long long zeros = -1, ones = -1;
for (long long i = 1; i <= n; i++) {
if (i * (i - 1) / 2 == a00) zeros = i;
if (i * (i - 1) / 2 == a11) ones = i;
}
if (zeros + ones > n) {
if (zeros == 1)
zeros--;
else if (ones == 1)
ones--;
}
if (zeros + ones != n) {
printf("Impossible");
return 0;
}
if (zeros < 0 || ones < 0) {
printf("Impossible");
return 0;
}
if (zeros == 0) {
for (int i = 0; i < ones; i++) printf("1");
return 0;
}
if (ones == 0) {
for (int i = 0; i < zeros; i++) printf("0");
return 0;
}
long long z = a01 / zeros;
long long m = a01 % zeros;
long long lft = m ? ones - z - 1 : ones - z;
for (int i = 0; i < lft; i++) printf("1");
for (int i = 0; i < m; i++) printf("0");
if (m) printf("1");
for (int i = m; i < zeros; i++) printf("0");
for (int i = 0; i < z; i++) printf("1");
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int readINT() {
register int x = 0, ch = getchar(), f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int q;
int n;
int h[201], ans, a[201];
inline void init() {
n = readINT();
int cnt = 0, ansl = 0, ansm = 0, sum = 0;
h[1] = readINT();
for (register int i = 2; i <= n; i++) {
h[i] = readINT();
a[i] = abs(h[i] - h[i - 1]);
if (abs(h[i] - h[i - 1]) != 1) {
cnt++;
}
if (a[i] == 0) {
ansm++;
}
}
if (abs(h[1] - h[n]) != 1) {
if (abs(h[1] - h[n]) != n - 1) {
ansl++;
}
}
if (cnt > 1) {
printf("NO\n");
return;
}
if (ansm > 0) {
printf("NO\n");
return;
}
if (n == 1) {
printf("YES\n");
return;
}
if (ansl > 0) {
printf("NO\n");
return;
}
printf("YES\n");
return;
}
int main() {
q = readINT();
while (q--) {
init();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d[1000005], i, j, k, ans;
int main() {
cin >> a >> b >> c;
for (i = 1; i <= 1e6; i++)
for (j = i; j <= 1e6; j += i) d[j]++;
for (i = 1; i <= a; i++)
for (j = 1; j <= b; j++)
for (k = 1; k <= c; k++) ans += d[i * j * k];
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long magic = 1e9 + 7;
int main() {
char s[2005];
char s1[2005];
char s2[2005];
scanf("%s", &s);
scanf("%s", &s1);
scanf("%s", &s2);
int sz = strlen(s);
int sz1 = strlen(s1);
int sz2 = strlen(s2);
unsigned long long t1 = 0, t2 = 0;
unsigned long long t = 0;
vector<int> v1, v2;
unsigned long long tmp1 = 1, tmp2 = 1;
for (int i = 0; i < sz1; i++) {
t1 = t1 * magic + s1[i];
tmp1 *= magic;
}
for (int i = 0; i < sz2; i++) {
t2 = t2 * magic + s2[i];
tmp2 *= magic;
}
for (int i = 0; i < sz; i++) {
t = t * magic + s[i];
if (i >= sz1) t -= s[i - sz1] * tmp1;
if (t == t1) v1.push_back(i - sz1 + 1);
}
t = 0;
for (int i = 0; i < sz; i++) {
t = t * magic + s[i];
if (i >= sz2) t -= s[i - sz2] * tmp2;
if (t == t2) v2.push_back(i - sz2 + 1);
}
if (s1 == s2) {
cout << v1.size() << endl;
return 0;
}
unordered_map<unsigned long long, int> mp;
int cnt = 0;
for (int i = 0; i < v1.size(); i++) {
int l = v1[i];
unsigned long long tmp = 0;
for (int j = 0; j < v2.size(); j++) {
int r = v2[j];
if (l + sz1 > r + sz2 || l > r) continue;
while (l != r) {
tmp = tmp * magic + s[l];
l++;
}
if (!mp[tmp + t2]) {
mp[tmp + t2]++;
cnt++;
}
}
}
cout << cnt << endl;
}
| 2 |
#include <iostream>
using namespace std;
int main(void){
long int n,m,d;
double ans,a,b;
cin >> n>>m>>d;
a = (double)n;
b = (double)m;
if (d == 0){
ans = (b-1)/a;
}
else{
ans = (b-1)*2*(a-d)/(a*a);
}
printf("%.10f\n",ans);
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n;
ll f[3010][3010],a[3010];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
f[i][i]=a[i];
}
for (int l=1;l<n;l++)
{
for (int i=1;i+l<=n;i++)
{
int j=i+l;
f[i][j]=max(a[i]-f[i+1][j],a[j]-f[i][j-1]);
}
}
cout<<f[1][n]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long counts[32];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int tmp, count = 0;
cin >> tmp;
for (int j = 1; j < tmp; j *= 2) {
if ((tmp & j) == j) {
count++;
}
}
counts[count]++;
}
long long res = 0;
for (int i = 0; i < 32; i++) {
res += counts[i] * (counts[i] - 1) / 2;
}
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 505;
long long n, d[N][N], x[N], a[N], dist[N][N];
void upd(long long &a, long long b) {
if (b < a) a = b;
}
int main() {
cin >> n;
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= n; j++) cin >> d[i][j], dist[i][j] = 1ll << 60;
for (long long i = 1; i <= n; i++) cin >> x[i], dist[i][i] = 0;
for (long long i = n; i; i--) {
for (long long j = i + 1; j <= n; j++) {
for (long long k = i + 1; k <= n; k++) {
upd(dist[x[i]][x[k]], d[x[i]][x[j]] + dist[x[j]][x[k]]);
upd(dist[x[k]][x[i]], dist[x[k]][x[j]] + d[x[j]][x[i]]);
}
}
for (long long j = i + 1; j <= n; j++) {
for (long long k = i + 1; k <= n; k++) {
upd(dist[x[j]][x[k]], dist[x[j]][x[i]] + dist[x[i]][x[k]]);
}
}
for (long long j = i; j <= n; j++) {
for (long long k = i; k <= n; k++) {
a[i] += dist[x[j]][x[k]];
}
}
}
for (long long i = 1; i <= n; i++) cout << a[i] << ' ';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a, b, n, k, s = -1e9;
int main() {
cin >> n >> k;
while (n--) {
cin >> a >> b;
if (k < b) {
a -= b - k;
}
s = max(s, a);
}
cout << s << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e6 + 100;
int t, m, n, k, tmo, ty, ans;
multiset<int> st;
bool mark[Maxn];
vector<int> nei[Maxn];
void dfs(int v) {
mark[v] = true, t++;
for (int u : nei[v])
if (!mark[u]) dfs(u);
}
void dfs_all() {
for (int i = 0; i < n; i++) {
if (!mark[i]) dfs(i), st.insert(t), t = 0, tmo++;
}
}
void find_ty() {
for (multiset<int>::iterator it = st.begin(); it != st.end(); it++) {
ty += min(*it, k);
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> k;
for (int i = 0, a, b; i < m; i++) {
cin >> a >> b;
nei[a - 1].push_back(b - 1);
nei[b - 1].push_back(a - 1);
}
dfs_all();
find_ty();
while (tmo - 1 > ty / 2) {
int f = *st.begin();
st.erase(st.find(f)), ty -= min(f, k);
int s = *st.begin();
st.erase(st.find(s)), ty -= min(s, k);
int h = min(f + s, k);
st.insert(f + s);
ty += h;
tmo--;
ans++;
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
template <typename T>
string NumberToString(T Number) {
ostringstream second;
second << Number;
return second.str();
}
inline int nxt() {
int aaa;
scanf("%d", &aaa);
return aaa;
}
inline long long int lxt() {
long long int aaa;
scanf("%lld", &aaa);
return aaa;
}
inline double dxt() {
double aaa;
scanf("%lf", &aaa);
return aaa;
}
template <class T>
inline T bigmod(T p, T e, T m) {
T ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % m;
p = (p * p) % m;
}
return (T)ret;
}
struct info {
long long int x, k;
int mi;
bool operator<(const info& other) const { return k > other.k; }
} ar[105];
int n, m;
long long int b;
long long int dp[3][(1 << 20) + 10];
int x = 0;
long long int go(int pos, int mask) {
if (mask == x) {
return 0;
}
if (pos >= n) return (long long int)3e18;
return dp[pos][mask];
}
long long int go() {}
int main() {
n = nxt(), m = nxt();
b = lxt();
for (int i = 0; i < n; i++) {
ar[i].x = lxt();
ar[i].k = lxt();
int t = nxt();
while (t--) {
int a = nxt();
a--;
ar[i].mi = biton(ar[i].mi, a);
}
}
sort(ar, ar + n);
for (int i = 0; i < m; i++) {
x = biton(x, i);
}
int cur = 1;
int prev = 0;
for (int i = 0; i <= x; i++) dp[cur][i] = (long long int)3e18;
for (int pos = n - 1; pos >= 0; pos--) {
swap(cur, prev);
for (int mask = x; mask >= 0; mask--) {
dp[cur][mask] =
min(go(prev, mask), (mask == 0 ? (ar[pos].k * b) : 0) +
go(prev, mask | ar[pos].mi) + ar[pos].x);
}
}
long long int res = dp[cur][0];
if (res >= (long long int)3e18)
printf("-1\n");
else
printf("%lld\n", res);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int d, s;
int dp[6100][610];
int mul[6100 * 610];
bool used[6100][610];
int len;
int xx[6100 * 610], yy[6100 * 610];
void out(int x, int y) {
if (dp[x][y] == 0) return;
bool Find = false;
for (int i = 0; i < 10 && i <= x && !Find; i++) {
if (dp[x - i][(((y - mul[dp[x][y] - 1] * i) % d) + d) % d] <=
dp[x][y] - 1) {
printf("%d", i);
int diff =
dp[x][y] - dp[x - i][(((y - mul[dp[x][y] - 1] * i) % d) + d) % d];
for (int j = 1; j < diff; j++) printf("0");
Find = true;
out(x - i, ((y - mul[dp[x][y] - 1] * i) % d + d) % d);
}
}
assert(Find);
}
int main() {
scanf("%d%d", &d, &s);
mul[0] = 1;
for (int i = 1; i <= d * s; i++) mul[i] = 10 * mul[i - 1] % d;
dp[0][0] = 0;
int q = 1, h = 1;
xx[1] = 0;
yy[1] = 0;
used[0][0] = true;
while (q <= h) {
for (int i = 0; i < 10; i++) {
int X = xx[q] + i, Y = (yy[q] * 10 + i) % d;
if (X <= s && !used[X][Y]) {
used[X][Y] = true;
dp[X][Y] = dp[xx[q]][yy[q]] + 1;
h++;
xx[h] = X;
yy[h] = Y;
}
}
q++;
}
if (!used[s][0]) {
printf("-1\n");
} else {
out(s, 0);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int alphabet = 26;
const int MAXINT = 1e5 + 1;
int n, m, a, b;
vector<int> G[MAXINT], test;
bool visit[MAXINT] = {};
priority_queue<int, vector<int>, greater<int>> pq;
void dfs(int node, int vis) {
if (vis <= n) {
cout << node << " ";
visit[node] = true;
for (int e : G[node])
if (!visit[e]) pq.push(e);
while (!pq.empty()) {
int t = pq.top();
pq.pop();
if (!visit[t]) {
dfs(t, vis + 1);
break;
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a >> b;
if (a != b) {
G[a].push_back(b);
G[b].push_back(a);
}
}
dfs(1, 1);
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main(int argc, char *argv[]) {
int n;
scanf("%d", &n);
int i;
if (n % 2 != 0) {
printf("-1\n");
} else {
if (n > 2) {
for (i = 1; i <= n - 2; i++) {
printf("%d ", i + 1);
printf("%d ", i);
i++;
}
}
printf("%d ", n);
printf("%d\n", n - 1);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 18;
inline long long f(int k) { return 1ll * k * (k - 1) / 2; }
int n, q;
vector<int> t[2 * N];
int idxRange(int i, int d, int u) {
int resu = upper_bound(t[i].begin(), t[i].end(), u) - t[i].begin();
int resd = lower_bound(t[i].begin(), t[i].end(), d) - t[i].begin();
return resu - resd;
}
int query(int l, int r, int d, int u) {
if (d > u) return 0;
++r;
int res = 0;
for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
if (l & 1) res += idxRange(l++, d, u);
if (r & 1) res += idxRange(--r, d, u);
}
return res;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 0, h; i < n; i++) {
scanf("%d", &h);
--h;
t[N + i].push_back(h);
}
for (int i = N - 1; i >= 1; i--) {
int jl = 0, jr = 0;
while (jl < t[i << 1].size() || jr < t[i << 1 | 1].size()) {
if (jr == t[i << 1 | 1].size() ||
(jl < t[i << 1].size() && t[i << 1][jl] < t[i << 1 | 1][jr]))
t[i].push_back(t[i << 1][jl++]);
else
t[i].push_back(t[i << 1 | 1][jr++]);
}
}
for (int i = 0, l, d, r, u; i < q; i++) {
scanf("%d %d %d %d", &l, &d, &r, &u);
--l;
--d;
--r;
--u;
long long ans = f(n) - (f(l) + f(d) + f(n - 1 - r) + f(n - 1 - u));
ans += f(query(0, l - 1, u + 1, n - 1));
ans += f(query(0, l - 1, 0, d - 1));
ans += f(query(r + 1, n - 1, u + 1, n - 1));
ans += f(query(r + 1, n - 1, 0, d - 1));
printf("%lld\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct DisjointSet {
DisjointSet(int n) {
pre.assign(n + 1, 0);
for (int i = 0; i <= n; i++) {
pre[i] = i;
}
}
vector<int> pre;
int find(int u) { return pre[u] == u ? pre[u] : pre[u] = find(pre[u]); }
void Union(int u, int v) { pre[find(u)] = find(v); }
};
int main(int argc, char *argv[]) {
int n, m;
scanf("%d%d", &n, &m);
vector<tuple<int, int, int>> a(m);
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
a[i] = {w, u, v};
}
vector<int> b(n + 1, 1);
DisjointSet ds(n);
sort(a.begin(), a.end(), greater<tuple<int, int, int>>());
int ans = 0;
for (auto e : a) {
int u, v, w;
tie(w, u, v) = e;
int fu = ds.find(u), fv = ds.find(v);
if (fu == fv) {
ans += (b[fu] ? w : 0);
b[fu] = 0;
} else {
ans += (b[fu] || b[fv] ? w : 0);
ds.Union(fu, fv);
b[fv] = (b[fv] + b[fu] == 2);
}
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; }
inline void pc(char x) {
*OS++ = x;
if (OS == OT) flush();
}
template <class I>
inline void rd(I &x) {
for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++);
c < '0' || c > '9';
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
if (c == '-') f = -1;
for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15),
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
x *= f;
}
inline void rds(char *s, int &x) {
for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++);
c < 33 || c > 126;
c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
for (x = 0; c >= 33 && c <= 126;
s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin),
IS == IT ? EOF : *IS++)
: *IS++))
;
s[x + 1] = '\0';
}
template <class I>
inline void print(I x, char k = '\n') {
if (!x) pc('0');
if (x < 0) pc('-'), x = -x;
while (x) ch[++t] = x % 10 + '0', x /= 10;
while (t) pc(ch[t--]);
pc(k);
}
inline void prints(string s) {
int x = s.length();
while (t < x) pc(s[t++]);
pc('\n'), t = 0;
}
struct Flush {
~Flush() { flush(); }
} flusher;
} // namespace io
using io::print;
using io::prints;
using io::rd;
using io::rds;
int P;
struct modint {
int x;
inline modint(int x = 0) : x(x) {}
inline modint &operator=(int o) { return x = o, *this; }
inline modint &operator+=(modint o) {
return x = x + o.x >= P ? x + o.x - P : x + o.x, *this;
}
inline modint &operator-=(modint o) {
return x = x - o.x < 0 ? x - o.x + P : x - o.x, *this;
}
inline modint &operator*=(modint o) { return x = 1ll * x * o.x % P, *this; }
inline modint &operator^=(long long b) {
modint a = *this, c;
if (!~b) b = P - 2;
c.x = 1 % P;
while (b) {
if (b & 1) c *= a;
a *= a, b >>= 1;
}
return x = c.x, *this;
}
inline modint &operator/=(modint o) { return *this *= o ^= -1; }
inline modint &operator+=(int o) {
return x = x + o >= P ? x + o - P : x + o, *this;
}
inline modint &operator-=(int o) {
return x = x - o < 0 ? x - o + P : x - o, *this;
}
inline modint &operator*=(int o) { return x = 1ll * x * o % P, *this; }
inline modint &operator/=(int o) {
modint y = modint(o);
return *this *= y ^= -1;
}
template <class I>
inline friend modint operator+(modint a, I b) {
return a += b;
}
template <class I>
inline friend modint operator-(modint a, I b) {
return a -= b;
}
template <class I>
inline friend modint operator*(modint a, I b) {
return a *= b;
}
inline friend modint operator^(modint a, long long b) { return a ^= b; }
template <class I>
inline friend modint operator/(modint a, I b) {
return a /= b;
}
inline friend bool operator==(modint a, int b) { return a.x == b; }
inline friend bool operator!=(modint a, int b) { return a.x != b; }
inline bool operator!() { return !x; }
inline modint operator-() { return x ? P - x : 0; }
};
inline void rd(modint &x) { rd(x.x); }
inline void print(modint x, char k = '\n') { print(x.x, k); }
const int N = 107, K = 207;
int n, m, f[N * N], fa[N * N], p[N * N], q[N * N];
char s[N][N];
bool ok[2] = {1, 1};
modint a[K][K];
inline int g(int x, int y) { return x * (m + 1) + y; }
inline void G(int &x, int &y, int z) { x = z / (m + 1), y = z % (m + 1); }
int get(int x) { return x == f[x] ? x : (f[x] = get(f[x])); }
inline void add(int x, int y) {
a[x][x] += 1, a[y][y] += 1, a[x][y] -= 1, a[y][x] -= 1;
}
inline modint calc(int o) {
if (!ok[o]) return 0;
for (int i = 0; i < (n + 1) * (m + 1); i++) fa[i] = f[i];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] == '*') {
int a = i, b = j, c = i - 1, d = j - 1;
if (((i ^ j) & 1) != o) swap(a, c);
int x = get(g(a, b)), y = get(g(c, d));
if (x != y) f[x] = y;
}
int rt = get(o);
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
if (((i ^ j) & 1) == o && get(g(i, j)) != rt) return 0;
for (int i = 0; i < (n + 1) * (m + 1); i++) f[i] = fa[i];
int t = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
if (((i ^ j) & 1) == o) {
int x = g(i, j);
if (x == get(x)) p[++t] = x, q[x] = t;
}
for (int i = 1; i <= t; i++)
for (int j = 1; j <= t; j++) a[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] == '*') {
int a = i, b = j, c = i - 1, d = j - 1;
if (((i ^ j) & 1) != o) swap(a, c);
int x = get(g(a, b)), y = get(g(c, d));
if (x != y) add(q[x], q[y]);
}
modint ans = 1;
for (int i = 1; i < t; i++) {
for (int j = i + 1; j < t; j++)
while (a[j][i] != 0) {
int d = a[i][i].x / a[j][i].x;
for (int k = i; k < t; k++) a[i][k] -= d * a[j][k];
swap(a[i], a[j]), ans = -ans;
}
ans *= a[i][i];
}
return ans;
}
int main() {
rd(n), rd(m), rd(P);
for (int i = 0; i < (n + 1) * (m + 1); i++) f[i] = i;
for (int i = 1; i <= n; i++) {
rds(s[i], m);
for (int j = 1; j <= m; j++)
if (s[i][j] != '*') {
int a = i, b = j, c = i - 1, d = j - 1;
if (s[i][j] == '/') swap(a, c);
int x = get(g(a, b)), y = get(g(c, d));
if (x == y)
ok[(a ^ b) & 1] = 0;
else
f[x] = y;
}
}
modint ans = calc(0);
print(ans + calc(1));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
s = "00" + s;
int l = s.length();
int count = 0;
for (int i = l - 1; i >= 0; i--) {
if (s[i] == '1') {
if (s[i - 1] == '1') {
while (s[i] == '1') {
s[i] = '0';
i--;
}
s[i] = '1';
i++;
count++;
} else
count++;
}
}
cout << count << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long d, k, a, b, t;
cin >> d >> k >> a >> b >> t;
if (k >= d) {
cout << a * d;
return 0;
}
long long ans = 0LL;
if (a * k + t > b * k) {
ans += a * k;
d -= k;
ans += d * b;
} else {
ans += a * k * (d / k) + (d / k - 1LL) * t;
d -= k * (d / k);
if (b * d < t + a * d)
ans += b * d;
else
ans += t + a * d;
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int hh, mm, h, d, c, n;
cin >> hh >> mm;
cin >> h >> d >> c >> n;
float a;
if (h % n == 0)
a = (h / n) * c;
else
a = (h / n + 1) * c;
float b;
int tim = hh * 60 + mm;
if (tim <= 1200) {
h += (1200 - tim) * d;
float rate = (float)c * 4 / 5;
if (h % n == 0) {
b = (h / n) * rate;
} else
b = (h / n + 1) * rate;
} else {
float rate = (float)c * 4 / 5;
if (h % n == 0)
b = (h / n) * rate;
else
b = (h / n + 1) * rate;
}
cout << fixed << setprecision(4) << min(a, b);
return 0;
}
| 1 |
#include<cstdio>
int main() {
int A,B,C;
scanf("%d%d%d", &A,&B,&C);
int Ans = 0;
while(Ans < 99) {
if(A&1 || B&1 || C&1) break;
int D=B+C, E=A+C, F=A+B;
A=D/2, B=E/2, C=F/2;
++Ans;
}
printf("%d", Ans<99?Ans:-1);
return 0;
}
// | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("03")
using namespace std;
const int maxn = (int)1e5 + 13;
const long long INFL = 9223372036854775807;
const int INF = 2147483646;
const long long MOD = 1e9 + 7;
const long long base = 31;
long long sum(long long a, long long b) { return (a + b) % MOD; }
long long dp[maxn][2];
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n, k, d;
cin >> n >> k >> d;
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= d - 1; ++j) {
if (i >= j) dp[i][0] = sum(dp[i - j][0], dp[i][0]);
}
for (int j = d; j <= k; ++j) {
if (i >= j) dp[i][1] = sum(dp[i - j][0], dp[i][1]);
}
for (int j = 1; j <= k; ++j) {
if (i >= j) dp[i][1] = sum(dp[i - j][1], dp[i][1]);
}
}
cout << dp[n][1] << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long b, w;
cin >> b >> w;
long long x = min(b, w);
long long y = max(b, w);
if (y > (3 * x + 1)) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
long long add = 0;
if (y == b) add = 1;
for (long long i = 1; i <= (2 * x); i++) {
cout << 3 << " " << (i + add) << endl;
}
y -= x;
if (y <= 0) return;
cout << 3 << " " << ((2 * x) + 1 + add) << endl;
y--;
if (y <= 0) return;
for (long long i = 1; i <= min(y, x); i++) {
cout << 2 << " " << ((2 * i) + add) << endl;
}
y -= x;
if (y <= 0) return;
for (long long i = 1; i <= min(y, x); i++) {
cout << 4 << " " << ((2 * i) + add) << endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, flag = 0;
cin >> n;
int a[n];
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 (i != j && (abs(a[i] - a[j]) % 2 != 0)) {
cout << "NO" << endl;
flag = 1;
break;
}
}
if (flag == 1) break;
}
if (flag == 0) cout << "YES" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[101];
int main() {
long int i, n, c;
while (cin >> n) {
c = 0;
for (i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 1) c++;
}
for (i = 0; i < n; i++) {
if (a[i] == 1 && a[i + 1] == 0 && a[i + 2] == 1) c++;
}
cout << c << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
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 * (b / gcd(a, b));
}
using namespace std;
vector<long long> L;
map<long long, bool> mapa;
void go(long long now) {
if (now > 1000000000LL) return;
L.push_back(now);
go(now * 10LL + 4LL);
go(now * 10LL + 7LL);
}
int main() {
go(0);
L.push_back(1000000000LL);
sort(L.begin(), L.end());
int len = L.size();
long long p1, pr, v1, vr, k;
while (cin >> p1 >> pr >> v1 >> vr >> k) {
long long tot = 0;
for (int i = 1; i < len - 1; i++) {
long long temp = 0;
if (p1 <= L[i] && pr >= L[i - 1] + 1 && v1 <= L[i + k] - 1 &&
L[i + k - 1] <= vr) {
temp = min(L[i], pr) - max(p1, L[i - 1] + 1LL) + 1LL;
temp *= (min(vr, L[i + k] - 1LL) - max(v1, L[i + k - 1]) + 1LL);
}
tot += temp;
temp = 0;
swap(v1, p1);
swap(vr, pr);
if (p1 <= L[i] && pr >= L[i - 1] + 1 && v1 <= L[i + k] - 1 &&
L[i + k - 1] <= vr) {
temp = min(L[i], pr) - max(p1, L[i - 1] + 1LL) + 1LL;
temp *= (min(vr, L[i + k] - 1LL) - max(v1, L[i + k - 1]) + 1LL);
if (temp > 0 && k == 1) temp--;
}
tot += temp;
swap(v1, p1);
swap(vr, pr);
}
double ans =
(double)tot / (double)(vr - v1 + 1LL) / (double)(pr - p1 + 1LL);
printf("%.9lf\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k > ((n - 1) / 2)) {
cout << "-1";
return 0;
}
cout << (n * k) << "\n";
for (int j = 1; j <= n; j++) {
for (int i = j; i <= j + k - 1; i++) {
cout << j << " " << (i) % n + 1 << "\n";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct Floor {
int X, Y;
} Edge[15];
struct Floor2 {
int X, Y, Z;
} A[105];
int N, M, Q;
long long DP[15][1000005];
bool Map[15][15];
int QP(int B, int K, int Mod) {
int Ans = 1;
for (; K; K >>= 1, B = B * B % Mod)
if (K & 1) Ans = Ans * B % Mod;
return Ans % Mod;
}
template <class T>
void Read(T &X) {
X = 0;
int F = 0;
char Ch = getchar();
while (Ch < '0' || Ch > '9') {
F |= (Ch == '-');
Ch = getchar();
}
while (Ch >= '0' && Ch <= '9') {
X = X * 10 + (Ch ^ 48);
Ch = getchar();
}
X = F ? -X : X;
}
inline void Write(int X) {
if (X < 0) {
putchar('-');
X = -X;
}
if (X > 9) Write(X / 10);
putchar((X % 10) ^ 48);
}
int GCD(int X, int Y) { return Y == 0 ? X : GCD(Y, X % Y); }
bool In(int X, int Mask) { return Mask & (1 << X); }
long long DFS(int U, int Mask) {
long long &Res = DP[U][Mask];
if (~Res) return Res;
Res = 0;
Mask -= 1 << U;
int Pos;
for (Pos = 0; Pos < N; Pos++)
if (In(Pos, Mask)) break;
for (register int SubMask = Mask; SubMask; SubMask = (SubMask - 1) & Mask)
if (In(Pos, SubMask)) {
bool Flag = 1;
int Cnt = 0, V;
for (register int i = 1; i <= Q; i++)
if (A[i].Z == U && In(A[i].X, SubMask) && In(A[i].Y, SubMask)) {
Flag = 0;
break;
}
if (!Flag) continue;
for (register int i = 1; i <= Q; i++)
if (In(A[i].Z, SubMask) &&
(!In(A[i].X, SubMask) || !In(A[i].Y, SubMask))) {
Flag = 0;
break;
}
if (!Flag) continue;
for (register int i = 1; i <= M; i++)
if (Edge[i].X != U && Edge[i].Y != U &&
(In(Edge[i].X, SubMask) ^ In(Edge[i].Y, SubMask))) {
Flag = 0;
break;
}
if (!Flag) continue;
for (register int i = 0; i < N; i++)
if (Map[U][i] && In(i, SubMask)) {
Cnt++;
V = i;
}
if (Cnt > 1) continue;
if (Cnt == 1) {
Res += DFS(V, SubMask) * DFS(U, Mask ^ SubMask ^ (1 << U));
} else {
for (V = 0; V < N; V++)
if (In(V, SubMask)) {
Res += DFS(V, SubMask) * DFS(U, Mask ^ SubMask ^ (1 << U));
}
}
}
DP[U][Mask] = Res;
return DP[U][Mask];
}
int main() {
Read(N);
Read(M);
Read(Q);
for (int i = 1; i <= M; i++) {
Read(Edge[i].X);
Read(Edge[i].Y);
Edge[i].X--;
Edge[i].Y--;
Map[Edge[i].X][Edge[i].Y] = 1;
Map[Edge[i].Y][Edge[i].X] = 1;
}
for (int i = 1; i <= Q; i++) {
Read(A[i].X);
Read(A[i].Y);
Read(A[i].Z);
A[i].X--;
A[i].Y--;
A[i].Z--;
}
memset(DP, -1, sizeof(DP));
for (int i = 0; i <= N; i++) DP[i][1 << i] = 1;
printf("%lld\n", DFS(0, (1 << N) - 1));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void test_case() {
int n;
cin >> n;
vector<pair<int, int> > vote(n + 1);
priority_queue<int, vector<int>, greater<int> > q;
for (int i = int(1); i < int(n + 1); i++) {
int m, p;
cin >> m >> p;
vote[i] = {m, p};
}
sort(vote.begin(), vote.end());
int curr = 0;
long long res = 0;
for (int i = n; i >= 0; i--) {
q.push(vote[i].second);
if (i - 1 + curr < vote[i].first) {
res += (long long)q.top();
q.pop();
curr++;
}
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
test_case();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long m = n;
int cnt = 0;
while (m) {
cnt++;
m /= 10;
}
int ans = 0;
long long a = 0;
for (int i = 1; i < cnt; i++) {
a *= 10;
a += 9;
}
ans += max(0, (cnt - 1) * 9);
long long b = n - a;
while (b) {
ans += b % 10;
b /= 10;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, arr[1000];
cin >> n;
int cnt = 0;
for (i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0) cnt++;
}
int loc = 0;
if (cnt == n - 1) {
for (i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
loc = i + 1;
break;
}
}
} else {
for (i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
loc = i + 1;
break;
}
}
}
cout << loc << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int INF = 1000 * 1000 * 1000 + 7;
const long long LINF = INF * (long long)INF;
struct point {
int x, y;
point() {}
point(int x1, int y1) {
x = x1;
y = y1;
}
double getAng() const {
double a = atan2(y, x);
if (a < 0) a += 2 * PI;
return a;
}
bool operator<(const point& a) const { return getAng() < a.getAng(); }
point operator-(const point& a) const { return point(x - a.x, y - a.y); }
int operator*(const point& a) const { return x * a.y - a.x * y; }
};
const int MAX = 1000 * 100 + 47;
point A[MAX];
int n;
bool isOnOneLine() {
point orign(0, 0);
for (int i = (1); i < (n); i++) {
if ((A[i] - A[i - 1]) * (A[i - 1] - orign) != 0) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = (0); i < (n); i++) cin >> A[i].x >> A[i].y;
if (isOnOneLine()) {
cout << 0 << endl;
return 0;
}
sort(A, A + n);
double ans = 2 * PI - (A[0].getAng() - A[n - 1].getAng() + 2 * PI);
for (int i = (1); i < (n); i++) {
double a1 = A[i - 1].getAng();
double a2 = A[i].getAng();
ans = min(ans, 2 * PI - (a2 - a1));
}
ans = 180 * ans / PI;
cout << fixed << setprecision(12) << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string s1, s2, t1, t2;
int n;
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> s1 >> s2;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << s1 << " " << s2 << "\n";
cin >> t1 >> t2;
if (s1 == t1)
s1 = t2;
else
s2 = t2;
}
cout << s1 << " " << s2;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }
V<string> split(const string& s, char c) {
V<string> res;
stringstream ss(s);
string t;
while (getline(ss, t, c)) {
res.push_back(t);
}
return res;
}
VV<> convert(const string& s) {
VV<> res;
for (const auto& e : split(s, '/')) {
V<> curr;
for (char c : e) {
if (c == 'b') {
curr.push_back(1);
} else {
while (c-- > '0') curr.push_back(0);
}
}
res.push_back(curr);
}
return res;
}
string invert(const VV<>& a) {
int h = a.size();
string res;
for (int i = 0; i < h; ++i) {
if (i) res += '/';
int w = a[i].size();
int t = 0;
for (int j = 0; j < w; ++j) {
if (a[i][j]) {
res += 'b';
} else {
++t;
if (j == w - 1 or a[i][j + 1]) {
res += '0' + t;
t = 0;
}
}
}
}
return res;
}
int main() {
cin.tie(nullptr); ios_base::sync_with_stdio(false);
while (true) {
string s; cin >> s;
if (s == "#") break;
auto a = convert(s);
for (int _ = 0; _ < 2; ++_) {
int i, j; cin >> i >> j, --i, --j;
a[i][j] ^= 1;
}
s = invert(a);
cout << s << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
inline int in();
inline void wr(int);
const int N = (int)1e2 + 5;
int a[N][N];
std::vector<std::pair<int, int> > v[N * 10];
int main(int argc, char** argv) {
register int T = in();
while (T--) {
register int n = in(), m = in();
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= m; ++j) scanf("%1d", &a[i][j]);
for (register int i = 1; i <= n - 2; ++i)
for (register int j = 1; j <= m; ++j)
if (a[i][j])
if (j != m) {
a[i][j] ^= 1, a[i + 1][j] ^= 1, a[i + 1][j + 1] ^= 1;
v[T].push_back(std::make_pair(i, j)),
v[T].push_back(std::make_pair(i + 1, j)),
v[T].push_back(std::make_pair(i + 1, j + 1));
} else {
a[i][j] ^= 1, a[i + 1][j] ^= 1, a[i + 1][j - 1] ^= 1;
v[T].push_back(std::make_pair(i, j)),
v[T].push_back(std::make_pair(i + 1, j)),
v[T].push_back(std::make_pair(i + 1, j - 1));
}
for (register int j = 1; j <= m - 2; ++j) {
if (a[n - 1][j]) {
a[n - 1][j] ^= 1, a[n - 1][j + 1] ^= 1, a[n][j + 1] ^= 1;
v[T].push_back(std::make_pair(n - 1, j)),
v[T].push_back(std::make_pair(n - 1, j + 1)),
v[T].push_back(std::make_pair(n, j + 1));
}
if (a[n][j]) {
a[n][j] ^= 1, a[n - 1][j + 1] ^= 1, a[n][j + 1] ^= 1;
v[T].push_back(std::make_pair(n, j)),
v[T].push_back(std::make_pair(n - 1, j + 1)),
v[T].push_back(std::make_pair(n, j + 1));
}
}
for (register int k = 0; k < (1 << 4); ++k) {
if (k & 1) a[n - 1][m - 1] ^= 1, a[n][m - 1] ^= 1, a[n - 1][m] ^= 1;
if (k & 2) a[n - 1][m] ^= 1, a[n][m - 1] ^= 1, a[n][m] ^= 1;
if (k & 4) a[n - 1][m - 1] ^= 1, a[n][m] ^= 1, a[n - 1][m] ^= 1;
if (k & 8) a[n - 1][m - 1] ^= 1, a[n][m - 1] ^= 1, a[n][m] ^= 1;
if ((!a[n - 1][m - 1]) && (!a[n][m]) && (!a[n - 1][m]) &&
(!a[n][m - 1])) {
if (k & 1)
v[T].push_back(std::make_pair(n - 1, m - 1)),
v[T].push_back(std::make_pair(n, m - 1)),
v[T].push_back(std::make_pair(n - 1, m));
if (k & 2)
v[T].push_back(std::make_pair(n - 1, m)),
v[T].push_back(std::make_pair(n, m - 1)),
v[T].push_back(std::make_pair(n, m));
if (k & 4)
v[T].push_back(std::make_pair(n - 1, m - 1)),
v[T].push_back(std::make_pair(n, m)),
v[T].push_back(std::make_pair(n - 1, m));
if (k & 8)
v[T].push_back(std::make_pair(n - 1, m - 1)),
v[T].push_back(std::make_pair(n, m - 1)),
v[T].push_back(std::make_pair(n, m));
break;
}
if (k & 1) a[n - 1][m - 1] ^= 1, a[n][m - 1] ^= 1, a[n - 1][m] ^= 1;
if (k & 2) a[n - 1][m] ^= 1, a[n][m - 1] ^= 1, a[n][m] ^= 1;
if (k & 4) a[n - 1][m - 1] ^= 1, a[n][m] ^= 1, a[n - 1][m] ^= 1;
if (k & 8) a[n - 1][m - 1] ^= 1, a[n][m - 1] ^= 1, a[n][m] ^= 1;
}
wr(v[T].size() / 3), putchar('\n');
for (register std::vector<std::pair<int, int> >::iterator it = v[T].begin();
it != v[T].end(); ++it) {
wr((*it).first), putchar(' '), wr((*it).second), putchar(' ');
++it;
wr((*it).first), putchar(' '), wr((*it).second), putchar(' ');
++it;
wr((*it).first), putchar(' '), wr((*it).second), putchar('\n');
}
}
}
inline int in() {
register char c = getchar();
register int x = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar())
x = (x << 1) + (x << 3) + (c & 15);
return x * f;
}
inline void wr(int x) {
if (x < 0) putchar('-'), x = -x;
if (x / 10) wr(x / 10);
putchar(x % 10 + '0');
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long dp[(1 << 18) + 3];
int ans[18];
int flag[18];
int pre[20];
int main() {
int n, m;
long long y;
scanf("%d%lld%d", &n, &y, &m);
int a, b;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
pre[b] |= 1 << (a - 1);
}
y -= 2001;
for (int i = 1; i <= n; i++) {
for (ans[i] = 1;; ans[i]++) {
if (!flag[ans[i]]) {
if (ans[i] > n) {
printf("The times have changed");
return 0;
}
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int s = 0; s < (1 << n); s++) {
if (dp[s]) {
int cnt = __builtin_popcount(s);
for (int k = 1; k <= n; k++) {
if (!(s & (1 << (k - 1)))) {
if ((ans[k] == 0 || ans[k] == cnt + 1) &&
((s & pre[k]) == pre[k])) {
dp[s | (1 << (k - 1))] += dp[s];
}
}
}
}
}
if (y >= dp[(1 << n) - 1]) {
y -= dp[(1 << n) - 1];
} else {
flag[ans[i]] = 1;
break;
}
}
}
}
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void add(int &x, int y) { x = x + y < 1000000007 ? x + y : x + y - 1000000007; }
int dp[2][405][405];
int main() {
int n, m, x;
scanf("%d%d%d", &n, &m, &x);
if (n > m) return puts("0"), 0;
dp[0][0][0] = 1;
for (int i = 1; i <= m; i++)
for (int l = 0; l <= n; l++)
for (int r = 0; r <= l; r++) {
int &y = dp[i & 1][l][r];
y = 0;
if (i != x) add(y, dp[(i - 1) & 1][l][r]);
if (i != x && r) add(y, dp[(i - 1) & 1][l][r - 1]);
if (l && r) add(y, dp[(i - 1) & 1][l - 1][r - 1]);
if (l > r) add(y, dp[(i - 1) & 1][l - 1][r]);
}
int ans = dp[m & 1][n][n];
for (int i = 1; i <= n; i++) ans = 1ll * ans * i % 1000000007;
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
long long fac[MAXN], pwr[MAXN], pwm[MAXN];
long long pw(long long a, long long b) {
long long ret = 1;
while (b > 0) {
if (b & 1)
ret = (ret * a) % MOD, b--;
else
a = (a * a) % MOD, b /= 2;
}
return ret;
}
long long inv(long long a) { return pw(a, MOD - 2); }
long long ncr(long long n, long long k) {
if (k > n) return 0;
return (fac[n] * inv((fac[k] * fac[n - k]) % MOD)) % MOD;
}
int main() {
int n, m, a, b;
scanf("%d %d %d %d", &n, &m, &a, &b);
fac[0] = 1;
pwr[0] = 1;
pwm[0] = 1;
for (long long i = 1; i <= max(n, m); i++) {
fac[i] = (fac[i - 1] * i) % MOD;
pwr[i] = (1ll * pwr[i - 1] * (long long)n) % MOD;
pwm[i] = (1ll * pwm[i - 1] * (long long)m) % MOD;
}
long long ans = 0;
for (long long k = 1; k < min(m + 1, n); k++) {
long long val = pwm[n - 1 - k];
val = (val * fac[n - 2]) % MOD;
val = (val * inv(fac[n - k - 1])) % MOD;
if (k != n - 1) {
val = (val * (1ll + k)) % MOD;
val = (val * pwr[n - k - 2]) % MOD;
}
val = (val * ncr(m - 1, k - 1)) % MOD;
ans = (ans + val) % MOD;
}
printf("%I64d\n", (ans));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long bin_pow(long long a, long long b) {
if (b == 0) return 1;
if (b % 2 == 0) {
long long t = bin_pow(a, b / 2);
return t * t % 1000000007;
} else
return a * bin_pow(a, b - 1) % 1000000007;
}
long long dp[101][101], dp1[101][101];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long t, n, m, k, x = 0, y = 0, ans = 0, z = 0, sum = 0, l = 0, r = 0;
cin >> n;
vector<pair<long long, long long> > a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second, sum += a[i].first;
if (a[i].first == 2) x++;
}
sort(a.begin(), a.end());
for (long long i = 0; i <= n; i++) {
for (int j = 0; j <= min(i, x); j++) {
if (i - j > n - x) continue;
dp[i][j] = 2 * j + i - j;
y = z = 0;
for (int f = 0; f < n; f++) {
if (z == j) break;
if (a[f].first == 2) {
z++;
y += a[f].second;
}
}
for (int f = 0; f < n; f++) {
if (z == i) break;
if (a[f].first == 1) {
z++;
y += a[f].second;
}
}
dp[i][j] += y;
if (dp[i][j] <= sum) dp1[i][j] = 1;
}
}
for (long long i = 0; i <= n; i++) {
for (long long j = 0; j <= min(i, x); j++) {
if (dp1[i][j]) ans = max(ans, i + j);
}
}
cout << sum - ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s, s1 = "AEIOUYaeiouy";
int main() {
getline(cin, s);
for (int j = s.size() - 1; j >= 0; --j) {
if (s[j] == '?' || s[j] == ' ')
s.pop_back();
else
break;
}
for (int j = 0; j < s1.size(); ++j)
if (s[s.size() - 1] == s1[j]) {
printf("YES");
return 0;
}
printf("NO");
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define what_is(x) cerr << #x << " is " << x << endl;
int main() {
ll n, k;
cin>>n>>k;
map<ll, ll> mp;
for(int i=0;i<n;i++) {
string s;
cin>>s;
mp[s[0]-'A'] ++;
}
map<ll, ll>::iterator it;
vector<ll> v;
for(it = mp.begin(); it != mp.end(); it ++) {
v.push_back(it->second);
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
ll ans = 0;
while(1) {
ll kk = k;
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for(int i=0;i<v.size();i++) {
if(v[i] != 0) {
v[i] --;
kk --;
}
if(kk == 0) {
// cout<<i<<" "<<v[i]<<" here\n";
break;
}
}
if(kk != 0) break;
ans ++;
}
cout<<ans<<"\n";
return 0;
} | 0 |
#include <iostream>
#include <algorithm>
using namespace std;
#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)
int n,m;
int t[102],s[102];
int sumT,sumS;
void solve(){
int ti=0,si=0;
while(ti < n && si < m){
int swapT = sumT - t[ti] + s[si];
int swapS = sumS - s[si] + t[ti];
if(swapT == swapS){
cout<<t[ti]<<" "<<s[si]<<endl;
return;
}
if(si == m-1 || swapS < swapT && ti != n-1){
ti++;
}
else{
si++;
}
}
cout<<-1<<endl;
}
int main(){
while(cin>>n>>m,n||m){
sumT = sumS = 0;
rep(i,n){
cin>>t[i];
sumT += t[i];
}
rep(i,m){
cin>>s[i];
sumS += s[i];
}
sort(t,t+n);
sort(s,s+m);
solve();
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 5;
vector<int> G[maxn];
int visited[maxn];
int n;
int ans;
void dfs(int u, int deep) {
visited[u] = 1;
if (deep == 2) {
ans++;
return;
}
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!visited[v]) {
dfs(v, deep + 1);
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
ans = 0;
for (int i = 1; i <= n; i++) {
memset(visited, 0, sizeof(visited));
dfs(i, 0);
}
printf("%d", ans / 2);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline void swap(int &x, int &y) {
int tmp = x;
x = y;
y = tmp;
}
int a[100001];
int main() {
int n, n1, n2;
cin >> n >> n1 >> n2;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
double sum1 = 0;
double sum2 = 0;
if (n1 > n2) swap(n1, n2);
for (int i = n - 1; i >= n - n1; i--) {
sum1 += a[i];
}
for (int i = n - 1 - n1; i >= n - n1 - n2; i--) {
sum2 += a[i];
}
double ans = sum1 / n1 + sum2 / n2;
printf("%.6f\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------
int T,N;
int L[202020];
int R[202020];
int LS[404040],RS[404040];
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>T;
while(T--) {
cin>>N;
vector<int> V;
V.push_back(-1);
FOR(i,N) {
cin>>L[i]>>R[i];
V.push_back(L[i]);
V.push_back(R[i]);
}
sort(ALL(V));
V.erase(unique(ALL(V)),V.end());
FOR(i,2*N+5) LS[i]=RS[i]=0;
FOR(i,N) {
L[i]=lower_bound(ALL(V),L[i])-V.begin();
R[i]=lower_bound(ALL(V),R[i])-V.begin();
LS[R[i]]++;
RS[L[i]]++;
}
FOR(i,2*N+4) LS[i+1]+=LS[i];
for(i=2*N+3;i>=0;i--) RS[i]+=RS[i+1];
int mi=N-1;
FOR(i,N) {
mi=min(mi,LS[L[i]-1]+RS[R[i]+1]);
}
cout<<mi<<endl;
}
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
vector<long long> nums;
void gener(long long x) {
if (x > 90000000000) return;
long long x1 = x * 10 + 4;
if (x1 < 90000000000) {
nums.push_back(x1);
gener(x1);
}
x1 = x * 10 + 7;
if (x1 < 90000000000) {
nums.push_back(x1);
gener(x1);
}
}
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
for (long long i = 0; i < s.size() - 1; i++) {
if (k == 0) break;
if (i + 2 < s.size()) {
if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '7' && i % 2 == 0) {
if (k % 2) s[i + 1] = '7';
cout << s << endl;
return 0;
}
if (s[i] == '4' && s[i + 1] == '7' && s[i + 2] == '7' && i % 2 == 0) {
if (k % 2) s[i + 1] = '4';
cout << s << endl;
return 0;
}
if (s[i] == '4' && s[i + 1] == '7') {
if (i % 2)
s[i] = s[i + 1] = '7';
else
s[i] = s[i + 1] = '4';
--k;
}
}
if (s[i] == '4' && s[i + 1] == '7') {
if (i % 2)
s[i] = s[i + 1] = '7';
else
s[i] = s[i + 1] = '4';
--k;
}
}
cout << s << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
const ll INF = 1e18;
struct Node {
long long val;
int idx;
Node(long long one = 0, int id = -1) : val(one), idx(id) {}
Node lazylazyMerge(const Node &rhs) {
Node a = *this;
a.val = (a.val + rhs.val);
return a;
}
Node seglazyMerge(const Node &rhs, const int &l, const int &r) {
Node a = *this;
a.val += (rhs.val);
return a;
}
Node segSegMerge(const Node &rhs) {
if (idx == -1) return rhs;
if (rhs.idx == -1) return *this;
if (val > rhs.val) return *this;
if (val < rhs.val) return rhs;
if (idx < rhs.idx) return *this;
if (idx > rhs.idx) return rhs;
return *this;
}
};
template <typename segNode>
struct Segtree {
vector<segNode> Seg, Lazy;
vector<segNode> Base;
vector<bool> isLazy;
int n;
Segtree(int _n = 2e5) {
this->n = _n;
Seg.resize(4 * _n + 10);
Lazy.resize(4 * _n + 10);
isLazy.resize(4 * _n + 10);
}
void propagate(int node, int L, int R) {
if (isLazy[node]) {
isLazy[node] = false;
Seg[node] = Seg[node].seglazyMerge(Lazy[node], L, R);
if (L != R) {
Lazy[2 * node] = Lazy[2 * node].lazylazyMerge(Lazy[node]);
Lazy[2 * node + 1] = Lazy[2 * node + 1].lazylazyMerge(Lazy[node]);
isLazy[2 * node] = true;
isLazy[2 * node + 1] = true;
}
Lazy[node] = segNode();
}
}
void build(int node, int start, int end) {
if (start == end) {
Seg[node] = Base[start];
return;
}
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
Seg[node] = Seg[2 * node].segSegMerge(Seg[2 * node + 1]);
}
void build(vector<segNode> &Arr) {
Base = Arr;
n = Arr.size();
Seg.resize(4 * n + 10);
Lazy.resize(4 * n + 10);
isLazy.resize(4 * n + 10);
build(1, 0, n - 1);
}
segNode Query(int node, int start, int end, int qstart, int qend) {
propagate(node, start, end);
if (qend < start || qstart > end || start > end) return segNode(-INF, -1);
if (qstart <= start && end <= qend) return Seg[node];
int mid = (start + end) / 2;
segNode l = Query(2 * node, start, mid, qstart, qend);
segNode r = Query(2 * node + 1, mid + 1, end, qstart, qend);
return l.segSegMerge(r);
}
segNode qQuery(int node, int start, int end, int pos) {
propagate(node, start, end);
if (start == end) return Seg[node];
int mid = (start + end) / 2;
if (pos <= mid) return qQuery(2 * node, start, mid, pos);
return qQuery(2 * node + 1, mid + 1, end, pos);
}
void Update(int node, int start, int end, int qstart, int qend, segNode val) {
propagate(node, start, end);
if (qend < start || qstart > end || start > end) return;
if (qstart <= start && end <= qend) {
isLazy[node] = true;
Lazy[node] = val;
propagate(node, start, end);
return;
}
int mid = (start + end) / 2;
Update(2 * node, start, mid, qstart, qend, val);
Update(2 * node + 1, mid + 1, end, qstart, qend, val);
Seg[node] = Seg[2 * node].segSegMerge(Seg[2 * node + 1]);
}
void pUpdate(int node, int start, int end, int pos, segNode val) {
propagate(node, start, end);
if (start == end) {
Seg[node] = val;
return;
}
int mid = (start + end) / 2;
if (pos <= mid)
pUpdate(2 * node, start, mid, pos, val);
else
pUpdate(2 * node + 1, mid + 1, end, pos, val);
Seg[node] = Seg[2 * node].segSegMerge(Seg[2 * node + 1]);
}
segNode query(int pos) { return qQuery(1, 0, n - 1, pos); }
segNode query(int left, int right) { return Query(1, 0, n - 1, left, right); }
void update(int pos, segNode val) { pUpdate(1, 0, n - 1, pos, val); }
void update(int start, int end, segNode val) {
Update(1, 0, n - 1, start, end, val);
}
};
void solveCase() {
int n;
cin >> n;
vector<vector<int>> a(n, vector<int>(3));
vector<int> vals;
for (auto &x : a) {
for (auto &y : x) cin >> y;
vals.push_back(x[0]);
vals.push_back(x[1]);
}
vals.push_back(0);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
vals.push_back(vals.back() + 1);
vector<pair<int, int>> ev[vals.size()];
for (auto &x : a) {
int one = lower_bound(vals.begin(), vals.end(), x[0]) - vals.begin();
int two = lower_bound(vals.begin(), vals.end(), x[1]) - vals.begin();
ev[min(one, two)].emplace_back(max(one, two), x[2]);
}
Segtree<Node> stree(vals.size());
;
for (size_t i = 0; i < vals.size(); i++) {
stree.update(i, Node(-vals[i], i));
auto q = stree.query(i);
;
}
ll ans = -INF;
int l, r;
l = r = -1;
for (int i = vals.size() - 1; i >= 0; i--) {
for (auto &it : ev[i]) stree.update(it.first, vals.size() - 1, it.second);
auto cur = stree.query(i, vals.size() - 1);
;
if (cur.val + vals[i] > ans) {
ans = cur.val + vals[i];
l = vals[i];
r = vals[cur.idx];
}
}
cout << ans << '\n';
cout << l << ' ';
cout << l << ' ';
cout << r << ' ';
cout << r << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
for (int i = 1; i <= t; ++i) {
solveCase();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int arr[100];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == i) continue;
for (int k = 0; k < n; k++) {
if (k == j || k == i) continue;
if (arr[i] + arr[j] == arr[k])
return cout << k + 1 << " " << j + 1 << " " << i + 1, 0;
}
}
}
cout << -1, 0;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
map<char, char> matching{
{'(', ')'},
{'[', ']'},
};
void Solve(string &str) {
int n = str.length();
vector<int> valid(n, false);
stack<pair<char, int>> stk;
for (int i = 0; i < n; i++) {
if (str[i] == '(' || str[i] == '[') {
stk.push({str[i], i});
} else {
if (!stk.empty() && str[i] == matching[stk.top().first]) {
valid[i] = true;
valid[stk.top().second] = true;
stk.pop();
} else {
while (!stk.empty()) stk.pop();
}
}
}
int pos = 0;
int maxbracket = 0;
string cand;
while (pos < n) {
if (valid[pos]) {
int npos = pos;
int bracket = 0;
while (npos < n && valid[npos]) {
if (str[npos] == '[') bracket++;
npos++;
}
if (bracket > maxbracket) {
cand = str.substr(pos, npos - pos);
maxbracket = bracket;
}
pos = npos;
} else {
pos++;
}
}
cout << maxbracket << '\n' << cand << '\n';
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
string str;
cin >> str;
Solve(str);
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,sum=0,sqsum=0;
cin >> n;
int a[n];
for(int i=0;i<n;i++){
cin >> a[i];
sum+=a[i];
sqsum+=a[i]*a[i];
}
cout << (sum*sum-sqsum)/2 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct _IO {
_IO() {
ios::sync_with_stdio(0);
cin.tie(0);
}
} _io;
using ll = long long;
using db = long double;
using pi = pair<int, int>;
using mi = map<int, int>;
const int N = 3e5 + 5, M = 998244353;
using vi = vector<int>;
ll ri() {
ll x;
cin >> x;
return x;
}
void prl() { cout << '\n'; }
template <class T, class... U>
void prl(T x, U... y) {
cout << x << ' ', prl(y...);
}
template <class T, class U>
ostream &operator<<(ostream &out, pair<T, U> p) {
out << "(" << p.first << ", " << p.second << ")";
return out;
}
template <class T>
ostream &operator<<(ostream &out, vector<T> &v) {
cout << "{";
for (int i = (0); i < (v.size()); ++i) {
if (i) cout << ", ";
cout << v[i];
}
cout << "}";
return out;
}
signed main() {
int tt = ri();
while (tt--) {
ll a = ri(), b = ri(), x = ri(), y = ri(), u = ri(), v = ri();
if (u > v) swap(u, v), swap(x, y);
ll res = 0;
for (int i = (0); i < (x + 1); ++i) {
if (i * u > a) break;
ll ax = i, ay = min(y, (a - ax * u) / v);
ll bx = min(x - ax, b / u), by = min(y - ay, (b - bx * u) / v);
res = max(res, ax + ay + bx + by);
}
prl(res);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265359;
const long long int Maxi = 1e18;
long long cr[100000];
vector<long long> v[10005];
long long b[100005];
long long a[100005] = {0};
long long k = 0;
void dfs(long long x) {
if (a[x] != b[x]) k++;
a[x] = b[x];
for (long long i = 0; i < v[x].size(); i++) {
a[v[x][i]] = b[x];
}
for (long long i = 0; i < v[x].size(); i++) {
dfs(v[x][i]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n, m, i, j, l, p = 0, s = 0, t = 1, x, y, z, r, c;
cin >> n;
for (i = 2; i <= n; i++) {
cin >> x;
v[x].push_back(i);
}
for (i = 1; i <= n; i++) cin >> b[i];
dfs(1);
for (i = 1; i <= n; i++) {
if (a[i] != b[i]) k++;
}
cout << k;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 10, mod = 1e9 + 7;
int fail[N], tl, sl, sz, ev[N];
long long dp[2][N], acc[N], ans;
char s[N], t[N];
void kmp() {
int i, j;
fail[0] = j = -1;
for (i = 1; i < tl; i++) {
while (~j && t[i] != t[j + 1]) j = fail[j];
if (t[i] == t[j + 1]) j++;
fail[i] = j;
}
j = -1;
for (i = 0; i < sl; i++) {
while (~j && s[i] != t[j + 1]) j = fail[j];
if (s[i] == t[j + 1]) j++;
if (j == tl - 1) {
j = fail[j];
ev[sz++] = i;
}
}
}
int main() {
scanf("%s%s", s, t);
tl = strlen(t);
sl = strlen(s);
kmp();
for (int i = 1, p = -1, j; i <= sl; i++) {
if (p + 1 < sz && ev[p + 1] <= i - 1) p++;
if (p == -1) continue;
j = ev[p] - tl + 1;
dp[0][i] = acc[j] + j + 1;
dp[1][i] = (dp[1][i - 1] + dp[0][i]);
if (dp[1][i] >= mod) dp[1][i] -= mod;
acc[i] = (acc[i - 1] + dp[1][i]) % mod;
if (acc[i] >= mod) acc[i] -= mod;
}
printf("%I64d\n", dp[1][sl]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 20004, MOD = 1000000007;
int f[N];
int power(int x, int y) {
int res = 1;
while (y > 0) {
if (y & 1) {
res = (res * 1ll * x) % MOD;
}
x = (x * 1ll * x) % MOD;
y /= 2;
}
return res;
}
int c(int n, int r) {
return f[n] * 1ll * power(f[n - r] * 1ll * f[r] % MOD, MOD - 2) % MOD;
}
void factorize(int x, map<int, int> &pf) {
for (int i = 2; i * i <= x; ++i) {
int cnt = 0;
while (x % i == 0) {
x /= i;
++cnt;
}
pf[i] += cnt;
}
++pf[x];
}
int main() {
int n;
scanf("%d", &n);
map<int, int> pf;
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
factorize(a, pf);
}
f[0] = 1;
for (int i = 1; i < N; ++i) {
f[i] = (f[i - 1] * 1ll * i) % MOD;
}
int res = 1;
for (auto k : pf) {
if (k.first > 1 && k.second) {
res = (res * 1ll * c(k.second + n - 1, n - 1)) % MOD;
}
}
printf("%d\n", res);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int y, b, r;
cin >> y >> b >> r;
int m = std::min({y, b, r});
if (m == r)
cout << r + (r - 1) + (r - 2);
else if (m == b)
cout << (b - 1) + b + (b + 1);
else {
if (y + 2 > r)
cout << r + (r - 1) + (r - 2);
else
cout << y + (y + 1) + (y + 2);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x, ans = 0;
cin >> n >> k;
int place[n];
for (int i = 0; i < n; i++) cin >> place[i];
while (place[0] != k) {
for (int i = 0; i < n - 1; i++)
if (place[i + 1] > place[i] && place[i] < k) place[i]++;
if (place[n - 1] < k) place[n - 1]++;
ans++;
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjList;
bool visited[100];
void dfs(int node) {
if (visited[node]) return;
visited[node] = true;
for (int i = 0; i < adjList[node].size(); i++) dfs(adjList[node][i]);
}
int main() {
int nodes, vertices;
cin >> nodes >> vertices;
adjList = vector<vector<int>>(nodes);
for (int i = 0; i < vertices; i++) {
int from, to;
cin >> from >> to;
adjList[from - 1].push_back(to - 1);
adjList[to - 1].push_back(from - 1);
}
if (vertices < nodes) {
cout << "NO";
return 0;
}
dfs(0);
bool found = false;
for (int i = 0; i < nodes; i++)
if (!visited[i]) found = true;
if (!found && nodes == vertices)
cout << "FHTAGN!";
else
cout << "NO";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("-O3")
using ld = long double;
const long long int mod = 1000000007;
const long long int inf = 1000000000000000000;
const long long int rk = 256;
const ld PI = 3.141592653589793;
ostream& operator<<(ostream& os, pair<long long int, long long int> const& x) {
os << x.first << "," << x.second;
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> const& x) {
os << "{ ";
for (auto& y : x) os << y << " ";
return os << "}";
}
template <class T>
ostream& operator<<(ostream& os, set<T> const& x) {
os << "{ ";
for (auto& y : x) os << y << " ";
return os << "}";
}
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os,
Container const& x) {
os << "{ ";
for (auto& y : x) os << y << " ";
return os << "}";
}
template <class X, class Y>
ostream& operator<<(ostream& os, pair<X, Y> const& p) {
return os << "[ " << p.first << ", " << p.second << "]";
}
const int tot = 1e6 + 5;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
vector<long long int> a(2 * tot, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
long long int q;
vector<long long int> pos(2 * tot, 0);
vector<long long int> ans(2 * tot, 0);
cin >> q;
for (int time = 1; time <= q; time++) {
long long int operation, p, x;
cin >> operation;
if (operation == 1) {
cin >> p >> x;
a[p] = x;
pos[p] = time;
} else {
cin >> ans[time];
}
}
for (int i = q; i >= 1; i--) {
ans[i] = max(ans[i], ans[i + 1]);
}
for (int i = 1; i <= n; i++) cout << max(a[i], ans[pos[i] + 1]) << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
int main() {
int n;
long long sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum += a[i] - 1;
if (sum % 2 == 1)
printf("1\n");
else
printf("2\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, j, k;
cin >> n;
long long a[n + 1];
map<long long, long long> mp1;
for (i = 1; i <= n; ++i) {
cin >> a[i];
mp1[a[i]]++;
}
map<long long, long long> mp;
long long ans = 0;
for (i = 1; i <= n; ++i) {
mp1[a[i]]--;
if (mp1[a[i]] == 0) mp1.erase(a[i]);
if (mp.find(a[i]) == mp.end()) {
ans += mp1.size();
}
mp[a[i]]++;
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct __s {
__s() {
if (1) {
ios_base::Init i;
cin.sync_with_stdio(0);
cin.tie(0);
}
}
~__s() {
if (!1)
fprintf(stderr, "Execution time: %.3lf s.\n",
(double)clock() / CLOCKS_PER_SEC);
long long n;
cin >> n;
}
} __S;
bool a[511111];
bool b[511111];
int main(void) {
long long n;
cin >> n;
for (long long i = 0; i < (long long)(n); i++) {
cin >> a[i];
}
vector<long long> v;
long long i = 0;
while (i < n) {
while (i + 1 < n && a[i] == a[i + 1]) {
v.push_back(i);
i++;
}
v.push_back(i);
i++;
while (i + 1 < n && a[i] != a[i + 1]) {
i++;
}
}
long long ans = 0;
for (long long i = 0; i < (long long)(v.size() - 1); i++) {
long long res = (v[i + 1] - v[i]) / 2;
ans = max(ans, res);
if (a[v[i]] == a[v[i + 1]]) {
for (long long j = v[i]; j <= v[i + 1]; j++) {
b[j] = a[v[i]];
}
} else {
for (long long j = v[i]; j <= v[i] + res; j++) {
b[j] = a[v[i]];
}
for (long long j = v[i] + res + 1; j <= v[i + 1]; j++) {
b[j] = a[v[i + 1]];
}
}
}
cout << ans << '\n';
for (long long i = 0; i < (long long)(n); i++) {
cout << b[i] << " ";
}
cout << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 10;
const long long M = 1e7 + 19;
long long Max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long Min(long long a, long long b) {
if (a > b) return b;
return a;
}
long long big_mul(long long a, long long b, long long m) {
long long x = 0, y = a % m;
while (b > 0) {
if (b & 1) x = (x + y) % m;
y = (2 * y) % m;
b >>= 1;
}
return x % m;
}
long long big_mod(long long a, long long b, long long m) {
long long x = 1, y = a % m;
while (b > 0) {
if (b & 1) x = (x * y) % m;
y = (y * y) % m;
b >>= 1;
}
return x % m;
}
long long Big_mod(long long a, long long b, long long m) {
long long x = 1, y = a % m;
while (b > 0) {
if (b & 1) x = big_mul(x, y, m);
y = big_mul(y, y, m);
b >>= 1;
}
return x % m;
}
long long mod_inverse(long long a, long long m) { return big_mod(a, m - 2, m); }
vector<pair<double, double> > save;
vector<pair<double, double> > tmp;
vector<bool> check(N);
int fnct(vector<pair<double, double> > &vec, int pos1, int pos2) {
int cnt = 2;
if (vec.size() <= 2) return vec.size();
double p1 = vec[pos1].first;
double q1 = vec[pos1].second;
double p2 = vec[pos2].first;
double q2 = vec[pos2].second;
double Y = q2 - q1;
double X = p2 - p1;
double slope;
if (X == 0)
slope = INT_MAX;
else
slope = (Y / X);
for (int i = 0; i < vec.size(); i++) {
if (i == pos1 || i == pos2) continue;
p1 = vec[pos1].first;
q1 = vec[pos1].second;
p2 = vec[i].first;
q2 = vec[i].second;
Y = q2 - q1;
X = p2 - p1;
double slope1;
if (X == 0)
slope1 = INT_MAX;
else
slope1 = (Y / X);
if (slope1 == slope) {
cnt++;
} else
check[i] = true;
}
return cnt;
}
bool res(int pos1, int pos2, int n) {
for (int i = 0; i < check.size(); i++) check[i] = false;
tmp.clear();
int ans1 = fnct(save, pos1, pos2);
for (int i = 0; i < n; i++) {
if (check[i] == true)
tmp.push_back(make_pair(save[i].first, save[i].second));
}
int ans2 = fnct(tmp, 0, 1);
if (ans1 + ans2 == n) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
double a, b;
cin >> a >> b;
save.push_back(make_pair(a, b));
}
if (n < 5)
cout << "YES" << endl;
else {
if (res(0, 1, n) == true)
cout << "YES" << endl;
else if (res(0, 2, n) == true)
cout << "YES" << endl;
else if (res(1, 2, n) == true)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7f7f7f7f;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
const int fx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int fy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void openfile() {
freopen("data.in", "rb", stdin);
freopen("data.out", "wb", stdout);
}
const int N = 0;
int cur;
map<string, int> m;
string s;
int main() {
long long ans = 0;
while (getline(cin, s)) {
if (s[0] == '+') {
m[s.substr(1)]++;
cur++;
} else if (s[0] == '-') {
m[s.substr(1)]--;
cur--;
} else {
int pos = s.find(":");
ans += (s.length() - pos - 1) * cur;
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
vector<int> v(m + 1);
for (int i = 0; i <= m; i++) {
cin >> v[i];
}
int ans = 0;
for (int j = 0; j < m; j++) {
if (__builtin_popcount(v[m] ^ v[j]) <= k) ans++;
}
cout << ans;
return 0;
}
| 2 |
#include "bits/stdc++.h"
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
typedef long long Weight;
struct Edge {
int src, dest;
int cap, rev;
Weight weight;
bool operator < (const Edge &rhs) const { return weight > rhs.weight; }
};
const int V = 4000;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
Weight h[V]; //ポテンシャル
Weight dist[V]; //最短距離
int prevv[V], preve[V]; //直前の辺と頂点
void add_edge(Graph &g, int src, int dest, int cap, Weight weight) {
g[src].push_back(Edge{ src, dest, cap, (int)g[dest].size(), weight });
g[dest].push_back(Edge{ dest, src, 0, (int)g[src].size() - 1, -weight });
}
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(auto i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const Weight INF = 2147483647;
const Weight ZERO = 0;
int eps=0;
//コストがdoubleの時はeps使おう
int min_cost_flow(Graph &g, int s, int t, int f) {
Weight res = ZERO;
memset(h, ZERO, sizeof(h));
typedef pair<Weight, int> P;
while (f > 0) {
priority_queue<P, vector<P>, greater<P> > que;
fill(dist, dist + V, INF);
dist[s] = 0;
que.push(P(ZERO, s));
while (!que.empty()) {
P p = que.top(); que.pop();
const int v = p.second;
if (dist[v] < p.first) continue;
REP(i, g[v].size()) {
Edge &e = g[v][i];
if (e.cap > 0 && dist[e.dest] > dist[v] + e.weight + h[v] - h[e.dest] + eps) {
dist[e.dest] = dist[v] + e.weight + h[v] - h[e.dest];
prevv[e.dest] = v;
preve[e.dest] = i;
que.push(P(dist[e.dest], e.dest));
}
}
}
if (dist[t] == INF) return f;
for (int v = 0; v < V; ++v) h[v] = h[v] + dist[v];
int d = f;
for (int v = t; v != s; v = prevv[v]) d = min(d, g[prevv[v]][preve[v]].cap);
f -= d;
res = res + d * h[t];
for (int v = t; v != s; v = prevv[v]) {
Edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return f;
}
int main() {
int N; cin >> N;
vector<pair<int, int>>ps;
for (int i = 0; i < N; ++i) {
int a, b; cin >> a >> b;
ps.push_back(make_pair(a, b));
}
const int start = 0;
const int t_in = 1;
const int t_out = t_in + N;
const int goal = t_out + N;
Graph g(goal + 1);
for (int i = 0; i < N; ++i) {
add_edge(g, start, t_in + i, 1, 0);
add_edge(g,t_out+i,goal,1,0);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
auto pi(ps[i]);
auto pj(ps[j]);
if ((pi.first < pj.first&&pi.second < pj.second) ||
(pi.first < pj.second&&pi.second < pj.first)) {
add_edge(g, t_in + i, t_out + j, 1, 0);
}
}
}
auto cost=min_cost_flow(g,start,goal,N);
cout<<cost<<endl;
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
int main() {
int N;
std::cin >> N;
std::cout << ((N/10)%111==0 || ((N%1000)%111==0) ? "Yes" : "No") << std::endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w = 0;
cin >> n;
long long a[n], b[n];
map<long long, pair<int, int> > m;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (m.count(a[i]) == 0) {
m[a[i]].first = i;
m[a[i]].second = i;
}
}
for (int i = n - 1; i >= 0; i--) {
pair<long long, long long> x = m[a[i]];
if (m.count(a[i]) == 1 && x.first == x.second && x.first != i) {
m[a[i]].second = i;
}
}
long long cou = 0;
int last = a[0];
b[w] = 0;
w++;
for (int i = 1; i < n; i++) {
if (a[i] != last) {
pair<long long, long long> l = m[a[i]], l1 = m[last];
if ((l.first > l1.second)) {
last = a[i], cou++, b[w] = cou, w++;
} else if ((l.first < l1.second && l.second > l1.second))
last = a[i];
}
}
long long x = w - 2, sum = 2, las = 1;
for (int i = 1; i < w - 1; i++) {
x = (2 * las) % 998244353;
las = x % 998244353;
sum = (sum + las) % 998244353;
}
if (x == 0)
cout << 2;
else if (x == -1)
cout << 1;
else
cout << sum;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 11;
const int oo = 0x3f3f3f3f;
int to[maxn << 1], nxt[maxn << 1], cap[maxn << 1], flow[maxn << 1];
int head[maxn], tot;
void init() {
memset(head, -1, sizeof head);
tot = 0;
}
void add(int u, int v, int w) {
to[tot] = v;
nxt[tot] = head[u];
cap[tot] = w;
flow[tot] = 0;
head[u] = tot++;
swap(u, v);
to[tot] = v;
nxt[tot] = head[u];
cap[tot] = 0;
flow[tot] = 0;
head[u] = tot++;
}
struct QUEUE {
int que[maxn << 3];
int front, rear;
void init() { front = rear = 0; }
void push(int u) { que[rear++] = u; }
int pop() { return que[front++]; }
bool empty() { return front == rear; }
} que;
int n, m, s, t;
int gap[maxn], dep[maxn];
void bfs() {
memset(dep, -1, sizeof dep);
memset(gap, 0, sizeof gap);
gap[0] = 1;
que.init();
que.push(t);
dep[t] = 0;
while (!que.empty()) {
int u = que.pop();
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (~dep[v]) continue;
que.push(v);
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int cur[maxn], S[maxn];
int isap() {
bfs();
memcpy(cur, head, sizeof head);
int ans = 0, top = 0, u = s;
while (dep[s] < n) {
if (u == t) {
int mn = oo;
int inser;
for (int i = 0; i < top; i++) {
if (mn > cap[S[i]] - flow[S[i]]) {
mn = cap[S[i]] - flow[S[i]];
inser = i;
}
}
for (int i = 0; i < top; i++) {
flow[S[i]] += mn;
flow[S[i] ^ 1] -= mn;
}
ans += mn;
top = inser;
u = to[S[top] ^ 1];
continue;
}
bool flag = 0;
int v;
for (int i = cur[u]; ~i; i = nxt[i]) {
v = to[i];
if (cap[i] - flow[i] && dep[v] + 1 == dep[u]) {
flag = 1;
cur[u] = i;
break;
}
}
if (flag) {
S[top++] = cur[u];
u = v;
continue;
}
int mn = n;
for (int i = head[u]; ~i; i = nxt[i]) {
if (cap[i] - flow[i] && dep[to[i]] < mn) {
mn = dep[to[i]];
cur[u] = i;
}
}
gap[dep[u]]--;
if (!gap[dep[u]]) return ans;
dep[u] = mn + 1;
gap[dep[u]]++;
if (u != s) u = to[S[--top] ^ 1];
}
return ans;
}
int G[122][122];
int main() {
int n1, ans, sum1, sum2, u, v;
int a[maxn], b[maxn];
while (scanf("%d%d", &n1, &m) != EOF) {
init();
sum1 = sum2 = 0;
n = 2 * n1 + 2;
s = n - 1;
t = n;
for (int i = 1; i <= n1; i++) {
scanf("%d", &a[i]);
sum1 += a[i];
add(s, i, a[i]);
}
for (int i = 1; i <= n1; i++) {
scanf("%d", &b[i]);
sum2 += b[i];
add(i + n1, t, b[i]);
}
for (int i = 1; i <= n1; i++) add(i, i + n1, oo);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
add(u, v + n1, oo);
add(v, u + n1, oo);
}
if (sum1 != sum2)
printf("NO\n");
else if (isap() != sum2)
printf("NO\n");
else {
printf("YES\n");
memset(G, 0, sizeof G);
for (int i = 1; i <= n1; i++) {
for (int j = head[i]; ~j; j = nxt[j]) {
int v = to[j], f = flow[j];
if (f > 0 && v != s) G[i][v - n1] = f;
}
}
for (int i = 1; i <= n1; i++) {
for (int j = 1; j <= n1; j++) {
if (j == n1)
printf("%d\n", G[i][j]);
else
printf("%d ", G[i][j]);
}
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
const int n1 = 100;
int n2;
string s2;
cin >> n2;
char s1[n1];
for (int x = 0; x < n2; x++) {
cin >> s1[x];
}
for (int z = 0; z < n2; z++) {
if (s1[z] == 'o' && s1[z + 1] == 'g' && s1[z + 2] == 'o') {
if (s1[z + 3] == 'g' && s1[z + 4] == 'o') {
s1[z + 3] = ' ';
s1[z + 4] = ' ';
}
s1[z] = '*';
s1[z + 1] = '*';
s1[z + 2] = '*';
}
if (s1[z + 3] == ' ' && s1[z + 4] == ' ') {
if (s1[z + 5] == 'g' && s1[z + 6] == 'o') {
s1[z + 5] = ' ';
s1[z + 6] = ' ';
}
}
}
for (int y = 0; y < n2; y++) {
if (s1[y] != ' ') {
s2 = s2 + s1[y];
}
}
cout << s2;
return 0;
}
| 1 |
// template version 1.14
using namespace std;
#include <bits/stdc++.h>
// varibable settings
#define int long long
const int INF=1e18;
const int MOD=1e9+7;
// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(a, b, c) make_tuple(a, b, c)
#define ceil(a,b) ((a)+(b)-1)/(b)
#define is_in(x, a, b) ((a)<=(x) && (x)<(b))
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;
#if defined(PCM) || defined(LOCAL)
#include "lib/dump.hpp"
#else
#define dump(...) 42
#define dump_1d(...) 42
#define dump_2d(...) 42
#endif
//}}}
signed main() {
int n,m;cin>>n>>m;
map<int, int> d;
vector<int> a(n);
rep(i, n) {
cin>>a[i];
d[a[i]]++;
}
dump(a);
vector<int> b(m);
rep(i, m) {
cin>>b[i];
d[b[i]]++;
}
dump(b);
vi vor, vand;
each(el, d){
vor.pb(el.first);
if (el.second==2) vand.pb(el.first);
}
dump(vand);
dump(vor);
cout << sz(vand) << " " << sz(vor) << endl;
rep(i, sz(vand)) cout << vand[i] << endl;
rep(i, sz(vor)) cout << vor[i] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, k, l, n, d, h;
scanf("%d %d %d", &n, &d, &h);
if (n == 2) {
printf("1 2\n");
return 0;
}
double a = ceil((double)d / 2);
if (h < (int)a) {
printf("-1\n");
return 0;
}
if (h < 1 || d < 2 || h > d || h > n - 1 || d > n - 1) {
printf("-1\n");
return 0;
}
j = 1;
for (k = 1; k <= h; k++, j++) printf("%d %d\n", j, j + 1);
j++;
int w = h - d;
if (w < 0) {
while (j <= n) {
w = abs(w);
int b = 0;
printf("1 %d\n", j);
j++;
for (k = 1; k < w && j < n; k++, j++) {
if (k == 1) j--;
printf("%d %d\n", j, j + 1);
b = 1;
}
if (b == 1) j++;
}
} else if (w == 0) {
while (j <= n) {
printf("2 %d\n", j);
int b = 0;
j++;
for (k = 1; k < w && j < n; k++, j++) {
if (k == 1) j--;
printf("%d %d\n", j, j + 1);
b = 1;
}
if (b == 1) j++;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void cases() {
long long int n;
cin >> n;
unordered_map<long long int, long long int> mpx;
unordered_map<long long int, long long int> mpy;
for (long long int i = 0; i < n; i++) {
long long int k;
cin >> k;
mpx[k] = (i + 1);
mpy[k] = n - i;
}
long long int q;
cin >> q;
long long int ans1 = 0;
long long int ans2 = 0;
while (q--) {
long long int k;
cin >> k;
ans1 += mpx[k];
ans2 += mpy[k];
}
cout << ans1 << " " << ans2 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int test = 1;
for (long long int tc = 0; tc < test; tc++) {
cases();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int c, d, n, m, k;
cin >> c >> d >> n >> m >> k;
int people = (n * m) - k;
int o1 = ceil((1.0 * people) / n) * c;
int o2 = (people)*d;
int o3 = (c * (people / n)) + ((people % n) * d);
cout << max(0, min(o1, min(o2, o3)));
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 100;
int v[N][N];
vector<int> G[N];
int used[N];
int dfs(int v){
used[v] = 1;
int res = 0;
for (auto &to : G[v]){
if (used[to]) continue;
res = max(res, dfs(to)+1);
}
used[v] = 0;
return res;
}
int main(){
int n;
while(cin >> n, n){
MEMSET(v, 0);
rep(i, N) G[i].clear();
rep(i, n){
int x, y;
cin >> x >> y;
--x, --y;
v[x][y] = v[y][x] = 1;
}
rep(i, N) rep(j, N) if (v[i][j]) G[i].push_back(j);
int ans = 2;
rep(i, N) ans = max(ans, dfs(i)+1);
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 9;
class LazySegmentTree {
public:
void reset(int sz, bool clr = false) {
n = sz;
treeMn.assign(n << 2, INT_MAX);
treeMx.assign(n << 2, INT_MAX);
lazyAssign.assign(n << 2, -1);
}
void minimize(int L, int R, int value) { minimize(1, n, 1, L, R, value); }
int getValue(int index) { return getValue(1, n, 1, index); }
private:
void push(int low, int high, int pos) {
if (lazyAssign[pos] != -1) {
treeMn[pos] = treeMx[pos] = lazyAssign[pos];
if (low != high) {
lazyAssign[pos << 1] = lazyAssign[pos << 1 | 1] = lazyAssign[pos];
}
lazyAssign[pos] = -1;
}
}
void minimize(int low, int high, int pos, int L, int R, int value) {
push(low, high, pos);
if (L > high || R < low || treeMx[pos] <= value) {
return;
}
if (L <= low && R >= high && treeMn[pos] >= value) {
lazyAssign[pos] = value;
push(low, high, pos);
return;
}
int mid = (low + high) >> 1;
minimize(low, mid, pos << 1, L, R, value);
minimize(mid + 1, high, pos << 1 | 1, L, R, value);
treeMn[pos] = min(treeMn[pos << 1], treeMn[pos << 1 | 1]);
treeMx[pos] = max(treeMx[pos << 1], treeMx[pos << 1 | 1]);
}
int getValue(int low, int high, int pos, int index) {
push(low, high, pos);
if (low == high) {
return treeMn[pos];
}
int mid = (low + high) >> 1;
if (index <= mid)
return getValue(low, mid, pos << 1, index);
else
return getValue(mid + 1, high, pos << 1 | 1, index);
}
int n;
vector<int> treeMx;
vector<int> treeMn;
vector<int> lazyAssign;
};
class SparseTable {
public:
SparseTable() {
parentSpt.assign(MAX, vector<int>(20));
maxSpt.assign(MAX, vector<int>(20));
lg2.resize(MAX);
depth.resize(MAX);
for (int i = 2; i < MAX; ++i) {
lg2[i] = lg2[i >> 1] + 1;
}
}
void build(int sz, const vector<vector<pair<int, int> > >& adj) {
n = sz;
dfs(1, adj);
}
int getMaxOnPath(int x, int y) {
if (depth[x] > depth[y]) {
swap(x, y);
}
int maxOnFirstPath = 0;
for (int k = lg2[n]; k >= 0; --k) {
if (depth[y] - (1 << k) >= depth[x]) {
maxOnFirstPath = max(maxOnFirstPath, maxSpt[y][k]);
y = parentSpt[y][k];
}
}
if (x == y) {
return maxOnFirstPath;
}
int maxOnSecondPath = 0;
for (int k = lg2[n]; k >= 0; --k) {
if (parentSpt[x][k] != parentSpt[y][k]) {
maxOnSecondPath = max(maxOnSecondPath, maxSpt[x][k]);
maxOnFirstPath = max(maxOnFirstPath, maxSpt[y][k]);
x = parentSpt[x][k];
y = parentSpt[y][k];
}
}
maxOnSecondPath = max(maxOnSecondPath, maxSpt[x][0]);
maxOnFirstPath = max(maxOnFirstPath, maxSpt[y][0]);
return max(maxOnFirstPath, maxOnSecondPath);
}
private:
void dfs(int node, const vector<vector<pair<int, int> > >& adj) {
for (const pair<int, int>& p : adj[node]) {
int child = p.first;
if (child != parentSpt[node][0]) {
int cost = p.second;
depth[child] = depth[node] + 1;
parentSpt[child][0] = node;
maxSpt[child][0] = cost;
for (int j = 1; (1 << j) <= n; ++j) {
parentSpt[child][j] = parentSpt[parentSpt[child][j - 1]][j - 1];
maxSpt[child][j] =
max(maxSpt[child][j - 1], maxSpt[parentSpt[child][j - 1]][j - 1]);
}
dfs(child, adj);
}
}
}
int n;
vector<int> lg2;
vector<vector<int> > parentSpt;
vector<vector<int> > maxSpt;
vector<int> depth;
};
class Hld {
public:
void build(int n, const vector<vector<pair<int, int> > >& adj) {
dfsCnt = 0;
tree.reset(n);
head.resize(n + 1);
heavy.resize(n + 1);
pos.resize(n + 1);
subtreeSize.resize(n + 1);
parent.resize(n + 1);
depth.resize(n + 1);
dfs(1, adj);
decompose(1, 1, adj);
}
void minimizePath(int x, int y, int value) {
for (; head[x] != head[y]; y = parent[head[y]]) {
if (depth[head[x]] > depth[head[y]]) {
swap(x, y);
}
tree.minimize(pos[head[y]], pos[y], value);
}
if (depth[x] > depth[y]) {
swap(x, y);
}
if (x != y) {
tree.minimize(pos[x] + 1, pos[y], value);
}
}
int getEdgeValue(int x, int y) {
int position = max(pos[x], pos[y]);
return tree.getValue(position);
}
private:
void dfs(int node, const vector<vector<pair<int, int> > >& adj) {
int maxSubtreeSize = 0;
subtreeSize[node] = 1;
for (const pair<int, int>& p : adj[node]) {
int child = p.first;
if (child != parent[node]) {
parent[child] = node;
depth[child] = depth[node] + 1;
dfs(child, adj);
subtreeSize[node] += subtreeSize[child];
if (subtreeSize[child] > maxSubtreeSize) {
maxSubtreeSize = subtreeSize[child];
heavy[node] = child;
}
}
}
}
void decompose(int node, int chainRoot,
const vector<vector<pair<int, int> > >& adj) {
head[node] = chainRoot;
pos[node] = ++dfsCnt;
if (heavy[node]) {
decompose(heavy[node], chainRoot, adj);
}
for (const pair<int, int>& p : adj[node]) {
int child = p.first;
if (child != parent[node] && child != heavy[node]) {
decompose(child, child, adj);
}
}
}
int dfsCnt;
vector<int> head;
vector<int> heavy;
vector<int> pos;
vector<int> subtreeSize;
vector<int> parent;
vector<int> depth;
LazySegmentTree tree;
};
void Kruskal(int n, vector<vector<pair<int, int> > >& adj,
vector<pair<pair<int, int>, pair<int, int> > >& edges,
vector<bool>& isInMst) {
vector<int> parent(n + 1);
vector<int> r(n + 1);
for (int i = 1; i <= n; ++i) {
parent[i] = i;
}
function<int(int)> findSet = [&parent, &findSet](int node) -> int {
if (parent[node] == node) {
return node;
} else {
return parent[node] = findSet(parent[node]);
}
};
auto mergeSets = [&findSet, &parent, &r](int x, int y) {
int firstParent = findSet(x);
int secondParent = findSet(y);
assert(firstParent != secondParent);
if (r[firstParent] > r[secondParent]) {
swap(firstParent, secondParent);
} else if (r[firstParent] == r[secondParent]) {
++r[secondParent];
}
parent[firstParent] = secondParent;
};
vector<pair<pair<int, int>, pair<int, int> > > tempEdges(edges);
sort(tempEdges.begin(), tempEdges.end());
for (int i = 0; i < tempEdges.size(); ++i) {
int x = tempEdges[i].second.first;
int y = tempEdges[i].second.second;
int w = tempEdges[i].first.first;
int idx = tempEdges[i].first.second;
if (findSet(x) != findSet(y)) {
adj[x].push_back({y, w});
adj[y].push_back({x, w});
isInMst[idx] = true;
mergeSets(x, y);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<pair<pair<int, int>, pair<int, int> > > edges(m);
for (int i = 0; i < m; ++i) {
int x, y, w;
cin >> x >> y >> w;
edges[i] = {{w, i}, {x, y}};
}
vector<vector<pair<int, int> > > adj(n + 1);
vector<bool> isInMst(m);
Kruskal(n, adj, edges, isInMst);
SparseTable spt;
spt.build(n, adj);
Hld hld;
hld.build(n, adj);
vector<int> answer(m, INT_MAX);
for (int i = 0; i < m; ++i) {
if (!isInMst[i]) {
int x = edges[i].second.first;
int y = edges[i].second.second;
int w = edges[i].first.first;
answer[i] = spt.getMaxOnPath(x, y) - 1;
hld.minimizePath(x, y, w - 1);
}
}
for (int i = 0; i < m; ++i) {
if (isInMst[i]) {
int x = edges[i].second.first;
int y = edges[i].second.second;
answer[i] = hld.getEdgeValue(x, y);
if (answer[i] == INT_MAX) {
answer[i] = -1;
}
}
}
for (int i = 0; i < m; ++i) {
cout << answer[i] << " ";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string s;
int ans[2000000];
int main() {
int i, j, n, t, m, k = 0, l;
cin >> s;
n = s.size();
j = 1;
for (i = 0; i < n; i++) {
if (s[i] == 'F') {
j++;
if (k != 0) {
if (k - ans[j - 1] + ans[j - 2] <= ans[j - 2])
ans[j] = ans[j - 1] + 1;
else
ans[j] = k;
}
} else
k++;
}
cout << ans[j] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const unsigned long long MOD = 1000000007;
const int INF = 1000000000;
int N, nxt[MAXN], cnt[MAXN][MAXN], len[MAXN][MAXN], tot = 1, sh;
string S;
unsigned long long P[MAXN], H[MAXN];
inline unsigned long long get(int p, int l) {
return (H[p + l] - H[p]) * P[N - p];
}
inline bool comp(int a, int b, int l) {
int lo = 0, hi = l, mid = (lo + hi) / 2;
for (; lo < mid; mid = (lo + hi) / 2)
(get(a, mid) == get(b, mid) ? lo : hi) = mid;
return S[a + lo] < S[b + lo];
}
void pre() {
N = S.size();
int l = 0;
P[0] = 1;
for (int i = 0; i < N; i++) {
P[i + 1] = MOD * P[i];
H[i + 1] = H[i] + P[i] * S[i];
nxt[i] = -1;
while (l <= i && S[i] == '1') nxt[l++] = i;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j <= N; j++) {
if (i == 0) {
cnt[j][i] = 1;
len[j][i] = 1;
} else
len[j][i] = INF;
}
}
}
int main() {
cin >> S;
pre();
int p = 1, v = 0;
for (int i = N - 1; i >= 0; i--) {
v = (v + p * (S[i] - '0')) % MOD;
p = 2 * p % MOD;
}
sh = (v + 1) % MOD;
for (int i = 1; i < N; i++) {
int t = 0, m = INF, p = i - 1;
for (int j = i + 1; j <= N; j++) {
if (S[i] == '0') continue;
int l = j - i, s = max(i - l, 0);
if (s == i - l && comp(i, s, l)) s++;
s = nxt[s];
if (s < 0) continue;
for (; p >= s; p--) {
t = (t + cnt[i][p]) % MOD;
m = min(m, len[i][p] + 1);
}
cnt[j][i] = t;
len[j][i] = min(m, INF);
if (j == N) {
tot = (tot + t) % MOD;
int p = 1, v = 0;
for (int k = j - 1; k >= i; k--) {
v = (v + p * (S[k] - '0')) % MOD;
p = 2 * p % MOD;
}
if (m < INF) {
if (v < 10000)
sh = min(sh, v + m);
else
sh = (v + m) % MOD;
}
}
}
}
cout << tot << '\n' << sh << '\n';
}
| 4 |
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ?????¢
// XCode??§???EOF??\??????Ctrl+D
// ?\???Alt+\
// ans???????§?INT?????????2,147,483,647????¶????????????¨??????????????§long long?????£???????????????????????????
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////
//ALDS1_14_D: Multiple String Matching
//Aho-Corasick????????????????????????MLE??????TLE?????£????????§???
//Suffix-Array????????????
////////////////////////////////////////
//#define debug //*******************************************************************************************************************************************
#ifdef debug
#include <chrono>
#endif
#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstring> //memcpy
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <map>
//#include <unordered_map> //hash func.
#include <fstream> //ifstream, ofstream
#include <iterator> //insert_iterator::inserter
//#define NDEBUG //If NDEBUG is defined before #include <cassert>, assert will be ignored. You had better define NDEBUG when u submit the code.
#include <cassert> //assert
using namespace std;
#define dout cout
//If u wanna output to a text file instead of standard output, plz define OUTPUTFILE.
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif
#define din cin
//If u wanna input from a text file instead of standard input, plz define INPUTFROMTEXTFILE???.
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif
#define scan(A) scanf("%d", &(A))
#define disp(A) dout << #A << " = " << setw(3) << (A) << endl
#define disP(A) dout << setw(3) << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl
//#define dispAll(A,n) cout << #A << " = "; rep(j, 0, (n)) {cout << setw(3) << A[j] << " ";} cout << endl
#define sign(x) ((x)>0)-((x)<0) //x<0: -1, x=0: 0, x>0: +1
#define p(i) (i)/2
#define l(i) (i)*2
#define r(i) (i)*2+1
int dx[] = {1,-1, 0, 0, 1, 1,-1,-1}; //???????????????????????????????¨??????????????????????????????????
int dy[] = {0, 0,-1, 1,-1, 1, 1,-1};
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long ull;
const int INF = (int)2e9+10;
const ll INF_LL = (ll)9e18-1LL; //Be careful for overflow.
const ull INF_ULL = (ull)1e19-1ULL;
const int NONE = -1;
const ll MOD = (ll)1e9+7; //??§???????´???°?????£??¨??¨????????°????????????10???7??????
const int N_MAX = 1000010; //num of vertex or element
const int M_MAX = 10010; //num of edge
const int DATA_MAX = 1010;
int N;
char T[N_MAX];
int SA[N_MAX];
int Rank[N_MAX];
int tmp[N_MAX];
int k;
int Q;
char* P[M_MAX];
vi placeOfP[M_MAX];
#ifdef debug
char subT[N_MAX];
#endif
bool compare_sa(int i, int j) { //compare Rank(i, i+k) & Rank(j, j+k)
if(Rank[i]!=Rank[j]) return Rank[i] < Rank[j];
else {
int rank_i = (i+k<N ? Rank[i+k] : -1);
int rank_j = (j+k<N ? Rank[j+k] : -1);
return rank_i < rank_j;
}
}
#ifdef debug
void subStr(int i, int k) { //subT = T[i ... i+k]
strncpy(subT, T+i, k);
subT[k] = '\0';
}
void display() {
dout << "-----\n";
disp(k);
rep(i,0,N) {
dout << " i = ";
disP(i);
dout << " : SA[i] = ";
disP(SA[i]);
dout << " ( ";
subStr(SA[i],k); disP(subT);
dout << " ) , Rank = ";
disP(Rank[SA[i]]);
dout << endl;
}
dout << "-----\n";
dout << " i Rank[i] Rank[i+k]\n";
rep(i,0,N) {
disP(i);
disP(Rank[i]);
dout << " ( ";
subStr(i, k); disP(subT);
dout << " ) ";
disP( i+k < N ? Rank[i+k] : -1);
dout << " ( ";
subStr(i+k, k); disP(subT);
dout << " ) ";
dout << endl;
}
}
#endif
void construct_sa() {
//initialize
k = 1;
rep(i,0,N) {
SA[i] = i;
Rank[i] = T[i]; //k=1?????¨??????Rank[i]=ASCII Code of T[i]
}
for(k=1; k<N; k*=2) {
#ifdef debug
display();
#endif
sort(SA, SA+N, compare_sa);
tmp[ SA[0] ] = 0;
rep(i,1,N) {
tmp[ SA[i] ] = tmp[ SA[i-1] ] + compare_sa(SA[i-1], SA[i]); //1?????????k???????????§???Rank?????????????????°????????????????¬????????????§???Rank??????????????????
}
rep(i,0,N) Rank[i] = tmp[i]; //??????????????§?????§????¬????k(=2k)???Rank????¨???????????????????
}
}
void binarySearch_P_from_SA() {
rep(i,0,Q) {
int lenP = strlen(P[i]);
int left = 0;
int right = N-1;
int mid;
while(left <= right) {
mid = left + (right - left) / 2;
if( strncmp(P[i],T+SA[mid],lenP) == 0 ) {
placeOfP[i].push_back(1);
break;
}
else if(strncmp(P[i],T+SA[mid],lenP) < 0) right = mid - 1;
else left = mid + 1;
}
}
}
int main() {
//cin, cout???????????? ?????¨??????cin?????????????????¨??¨cin??§???scanf?????????????????¨??¨scanf??§??±?????????????????????
cin.tie(0); //cin??¨cout??????????????????
ios::sync_with_stdio(false); //iostream??¨stdio??????????????????
//read input data
scanf("%s", T);
N = strlen(T);
scanf("%d ", &Q);
rep(i,0,Q) {
P[i] = (char *)malloc(sizeof(char)*DATA_MAX);
scanf("%s", P[i]);
}
#ifdef debug
disp(T);
disp(N);
rep(i,0,Q) { disP(P[i]); disp(strlen(P[i])); }
#endif
//------------------------------------------------------------------------------------------
#ifdef debug
//start timer
auto startTime = chrono::system_clock::now();
#endif
//------------------------------------------------------------------------------------------
//Suffix-Array Algorithm
construct_sa();
binarySearch_P_from_SA();
#ifdef debug
dout << "=====================================\n";
rep(i,0,Q) {
disP(P[i]); dout << " : ";
rep(j,0,placeOfP[i].size()) {
disP(placeOfP[i][j]);
}
dout << endl;
}
dout << "=====================================\n";
#endif
rep(i,0,Q) {
printf("%d\n", placeOfP[i].size()!=0);
}
//------------------------------------------------------------------------------------------
#ifdef debug
//stop timer
auto endTime = chrono::system_clock::now();
auto dur = endTime - startTime;
auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
dout << fixed << setprecision(4) << (double)msec/1000 << " sec \n";
#endif
//------------------------------------------------------------------------------------------
#ifdef INPUTFILE
inputfile.close();
#endif
#ifdef OUTPUTFILE
outputfile.close();
cout << "\"" << OutputFilePath << "\"" << endl;
#endif
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<long long int, long long int>> v;
priority_queue<long long int, vector<long long int>, greater<long long int>>
q;
long long int b, w;
for (int i = 0; i < n; i++) {
cin >> b >> w;
v.push_back(make_pair(b, w));
}
sort(v.begin() + 1, v.end());
reverse(v.begin() + 1, v.end());
int i = 1;
while (i < n && v[i].first > v[0].first) {
q.push((v[i].second - v[i].first + 1));
i++;
}
int org = i;
int f = 0;
while (!q.empty()) {
v[0].first -= q.top();
q.pop();
if (v[0].first < 0) {
break;
}
f++;
while (i < n && v[i].first > v[0].first) {
q.push(v[i].second - v[i].first + 1);
i++;
}
org = min(org, i - f);
}
cout << org;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int cnt = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'A' || cnt == 0)
cnt++;
else
cnt--;
}
cout << cnt << endl;
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int tree[500000];
int n;
void update(int i,int x);
int find(int s,int t,int node,int left,int right);
int main(){
int i,q,com,x,y,a=2;
scanf("%d %d",&n,&q);
while(a<n){
a*=2;
}
n=a;
for(i=0;i<2*n-1;i++)tree[i]=(1<<31)-1;
for(i=0;i<q;i++){
scanf("%d %d %d",&com,&x,&y);
if(com==0){
update(x,y);
}
if(com==1){
printf("%d\n",find(x,y,0,0,n-1));
}
}
return(0);
}
void update(int i,int x){
i=i+n-1;
tree[i]=x;
while(i>0){
i=(i-1)/2;
tree[i]=min(tree[i*2+1],tree[i*2+2]);
}
return;
}
int find(int s,int t,int node,int left,int right){
if(right<s||t<left)return((1<<31)-1);
if(s<=left&&right<=t)return(tree[node]);
int vleft=find(s,t,node*2+1,left,(right+left)/2);
int vright=find(s,t,node*2+2,(left+right)/2+1,right);
return min(vleft,vright);
} | 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
const int INF = 2e9 + 179, MOD = 1e9 + 7;
const long long int BIGINF = 2e18 + 179;
const long double EPS = 0.000001;
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
srand(179);
int n;
cin >> n;
vector<pair<long long int, long long int>> a(n);
long long int px = 0, py = 0;
vector<int> ans(n, 1);
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
if (rand() % 2) {
ans[i] = -1;
px -= a[i].first;
py -= a[i].second;
} else {
px += a[i].first;
py += a[i].second;
}
}
long long int urmom = 2250000000000;
long long int urass = 1500000;
int RenoInDeck = 1e8;
while (RenoInDeck) {
if (abs(px) < urass && abs(py) < urass) {
if (px * px + py * py < urmom) {
for (int i = 0; i < n; ++i) {
cout << ans[i] << ' ';
}
cout << endl;
return 0;
}
}
int ind = rand() % n;
if (ans[ind] == 1) {
px -= 2 * a[ind].first;
py -= 2 * a[ind].second;
ans[ind] = -1;
} else {
px += 2 * a[ind].first;
py += 2 * a[ind].second;
ans[ind] = 1;
}
RenoInDeck--;
}
cout << "Skritiy pool" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
typedef long long ll;
int T, n;
ll a[maxn];
char s[maxn];
ll b[61];
void add(ll x) {
for (int i = 60; i >= 0; i--) {
if ((x >> i) & 1) {
if (!b[i]) {
b[i] = x;
break;
} else x ^= b[i];
}
}
}
int check(ll x) {
for (int i = 60; i >= 0; i--) {
if ((x >> i) & 1) {
if (!b[i]) return 0;
else x ^= b[i];
}
}
return 1;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
scanf("%s", s+1);
for (int i = 0; i <= 60; i++) b[i] = 0;
int ok = 1;
for (int i = n; i >= 1; i--) {
if (s[i] == '0') {
add(a[i]);
} else if (!check(a[i])) {
ok = 0;
break;
}
}
printf("%d\n", 1-ok);
}
return 0;
} | 0 |
#include<cstdio>
#define N 100005
int n,a[N];
inline int geta(int i){
if(a[i])
return a[i];
printf("? %d\n",i);
fflush(stdout);
scanf("%d",&a[i]);
return a[i];
}
int main(){
scanf("%d",&n);
a[0]=a[n+1]=n+1;
int L=1,R=n;
while(L<=R){
int mid=(L+R)>>1;
int x=geta(mid-1),y=geta(mid),z=geta(mid+1);
if(x>y&&y<z){
printf("! %d\n",mid);
break;
}
if(x<z)
R=mid-1;
else
L=mid+1;
}
} | 1 |
#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;
int n,k,t,u,v,l;
int d[210];
int main(void){
cin >> n >> k >> t >> u >> v >> l;
rep(i,n) cin >> d[i];d[n]=l;
double ans=0.0,res=0.0,num=0;
rep(i,n+1){
double dist=d[i];
if(i-1>=0) dist-=d[i-1];
while(dist>0.0){
if(res>0.0){
ans+=min(dist/v,res);
double ndist=dist-min(dist,res*v);
double nres=res-min(dist/v,res);
dist=ndist,res=nres;
}
if(num>0&&res==0.0) res=t,num--;
if(res==0.0) ans+=1.0*dist/u,dist=0.0;
}
if(num==k)
res=t;
else
num++;
}
cout.precision(10);
cout << fixed << ans << endl;
return 0;
} | 0 |
Subsets and Splits