0% found this document useful (0 votes)
7 views

binarysearch

The document contains multiple C++ code snippets from various Codeforces problems. Each snippet includes the problem's logic, input handling, and output formatting. The problems range from sorting and searching algorithms to mathematical computations and string manipulations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

binarysearch

The document contains multiple C++ code snippets from various Codeforces problems. Each snippet includes the problem's logic, input handling, and output formatting. The problems range from sorting and searching algorithms to mathematical computations and string manipulations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,l;
cin >> n >> l;
ll a[n+2];
for (ll i=1;i<=n;i++) cin >> a[i];
sort(a+1,a+n+1);
double Max=max(a[1],l-a[n]);
for (ll i=2;i<=n;i++)
{
Max=max(Max,double(a[i]-a[i-1])/2);
}
cout << setprecision(10) << fixed << Max;
}
https://codeforces.com/contest/492/problem/B

2
1. #include <iostream>
2. using namespace std;
3.
4. const int MAXN = 11;
5.
6. int check(int x, int d) {
7. int res = 0;
8. for (int i = 1;i <= x;i ++) res += 5 * i;
9. return res > d;
10. }
11.
12. int main() {
13. int n, k;
14. cin >> n >> k;
15. int d = 240 - k;
16.
17. int l = 0, r = n;
18. while ( l < r ) {
19. int mid = (l + r + 1) >> 1;
20. if ( check(mid, d) ) {
21. r = mid - 1;
22. }
23. else {
24. l = mid;
25. }
26. }
27.
28. cout << r;
29. return 0;
30. }
https://codeforces.com/contest/750/problem/A

3
1. #include <bits/stdc++.h>
2. using namespace std;
3. const int N=1e7;
4. int A[N+1];
5. bool is_prime[10000001];
6. void sangEratosthenes()
7. {
8. is_prime[0]=is_prime[1]=true;
9. for (long long i = 2; i*i < N; ++i)
10. if (!is_prime[i])
11. for (long long j=i*i; j<= N; j+=i)
12. is_prime[j] = true;
13. }
14. int main()
15. {
16. ios_base::sync_with_stdio(false);
17. cin.tie(NULL);
18. sangEratosthenes();
19. long long n;
20. cin>>n;
21. long long a[n+1];
22. for (long long i=1;i<=n;i++)
23. cin>>a[i];
24. for (long long i=1;i<=n;i++)
25. {
26. long double kq=sqrt(a[i]);
27. if (kq==(long long)kq)
28. {
29. if (is_prime[(long long)kq])
30. cout<<"NO"<<endl;
31. else
32. cout<<"YES"<<endl;
33. }
34. if (kq!=(long long)kq)
35. cout<<"NO"<<endl;
36. }
37.
38. return 0;
39. }
https://codeforces.com/contest/230/problem/B

4
1. #include <algorithm>
2. #include <cmath>
3. #include <cstdint>
4. #include <iostream>
5. #include <vector>
6.
7. using namespace std;
8.
9. using i32 = int32_t;
10. using i64 = int64_t;
11. using u64 = uint64_t;
12. using f64 = double;
13.
14. int main() {
15. ios::sync_with_stdio(false);
16. cin.tie(nullptr);
17.
18. i32 N;
19. cin >> N;
20.
21. vector<i32> A(N);
22. for (auto& a : A)
23. cin >> a;
24.
25. sort(A.begin(), A.end());
26.
27. i32 Q, q;
28. cin >> Q;
29. for (i32 i = 0; i < Q; ++i) {
30. cin >> q;
31. cout << (upper_bound(A.cbegin(), A.cend(), q) -
A.cbegin()) << endl;
32. }
33. }
https://codeforces.com/problemset/problem/706/B

5
1. #include<bits/stdc++.h>
2. using namespace std;
3. int main()
4. {
5. int t;
6. cin >>t;
7. while(t--)
8. {
9. long long n,k;
10. cin >>n>>k;
11. long long t=0;
12. long long a=k/(n-1);
13. long long b=k%(n-1);
14. t=a*n-1;
15. if(b>0)
16. t+=b+1;
17. cout <<t <<endl;
18. }
19. }
https://codeforces.com/problemset/problem/1352/C

1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. #define int long long
5. #define endl "\n"
6.
7. int gcd(int a,int b) {return b ? gcd(b,a%b) : a;}
8. int lcm(int a,int b) {return a*b/gcd(a,b);}
9.
const int mod=1e9+7;
signed main() {
10. int n,k; cin>>n>>k;
11. vector<int> arr(n);
12. for(auto &it:arr) cin>>it;
13. vector<int> pref(n);
14. pref[0]=arr[0];
15. for(int i=1;i<n;i++) pref[i]=pref[i-1]+arr[i];
16.
17. int ans=0;
18. int need=k;
19. for(int i=0;i<n;i++){
20. int it=lower_bound(pref.begin(),pref.end(),need)-
pref.begin();
21. int x;
22. if(it==n){
23. x=it-i;
24. }
25. else if(pref[it]==need){
26. x=it-i+1;
27. }
28. else {
29. it--;
30. x=it-i+1;
31. }
32. ans=max(ans,x);
33. need+=arr[i];
34. }
35.
36. cout<<ans<<endl;
37.
38.
39. return 0;
40. }

