Skip to content

Commit 49140e2

Browse files
committed
CF Update
1 parent b3777c6 commit 49140e2

File tree

6 files changed

+238
-0
lines changed

6 files changed

+238
-0
lines changed

Codeforces/1200/187-2-B.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
int main(int argc, char const *argv[])
7+
{
8+
int n, m;
9+
cin >> n >> m;
10+
vector <int> a(n + 1);
11+
for(int i = 1 ; i <= n ; ++i) cin >> a[i];
12+
int increment = 0;
13+
for(int i = 1 ; i <= m ; ++i){
14+
int t; cin >> t;
15+
if(t == 1){
16+
int v, x;
17+
cin >> v >> x;
18+
a[v] = x - increment;
19+
} else if(t == 2){
20+
int y;
21+
cin >> y;
22+
increment += y;
23+
} else{
24+
int q;
25+
cin >> q;
26+
cout << a[q] + increment << '\n';
27+
}
28+
}
29+
// vector <int> last_updated(n + 1, 0);
30+
// vector <int> increments(m + 1, 0);
31+
// for(int i = 1 ; i <= m ; ++i){
32+
// increments[i] += increments[i - 1];
33+
// int t;
34+
// cin >> t;
35+
// if(t == 1){
36+
// int v, x;
37+
// cin >> v >> x;
38+
// a[v] = x;
39+
// last_updated[v] = i;
40+
// } else if(t == 2){
41+
// int y;
42+
// cin >> y;
43+
// increments[i] += y;
44+
// } else{
45+
// int q;
46+
// cin >> q;
47+
// cout << a[q] + increments[i] - increments[last_updated[q]] << '\n';
48+
// }
49+
// }
50+
return 0;
51+
}

Codeforces/1300/263-2-B.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <functional>
5+
6+
using namespace std;
7+
8+
int main(int argc, char const *argv[])
9+
{
10+
int n, k;
11+
cin >> n >> k;
12+
string input;
13+
cin >> input;
14+
vector <int> cards(26);
15+
for(int i = 0 ; i < input.length() ; ++i){
16+
int _id = input[i] - 'A';
17+
++cards[_id];
18+
}
19+
sort(cards.begin(), cards.end(), greater<int>());
20+
long long answer = 0;
21+
for(int i = 0 ; i < 26 && k != 0 ; ++i){
22+
int can = min(cards[i], k);
23+
if(can <= cards[i]){ // if I can't take more than what cards[i] has, then double the quantity
24+
answer += 1LL * can * can;
25+
} else{ // I can take more than what cards[i] has
26+
answer += can;
27+
}
28+
k -= can;
29+
}
30+
cout << answer << '\n';
31+
return 0;
32+
}

Codeforces/1300/E4-2-A.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main(int argc, char const *argv[])
6+
{
7+
int n, p, q;
8+
cin >> n >> p >> q;
9+
string s;
10+
cin >> s;
11+
// find non-negative solutions to px + qy = n
12+
// Complexity: O(n^2)
13+
int x = -1, y = -1;
14+
// for(int i = 0 ; i <= n ; ++i){
15+
// for(int j = 0 ; j <= n ; ++j){
16+
// if(p * i + q * j == n){
17+
// x = i;
18+
// y = j;
19+
// break;
20+
// }
21+
// }
22+
// }
23+
// Complexity: O(n)
24+
for(int i = 0 ; i <= n / p ; ++i){ // iterate all possible values for x
25+
// check if (n - p * i) % q == 0
26+
if((n - p * i) % q == 0){
27+
x = i;
28+
y = (n - p * i) / q;
29+
break;
30+
}
31+
}
32+
if(x == -1 && y == -1){
33+
cout << "-1\n";
34+
} else{
35+
//cout << "x = " << x << " y = " << y << '\n';
36+
cout << x + y << '\n';
37+
int index = 0;
38+
while(x--){
39+
for(int i = 0 ; i < p ; ++i){
40+
cout << s[index++];
41+
}
42+
cout << '\n';
43+
}
44+
while(y--){
45+
for(int i = 0 ; i < q ; ++i){
46+
cout << s[index++];
47+
}
48+
cout << '\n';
49+
}
50+
}
51+
return 0;
52+
}

Codeforces/1400/187-2-A.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <map>
4+
5+
using namespace std;
6+
7+
struct Bottle{
8+
int self, other;
9+
};
10+
11+
int main(int argc, char const *argv[])
12+
{
13+
// O(nlogn) solution
14+
int n;
15+
cin >> n;
16+
vector <Bottle> bottles(n);
17+
vector <int> count(1000 + 1, 0);
18+
map <pair<int,int>, int> duplicate;
19+
for(int i = 0 ; i < n ; ++i){
20+
cin >> bottles[i].self >> bottles[i].other;
21+
++count[bottles[i].self];
22+
if(bottles[i].self == bottles[i].other){
23+
++duplicate[{bottles[i].self, bottles[i].other}];
24+
}
25+
}
26+
for(int i = 0 ; i < n ; ++i){
27+
if(count[bottles[i].other] == 0) continue;
28+
if(bottles[i].self != bottles[i].other){
29+
count[bottles[i].other] = 0; // if a_i != b_i, then a_i can open every bottle of brand b_i
30+
} else if(duplicate[{bottles[i].self, bottles[i].other}] > 1){
31+
count[bottles[i].other] = 0; // a_i == b_i, this can only be opened if there exists some other pair (a_i', b_i') such that (a_i' == a_i) && (b_i' == b_i) [a_i' != a_i has been handled above]
32+
} else{
33+
count[bottles[i].other] = 1; // otherwise it can open every bottle of b_i except itself
34+
}
35+
}
36+
int unopened = 0;
37+
for(int i = 1 ; i < count.size() ; ++i){
38+
unopened += count[i];
39+
}
40+
cout << unopened << '\n';
41+
return 0;
42+
}

Codeforces/665-2-B.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
4+
using namespace std;
5+
6+
int main(int argc, char const *argv[])
7+
{
8+
int t;
9+
cin >> t;
10+
while(t--){
11+
int x1, y1, z1, x2, y2, z2;
12+
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
13+
int sum = 0;
14+
int q = min(z1, y2);
15+
sum += q * 2;
16+
z1 -= q;
17+
y2 -= q;
18+
z2 -= min(z1, z2);
19+
z2 -= min(x1, z2);
20+
y1 -= min(y1, x2);
21+
y1 -= min(y1, y2);
22+
sum -= z2 * 2;
23+
cout << sum << '\n';
24+
}
25+
return 0;
26+
}

Codeforces/665-2-C.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <limits>
5+
6+
using namespace std;
7+
8+
int main(int argc, char const *argv[])
9+
{
10+
int t;
11+
cin >> t;
12+
while(t--){
13+
int n;
14+
cin >> n;
15+
vector <int> a(n), sorted(n);
16+
int minimum = numeric_limits<int>::max();
17+
for(int i = 0 ; i < n ; ++i){
18+
cin >> a[i];
19+
sorted[i] = a[i];
20+
minimum = min(minimum, a[i]);
21+
}
22+
sort(sorted.begin(), sorted.end());
23+
bool possible = true;
24+
for(int i = 0 ; i < n ; ++i){
25+
if(sorted[i] == a[i]) continue;
26+
int g = __gcd(minimum, a[i]);
27+
if(g != minimum){
28+
possible = false;
29+
break;
30+
}
31+
}
32+
cout << (possible ? "YES" : "NO") << '\n';
33+
}
34+
return 0;
35+
}

0 commit comments

Comments
 (0)