搴忓垪鍚堝苟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搴忓垪鍚堝苟相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e7%ac%ac%e4%b8%80%e4%b8%aa' title='绗竴涓?>绗竴涓?/a>   heap   搴忓垪   list   radius   color   return   鏁板瓧   -o   

棰樼洰鎻忚堪 鏈変袱涓暱搴﹂兘鏄疦鐨勫簭鍒桝鍜孊锛屽湪A鍜孊涓悇鍙栦竴涓暟鐩稿姞鍙互寰楀埌N^2N2涓拰锛屾眰杩?span class="katex">N^2N2涓拰涓渶灏忕殑N涓€?/span>

 

杈撳叆杈撳嚭鏍煎紡

杈撳叆鏍煎紡锛?/span>

绗竴琛屼竴涓鏁存暟N锛?/span>

绗簩琛孨涓暣鏁?span class="katex">A_iAi?, 婊¤冻A_ile A_{i+1}Ai?Ai+1?涓?span class="katex">A_ile 10^9Ai?109;

绗笁琛孨涓暣鏁?span class="katex">B_iBi?, 婊¤冻B_ile B_{i+1}Bi?Bi+1?涓?span class="katex">B_ile 10^9Bi?109.

銆愭暟鎹妯°€?/span>

瀵逛簬50%鐨勬暟鎹腑锛屾弧瓒?<=N<=1000锛?/span>

瀵逛簬100%鐨勬暟鎹腑锛屾弧瓒?<=N<=100000銆?/span>

杈撳嚭鏍煎紡锛?/span>

杈撳嚭浠呬竴琛岋紝鍖呭惈N涓暣鏁帮紝浠庡皬鍒板ぇ杈撳嚭杩橬涓渶灏忕殑鍜岋紝鐩搁偦鏁板瓧涔嬮棿鐢ㄧ┖鏍奸殧寮€銆?/span> 

杈撳叆杈撳嚭鏍蜂緥

杈撳叆鏍蜂緥#1锛?nbsp;
3
2 6 6
1 4 8
杈撳嚭鏍蜂緥#1锛?nbsp;
3 6 7
寰堟樉鐒舵垜浠湁 a鍜宐涓や釜鏁扮粍 銆傚緢鏄剧劧鎴戜滑瑕佺敤涓€涓?c鏁扮粍瀛樺拰 銆傚緢鏄剧劧瑕佺敤heap鍫嗘帓(瀹為檯涓婃槸鍙敤浜嗗悜涓嬬淮鎶ゆ搷浣?銆?/div>
鎴戜滑鐨刢[ i ][ j ]锛岀敤鏉ュ瓨鍌╝涓i涓暟鍒嗗埆鍜宐涓殑鎵€鏈夋暟鐩稿姞寰楀埌鐨勭粨鏋溿€傚緢鏄剧劧 浼?鐖嗙┖闂达紝鎵€浠ョ瓑涓嬩細鏈変紭鍖栥€?/div>
 
for (int i = 1;i <= n;i++)
            for (int j = 1;j <= n;j++)
                c[i][j] = a[i]+b[j];
渚濋鎰忓緱锛歛鏄湁搴忔暟鍒楋紝b涔熸槸鏈夊簭鏁板垪锛屽垯瀵逛簬浠绘剰c[ i ]涔熶細鏄竴涓湁搴忔暟鍒椼€傞偅涔堟垜浠氨鎶奵[ i ]鐨勭涓€涓暟瀛樺叆heap銆?/div>
for (int i = 1;i <= n;i++) 
銆€銆€heap[i] = c[i][j];
鐒跺悗缁存姢涓€涓媓eap
while锛堟病鏈夎緭鍑哄n涓暟锛?
{
    杈撳嚭锛?
    鏀惧叆 鍫嗛《鏁版墍鍦ㄧ殑鏁扮粍鐨勪笅涓€涓暟
    缁存姢
}

浼樺寲锛?/p>

棣栧厛鎴戜滑鐭ラ亾锛宧eap[ i ]鍙渶瑕佺敤涓€涓€硷紝閭f垜浠彲涓嶅彲浠ュ湪heap闇€瑕佺殑鏃跺€欏啀鎶奵[ i ][ j ]绠楀嚭鏉ュ憿锛?/div>
鐒跺悗鎴戜滑鍙戠幇 c[ i ][ j ] = a[ i ]+b[ j ]涓璱鐩稿綋浜庣洰鍓嶅椤舵暟鎵€鍦ㄧ殑鏁扮粍锛宩灏辫〃绀轰笅涓€涓暟鐨勪笅鏍囥€?/div>
浜庢槸浼樺寲灏卞嚭鏉ヤ簡銆?/div>
for (int i = 1;i <= n;i++) heap[i] = a[i]+b[1]
     ……
    int t = from[1];
    step[t]++;
    heap[1]=a[t] + b[ step[t] ];

浠g爜锛?/p>

#include<bits/stdc++.h>
using namespace std;
int a[100000],b[100000],heap[100000],from[100000],step[100000],n,sum=1;
void swap(int x,int y)//鎵嬫墦swap浜ゆ崲锛屽悓鏃朵氦鎹㈡潵婧愭暟缁勩€?/span>
{
    int k = heap[x];
    heap[x] = heap[y];
    heap[y] = k;
    k = from[x];
    from[x] = from[y];
    from[y] = k;
}
int main()
{
    scanf("%d",&n);
    for (int i = 1;i <= n;i++) scanf("%d",&a[i]);
    for (int i = 1;i <= n;i++) scanf("%d",&b[i]);
    for (int i = 1;i <= n;i++) heap[i] = a[i]+b[1],from[i] = i,step[i] = 1; 
    //杩欎竴姝ュ氨鏄紭鍖栥€傛妸c鍘绘帀浜嗭紝鍙栬€屼唬涔嬬殑鏄幇鍋氱幇鍗栫殑鍚堟垚銆?/span>
    while (sum <= n)
    {
        printf("%d ",heap[1]);
        int t = from[1];
        step[t]++;
        heap[1]=a[t] + b[ step[t] ];//鐜板仛鐜板崠鐨勫悎鎴愩€?/span>
        int x = 1,s;
        while (x<<1 <= n)//缁忓吀鐨勪笅浼?/span>
        {
            s = x<<1;
            if (heap[s] > heap[s + 1] && s + 1 <= n) s++;
            if (heap[x] > heap[s])
            {
                swap(x,s);
                x = s;  
            }else break;
        }
        sum++;  
    }
    return 0;
}

 

 

以上是关于搴忓垪鍚堝苟的主要内容,如果未能解决你的问题,请参考以下文章

MapReduce涔婥ombiner鍚堝苟

SQL Server 鍚堝苟澶嶅埗 蹇収鎬荤粨

windows涓嬪皢澶氫釜鏂囦欢鍚堝苟鎴愪竴涓枃浠讹紝灏唗s鏂囦欢鍙樻垚MP3鏍煎紡

鍗曡皟搴忓垪渚嬮

Acwing-100-IncDec搴忓垪(宸垎)

92 搴忓垪鍖? 鍙嶅簭鍒楀寲