Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя raghavvmittal11

Автор raghavvmittal11, история, 8 месяцев назад, По-английски

Intution — here we make 3 multiset to store the unique elements now we insert the input of the first array into the multiset (ms1) similarly we insert the input of the second array into the multiset (ms2) now we store the common elements in the ms1 and ms2 into the common named multiset. now we make conditions: 1. element not present in either ms1 or ms2 we return false 2. element either present in the first array but not in the second array we increase its count1 and remove the item. 3.similarly we do for other one element is present in the second array but not in the first array we increase its count2 and remove the item.

now for the case where its the common elemnt in both of the arrays we can condition it like if(common.size() < (k/2-cnt1)+(k/2-cnt2)) where we check is remaining elements of both the array sum is equal to the common array size. If then print the yes else no.

Your code here...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define endl "\n"
#define pb push_back
#define mod 1000000007
#define N 500010
#define inf 100000000000000000

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int testcases = 1;
    cin>>testcases;
    while (testcases--)
    {
        // 1. keep it simple
        int n,m,k;
        cin>>n>>m>>k;
        int a[n], b[m];// array to store the two array in input
        multiset<int>ms1, ms2, common;// set to store the unique elements
        
        // insert each element from input to arr1
        for(int i=0;i<n;i++)
        {
        	cin>>a[i];
        	ms1.insert(a[i]);
        }
        // insert each element from input to arr2
        for(int i=0;i<m;i++)
        {
        	cin>>b[i];
        	ms2.insert(b[i]);
        	
        	// if the element of the b(arr2) is present in arr1 then insert it the common
        	if(ms1.find(b[i])!=ms1.end())
        	{
        		common.insert(b[i]);
        	}
        }
        
        bool ans=true;
        // Maintain the two count
        int cnt1=0, cnt2=0;
        for(int i=1;i<=k;i++)
        {
            // if element if not present in the both array return false
        	if(ms1.find(i)==ms1.end() and ms2.find(i)==ms2.end())
        	{
        		ans=false;
        		break;
        	}
        	// if common if take this case later
        	if(common.find(i)!=common.end())
        	{
        		continue;
        	}
        	// if found in either of the array then increas the respective count and remove that no 
        	else if(ms1.find(i)!=ms1.end())
        	{
        		cnt1++;
        		ms1.erase(ms1.find(i));
        	}
        	else
        	{
        		cnt2++;
        		ms2.erase(ms2.find(i));
        	}
        	
        	// If the count of any array is greater than the k/2 return false
        	if(cnt1 >k/2 or cnt2>k/2)
        	{
        		ans=false;
        		break;
        	}
        }
        // now for common 
        // remaining elements1(k/2-cnt1) + remaining elements2(k/2-cnt2)  if not equal to the common size return false
        if(common.size() < (k/2-cnt1)+(k/2-cnt2))
        	ans=false;
        if(ans)
        	cout<<"YES"<<endl;
        else
        	cout<<"NO"<<endl;
    }
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -13
  • Проголосовать: не нравится

Автор raghavvmittal11, история, 8 месяцев назад, По-английски

Intution — Here the intution is we create a map and store the chararcter and map it with there count. Example 0 0 1 1 now here firstly we encounter 0 now we check is there any character in the answer string if (yes) then we we increase its count in the map and if(no) then we ad the character into the string and increase its count in map and then move to the next character. now for 0 string become "a", now we encounter 0 again but we already move to the next character hence string become "ab" now intresting part come we encounter 1 then we check is are any character count maps the 1 we encounter and the answer is yes its "a" hence we add a to the string "aba" and increase its count and move to the next character.Now finally we again encounter the 1 and we have one character whose count maps the 1 and its b hence we add b to our ans string and its becomes "abab".and then we return our answer string.

Your code here...

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define endl "\n"
#define pb push_back
#define mod 1000000007
#define N 500010
#define inf 100000000000000000

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int testcases = 1;
    cin>>testcases;
    while (testcases--)
    {
        // 1. simply take input of testcases
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
        	cin>>a[i];
        }
        
        // create a map to store the count correspond to the each character
        map<char,int>mp;
        string s="";
        //take the first element as a string 
        char ch='a';
        for(int i=0;i<n;i++)// for the count ==0 here it add new char and increament the map count for the character
        {
        	if(a[i]==0)
        	{
        		s.push_back(ch);
        		mp[ch]++;
        		ch++;// make the character increase its value 
        	}
        	
        	else
        	{
        	    // Now if the char occured once then check the count of the character from map and increase its count
        		for(char j='a';j<='z';j++)
        		{// check character in map (count)
        			if(mp[j]==a[i])
        			{ // insert the char into ans string
        				s.push_back(j);
        				mp[j]++;// increase its map count value
        				break;
        			}
        		}
        	}
        }
        cout<<s<<endl;// print the anser string
    }
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -17
  • Проголосовать: не нравится