1316A - Grade Allocation
Author: gaurav172
Development: gaurav172, firebolt, night_fury208
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll test;
cin>>test;
while(test--)
{
ll n,m;
cin>>n>>m;
ll s=0;
for(ll i=1;i<=n;i++)
{
ll x;
cin>>x;
s=s+x;
}
cout<<min(s,m)<<"\n";
}
return 0;
}
1316B - String Modification
Author: preet_t
Development: preet_t, shaanknight, night_fury208
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
string modified(string& s, int n, int k) {
string result_prefix = s.substr(k — 1, n — k + 1);
string result_suffix = s.substr(0, k — 1);
if (n % 2 == k % 2)
reverse(result_suffix.begin(), result_suffix.end());
return result_prefix + result_suffix;
}
int main () {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
string s, best_s, temp;
int t, n, best_k;
cin >> t;
while (t--) {
cin >> n >> s;
best_s = modified(s, n, 1);
best_k = 1;
for (int k = 2; k <= n; ++k) {
temp = modified(s, n, k);
if (temp < best_s) {
best_s = temp;
best_k = k;
}
}
cout << best_s << '\n' << best_k << '\n';
}
return 0;
}
1316C - Primitive Primes
Author: lazyneuron
Development: lazyneuron, shaanknight,firebolt
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll n,i,m,x,a,p,fir,sec;
cin >> n >> m >> p;
a=0;
for(i=0;i<n;i++)
{
cin >> x;
if(x%p && !a)
{
a=1;
fir=i;
}
}
a=0;
for(i=0;i<m;i++)
{
cin >> x;
if(x%p && !a)
{
a=1;
sec=i;
}
}
cout << fir+sec << endl;
return 0;
}
1316D - Nash Matrix
Author: shaanknight
Development: shaanknight, riz_1_, coder_h,Altitude
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int M = (1<<10)+5;
char mat[M][M];
int x[M][M],y[M][M];
int n;
bool connect(int p,int q,int r,int s,char d1,char d2)
{
if(x[r][s] == -1)
{
mat[p][q] = d1;
if(mat[r][s] == '\0') mat[r][s] = d2;
return 1;
}
else
return 0;
}
void dfs(int p,int q,char d)
{
if(mat[p][q] != '\0') return;
mat[p][q] = d;
if(x[p][q] == x[p+1][q] && y[p][q] == y[p+1][q])
dfs(p+1,q,'U');
if(x[p][q] == x[p-1][q] && y[p][q] == y[p-1][q])
dfs(p-1,q,'D');
if(x[p][q] == x[p][q+1] && y[p][q] == y[p][q+1])
dfs(p,q+1,'L');
if(x[p][q] == x[p][q-1] && y[p][q] == y[p][q-1])
dfs(p,q-1,'R');
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i,j;
cin >> n;
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
cin >> x[i][j] >> y[i][j];
}
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
if(x[i][j] == -1)
{
bool res = (mat[i][j] != '\0');
if(res == 0) res = connect(i,j,i+1,j,'D','U');
if(res == 0) res = connect(i,j,i,j+1,'R','L');
if(res == 0) res = connect(i,j,i-1,j,'U','D');
if(res == 0) res = connect(i,j,i,j-1,'L','R');
if(res == 0)
{
cout << "INVALID" << "\n";
return 0;
}
}
else if(x[i][j] == i && y[i][j] == j)
dfs(i,j,'X');
}
}
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
if(mat[i][j] == '\0')
{
cout << "INVALID" << "\n";
return 0;
}
}
}
cout << "VALID" << "\n";
for(i=1;i<=n;++i)
{
for(j=1;j<=n;++j)
{
cout << mat[i][j];
}
cout << "\n";
}
return 0;
}
1316E - Team Building
Author: gaurav172
Development: gaurav172, shaanknight, coder_h, madlad
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll M=1e5+5;
ll dp[M][(1<<7)+1],skill[M][7];
ll ind[M],a[M];
bool cmp(ll p,ll q)
{
return a[p]>a[q];
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll n,p,k;
cin>>n>>p>>k;
memset(dp,-1,sizeof(dp));
for(ll i=1;i<=n;i++)
cin>>a[i];
for(ll i=1;i<=n;i++)
for(ll j=0;j<p;j++)
cin>>skill[i][j];
for(ll i=1;i<=n;i++)
ind[i]=i;
sort(ind+1,ind+n+1,cmp);
dp[0][0]=0;
for(ll i=1;i<=n;i++)
{
ll x=ind[i];
for(ll mask=0;mask<(1<<p);mask++)
{
ll ct=0;
for(ll j=0;j<p;j++)
if((mask&(1<<j)))
ct++;
ll z=(i-1)-ct;
if(z<k)
{
if(dp[i-1][mask]!=-1)
dp[i][mask]=dp[i-1][mask]+a[x];
}
else
{
if(dp[i-1][mask]!=-1)
dp[i][mask]=dp[i-1][mask];
}
for(ll j=0;j<p;j++)
{
if((mask&(1<<j)) && dp[i-1][(mask^(1<<j))]!=-1)
{
ll z=dp[i-1][(mask^(1<<j))]+skill[x][j];
if(z>dp[i][mask])
dp[i][mask]=z;
}
}
}
}
cout<<dp[n][(1<<p)-1]<<"\n";
return 0;
}
1316F - Battalion Strength
Editorial will pe added soon.