Skip to content

Instantly share code, notes, and snippets.

@Chgtaxihe
Last active February 18, 2020 06:37
Show Gist options
  • Save Chgtaxihe/b89358bd1628631bdcd5a9a33537a24a to your computer and use it in GitHub Desktop.
Save Chgtaxihe/b89358bd1628631bdcd5a9a33537a24a to your computer and use it in GitHub Desktop.
Codeforces 1282 #Codeforces
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
#define F(i, n) for(int i=0, _iter_max=n; i<_iter_max; i++)
#define print(s) std::cout << s << std::endl
using namespace std;
void redirect_input() { freopen("./input.txt", "r", stdin); }
void redirect_output() { freopen("./output.txt", "w", stdout); }
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 100;
void solve() {
int a, b, c, r;
cin >> a >> b >> c >> r;
if(a > b) swap(a, b);
int c_l = c - r, c_r = c + r;
int coverd = 0;
if(c_l <= a){
coverd = max(0, min(b - a, c_r - a));
}else{
coverd = max(0, min(c_r - c_l, b - c_l));
}
cout << (b - a - coverd) << '\n';
}
signed main() {
Android;
int t;
cin >> t;
while(t--) solve();
}
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
#define F(i, n) for(int i=0, _iter_max=n; i<_iter_max; i++)
#define print(s) std::cout << s << std::endl
using namespace std;
void redirect_input() { freopen("./input.txt", "r", stdin); }
void redirect_output() { freopen("./output.txt", "w", stdout); }
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 100;
int n, p, k;
int val[maxn];
void solve() {
cin >> n >> p >> k;
for(int i=1; i<=n; i++) cin >> val[i];
sort(val + 1, val + 1 + n);
int ans = val[1] <= p?1:0;
int remain = p - val[1];
for(int i=3; i<=n; i+=2){
remain -= val[i];
if(remain >= 0) ans = i;
else break;
}
remain = p;
for(int i=2; i<=n; i+=2){
remain -= val[i];
if(remain >= 0) ans = max(ans, i);
else break;
}
cout << ans << '\n';
}
signed main() {
Android;
int t;
cin >> t;
while(t--) solve();
}
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
#define F(i, n) for(int i=0, _iter_max=n; i<_iter_max; i++)
#define print(s) std::cout << s << std::endl
using namespace std;
void redirect_input() { freopen("./input.txt", "r", stdin); }
void redirect_output() { freopen("./output.txt", "w", stdout); }
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 100;
int n, p, k;
int val[maxn];
void solve() {
cin >> n >> p >> k;
val[0] = 0;
for(int i=1; i<=n; i++) cin >> val[i];
sort(val + 1, val + 1 + n);
ll ans = 0, presum = 0;
for(int i=0; i<k; i++){
presum += val[i];
ll remain = p - presum;
if(remain >= 0) ans = max(ans, 1ll * i);
for(int j=1; k * j + i<=n; j++){
int idx = k * j + i;
if(val[idx] <= remain){
ans = max(ans, 1ll * idx);
remain -= val[idx];
}
}
}
cout << ans << '\n';
}
signed main() {
Android;
int t;
cin >> t;
while(t--) solve();
}
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
#define F(i, n) for(int i=0, _iter_max=n; i<_iter_max; i++)
#define print(s) std::cout << s << std::endl
using namespace std;
void redirect_input() { freopen("./input.txt", "r", stdin); }
void redirect_output() { freopen("./output.txt", "w", stdout); }
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 100;
ll presum[maxn];
struct prob{
int cost, t, easy, hard;
bool operator <(prob other){
if(t == other.t) return cost < other.cost;
return t < other.t;
}
}prob[maxn];
void solve() {
prob[0] = {0, 0, 0, 0};
ll n, t, a, b, v;
cin >> n >> t >> a >> b;
for(int i=1; i<=n; i++){
cin >> v;
prob[i].cost = v?b:a;
}
for(int i=1; i<=n; i++) cin >> prob[i].t;
sort(prob + 1, prob + 1 + n);
ll easy = 0, hard = 0;
presum[0] = 0;
for(int i=1; i<=n; i++){
presum[i] = presum[i-1] + prob[i].cost;
if(prob[i].cost == a) easy++;
if(prob[i].cost == b) hard++;
prob[i].easy = easy, prob[i].hard = hard;
}
prob[n+1] = {inf, t + 1, easy, hard};
ll ans = 0;
for(int i=1; i<=n+1;){
ll t_left = prob[i].t - 1;
t_left -= presum[i-1];
if(t_left >= 0){
ll e = easy - prob[i-1].easy;
ll h = hard - prob[i-1].hard;
e = min(e, t_left / a);
t_left -= e * a;
h = min(h, t_left / b);
ans = max(ans, i-1 + e + h);
}
int to = i;
for(;to <= n+1 && prob[to].t == prob[i].t; to++);
i = to;
}
cout << ans << '\n';
}
signed main() {
Android;
int t;
cin >> t;
while(t--) solve();
}
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define Android ios::sync_with_stdio(false), cin.tie(NULL)
#define F(i, n) for(int i=0, _iter_max=n; i<_iter_max; i++)
#define print(s) std::cout << s << std::endl
using namespace std;
void redirect_input() { freopen("./input.txt", "r", stdin); }
void redirect_output() { freopen("./output.txt", "w", stdout); }
const int inf = 0x3f3f3f3f;
const int maxn = 300 + 100;
char buffer[maxn];
void solve() {
int a, b, dist, a_cnt, b_cnt;
F(i, 300) buffer[i] = 'a'; buffer[300] = 0;
cout << buffer << endl;
cin >> a;
if(a == 0) exit(0);
a_cnt = 300 - a;
F(i, 300) buffer[i] = 'b'; buffer[300] = 0;
cout << buffer << endl;
cin >> b;
if(b == 0) exit(0);
b_cnt = 300 - b;
F(i, a_cnt + b_cnt) buffer[i] = 'a'; buffer[a_cnt + b_cnt] = 0;
dist = a - (300 - (a_cnt + b_cnt));
for(int i=0; i<a_cnt + b_cnt-1; i++){
int l_dist;
buffer[i] = buffer[i]=='a'?'b':'a';
cout << buffer << endl;
cin >> l_dist;
if(l_dist == -1) exit(0);
if(l_dist == 0) exit(0);
if(l_dist > dist){
buffer[i] = buffer[i]=='a'?'b':'a';
}else{
dist = l_dist;
}
}
int pa=a_cnt, pb=b_cnt;
for(int i=0; i<a_cnt + b_cnt-1; i++) {
if(buffer[i] == 'a') pa--;
if(buffer[i] == 'b') pb--;
}
if(pa > 0) buffer[a_cnt+b_cnt-1] = 'a';
if(pb > 0) buffer[a_cnt+b_cnt-1] = 'b';
cout << buffer << endl;
exit(0);
}
signed main() {
Android;
solve();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment