brute force data structures greedy implementation *1500

Please click on ads to support us..

C++ Code:

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

bool areSame(vector<string> vs, vector<int> currStrings)
{
  int is = vs[currStrings[0]].size();
  for (int i = 0; i < is; i++)
  {
    bool samei = true;
    samei &= vs[currStrings[1]][i] == vs[currStrings[0]][i];
    samei &= vs[currStrings[2]][i] == vs[currStrings[0]][i];
    if (samei)
      return true;
  }
  return false;
}
bool areDifferent(vector<string> vs, vector<int> currStrings)
{
  int is = vs[currStrings[0]].size();
  bool diffrent = true;
  for (int i = 0; i < is; i++)
  {
    bool localDifferent = true;
    localDifferent &= vs[currStrings[1]][i] != vs[currStrings[0]][i];
    localDifferent &= vs[currStrings[2]][i] != vs[currStrings[0]][i];
    diffrent &= localDifferent;
  }
  return diffrent;
}
int arr[1501][1501];

/*

Max Freq of feature in card number.
n=8
k=3

134  224 521
SET  SET SET

5cards with 1st card == S forms group in bw

5cards with 3 cards whose 1st != S form groups with d
NS-1  N-1  N-1
-      -    -


*/
bool check(vector<int> &arr, int prevPos, int prevEle)
{
  int currEle = prevEle + 1;
  int n = arr.size();
  if (currEle > n)
    return true;
  int currPos = find(arr.begin(), arr.end(), currEle) - arr.begin();
  if (currPos > prevPos)
  {
    if (currPos == prevPos + 1)
      return check(arr, currPos, currEle);
    else
      return false;
  }
  bool wrong = true;
  for (int i = currPos + 1; i < prevPos; i++)
  {
    if (arr[i] != currEle + 1)
    {
      wrong = false;
      break;
    }
    currEle++;
  }
  return wrong && check(arr, currPos, currEle);
}

void solve()
{
  int n;
  cin >> n;
  vector<int> arr(n);
  map<int, int> item2pos;
  for (int i = 0; i < n; i++)
  {
    cin >> arr[i];
    item2pos[arr[i]] = i;
  }
  int curr = 1;
  int end = n;

  while (curr <= n)
  {
    int pos = item2pos[curr];
    bool ans = true;
    for (int i = pos + 1; i < end; i++)
    {
      ans &= arr[i] == curr + 1;

      curr++;
    }
    if (!ans)
    {
      cout << "No";
      return;
    }
    end = pos;
    curr++;
  }
  cout << "Yes";
}

signed main()
{

  int t = 1;
  cin >> t;
  while (t--)
  {
    solve();
    cout << endl;
  }
}


Comments

Submit
0 Comments
More Questions

1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies