https://codeforces.com/contest/1773/problem/E
Emma loves playing with blocks. She has several cubic blocks of the same size that are numbered with distinct integers written on them. She assembles towers from those blocks by stacking them vertically.
A configuration of her game is a set of towers that she has assembled from the blocks. Emma can perform two kinds of operations on a configuration of towers:
- Split any tower with more than one block in it by taking any number of blocks from the top of the tower and moving them to a new tower keeping their order, so that the top block of the old tower becomes the top block of the new tower. As a result of this operation, the number of towers increases by one.
- Combine any two towers by moving blocks from one tower on top of the other tower in the same order. As a result of this operation, the number of towers decreases by one.
Emma wants to stack all the blocks into a single tower so that all blocks come in order sorted by the numbers — from the block with the minimal number at the top to the block with the maximal number at the bottom. Emma wants to do as little of splitting and combining operations as possible. Your task is to find the minimal number of operations she has to make and output how many splits and combines are needed.
Input
The first line of the input file contains an integer 𝑛n (1≤𝑛≤100001≤n≤10000) — the number of towers in the initial configuration. Next 𝑛n lines describe towers. Each tower 𝑖i is described by a line that starts with the number 𝑘𝑖ki (𝑘𝑖≥1ki≥1; ∑𝑛1𝑘𝑖≤10000∑1nki≤10000) — the number of blocks in the tower, followed by 𝑘𝑖ki numbers 𝑏𝑖,𝑗bi,j (1≤𝑏𝑖,𝑗≤1091≤bi,j≤109) — numbers written on the blocks in the 𝑖i-th tower, listed from top to bottom. All block numbers listed in the input are different.
Output
Output a line with two integers 𝑠s and 𝑐c — the number of split and combine operations Emma should make to get a single tower with blocks sorted by their numbers, so that the total number of operations is minimized.
Example
input
Copy
2 3 3 5 8 2 9 2
output
Copy
1 2
Note
The example needs the following operations (1 split and 2 combines).
Initial | Split last | Combined 2nd onto 1st | Combined 1st onto 2nd |
分析:
当输入完虽有的积木的情况之后,就能够得到最后结果是怎么排序的。那么由此就可以通过比较,去判断需要多少次操作,才能达到目标
输入的时候可以看做一个二维数组去看,那么在得到最后结果的时候,也就要把结果存到对应的二维数组里面,这样才能达到一一对应的效果。
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
#define rep(a,b) for(int i=a;i<=b;i++)
#define rrep(a,b) for(int j=a;j<=n;j++)
#define per(a,b) for(int i=a;i>=b;i--)
#define pper(a,b) for(int j=a;j>=b;j--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
ll t,n,m,a,b,cnt,ans,ant,q,p;
ll brr[N],crr[N];
map<int,int>mp;//用于存储二维数组当中每个位置对应的数
vector<int >arr[N];//这里相当于一个二维数组
int main()
{
cin>>n;
cnt=0,ant=1;
for(int i=0;i<n;i++)
{
cin>>brr[i];
for(int j=0;j<brr[i];j++)
{
int x;
cin>>x;
arr[i].push_back(x);
crr[cnt++]=x;
}
}
sort(crr,crr+cnt);
for(int i=0;i<cnt;i++)
{
mp[crr[i]]=i;
}
ans=n;
for(int i=0;i<n;i++)
{
for(int j=1;j<brr[i];j++)
{
if(mp[arr[i][j-1]]+1!=mp[arr[i][j]])
{
ans++;
}
}
}
cout<<ans-n<<' '<<ans-1;
return 0;
}