[ABC261F] Sorting Color Balls
Problem Statement
There are balls arranged from left to right. The color of the -th ball from the left is Color , and an integer 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 , the number written on the -th ball from the left is greater than or equal to the number written on the -th ball from the left.
For this, Takahashi can repeat the following operation any number of times (possibly zero):
Choose an integer such that .
If the colors of the -th and -th balls from the left are different, pay a cost of . (No cost is incurred if the colors are the same).
Swap the -th and -th balls from the left.
Find the minimum total cost Takahashi needs to pay to achieve his objective.
Constraints
- 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 , , , , . Here is a possible sequence of operations for Takahashi:
- Swap the -st ball (Color ) and -nd ball (Color ). Now the balls are arranged in the order , , , , .
- Swap the -nd ball (Color ) and -rd ball (Color ). Now the balls are arranged in the order , , , , .
- Swap the -rd ball (Color ) and -th ball (Color ). Now the balls are in the order , , , , .
- Swap the -th ball (Color ) and -th ball (Color ). Now the balls are in the order , , , , .
- Swap the -rd ball (Color ) and -th ball (Color ). Now the balls are in the order, , , , .
- Swap the -st ball (Color ) and -nd ball (Color ). Now the balls are in the order , , , , .
- Swap the -nd ball (Color ) and -rd ball (Color ). Now the balls are in the order , , , , .
After the last operation, the numbers written on the balls are from left to right, which achieves Takahashi's objective.
The -st, -nd, -rd, -th, -th, and -th operations incur a cost of each, for a total of , which is the minimum. Note that the -th operation does not incur a cost since the balls are both in Color .
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
首先当且仅当 才会交换第 个和第 个。交换完后会减少一个逆序对。所以不考虑颜色,交换次数等于逆序对个数。
考虑颜色如果两个数颜色相同,那么不计价值。所以答案还要减去同颜色的逆序对个数即可。
#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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】