日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 1804 Brainman (归并排序 -- 求逆序对数)

發布時間:2025/3/11 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 1804 Brainman (归并排序 -- 求逆序对数) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

歸并排序求逆序對數:

和歸并排序一樣,劃分和遞歸求解都好理解,關鍵在于合并,對于右邊的j ,統計出左邊比j 大 的元素個數 f(j),所有的f(j)家和就是我們要的逆序對數!

在歸并排序中,我們將右邊的元素向臨時數組中加入的時候,左邊還沒加入得便是比j 大的元素! ?既有m-p個

在加右邊時,不斷累加m-p即可!

以 POJ 1804 為例!

?

  • #include <cstdio>
  • #include <cstring>
  • #include <algorithm>
  • using namespace std;
  • int a[5007],b[5007],n,sum,ks;
  • void sort(int x,int y){
  • if (x + 1 >= y)return;
  • int m = x + (y-x)/2;
  • sort(x,m); sort(m,y);
  • int p = x, q = m, i = x;
  • while (p < m || q < y){
  • if (q >= y || (p < m && a[p] <= a[q])) b[i++] = a[p++];
  • else b[i++] = a[q++], sum+=m-p;
  • }
  • for (int i = x; i < y; ++i) a[i] = b[i];
  • }
  • int main(){
  • int T;
  • scanf("%d",&T);
  • while(T--){
  • scanf("%d",&n);
  • for (int i = 0; i < n; ++i)scanf("%d",a+i);
  • sum = 0;
  • sort(0,n);
  • if (ks++)puts("");
  • printf("Scenario #%d:\n%d\n",ks,sum);
  • }
  • return 0;
  • }

  • Brainman
    Time Limit:?1000MS?Memory Limit:?30000K
    Total Submissions:?9861?Accepted:?5227

    Description

    Background?
    Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.?

    Problem?
    Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:?
    Start with: 2 8 0 3?
    swap (2 8) 8 2 0 3?
    swap (2 0) 8 0 2 3?
    swap (2 3) 8 0 3 2?
    swap (8 0) 0 8 3 2?
    swap (8 3) 0 3 8 2?
    swap (8 2) 0 3 2 8?
    swap (3 2) 0 2 3 8?
    swap (3 8) 0 2 8 3?
    swap (8 3) 0 2 3 8
    So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:?
    Start with: 2 8 0 3?
    swap (8 0) 2 0 8 3?
    swap (2 0) 0 2 8 3?
    swap (8 3) 0 2 3 8
    The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

    Input

    The first line contains the number of scenarios.?
    For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

    Output

    Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.

    Sample Input

    4 4 2 8 0 3 10 0 1 2 3 4 5 6 7 8 9 6 -42 23 6 28 -100 65537 5 0 0 0 0 0

    Sample Output

    Scenario #1: 3

    Scenario #2:
    0

    Scenario #3:
    5

    Scenario #4:
    0

    Source

    TUD Programming Contest 2003, Darmstadt, Germany

    [Submit]?? [Go Back]?? [Status]?? [Discuss]

    總結

    以上是生活随笔為你收集整理的POJ 1804 Brainman (归并排序 -- 求逆序对数)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 伊人久久五月天 | 亚洲91网站 | 男人肌肌桶女人肌肌 | 十大黄台在线观看 | 亚洲激情二区 | 久久视频在线观看 | 日韩精品一区二区三区视频在线观看 | 欧美日韩精品在线观看 | 亚洲精品高清视频在线观看 | 国产精品视频免费看 | 中文字幕一区二区三区四区 | 福利一区福利二区 | 6080av| 超碰2020| 成人精品综合 | 国产精品免费一区二区区 | 成年激情网 | 久久99色| 麻豆极品 | 在线国产视频 | 国产男男gay网站 | 欧美春色 | 456亚洲影视 | 亚洲一区二区网站 | 亚洲精品第二页 | 国产视色| 奇米影视777四色 | 中文字幕网站 | 日本激情在线 | www.youjizz.com在线| 国产精品ⅴa有声小说 | 久久久久亚洲av成人无码电影 | 欧美日韩国产一区二区三区 | а√天堂资源官网在线资源 | 亚洲AV无码精品久久一区二区 | 亚洲一区精品在线观看 | 少妇高潮久久久久久潘金莲 | 日本一级黄色大片 | 36d大奶| 亚洲精品伦理 | 亚洲成av | 99色综合网| 天堂av网手机版 | 99国产精品视频免费观看一公开 | 欧美一区二区性久久久 | 福利一区在线观看 | 久久精品视屏 | 天天爽天天射 | 日本www视频在线观看 | 先锋影音资源av | 欧美三级一区二区三区 | 妻色成人网 | 欧美日韩免费看 | 久久成人久久 | 久久久久久久久久av | 天堂中文在线视频 | 日日摸日日碰 | 麻豆av影院 | 性生活网址 | 久久久噜噜噜久久 | 国产传媒视频在线观看 | 91成人免费网站 | 国产图片一区 | 日韩一级精品 | 狠狠干超碰 | 欧美美女一区二区 | 久久久久久麻豆 | 久久久情 | 国产精品美女网站 | 中国av在线| 黄色xxxxx | 精品99久久 | av激情四射 | 可以看黄色的网站 | 人与动物毛片 | 女久久| 91国语对白 | 午夜在线观看视频 | 一区二区三区四区视频在线观看 | 免费观看黄色 | 亚洲伊人网站 | 肌肉猛男裸体gay网站免费 | 九月婷婷丁香 | 欧美体内she精高潮 日韩一区免费 | 国产精品第一区 | 精品免费国产 | 青青草原国产在线 | 交专区videossex | 都市乱淫 | 韩国激情呻吟揉捏胸视频 | 国产伦精品一区二区三区四区免费 | 精品国产亚洲av麻豆 | 涩涩视频在线免费看 | 欧美国产三级 | 亚洲在线一区 | 亚洲激情五月 | 青青视频网站 | 91偷拍一区二区三区精品 | 国产综合在线观看 |