Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int mn = 1e9, mx = 0;
for (int i = 1; i <= n; i++){
int x;
cin >> x;
mn = min(mn, x);
mx = max(mx, x);
}
cout << max(0, (mx-mn)-n+1);
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll gc = __gcd(c, d);
c /= gc;
d /= gc;
cout << min(a/c, b/d);
}
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
set<pair<int, int> > q;
int ans[N], n, a[N], m, k;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i++){
cin >> a[i];
q.insert({a[i], i});
}
int cnt = 0;
while(!q.empty()){
++cnt;
int pos = q.begin()->second;
ans[pos] = cnt;
q.erase(q.begin());
while(true){
auto it = q.lower_bound({a[pos]+1+k, 0});
if (it == q.end())
break;
pos = it->second;
q.erase(it);
ans[pos] = cnt;
}
}
cout << cnt << "\n";
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define x first
#define y second
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const int N = 200 * 1000 + 555;
int n, h;
pt a[N];
inline bool read() {
if(!(cin >> n >> h))
return false;
fore(i, 0, n)
assert(scanf("%d%d", &a[i].x, &a[i].y) == 2);
sort(a, a + n);
return true;
}
int ps[N];
int getH(int lf, int rg) {
int l = int(lower_bound(a, a + n, pt(lf, -1)) - a);
int r = int(lower_bound(a, a + n, pt(rg, -1)) - a);
int sum = ps[r] - ps[l];
if(l > 0)
sum += max(0, a[l - 1].y - lf);
assert(rg - lf - sum >= 0);
return rg - lf - sum;
}
inline void solve() {
ps[0] = 0;
fore(i, 0, n)
ps[i + 1] = ps[i] + (a[i].y - a[i].x);
int ans = 0;
fore(i, 0, n) {
int lx = a[i].y + 1;
int lf = -(h + 1), rg = lx;
while(rg - lf > 1) {
int mid = (lf + rg) / 2;
if(getH(mid, lx) > h)
lf = mid;
else
rg = mid;
}
assert(getH(rg, lx) == h);
ans = max(ans, lx - rg);
}
cout << ans << endl;
}
int main() {
if(read()) {
solve();
}
return 0;
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int N = 200043;
int cnt[N];
int main()
{
int n;
scanf("%d", &n);
for(int i = 0; i < n - 1; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if(y != n)
{
puts("NO");
return 0;
}
cnt[x]++;
}
int cur = 0;
for(int i = 1; i < n; i++)
{
cur += cnt[i];
if(cur > i)
{
puts("NO");
return 0;
}
}
int last = -1;
puts("YES");
set<int> unused;
for(int i = 1; i < n; i++)
unused.insert(i);
for(int i = 1; i < n; i++)
{
if(cnt[i] > 0)
{
unused.erase(i);
if(last != -1)
printf("%d %d\n", last, i);
last = i;
cnt[i]--;
}
while(cnt[i] > 0)
{
printf("%d %d\n", last, *unused.begin());
last = *unused.begin();
cnt[i]--;
unused.erase(unused.begin());
}
}
printf("%d %d\n", last, n);
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const int N = 100 * 1000 + 555;
int n[2], y[2];
int x[2][N];
inline bool read() {
fore(k, 0, 2) {
if(!(cin >> n[k] >> y[k]))
return false;
fore(i, 0, n[k])
assert(scanf("%d", &x[k][i]) == 1);
}
return true;
}
inline void solve() {
int ans = 2;
for(int dx = 1; dx < int(1e9); dx *= 2) {
int mod = 2 * dx;
map<int, int> cnt;
int add[2] = {0, dx};
fore(k, 0, 2) {
fore(i, 0, n[k])
cnt[(x[k][i] + add[k]) & (mod - 1)]++;
}
for(auto curAns : cnt)
ans = max(ans, curAns.second);
}
cout << ans << endl;
}
int main() {
if(read()) {
solve();
}
return 0;
}