1826A - Trust Nobody - CodeForces Solution


brute force greedy implementation

Please click on ads to support us..

C++ Code:

/***
   **    Author: MD.POLASH ISLAM (PRINCE)
   ***/

#include<bits/stdc++.h>
using namespace std;

//constants :
const int   N           = (int) 1e6+5;
const int   M           = (int) 1e9+5;
const int   mod         = (int) 1000000007;
const int   max_prime   = (int) 1e6+3;
const int   BLK         = (int) 700;
const double pi = acos(-1.0);

#define ll           long long int
#define pb           push_back
#define pob          pop_back
#define F            first
#define S            second
#define vll                 vector<ll>
#define vvll               vector<vll>
#define vcc                vector<char>
#define endl               "\n"
#define vbb                 vector<bool>
#define  all_0(x)              memset(x,0,sizeof(x))
#define  all_neg(x)            memset(x,-1,sizeof(x))
#define  all_1(x)               memset(x,1,sizeof(x))
#define for0(n) for (int i = 0; i < (int)(n); ++i)
#define in_range(i,x,y)                 for(int i=x;i<=y;i++)
#define in_range_back(i,x,y)            for(int i=y;i>=x;i--)
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vll vector<ll>
#define yes "YES"
#define no "NO"
 

int divs[N+10];
int ans[N+10];

bool cmp(pair<int,int>a,pair<int,int> b)
{
    if (a.second!=b.second)
    {
        return a.second>b.second;
    }
    else
    {
        return a.second<b.second;
    }
}

int gcd(int  a, int  b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int removeZeros(int num)
{
    int ret = 0;
    int ten = 1;
    while (num)
    {
        int dig = num % 10;
        num /= 10;
        if (dig)
        {
            ret += dig * ten;
            ten *= 10;
        }
    }
    return ret;
}

void divcount()
{
    for (int i=1; i<=N; i++)
    {
        for (int j=i; j<=N; j+=i)
        {
            divs[j]++;
        }
    }

}

ll bigmod()
{
    ll ans=1;
    ll a=6;
    ll n=mod-2;
    ll md=mod;
    while (n)
    {
        if (n%2)
        {
            ans=(ans*a)%md;
            n--;
        }
        else
        {
            a=(a*a)%md;
            n=n/2;
        }
    }
    return ans;
}
int countoftwo(int ans){
    int cnt=0;
    while ( ans>0)
    {
         ans=ans/2;
         cnt++;
    }
    return cnt;  

}


bool contains(string s, string target) {
    for (int i = 0; i < s.size(); i++) {
        if (i + target.size() <= s.size() && s.substr(i, target.size()) == target) {
            return true;
        }
    }
    return false;
}

string rearrange(string s) {
    string selise = "SELISE";
    string selise_dp = "SELISE Digital Platforms";

    
    sort(s.begin(), s.end());
    if (contains(s, selise_dp)) {
        return "BOTH";
    } else if (contains(s, selise)) {
        return "SELISE";
    }
    return "NONE";
}


/* void print(__int128 x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) print(x / 10);
    putchar(x % 10 + '0');
}

__int128 read() {
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}*/


// starting -->>

 bool isprime(int n) {
    if (n < 2) {
        return false;
    }
    int sqrt_n = sqrt(n);
    for (int i = 2; i <= sqrt_n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

bool checkPowerOfTwo(ll num){
    return (num!=0 && ((num & (num - 1)) == 0));
}

 
ll sqrta(ll n) {
   ll left = 0, right = sqrt(n) + 1;
    while (left < right) {
        ll mid = (left + right + 1) / 2;
        if (mid * mid <= n) {
            left = mid;
        } else {
            right = mid - 1;
        }
    }
    return left;
} 
 
string checkFactors(int n, int m) {
    for (int i = 1; i <= m; i++) {
        if (n % i == 0 && n/i <= m) {
            return "NO";
        }
    }
    
    
} 
  
void solve()
{  
      
    int n; 
    cin >> n;
    int a[n + 1];
    
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    
    int ans = -1;
    
    for (int k = 0; k <= n; k++) {  
      int truth = 0, lie = 0;
      for (int i = 1; i <= n; i++) {
        if (k >= a[i]) {
          truth++;
        }
        else {
          lie++;
        }
      }
      if (lie == k) {
        ans = k;
        break;
      }
    }
    cout << ans << '\n';
        
    
}


int main()
{
    ios_base :: sync_with_stdio (false);
    cin.tie(NULL);
    
    //solve2();
     int t,k=1;
     cin>>t;
    
    while (t--)
    {   // cout<<"Case "<<k++<<": ";
         solve();
        
    }      
     
  //solve();


    return 0;


}

 


Comments

Submit
0 Comments
More Questions

780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack