Skip to content

Commit 851fb08

Browse files
Add files via upload
1 parent d579afc commit 851fb08

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Find the frequency of each alphabet in each range.
2+
The i-th alphabet contributes i x sum(i) to the whole answer
3+
4+
-----
5+
6+
int main()
7+
{
8+
int length, no_of_queries;
9+
string S;
10+
cin >> length >> no_of_queries >> S;
11+
12+
const int NO_OF_ALPHABETS = 26;
13+
vector <vector <int> > frequency(NO_OF_ALPHABETS, vector <int> (length + 1, 0));
14+
for(int i = 1; i <= length; i++)
15+
{
16+
for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++)
17+
{
18+
frequency[alpha][i] = frequency[alpha][i - 1];
19+
}
20+
frequency[S[i - 1] - 'a'][i]++;
21+
}
22+
23+
for(int i = 1; i <= no_of_queries; i++)
24+
{
25+
int left, right;
26+
cin >> left >> right;
27+
28+
long long length = 0;
29+
for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++)
30+
{
31+
long long frequency_here = frequency[alpha][right] - frequency[alpha][left - 1];
32+
33+
length += (alpha + 1)*frequency_here;
34+
}
35+
36+
cout << length << "\n";
37+
}
38+
39+
return 0;
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Sort the items in descending order of B.
2+
Each item costs the same so we want to buy as many items for cost 1 as possible.
3+
4+
Go through the items one by one.
5+
6+
Have 2 pointers L and R
7+
L points to the first available element in the prefix and R to the suffix.
8+
9+
At each point, check if
10+
11+
1. We have bought enough items to avail the discount at R and buy item R at a discount
12+
2. If not, is it possible to partially buy the items at L and then use the discount for R
13+
3. If not, buy L completely and increment L
14+
15+
-----
16+
17+
int sort_by_second(pair <long long, long long> &P, pair <long long, long long> &Q)
18+
{
19+
if(P.second == Q.second)
20+
{
21+
return (P.first > Q.first);
22+
}
23+
24+
return (P.second > Q.second);
25+
}
26+
27+
int main()
28+
{
29+
int no_of_elements;
30+
cin >> no_of_elements;
31+
32+
vector <pair <long long, long long> > A(no_of_elements + 1);
33+
for(int i = 1; i <= no_of_elements; i++)
34+
{
35+
cin >> A[i].first >> A[i].second;
36+
}
37+
38+
sort(A.begin() + 1, A.end(), sort_by_second);
39+
/*for(int i = 1; i <= no_of_elements; i++)
40+
{
41+
cout << A[i].first << " " << A[i].second << "\n";
42+
}*/
43+
44+
long long total = 0, cost = 0;
45+
for(int left = 1, right = no_of_elements; left <= right; )
46+
{
47+
if(total >= A[right].second)
48+
{
49+
cost += A[right].first;
50+
total += A[right].first; //cout << "Buying R = " << right << " Cost = " << cost << " Total = " << total << "\n";
51+
52+
right--;
53+
continue;
54+
}
55+
56+
if(total + A[left].first < A[right].second)
57+
{
58+
cost += 2*A[left].first;
59+
total += A[left].first; //cout << "Buying L = " << left << " Cost = " << cost << " Total = " << total << "\n";
60+
61+
left++;
62+
continue;
63+
}
64+
65+
long long remaining_2 = A[right].second - total;
66+
67+
cost += 2*remaining_2;
68+
total += remaining_2; //cout << "Buying L = " << left << " Cost = " << cost << " Total = " << total;
69+
70+
A[left].first -= remaining_2; //cout << " New Left = " << A[left].first << "\n";
71+
}
72+
73+
cout << cost << "\n";
74+
return 0;
75+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Keep track of all differences that are > x
2+
3+
Put them in a priority queue and work on the smallest difference at each time.
4+
5+
In order to split an integer N into parts such that each of them is <= x, we need N/x - 1 cuts
6+
7+
Check if there are enough operations to cut the current part and then cut them accordingly.
8+
9+
It is similar to the problem Carrots for Rabbits
10+
-----
11+
12+
long long ceil(long long numerator, long long denominator)
13+
{
14+
return (numerator/denominator) + (numerator%denominator != 0);
15+
}
16+
17+
int main()
18+
{
19+
int no_of_elements;
20+
long long new_additions, difference;
21+
cin >> no_of_elements >> new_additions >> difference;
22+
23+
vector <long long> A(no_of_elements + 1);
24+
for(int i = 1; i <= no_of_elements; i++)
25+
{
26+
cin >> A[i];
27+
}
28+
29+
sort(all(A));
30+
31+
priority_queue <long long, vector <long long>, greater <long long> > PQ;
32+
for(int i = 2; i <= no_of_elements; i++)
33+
{
34+
if(A[i] - A[i - 1] > difference)
35+
{
36+
PQ.push(A[i] - A[i - 1]);
37+
}
38+
}
39+
40+
while(new_additions > 0 && PQ.size() > 0)
41+
{
42+
long long current = PQ.top(); //cout << "Current = " << current << "\n";
43+
44+
long long required_new_additions = ceil(current,difference) - 1;
45+
46+
long long possible_additions = min(required_new_additions, new_additions);
47+
48+
if(possible_additions >= required_new_additions)
49+
{
50+
PQ.pop();
51+
}
52+
53+
new_additions -= possible_additions; //cout << "Operations = " << new_additions << "\n";
54+
}
55+
56+
int groups = PQ.size() + 1;
57+
cout << groups << "\n";
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)