856A - Set Theory - CodeForces Solution


brute force constructive algorithms *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
//for math u can observe something related to gcd or something related to even or odd to draw some conclusions
//if your answer is lieing in a range then binary search is the best approach to solve that problem
//for errors check out of bound and overflow of integers
//sorting,binary search,dynamic programming,greedy are some of the most used techniques to solve the problems
//until some element it is good and then next it is bad u should use binary search 
//to find the first good element or to find the first bad element we use binary search
//if there are digits then remember that the digits are just from 0-9 so u can use two loops the same for character a-z they are just 26
const int M=998244353;
const int INF=1e9+5;
void dfs(int currentNode,vector<vector<int>>& adj,int parent,vector<int>& ans)
{
    ans.push_back(currentNode);
    for(int neighbour : adj[currentNode])
    {
        if(neighbour!=parent)
        {
            dfs(neighbour,adj,currentNode,ans);
        }
    }
}
ll mod(ll x)
{
    return (((x%M)+M)%M);
}
ll add(ll a,ll b)
{
    return mod(mod(a)+mod(b));
}
ll mul(ll a,ll b)
{
    return mod(mod(a)*mod(b));
}
int sumd(ll a)
{
    int res=0;
    while(a!=0)
    {
        res+=a%10;
        a/=10;
    }
    return res;
}
int main() {
	// your code goes here
	int t;
    cin>>t;
	while(t--)
	{
	  int n;
	  cin>>n;
	  int a[n];
	  for(int i=0;i<n;i++)
	  {
	      cin>>a[i];
	  }
	  if(n==1)
	  {
	      cout<<"YES"<<endl;
	      cout<<1<<endl;
	  }
	  else
	  {
	      cout<<"YES"<<endl;
	   int b[n];
	   b[0]=1;
	   vector<int>diff(1000005,0);
	   for(int i=0;i<n;i++)
	   {
	     int x=abs(a[i]-1);
	     diff[x]=1;
	   }
	   int i=1;
	   int k=2;
	   while(i<n)
	   {
	       bool ok=true;
	       int p=0;
	       while(p==0)
	       {
	         for(int j=0;j<n;j++)
	         {
	             int x=abs(a[j]-k);
	             if(diff[x]==1)
	             {
	                 p=0;
	                 break;
	             }
	             else
	             {
	                 p=1;
	             }
	         }
	         if(p==0) k++;
	         else
	         {
	             b[i]=k;
	             for(int j=0;j<n;j++)
	             {
	                 int x=abs(a[j]-k);
	                 diff[x]=1;
	             }
	             
	             k++;
	             p=1;
	         }
	       }
	       i++;
	   }
	   for(int z=0;z<n;z++)
	   {
	       cout<<b[z]<<" ";
	   }
	   cout<<endl;
	  }
	  
	  }
	

	return 0;

}


Comments

Submit
0 Comments
More Questions

1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft