Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codeforces 171 A,B,C,D #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added edu_cf_round_171/a.exe
Binary file not shown.
59 changes: 59 additions & 0 deletions edu_cf_round_171/actionFig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()

void solve(){
int n;
cin>>n;
string s;
cin>>s;

vector<int> prevzeros(0);
vector<int> prevones(0);
for (int i{};i<n-1;i++){
if (s[i] == '0')
prevzeros.pb(i);
else
prevones.pb(i);
}
reverse(all(prevones));
int pzi = prevzeros.size();
int poi = prevones.size();
pzi--;
poi--;

//cout<<poi<<" "<<pzi<<endl;

int total = n * (n+1);
total /= 2;

for (int i{n-1};i>=0;i--){
//cout<<i<<" "<<total<<endl;
while(pzi>=0 && prevzeros[pzi]>=i)
pzi--;
while(poi>=0 && prevones[poi]>=i)
poi--;
if (s[i] == '1'){
if (pzi >= 0){
total -= i+1;
pzi--;
}
else if (poi >= 0){
total -= i+1;
poi--;
}
}
}
cout<<total<<endl;
}

int main() {
int t;
cin >> t;
while (t--) {
solve();

}
return 0;
}
94 changes: 94 additions & 0 deletions edu_cf_round_171/blackCells.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#include <vector>
#include <climits>
#define ll long long

using namespace std;

bool is_pos(int n, const vector<ll>& a, ll k){
vector<bool> dp0(n+1, false);
vector<bool> dp1(n+1, false);
dp0[0] = true;
dp1[0] = false;
for(int i=0; i<n; i++){
vector<bool> next0(n+1, false);
vector<bool> next1(n+1, false);
if(dp0[i]){
if(i + 1 < n && abs(a[i+1] - a[i]) <= k){
next0[i+2] = true;
}
if(k >=1){
next1[i+1] = true;
}
if(dp1[i]){
if(i + 1 < n && abs(a[i+1] - a[i]) <= k){
next1[i+2] = true;
}
}
for(int j = 0; j <= n; ++j){
if(next0[j]) dp0[j] = true;
if(next1[j]) dp1[j] = true;
}
}
return dp0[n] || dp1[n];
}
}

int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<ll> a(n);
for(auto &x: a) cin >> x;
ll left = 0, right = (ll)1e18;
ll ans = right;
while(left <= right){
ll mid = left + (right - left)/2;
if(is_pos(n, a, mid)){
ans = mid;
right = mid - 1;
}else{
left = mid + 1;
}
}

cout << ans << endl;

}
return 0;
}

// int main() {
// int t;
// cin>>t;
// while(t--){
// int n;
// cin>>n;
// vector<long long> a(n);
// for(int i = 0; i < n;i++){
// cin>>a[i];
// }
// long long ans = 0;
// for(int i = 0; i < n;i+=2){
// if(i != n-1){
// ans = max(ans,a[i+1]-a[i]);
// }
// }
// if(n%2 != 0){
// long long again = 0;
// for(int i = 1; i < n;i+=2){
// again = max(again,a[i+1]-a[i]);
// }
// ans = min(ans,again);
// }
// if(n == 1){
// cout<<1<<endl;
// }else {
// cout<<ans<<endl;
// }
// }

// return 0;
// }
45 changes: 45 additions & 0 deletions edu_cf_round_171/black_cells.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <iostream>
#include <vector>
#include <climits>

using namespace std;


int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
cin >> a[0];
if(n == 1){
cout << "1" << endl;
}else{
for(int i = 1; i < n;i++)
cin >> a[i];
if(n == 2){
cout << a[1] - a[0] << endl;
}else{
int _even = INT_MIN;
int _odd = INT_MIN;
for(int i = 1; i < n; i++){
if(i%2 == 0){
_even = max(_even, a[i] - a[i-1]);
}else{
_odd = max(_odd, a[i] - a[i-1]);
}
}
cout << min(_odd, _even) << endl;
}
}


// if(n%2 == 0)
// cout << n/2 << endl;
// else
// cout << (n+1)/2 << endl;

}
return 0;
}
42 changes: 42 additions & 0 deletions edu_cf_round_171/perpendicular_segments.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <cmath>

using namespace std;


int main() {
int t;
cin >> t;
while (t--) {
int x, y, k;
cin >> x >> y >> k;
if(x == y){
cout << "0 0 " << x << " " << y << endl;
cout << "0 " << y << " " << x << " 0" << endl;
}else{
int m = (x > y)? y : x;
cout << "0 0 " << m << " " << m << endl;
cout << "0 " << m << " " << m << " 0" << endl;
}
// if(k <= x && k <= y){
// cout << "0 0 0 " << k<< endl;
// cout << "0 0 " << k << " 0 "<< endl;
// }else{
// /*
// p p p B
// p p p p
// A p p p

// A = ( 0 0)
// B = (x , ceil(root(k**2 - (x+1)**2)) - 1 )
// C = (0, y)
// D = ( ceil(root(k**2 - (y+1)**2)) - 1, 0)
// */

// // cout <<"0 0 " << x << " "<< ceil(sqrt(k*k - (x)*(x))) - 1 << endl;
// // cout << "0 " << y << " " << ceil(sqrt(k*k - (y)*(y))) << " 0" << endl;
// }

}
return 0;
}
96 changes: 96 additions & 0 deletions edu_cf_round_171/sumSeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);

int n;
cin >> n;

vector<int> a(n + 1, 0);
for (int i = 1; i <= n; ++i) cin >> a[i];

// Compute prefix and prefix_weight
vector<ll> prefix(n + 1, 0);
vector<ll> prefix_weight(n + 1, 0);
for (int i = 1; i <= n; ++i) {
prefix[i] = prefix[i - 1] + a[i];
prefix_weight[i] = prefix_weight[i - 1] + (ll)i * a[i];
}

// Compute sum_prefix and sum_prefix_weight
vector<ll> sum_prefix(n + 1, 0);
vector<ll> sum_prefix_weight(n + 1, 0);
for (int i = 1; i <= n; ++i) {
sum_prefix[i] = sum_prefix[i - 1] + prefix[i];
sum_prefix_weight[i] = sum_prefix_weight[i - 1] + prefix_weight[i];
}

// Compute sum_blocks
vector<ll> sum_blocks(n + 1, 0); // sum_blocks[1..n]
for (int i = 1; i <= n; ++i) {
sum_blocks[i] = (ll)i * n - ((ll)i * (i - 1)) / 2;
}

// Compute C
ll C = (ll)(n + 1) * prefix[n] - prefix_weight[n];

// Function to compute sum_b(k)
auto get_sum_b = [&](ll k) -> ll {
if (k == 0) return 0;

// Binary search to find l
int left = 1, right = n;
while (left < right) {
int mid = left + (right - left) / 2;
if (sum_blocks[mid] >= k) {
right = mid;
} else {
left = mid + 1;
}
}

int l = left;
ll sum_blocks_lm1 = (l > 1) ? sum_blocks[l - 1] : 0;
ll p = k - sum_blocks_lm1;

// Compute the sum
ll term1 = (ll)(l) * C;
ll term2 = (ll)(n + 1) * ((l - 2) >= 0 ? ((l - 2) >= 1 ? sum_prefix[l - 2] : 0) : 0);
ll term3 = (l - 2) >= 0 ? ((l - 2) >= 1 ? sum_prefix_weight[l - 2] : 0) : 0;

// Handle sum_prefix and sum_prefix_weight for l-2
ll sum_prefix_lm2 = 0;
if (l > 2) {
sum_prefix_lm2 = sum_prefix[l - 2];
}

ll sum_prefix_weight_lm2 = 0;
if (l >= 2) {
sum_prefix_weight_lm2 = sum_prefix_weight[l - 2];
}

term2 -= (ll)(n + 1) * sum_prefix_lm2;
term3 = sum_prefix_weight_lm2;

// Compute (p + l)*(prefix[l+p-1] - prefix[l-1]) - (prefix_weight[l+p-1] - prefix_weight[l-1])
ll prefix_sum = prefix[l + p - 1] - prefix[l - 1];
ll prefix_weight_sum = prefix_weight[l + p - 1] - prefix_weight[l - 1];
ll term4 = (p + (ll)l) * prefix_sum;
ll term5 = prefix_weight_sum;
return ((ll)(l - 1) * C - (ll)(n + 1) * sum_prefix_lm2 + sum_prefix_weight_lm2) + (term4 - term5);
};

// Read queries
int q;
cin >> q;
while (q--) {
ll li, ri;
cin >> li >> ri;
ll sum_ri = get_sum_b(ri);
ll sum_li_1 = get_sum_b(li - 1);
cout << (sum_ri - sum_li_1) << "\n";
}
return 0;
}