首页 > 其他分享 >Sorting Color Balls

Sorting Color Balls

时间:2022-09-04 21:12:51浏览次数:50  
标签:Sorting Color balls ball leq Balls Swap th

Problem Statement

There are $N$ balls arranged from left to right. The color of the $i$-th ball from the left is Color $C_i$, and an integer $X_i$ is written on it.

Takahashi wants to rearrange the balls so that the integers written on the balls are non-decreasing from left to right. In other words, his objective is to reach a situation where, for every $1\leq i\leq N-1$, the number written on the $(i+1)$-th ball from the left is greater than or equal to the number written on the $i$-th ball from the left.

For this, Takahashi can repeat the following operation any number of times (possibly zero):

Choose an integer $i$ such that $1\leq i\leq N-1$.
If the colors of the $i$-th and $(i+1)$-th balls from the left are different, pay a cost of $1$. (No cost is incurred if the colors are the same).
Swap the $i$-th and $(i+1)$-th balls from the left.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • $2 \leq N \leq 3\times 10^5$
  • $1\leq C_i\leq N$
  • $1\leq X_i\leq N$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$C_1$ $C_2$ $\ldots$ $C_N$
$X_1$ $X_2$ $\ldots$ $X_N$

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective, as an integer.


Sample Input 1

5
1 5 2 2 1
3 2 1 2 1

Sample Output 1

6

Let us represent a ball as $($Color$,$ Integer$)$. The initial situation is $(1,3)$, $(5,2)$, $(2,1)$, $(2,2)$, $(1,1)$. Here is a possible sequence of operations for Takahashi:

  • Swap the $1$-st ball (Color $1$) and $2$-nd ball (Color $5$). Now the balls are arranged in the order $(5,2)$, $(1,3)$, $(2,1)$, $(2,2)$, $(1,1)$.
  • Swap the $2$-nd ball (Color $1$) and $3$-rd ball (Color $2$). Now the balls are arranged in the order $(5,2)$, $(2,1)$, $(1,3)$, $(2,2)$, $(1,1)$.
  • Swap the $3$-rd ball (Color $1$) and $4$-th ball (Color $2$). Now the balls are in the order $(5,2)$, $(2,1)$, $(2,2)$, $(1,3)$, $(1,1)$.
  • Swap the $4$-th ball (Color $1$) and $5$-th ball (Color $1$). Now the balls are in the order $(5,2)$, $(2,1)$, $(2,2)$, $(1,1)$, $(1,3)$.
  • Swap the $3$-rd ball (Color $2$) and $4$-th ball (Color $1$). Now the balls are in the order$(5,2)$, $(2,1)$, $(1,1)$, $(2,2)$, $(1,3)$.
  • Swap the $1$-st ball (Color $5$) and $2$-nd ball (Color $2$). Now the balls are in the order $(2,1)$, $(5,2)$, $(1,1)$, $(2,2)$, $(1,3)$.
  • Swap the $2$-nd ball (Color $5$) and $3$-rd ball (Color $1$). Now the balls are in the order $(2,1)$, $(1,1)$, $(5,2)$, $(2,2)$, $(1,3)$.

After the last operation, the numbers written on the balls are $1,1,2,2,3$ from left to right, which achieves Takahashi's objective.

The $1$-st, $2$-nd, $3$-rd, $5$-th, $6$-th, and $7$-th operations incur a cost of $1$ each, for a total of $6$, which is the minimum. Note that the $4$-th operation does not incur a cost since the balls are both in Color $1$.


Sample Input 2

3
1 1 1
3 2 1

Sample Output 2

0

All balls are in the same color, so no cost is incurred in swapping balls.


Sample Input 3

3
3 1 2
1 1 2

首先当且仅当 \(X_i<X_{i+1}\) 才会交换第 \(i\) 个和第 \(i+1\) 个。交换完后会减少一个逆序对。所以不考虑颜色,交换次数等于逆序对个数。

考虑颜色如果两个数颜色相同,那么不计价值。所以答案还要减去同颜色的逆序对个数即可。

#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int x[N],c,y,t[N],n,m;
long long dp[N][N],ans;
int main()
{
	memset(dp,-0x7f,sizeof(dp));
	dp[0][0]=0;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",x+i);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&c,&y),t[c]+=y;
	for(int i=1;i<=n;i++)
	{
		dp[i][0]=dp[i-1][0];
		for(int j=1;j<=n;j++)
			dp[i][j]=dp[i-1][j-1]+t[j]+x[i],dp[i][0]=max(dp[i][0],dp[i-1][j-1]),ans=max(ans,dp[i][j]);
	}
	printf("%lld",ans);
}

标签:Sorting,Color,balls,ball,leq,Balls,Swap,th
From: https://www.cnblogs.com/mekoszc/p/16656093.html

相关文章

  • CF1716F Bags with Balls
    纪念第一个场切的EDU的F。题意:有\(n\)个不同的盒子,每个盒子里有\(m\)个编号分别为\(1\dotsm\)的小球。现在要从每个盒子中恰好取出\(1\)个球,计算每种取法中,......
  • 学习随笔——codeforces题目Color the Picture解答
    摘要:构造类题目题目原地址如下:https://codeforces.com/problemset/problem/1710/A题目截图如下:  关键词:构造算法,递归,*1500简要翻译:给予k种颜料,第i种颜料可以涂满a......
  • Color Map
    Source1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#include<math.h>56#defineXLEN10247#defineYLEN102489intmain......
  • PAT Advanced 1027 Colors in Mars(20)
    题目描述:PeopleinMarsrepresentthecolorsintheircomputersinasimilarwayastheEarthpeople.Thatis,acolorisrepresentedbya6-digitnumber,wher......
  • IOS OpenGL ES GPUImage 色彩减淡混合 GPUImageColorDodgeBlendFilter
    目录一.简介二.效果演示三.源码下载四.猜你喜欢零基础OpenGL(ES)学习路线推荐:OpenGL(ES)学习目录>>OpenGLES基础零基础OpenGL(ES)学习路线推荐:......