You are on page 1of 5

Experiment 10

Student Name: Srishti Jaitly


UID: 20BCS2957
Branch: CSE
Section/Group: 613A
Semester: 5th
Subject Name: Competitive Coding-I
Subject Code: 20CSP-314

1. Aim/Overview of the practical:

Marc's Cakewalk
2. Task to be done/ Which logistics used:
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and Marc can walk a distance
to expend those calories. If Marc has eaten cupcakes so far, after eating a cupcake with calories he must walk at
least miles to maintain his weight.
consumption. In this case, our minimum miles is calculated as .
Given the individual calorie counts for each of the cupcakes, determine the minimum number of miles Marc
must walk to maintain his weight. Note that he can eat the cupcakes in any order.
Function Description
Complete the marcsCakewalk function in the editor below.
marcsCakewalk has the following parameter(s):
int calorie[n]: the calorie counts for each cupcake
Returns
long: the minimum miles necessary
Input Format
The first line contains an integer , the number of cupcakes in .
The second line contains space-separated integers, . .
3. Steps for experiment/practical/Code:
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii;
typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }

int main() {
int n;
while (~scanf("%d", &n)) {
vector<int> c(n);
for (int i = 0; i < n; ++ i)
scanf("%d", &c[i]);
sort(c.begin(), c.end());
ll ans = 0;
rep(i, n)
ans += (ll)c[i] << (n - 1 - i);
printf("%lld\n", ans);
}
return 0;
}
4. Output:
1. Aim/Overview of the practical:
Grid Challenge
2. Task to be done/ Which logistics used:
Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically,
ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they
are or NO if they are not.
Example
The grid is illustrated below.
abc
ade
efg
The rows are already in alphabetical order. The columns a a e, b d f and c e g are also in alphabetical order, so
the answer would be YES. Only elements within the same row can be rearranged. They cannot be moved to a
different row.
Function Description
Complete the gridChallenge function in the editor below.
gridChallenge has the following parameter(s):
string grid[n]: an array of strings
Returns
string: either YES or NO
Input Format
The first line contains , the number of testcases.
Each of the next sets of lines are described as follows:
- The first line contains , the number of rows and columns in the grid.
- The next lines contains a string of length
Output Format
For each test case, on a separate line print YES if it is possible to rearrange the grid alphabetically ascending in
both its rows and columns, or NO otherwise.
3. Steps for experiment/practical/Code:
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <iterator>
#include <bitset>

using namespace std;


typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)(c).size())
#define ten(x) ((int)1e##x)
#define tenll(x) ((ll)1e##x)
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
void solve(){
int n; cin >> n;
vector<string> v(n);
FOR(i, n) cin >> v[i];
FOR(i, n) sort(v[i].begin(), v[i].end());
bool b = true;
FOR(i, n){
string x;
FOR(j, n) x.push_back(v[j][i]);
string y = x;
sort(y.begin(), y.end());
if (x != y) {
b = false;
}
}
puts(b ? "YES" : "NO");
}

int main(){
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
4. Output:

You might also like