1348C - Phoenix and Distribution - CodeForces Solution


constructive algorithms greedy sortings strings *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
 
#define N 10000000
#define mod 1000000007
#define pii pair<ll,ll>
#define ll long long int
#define lli unsigned long long int
 
 
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<" "; _print(x);cerr<<endl;
#else 
#define debug(x)
#endif
 
 
#define pv(v){for(auto i:v)cout<<i<<" ";cout<<endl;}
#define fbo find_by_order
#define ook order_of_key
#define read(v){for(auto &i:v)cin>>i;}
using namespace std;
 
void _print(int x){cerr<<x;}
void _print(bool x){cerr<<x;}
void _print(char x){cerr<<x;}
void _print(ll x){cerr<<x;}
void _print(double x){cerr<<x;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
 
bool sieve[N+1];
 
void createSieve()
{
	for(int i=2;i<=N;i++)sieve[i] = true;
	for(int i=2;i*i<=N;i++)
	{
		if(sieve[i])
		{
			for(int j=i*i;j<=N;j+=i)
			{
				sieve[j] = false;
			}
		}
	}
}
vector<ll>primes;
void createPrimes()
{
	for(int i=2;i<=N;i++)
	{
		if(sieve[i])primes.push_back(i);
	}
	return;
}
int power(long long x, unsigned int y, int p)
{
    int res = 1;     // Initialize result
 
    x = x % p; // Update x if it is more than or
                // equal to p
  
    if (x == 0) return 0; // In case x is divisible by p;
 
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
 
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
bool isPalindromee(string str)
{
	int start = 0;
	int end = str.length()-1;
	while(start<end)
	{
		if(str[start++]!=str[end--])return false;
	}
	return true;
}
vector<int> prefix_kmp(string str)
{
	int n = str.size();
	vector<int>pi(n,0);
	for(int i=1;i<n;i++)
	{
		int j = pi[i-1];
		while(j>0 && str[i]!=str[j])
		{
			j = pi[j-1];
		}
		if(str[i]==str[j])j++;
		pi[i] = j;
	}
	return pi;
}
vector<int> rabin_karp_patternMatch(string s,string t)
{
	vector<int>occurences;
	const int p = 31; 
    int S = s.size(), T = t.size();
 
    vector<long long> p_pow(max(S, T)); 
    p_pow[0] = 1; 
    for (int i = 1; i < (int)p_pow.size(); i++) 
        p_pow[i] = (p_pow[i-1] * p) % mod;
 
    vector<long long> h(T + 1, 0); 
    for (int i = 0; i < T; i++)
        h[i+1] = (h[i] + (t[i] - 'a' + 1) * p_pow[i]) % mod; 
    long long h_s = 0; 
    for (int i = 0; i < S; i++) 
        h_s = (h_s + (s[i] - 'a' + 1) * p_pow[i]) % mod; 
    for (int i = 0; i + S - 1 < T; i++) { 
        long long cur_h = (h[i+S] + mod - h[i]) % mod; 
        if (cur_h == h_s * p_pow[i] % mod)
            occurences.push_back(i);
    }
    return occurences;
}
vector<int> z_function(string str)
{
	int n = str.size();
	vector<int>z(n,0);
	for(int i=1,l=0,r=0;i<n;i++)
	{
		if(i<=r)z[i] = min(z[i-l],r-i+1);
		while(i+z[i]<n && str[z[i]]==str[i+z[i]])++z[i];
		if(i+z[i]-1>r)
		{
			l = i;
			r = i+z[i]-1;
		}
	}
	return z;
}
ll compute_hash(string str)
{
	ll hashValue = 0;
	ll p = 31;
	int n = str.size();
	ll pow = 1;
	for(ll i=0;i<n;i++)
	{
		hashValue+=((str[i]-'a'+1)*pow)%mod;
		pow = (pow*p)%mod;
	}
	return hashValue;
}
class SegmentTree{
public:
    vector<int>seg;
	public: SegmentTree(int n)
	{
		seg.resize(4*n+1);
	}

	void BuildSegmentTree(int index,int low,int high,vector<int>&v)
	{
		if(low==high)
		{
			seg[index] = v[low];
			return;
		}
		int mid = (low+high)/2;
		BuildSegmentTree(2*index+1,low,mid,v);
		BuildSegmentTree(2*index+2,mid+1,high,v);
		seg[index] = min(seg[2*index+1],seg[2*index+2]);
	}
	int query(int index,int low,int high,int l,int r)
	{
		if(r<low || high<l)return INT_MAX; // no overlap
		if(low>=l && high<=r)return seg[index]; // partial Overlap
		int mid = (low+high)/2; 
		int left = query(2*index+1,low,mid,l,r);
		int right = query(2*index+2,mid+1,high,l,r);
		return min(left,right);
	}
	void update(int index,int low,int high,int i,int val)
	{
		if(low==high)
		{
			seg[index] = val;
			return;
		}
		int mid = (low+(high-low)/2);
		if(i<=mid)update(2*index+1,low,mid,i,val);
		else update(2*index+2,mid+1,high,i,val);
		seg[index] = min(seg[2*index+1],seg[2*index+2]);
	}
};
class SegmentTreeWithLazy{
	public:
	vector<int>seg,lazy;
	SegmentTreeWithLazy(int n)
	{
		seg.resize(4*n);
		lazy.resize(4*n);
	}
    public:
    	void build(int index,int low,int high,vector<int>&v)
    	{
    		if(low==high)
    		{
    			seg[index] = v[low];
    			return;
    		}
    		int mid = (low+(high-low)/2);
    		build(2*index+1,low,mid,v);
    		build(2*index+2,mid+1,high,v);
    		seg[index] = seg[2*index+1]+seg[2*index+2];
    	}
    public:
    void update(int index,int low,int high,int l,int r,int val){
    	if (lazy[index] != 0)
    	{
	        seg[index] += (high-low+1)*lazy[index];
	        if (low != high)
	        {
	            lazy[index*2 + 1]   += lazy[index];
	            lazy[index*2 + 2]   += lazy[index];
	        }
	        lazy[index] = 0;
        }
  
    if (low>high || low>r || high<l)
        return ;
    if (low>=l && high<=r)
	    {
	        seg[index] += (high-low+1)*val;
	        if (high != low)
	        {
	            lazy[index*2 + 1]   += val;
	            lazy[index*2 + 2]   += val;
	        }
	        return;
	    }
	    int mid = (high+low)/2;
	    update(index*2+1,low, mid, l,r,val);
	    update(index*2+2,mid+1,high,l,r,val);
	    seg[index] = seg[index*2+1] + seg[index*2+2];
    }
    public:
    	int query(int index,int low,int high,int l,int r)
    	{
    		// update if the updates are remaining
    		if(lazy[index]!=0)
    		{
    			seg[index]+=(high-low+1)*lazy[index];
    			if(low!=high)
    			{
    				lazy[2*index+1]+=lazy[index];
    				lazy[2*index+2]+=lazy[index];
    			}
    			lazy[index] = 0;
    		}
    		if(high<l || r<low)return 0;
    		if(low>=l && high<=r)return seg[index];
    		int mid = (low+(high-low)/2);
    		int left = query(2*index+1,low,mid,l,r);
    		int right = query(2*index+2,mid+1,high,l,r);
    		return left+right;
    	}
};
ll gcd(ll a, ll b)
{
    if (!a)
        return b;
    return gcd(b % a, a);
}
ll lcm(ll a, ll b){
	ll y = gcd(a,b);
    return a / y * b;
}
	void BuildSegmentTree(int index,int low,int high,vector<int>&v,bool XORTurn,vector<int>&seg)
	{
		if(low==high)
		{
			seg[index] = v[low];
			return;
		}
		int mid = (low+high)/2;
		BuildSegmentTree(2*index+1,low,mid,v,!XORTurn,seg);
		BuildSegmentTree(2*index+2,mid+1,high,v,!XORTurn,seg);
		if(XORTurn)seg[index] = seg[2*index+1]^seg[2*index+2];
		else seg[index] = seg[2*index+1]|seg[2*index+2];
		return;
}
bool isValid(int row,int col,int n)
{
	if(row>=0 && row<n && col>=0 && col<n)return true;
	return false;
}
void createSieve(vector<int>&sieve)
    {
        for(int i=2;i*i<=300000;i++)
        {
            if(sieve[i]==-1)
            {
                for(int j=i*i;j<=300000;j+=i)
                {
                    sieve[j] = i;
                }
            }
        }
    }
 
void solve(int g)
{
	int n,k;
	cin>>n>>k;
	string str;
	cin>>str;
	sort(str.begin(),str.end());
	set<char>s;
	for(int i=0;i<k;i++)s.insert(str[i]);
	if(s.size()==1)
	{
		s.erase(s.begin(),s.end());
		for(int i=k;i<n;i++)s.insert(str[i]);
		if(s.size()==1)
		{
			int left = n-k;
			int mini = left/k;
			cout<<str[k-1];
			for(int i=0;i<mini;i++)cout<<str[k];
			if(left%k!=0)
			{
				cout<<str[k]<<endl;
				return;
			}
			cout<<endl;
		}
		else
		{
			cout<<str[k-1];
			cout<<str.substr(k)<<endl;
			return;
		}
	}
	else
	{
		cout<<str[k-1]<<endl;
		return;
	}
}
int main() 
{
	#ifndef ONLINE_JUDGE
	// for getting input from in
	freopen("input.txt","r",stdin);
	freopen("Error.txt","w",stderr);
	// for writing output to output.txt
	freopen("output.txt","w",stdout);
	#endif 
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin>>t;
	int g = 1;
	while(t--)
	{
		solve(g);
		g++;
	}
	return 0; 
}


Comments

Submit
0 Comments
More Questions

1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares