1798D - Shocking Arrangement - CodeForces Solution


constructive algorithms dp greedy math

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<iterator>
#include<algorithm>
#include<ctype.h>
#include<bits/stdc++.h>
#define BHUSHAN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define vi vector<ll>
#define vip vector<pair<ll,ll>>
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
ll factorial(ll n){return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);}
ll max(ll a,ll b){if(a>b)return a;return b;}
void inputv(vector<ll>&v,ll n){for(ll i=0;i<n;i++){ll x;cin>>x;v.push_back(x);}return;}
void coutv(vector<ll>v){for(ll i=0;i<v.size();i++)cout<<v[i]<<" ";cout<<endl;return;}
bool isPowerOfTwo(ll n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));}
bool isPrime(ll n){if (n <= 1)return false;for (int i = 2; i <= sqrt(n); i++)if (n % i == 0)return false;return true;}
int gcd(int a, int b){if (a == 0)return b;return gcd(b % a, a);}ll ceil_div(ll a, ll b) {return a % b == 0 ? a / b : a / b + 1;}
ll mex(vector<ll>v){ll ans=-1;unordered_map<ll,ll>mp;for(ll i=0;i<v.size();i++)mp[v[i]]++;for(ll i=0;i<=v.size();i++){if(mp[i]==0)return i;}}
bool isSorted(vector<ll>v){ ll n=v.size();vector<ll>s;s=v;sort(s.begin(),s.end());for(ll i=0;i<n;i++){if(s[i]!=v[i]){return false;}}return true;}
//***************************************START YOUR CODE FROM HERE******************************************//
// vector<ll>ans;

 ll mod=1000000000+7;
int main()
{
	ll T;
	cin>>T;
    while((T--))
    {
       ll n;
       cin>>n;
       ll zero=0;
       vector<ll>v;
       for(ll i=0;i<n;i++)
       {
       	ll x;
       	cin>>x;
       	if(x==0)
       	zero++;
       	else
       	v.push_back(x);
	   }
	   if(zero==n)
	   cout<<"NO"<<endl;
	   else
	   {
	   sort(v.begin(),v.end());
	    vector<ll>ans;
	    while(zero--)
	    ans.push_back(0);
	    ll sum=0;
	    ll l=0,r=v.size()-1;
	    while(l<=r)
	    {
	    	if(sum>=0)
	    	{
	    		ans.push_back(v[l]);
	    		
	    		sum+=v[l];
	    		l++;
			}
			else
			{
				ans.push_back(v[r]);
				sum+=v[r];
				r--;
				
			}
		}
		cout<<"YES"<<endl;
		for(auto i:ans)
		cout<<i<<" ";
		cout<<endl;
	    
	   }
	   	
       
       
	}
	 return 0;
}



Comments

Submit
0 Comments
More Questions

20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space