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.