# Daily Coding Challenge #36

Wing-Kam ・4 min read

Daily Coding Challenge (87 Part Series)

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

/*
CodeChef June Challenge 2020 Division 2 - Chef and Price Control
https://www.codechef.com/JUNE20B/problems/PRICECON
*/

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

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()
{
FAST_INP;
int t,n,p,k;
cin >> t;
while(t--){
cin >> n >> k;
int ans1=0,ans2=0;
for(int i=0;i<n;i++){
cin >> p;
// for each item i such that Pi>K,
// its price should be reduced from Pi to K.
if(p>k) ans2+=k;
else ans2+=p;
// initial revenue
ans1+=p;
}
// lost revenue
cout << ans1-ans2 << "\n";
}
return 0;
}



/*
CodeChef June Challenge 2020 Division 2 - Chef and String
https://www.codechef.com/JUNE20B/problems/XYSTR
*/

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

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()
{
FAST_INP;
int t;
string s;
cin >> t;
while(t--){
cin >> s;
int ans=0;
// traverse each person starting from the second person
for(int i=1;i<s.size();i++){
if(
(s[i]=='x'&&s[i-1]=='y') || // yx can be a pair
(s[i]=='y'&&s[i-1]=='x')    // xy can be a pair
) {
ans++;
// since they are paired, increase 1 to avoid checking again
i++;
}
}
cout << ans << "\n";
}
return 0;
}



/*
CodeChef June Challenge 2020 Division 2 - Chef and Icecream
https://www.codechef.com/JUNE20B/problems/CHFICRM
*/

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

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

bool solve(int n){
vector<int> a(n);
// store how many coins the Chef has
int c5=0,c10=0,c15=0;
for(int i=0;i<n;i++) cin >> a[i];
for(int i=0;i<n;i++){
if(a[i]==5) {
// no change is required
c5++;
} else if(a[i]==10) {
// need to give back Rs. 5 coin
// if no Rs .5, we can end it and the ans is NO
if(c5==0) {
return false;
}
// update the coins
c5--;
c10++;
}
else if(a[i]==15) {
// I got WA on my first try as I thought "exact change" means giving back Rs. 10 only in this case...
// --------------------------------------------------------------------------------------------------------
// The Chef needs to give back Rs. 10 coin which can further break into 2 cases
// he either gives back 2 x Rs .5 or 1 x Rs .10
if(c10==0&&c5<2){
return false;
}
// if Chef doesn't have Rs .10 but 2 x Rs .5, deduct 2 x Rs .5
if(c10==0) c5-=2;
// else deduct 1 x Rs .10
else c10--;
// receive Rs .15
c15++;
}
}
return true;
}

int main()
{
FAST_INP;
int t,n;
cin >> t;
while(t--){
cin >> n;
if(solve(n)) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}



/*
CodeChef June Challenge 2020 Division 2 - Even Matrix
https://www.codechef.com/JUNE20B/problems/EVENM
*/

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

#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()
{
FAST_INP;
int t,n;
cin >> t;

while(t--){
cin >> n;
// the pattern should be easy to observe
if(n%2==0){
// if it is even,
// print in ascending order for every odd row
// print in descending order for every even row
// ---------------------------------------------
// 1 2 3 4 5 6 7 8 9 10
// 20 19 18 17 16 15 14 13 12 11
// 21 22 23 24 25 26 27 28 29 30
// 40 39 38 37 36 35 34 33 32 31
// 41 42 43 44 45 46 47 48 49 50
// 60 59 58 57 56 55 54 53 52 51
// 61 62 63 64 65 66 67 68 69 70
// 80 79 78 77 76 75 74 73 72 71
// 81 82 83 84 85 86 87 88 89 90
// 100 99 98 97 96 95 94 93 92 91
for(int i=1;i<=n;i++){
if(i%2==0){
// even row
for(int j=0;j<n;j++) {
cout << i*n-j << " ";
}

} else {
// odd row
for(int j=1;j<=n;j++) {
cout << (i-1)*n+j  << " ";
}
}
cout << "\n";
}
} else {
// if it is odd, just print all numbers in ascending order
// ---------------------------------------------
// 1 2 3 4 5 6 7 8 9 10 11
// 12 13 14 15 16 17 18 19 20 21 22
// 23 24 25 26 27 28 29 30 31 32 33
// 34 35 36 37 38 39 40 41 42 43 44
// 45 46 47 48 49 50 51 52 53 54 55
// 56 57 58 59 60 61 62 63 64 65 66
// 67 68 69 70 71 72 73 74 75 76 77
// 78 79 80 81 82 83 84 85 86 87 88
// 89 90 91 92 93 94 95 96 97 98 99
// 100 101 102 103 104 105 106 107 108 109 110
// 111 112 113 114 115 116 117 118 119 120 121
for(int i=1;i<=n*n;i++){
cout << i << " ";
if(i%n==0) cout << "\n";
}
}
}
return 0;
}



The source code is available in corresponding repo below. Star and watch for timely updates!

## wingkwong / atcoder

### 🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Daily Coding Challenge (87 Part Series)

Posted on by:

### Wing-Kam

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

### Discussion

Nice, learning C++ seriously, this gave me a few new ways to tackle writing in it.