Diary_2.28
codeforces
3. Limited Repainting
const int maxn = 3e5+10;
int n, k, a[maxn];
string s;
bool check(int mx){
int len = 0, cnt = 0;
bool not_need = true;
for(int i=0; i<n; i++){
if(a[i] > mx){
if(s[i] == 'R'){
if(len && not_need == false) cnt++;
not_need = true;
len = 0;
}else{
not_need = false;
len++;
}
}else len++;
}
if(len && not_need == false) cnt++;
if(cnt > k) return false;
else return true;
}
void solve(){
cin >> n >> k >> s;
int mx = 0;
for(int i=0; i<n; i++){
cin >> a[i];
mx = max(mx, a[i]);
}
int l = 0, r = mx, ans = -1;
while(l <= r){
int mid = (l+r)/2;
if(check(mid)){
r = mid-1;
ans = mid;
}else l = mid+1;
}
if(ans == -1) cout << "ERROR" << endl;
else cout << ans << endl;
}
4. 砍树
#define int long long
const int maxn = 1e6+10;
int n, m, a[maxn];
bool check(int mx){
int ans = 0;
for(int i=0; i<n; i++){
if(a[i] > mx){
ans += a[i]-mx;
}
}
if(ans >= m) return true;
else return false;
}
void solve(){
cin >> n >> m;
int mx = 0;
for(int i=0; i<n ;i++){
cin >> a[i];
mx = max(mx, a[i]);
}
int l = 0, r = mx, ans = -1;
while(l <= r){
int mid = (l+r)>>1;
if(check(mid)){
ans = mid;
l = mid+1;
}else{
r = mid-1;
}
}
if(ans == -1) cout << "ERROR" << endl;
else cout << ans << endl;
}
5. Skibidus and Fanum Tax (hard version)
#define int long long
const int maxn = 2e5+10;
int l[maxn], r[maxn], n, m, inf = 1e17;
void solve(){
cin >> n >> m;
for(int i=1; i<=n; i++) cin >> l[i];
for(int k=1; k<=m; k++) cin >> r[k];
sort(r, r+m+1);
l[0] = -inf;
for(int i=1; i<=n; i++){
int opl = l[i], opr;
int pos = lower_bound(r+1, r+m+1, l[i]+l[i-1])-r;
if(pos <= m && pos >= 0) opr = r[pos]-l[i];
else opr = inf;
if(opl >= l[i-1]) opl = l[i];
else opl = inf;
l[i] = min(opl, opr);
}
if(l[n] >= inf){
cout << "NO" << endl;
}else{
cout << "YES" << endl;
}
}
6. Counting Pairs
#define int long long
const int maxn = 2e5+10;
int a[maxn], n, x, y;
void solve(){
cin >> n >> x >> y;
int sum = 0;
for(int i=0; i<n; i++){
cin >> a[i];
sum += a[i];
}
sort(a, a+n);
int ans = 0;
// sum-(a[i]+a[j]) <= y 找右边界
// sum-a[i]-a[j] <= y
// a[j] >= sum-a[i]-y
// sum-(a[i]+a[j]) >= x 找左边界
// sum-a[i]-a[j] >= x
// a[j] <= sum-a[i]-x
for(int i=0; i<n; i++){
int pl = lower_bound(a, a+n, sum-y-a[i])-a;
int pr = upper_bound(a, a+n, sum-a[i]-x)-a-1;
// cout << pl << ' ' << pr << endl;
if(pl!=n && pr!=n){
if(pr != -1)
ans += pr-pl+1;
if(a[i] >= a[pl] && a[i] <= a[pr]) ans--;
}
}
cout << ans/2 << endl;
}
突然发现而非几乎没练过,我要爆学二分!!!

浙公网安备 33010602011771号