Towers
题目描述
You are given n cubes in a certain order, and your task is to build towers using them. Whenever two cubes are one on top of the other, the upper cube must be smaller than the lower cube.
You must process the cubes in the given order. You can always either place the cube on top of an existing tower, or begin a new tower. What is the minimum possible number of towers?
输入
The first input line contains an integer n(1 ≤ n ≤ ): the number of cubes.
The next line contains n integers k1,k2,...,kn(1 ≤ ki ≤ ): the sizes of the cubes.
输出
Print one integer: the minimum number of towers.
样例输入
5
3 8 2 1 5
样例输出
2
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,ans;
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n;vector<ll>k(n);for(ll i=0;i<n;i++){cin>>k[i];}multiset<ll>s;for(ll i:k){if(s.empty()){ans++;s.insert(i);}else{auto it=s.upper_bound(i);if(it==s.end()){ans++;s.insert(i);}else{s.erase(it);s.insert(i);}}}cout<<ans;return 0;
}