https://codeforces.com/problemset/problem/
279/B

7
1. #include <bits/stdc++.h>
2.
3. #define ll long long
4. #define pb push_back
5.
6. using namespace std;
7.
8. int main()
9. {
10. ios::sync_with_stdio(false);
11. cin.tie(0);
12.
13.
14. int n;
15. cin >> n;
16.
17. vector <int> v(n), u(n), l(n);
18. for (int i = 0; i < n; i++) cin >> v[i];
19.
20. int m;
21. cin >> m;
22.
23. l[0] = 1, u[0] = v[0];
24. for (int i = 1; i < n; i++) {
25. u[i] = u[i-1] + v[i];
26. l[i] = u[i] - v[i] + 1;
27. }
28.
29. int k;
30. for (int i = 0; i < m; i++) {
31. cin >> k;
32.
33. int l = 0, r = n-1, mid = (l+r)/2;
34.
35. while (l <= r) {
36. mid = l+ (r-l)/2;
37.
38. if (u[mid] > k)
39. r = mid-1;
40. else if (u[mid] < k)
41. l = mid+1;
42. else
43. break;
44. }
45.
46. if (u[mid] == k)
47. cout << mid+1 << endl;
48. else
49. cout << l+1 << endl;
50. }
51.
52. return 0;
53. }

https://codeforces.com/problemset/problem/
474/B

8
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
long long int a[n];
long long int sum=0;
for(int i=0;i<n;i++){
cin>>a[i];
sum+=a[i];
}
long long int pre[n];
pre[0]=a[0];
for(int i=1;i<n;i++){
pre[i]=pre[i-1]+a[i];
}
long long int cnt1=0,cnt2=0;
if(sum%3)cout<<0<<endl;
else{
long long int s=sum/3;
for(int i=0;i<n-1;i++){
if(2*s==pre[i])cnt2+=cnt1;
if(s==pre[i])cnt1++;
}
cout<<cnt2<<endl;
}
}
https://codeforces.com/problemset/problem/
466/C

9
1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. int main()
5. {
6. int t;
7. cin >> t;
8. while (t--)
9. {
10. int n, k;
11. cin >> n >> k;
12. int d = 1;
13. if (n > k)
14. {
15. if (n % k == 0)
16. d = n / k;
17. else
18. d = (n / k) + 1;
19. }
20. k *= d;
21. if (k % n == 0)
22. cout << k / n << endl;
23. else
24. cout << (k / n) + 1 << endl;
25. }
26. return 0;
27. }

https://codeforces.com/problemset/problem/
1476/A

10
1. #include <bits/stdc++.h>
2. using namespace std;
3. using ll = long long;
4.
5. const char nl = '\n';
6.
7. void solve() {
8. int n; cin >> n;
9. unordered_map<int, int> cnt;
10. int most = 0;
11. for (int i = 0; i < n; ++i) {
12. int a; cin >> a;
13. most = max(most, ++cnt[a]);
14. }
15.
16. if (most > cnt.size()) cout << cnt.size() << nl;
17. else if (most == cnt.size()) cout << cnt.size() - 1 << nl;
18. else cout << most << nl;
19. }
20.
21. int main() {
22. int t; cin >> t;
23. while (t--) solve();
24. }

https://codeforces.com/problemset/problem/
1335/C

11
#include <bits/stdc++.h>

#define int long long


#define MOD 1000000007
#define endl "\n"
#define all(s) (s).begin(), (s).end()

using namespace std;

bool isCube(int num){


int l = 1; int r = 10000;
int mid ;
while(l<=r){
mid = l + (r-l)/2;
int cube = mid*mid*mid;
if(cube == num){
return true;
}
else if (cube > num){
r = mid -1;
}
else if (cube < num){
l = mid+1;
}
}
return false;
}

signed main (){

ios_base::sync_with_stdio(false);cin.tie(0);cout.
tie(0);

int t;
cin>>t;
while(t--){
int n;
cin>>n;
bool check = 0;
for (int p=1;p*p*p<=n;p++){
int y = n - p*p*p;
if(isCube(y)){
check =1;
break;
}
}
if(check){
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}

https://codeforces.com/problemset/problem/
1490/C

12
#include<bits/stdc++.h>
using namespace std;

int main(){
int n,dem = 0,MAX;
string s;
cin>>n;
while(n--){
cin>>s;
for(int i=0;i<=s.size();i++)
if(s[i]=='L')
dem++;
else{
MAX=max(MAX,dem);
dem=0;
}
cout<<MAX+1<<endl;MAX=0;
}
return 0;
}

https://codeforces.com/contest/1324/
problem/C

You might also like