NJUST 1925 sequence

原题

Description

将一个给定的数列,拆分成K个不降序列,每个数出现且只出现一次,且在各序列中各个数相对于原数列的相对顺序不变。
如7 6 9 8 10可以拆成 7 9 10和6 8。求最小的K值。

Algorithm

按顺序生产
比如先 7 9 10
再 6 8
然后拿链表实现

Code

#include <cstdio>
#include <iostream>
#include <list>
using namespace std;
void solve()
{
  list<int> mylist;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
  {
    int x;
    scanf("%d", &x);
    mylist.push_back(x);
  }
  int ans = 0;
  while (!mylist.empty())
  {
    int head = *mylist.begin();
    mylist.pop_front();
 // for (list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
   // cout << *it;
    for (list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
      if (*it >= head)
      {
        head = *it;
        it = mylist.erase(it);
        it--;
      }
    ans++;
  }
  cout << ans << endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
  int t = 1;
    scanf("%d", &t);
  for (int i = 0; i < t; i++)
    solve();
}

你可能感兴趣的:(NJUST 1925 sequence)