Skip to content

Commit 14839dc

Browse files
authored
Merge branch 'main' into main
2 parents 55be803 + 2ff955f commit 14839dc

5 files changed

Lines changed: 264 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Submission Link: https://codeforces.com/contest/2014/submission/356141020
3+
4+
Approach:
5+
1. If n <= 2, impossible to have more than half unhappy -> return -1
6+
2. Sort the wealth array
7+
3. Calculate current sum
8+
4. Find the median position (n//2)
9+
5. Use mathematical formula: For median person to be unhappy:
10+
(sum + X) / (2 * n) > a[median]
11+
Solving: X > 2 * n * a[median] - sum
12+
Therefore: X = 2 * n * a[median] - sum + 1
13+
14+
"""
15+
16+
def solve():
17+
n = int(input())
18+
a = list(map(int, input().split()))
19+
20+
if n <= 2:
21+
print(-1)
22+
return
23+
24+
a.sort()
25+
26+
total_sum = sum(a)
27+
28+
mid = n // 2
29+
30+
current_avg = total_sum / n
31+
unhappy_count = sum(1 for wealth in a if wealth < current_avg / 2)
32+
33+
if unhappy_count > n / 2:
34+
print(0)
35+
return
36+
37+
X = 2 * n * a[mid] - total_sum + 1
38+
39+
if X < 0:
40+
print(0)
41+
else:
42+
print(X)
43+
44+
t = int(input())
45+
for _ in range(t):
46+
solve()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Approach:
3+
For each power r (2 to 64):
4+
- Calculate k ≈ n^(1/r)
5+
- Check if 1 + k + k² + ... + k^r = n
6+
- Verify k-1, k, k+1 for floating point precision
7+
8+
Submission Link: https://codeforces.com/contest/1846/submission/356143831
9+
"""
10+
11+
def solve():
12+
n = int(input())
13+
14+
if n < 3:
15+
print("NO")
16+
return
17+
18+
for r in range(2, 65):
19+
k = int(n ** (1.0 / r))
20+
21+
for candidate_k in [max(2, k - 1), k, k + 1]:
22+
if candidate_k < 2:
23+
continue
24+
25+
total = 1
26+
power = 1
27+
28+
for i in range(r):
29+
power *= candidate_k
30+
total += power
31+
if total > n:
32+
break
33+
34+
if total == n:
35+
print("YES")
36+
return
37+
38+
if k < 2:
39+
break
40+
41+
print("NO")
42+
43+
t = int(input())
44+
for _ in range(t):
45+
solve()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Approach: Two Pointers approach
3+
Submission Link: https://codeforces.com/problemset/submission/1538/356144559
4+
"""
5+
6+
def solve_two_pointers():
7+
n, l, r = map(int, input().split())
8+
a = list(map(int, input().split()))
9+
10+
a.sort()
11+
count = 0
12+
13+
for i in range(n):
14+
min_sum = l - a[i]
15+
max_sum = r - a[i]
16+
17+
left = i + 1
18+
right = n - 1
19+
20+
lo, hi = i + 1, n
21+
while lo < hi:
22+
mid = (lo + hi) // 2
23+
if a[mid] < min_sum:
24+
lo = mid + 1
25+
else:
26+
hi = mid
27+
left_bound = lo
28+
29+
lo, hi = i + 1, n
30+
while lo < hi:
31+
mid = (lo + hi) // 2
32+
if a[mid] <= max_sum:
33+
lo = mid + 1
34+
else:
35+
hi = mid
36+
right_bound = lo - 1
37+
38+
if left_bound < n and right_bound >= i + 1 and left_bound <= right_bound:
39+
count += (right_bound - left_bound + 1)
40+
41+
print(count)
42+
43+
t = int(input())
44+
for _ in range(t):
45+
solve_two_pointers()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
26+
27+
// submission link :-
28+
//https://codeforces.com/contest/1538/submission/352943894
29+
// C_Number_of_Pairs.cpp
30+
31+
32+
33+
34+
35+
36+
void solve(){
37+
38+
//-------------INPUT-------------
39+
ll n;
40+
cin >> n;
41+
ll l,r;
42+
cin >> l >> r;
43+
vll v(n); input(v);
44+
map<ll,ll>mp1;
45+
map<ll,ll>mp2;
46+
map<ll,ll>mp;
47+
ll count=0;
48+
as(v);
49+
vll temp;
50+
51+
for(ll i=0;i<n;i++)
52+
{
53+
ll req1 = l-v[i];
54+
ll a,b;
55+
ll req2 = r-v[i];
56+
// apply lowerbound to find req1
57+
auto it = lower_bound(temp.begin(),temp.end(),req1);
58+
59+
// using binary search to find the req1 index
60+
61+
62+
if( it!=temp.end() && *it>=req1)
63+
{
64+
a = mp[*it];
65+
}
66+
else
67+
{
68+
a =-1;
69+
}
70+
71+
//cout << a << endl;
72+
73+
// if got a particular a then try to find b
74+
75+
if(a!=-1 && !temp.empty())
76+
{
77+
78+
auto it = lower_bound(temp.begin(),temp.end(),req2);
79+
//cout << req2 << " " << mp2[*it] << " " << *it << endl;
80+
//cout << mp1[*it];
81+
if(it == temp.end() && !temp.empty())
82+
{
83+
b = mp2[*(it-1)];
84+
//cout << "j";
85+
}
86+
else if(*it>req2)
87+
{
88+
b = mp[*it]-1;
89+
//cout << "l";
90+
}
91+
else{
92+
b= mp2[*it];
93+
//cout << "lf";
94+
}
95+
if(b>=a)
96+
{
97+
// this will be total pairs
98+
count = count+b-a+1;
99+
}
100+
}
101+
//cout << "b" << " " << b << endl;
102+
103+
temp.push_back(v[i]);
104+
if(mp[v[i]]==0 && v[i]!=v[0])
105+
{
106+
107+
mp[v[i]] = i;
108+
}
109+
mp2[v[i]] = i;
110+
}
111+
112+
cout << count << endl;
113+
114+
115+
116+
//time complexity o(nlogn)
117+
//-------------CODE--------------
118+
119+
120+
121+
}
122+
123+
124+
int main(){
125+
int tt; cin >> tt; while(tt--)
126+
{solve();};
127+
}

contributers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@
8383
| YOGESH M | yogesh4216 | CIT Chennai |
8484
| Krishna Sikheriya | Krishna200608 | IIIT Allahabad |
8585
| Sarthak Tewari | strongFingers2 | IIIT Allahabad |
86+
| Gayatri Duse | Gayatrii4506 | KKWIEER |
8687
<!-- ADD ABOVE THIS -->
8788
<!-- example | Korvac | Betty | Reyansh College | -->

0 commit comments

Comments
 (0)