Inverse Pair

Inverse Pair

Now it is 4:30 p.m. on July 26,2021. In the past three days,I have been so afflicted with stomach flu that I have been unable to study.I think I caught a cold while sleeping and woke up feeling dizzy.Then I thought I’d go to the drugstore and buy something for my headache.
When I finished taking the medicine,I did feel the headache was gone,and then I went to sleep at ease.However,the next day,althought the head is not dizzy,but I still feel very wrong body,very uncomfortable.So I found that I made a serious mistake, I should not buy the medicine by myself,I should go to see the doctor int time when I was sick.
Luckily,I went to see the doctor on the third day and I got well very quickly and felt much better the next day.
So I took part in this afternoon’s cowrider competition and solved a problem in the competition,so I wrote a diary solution.

题意: 给定一个长度为n的排列(注意是排列),你可以对于序列里的每个数进行+1或者不操作,使得其逆序对的数量变得尽量的少。

题解: 由于是一个排列,而且你只能对于数组进行+1操作,所以如果你想减少逆序的数量只能使得两个数变得相等。而且有一个性质,你把一个数+1了,最多只能减少一对逆序对,因为最多只有一个数会和他变得相等。所以我们只需要从后往前贪心的遍历,对于每个数 a i a_i ai,如果在它前面存在 a i a_i ai+1,而且他没被标记(标记后面会说),那么我们就把他+1,并且把 a i a_i ai+1,做上标记。这样子做一遍,得到的就是答案序列。

为什么要做标记?

因为如果不把 a i a_i ai+1做上标记,那么意味着, a i a_i ai+1很有可能也被+1,那么这样我们之前的把 a i a_i ai加上1就等于白加了。

The way ahead is so long without ending,yet high ans low I’II search whith my will unbending.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值