hdu 6406 Taotao Picks Apples 锛?018 Multi-University Training Contest 8 1010锛夛紙浜屽垎锛屽墠缂€鍜岋級

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 6406 Taotao Picks Apples 锛?018 Multi-University Training Contest 8 1010锛夛紙浜屽垎锛屽墠缂€鍜岋級相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/ota' title='ota'>ota   鍋囪   problem   濂界殑   space   its   tao   鏆村姏   int   

閾炬帴锛歨ttp://acm.hdu.edu.cn/showproblem.php?pid=6406

鎬濊矾锛?/p>

鏆村姏锛岄澶勭悊涓変釜鍓嶇紑鍜岋細銆?,n銆戞瀛愪細琚憳鎺夛紝1鍒板綋鍓嶇偣鐨勬渶澶у€硷紝1鍒板綋鍓嶇偣琚憳鎺夌殑妗冨瓙鐨勬暟閲忥紝鐒跺悗鎴戜滑鏋氫妇淇敼p鐐归€犳垚鐨勬墍鏈夊奖鍝嶏紝锛?/p>

 1锛屽亣濡傛柊杈撳叆鐨勭偣姣斿師鍏堢殑鐐圭殑鍊兼洿澶э紝閭d箞鎴戜滑瀵逛慨鏀瑰悗p杩欎釜鐐圭殑鍊煎拰銆?,p-1銆戠殑鏈€澶у€煎叧绯昏繘琛屽垎鏋愶紝涔熷氨鏄垎鏋愬墠鍗婃鐨勫奖鍝嶏細锛?锛夊鏋減鐐瑰ぇ浜?-p-1鐨勬渶澶у€肩殑鏃跺€欐垜浠洿鎺ュ埄鐢ㄥ墠缂€鍜孫(1)寰楀埌銆?锛宲-1銆戞湁澶氬皯涓瀛愯鎽樻帀锛岀劧鍚庡姞涓婂綋鍓嶈繖涓€傦紙2锛夊鏋減鐐瑰皬浜庣瓑浜庛€?锛宲-1銆戠殑鏈€澶у€兼椂锛屽鍓嶅崐娈靛拰鍚庡崐娈甸兘涓嶄細閫犳垚褰卞搷锛岀洿鎺ヨ緭鍑洪澶勭悊鐨勫埌鐨勩€?锛宯銆戠殑鍊煎氨濂戒簡锛涚劧鍚庢垜浠垎鏋愬悗鍗婃鐨勫奖鍝嶏細锛?锛夊鏋減鐐瑰ぇ浜庛€?,p-1銆戠殑鏈€澶у€硷紝閭d箞鍚庡崐娈典腑鍙湁琚憳鎺夌殑妗冨瓙浼氬彈鍒板奖鍝嶏紝鎴戜滑鐩存帴浜屽垎鎵惧埌銆恜+1,n銆戜腑琚憳鎺夌殑妗冨瓙鐨勫€煎ぇ浜庤淇敼鍚庣殑鍊糲鐨勪笅鏍囷紝鍑忎竴涓嬶紝灏卞緱鍑轰簡锛屽悗鍗婃浼氳鎽樻帀鐨勬瀛愪腑鏈夊嚑涓湪p鐐硅淇敼鍚庝緷鏃ч渶瑕佹憳鎺夛紝灏嗗悗鍗婃鐨勫€间笌鍓嶅崐娈电殑鍊煎姞璧锋潵灏卞ソ浜嗐€傦紙2锛夊鏋減鐐瑰皬浜庣瓑浜庛€?,p-1銆戠殑鏈€澶у€硷紝閭d箞鍚庡崐娈典笉閫犳垚褰卞搷锛?/p>

2.鍋囧鏂拌緭鍏ョ殑鐐瑰皬浜庣瓑浜庡師鍏堢殑鐐瑰€硷紝渚濇棫瀵逛慨鏀瑰悗p杩欎釜鐐圭殑鍊煎拰銆?,p-1銆戠殑鏈€澶у€艰繘琛屽垎鏋愶細鍏堝垎鏋愬墠鍗婃鐨勫奖鍝嶏細锛?锛夊鏋減鐐瑰ぇ浜?-p-1鐨勬渶澶у€肩殑鏃跺€欐垜浠洿鎺ュ埄鐢ㄥ墠缂€鍜孫(1)寰楀埌銆?锛宲-1銆戞湁澶氬皯涓瀛愯鎽樻帀锛岀劧鍚庡姞涓婂綋鍓嶈繖涓€傦紙2锛夊鏋減鐐瑰皬浜庣瓑浜庛€?,p-1銆戠殑鏈€澶у€硷紝閭d箞杩欎釜鐐逛緷鏃у鍓嶅崐娈典笉閫犳垚褰卞搷锛屽墠鍗婃鐨勫€肩瓑浜庛€?,p-1銆戜腑琚憳鎺夌殑妗冨瓙鐨勬暟閲忥紝

鎺ヤ笅鏉ュ氨鏄渶闅惧緱鍚庡崐娈电殑璁ㄨ锛?/p>

涔嬪墠澶勭悊杩欓噷鎬濊矾琚崱浜嗗緢涔咃細锛?锛夊鏋減鐐瑰ぇ浜?-p-1鐨勬渶澶у€?/p>

鍥犱负褰撳墠鐐瑰彉灏忎簡锛屽鍚庨潰鐐归€犳垚鐨勫奖鍝嶅氨鏄細鎽樻帀涓€浜涘師鍏堜笉浼氳鎽樼殑妗冨瓙锛岄偅涔堟€庝箞纭畾杩欎簺妗冨瓙鐨勫叿浣撴槸閭d簺鍛紵 瀹為檯涓婃垜浠彲浠ユ帹鍑猴紝杩欎簺澶氭憳鐨勬瀛愰兘鏄湪1鍒板綋鍓嶅潗鏍囦腑鍙皬浜巔鐨勬暟锛屽洜涓鸿繖浜涙暟鏄綋p鍙樺皬浜嗘墠澶氬嚭鏉ョ殑锛岄偅涔堜粬浠湪1鍒板綋鍓嶇偣鑲畾鏄彧灏忎簬p鐐癸紝閭d箞鎴戜滑鎶婂畠鍔犲埌鍓嶄竴缁翠负p鐨剉ecvector鏁扮粍閲岋紝鑷充簬鎬庝箞鎵惧埌杩欎簺鍊硷紝鎴戜滑鍙澶氱淮鎶や竴涓浜屽ぇ鍊煎氨鍙互浜嗭紝杩欎簺绗簩澶у€煎氨鏄湁鍙兘浼氬鎽樼殑妗冨瓙銆傚綋p鐐逛负鍘熷厛浼氳鎽樼殑妗冨瓙鏃舵垜浠瀛樺湪v[p]閲岀殑鍊艰繘琛屼簩鍒嗘壘鍒版瘮p鍊间慨鏀瑰悗澶х殑閭d簺鍊硷紝涔熷氨鏄悗鍗婃鏂板鐨勫€硷紝鍐嶅姞涓婂師鍏堝氨瑕佹憳寰楀€笺€?/p>

锛?锛?濡傛灉灏忎簬绛変簬鐨勮瘽锛屼緷鏃т笉閫犳垚褰卞搷锛岀洿鎺ュ幓鍓嶇紑鍜屽氨濂戒簡

3锛屽亣璁捐緭鍏ョ殑鐐瑰拰鍘熸潵鐨勭偣鍊间竴鏍凤紝鐩存帴鍓嶇紑鍜屽彇

 

杩欐牱涓€鍏辨槸浜旂涓昏鎯呭喌锛?涓ょ鐢ㄤ簩鍒哋(logn)锛屽彟澶栦笁绉嶇洿鎺ュ墠缂€鍜孫(1)锛岀◢寰绠椾究鍙煡涓嶄細瓒呮椂銆傛渶鍚庤窇浜?05ms锛岄鐩粰浜?000mS,绠楄窇鐨勫緢蹇殑浜嗭紝 灏辨槸瀹炵幇鏈夌偣澶嶆潅锛屽簲璇ヨ繕鏈夋洿濂界殑瑙f硶锛屼笉杩囨瘮璧涚殑鏃跺€欐病鎯抽偅涔堝锛岀洿鎺ユ毚鍔涜幗杩囧幓浜嗭紝杩樺ソ杩囦簡瑕佷笉鏀逛唬鐮佽鏀瑰埌鍚愩€傘€俀AQ

瀹炵幇浠g爜锛?/p>

#include<bits/stdc++.h>
using namespace std;
const int M = 1e5+10;
struct node{
    int id,val;
};
vector<int>v[M];
int a[M],ans[M],maxx[M],b[M],arr[M];

int main()
{
    int t,n,q,p,c;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&q);
        for(int i = 1;i <= n;i ++){
            scanf("%d",&a[i]);
        }
        node mx,mx1;
        mx.val = 0; mx1.val = 0,mx.id = 0,mx1.id = 0;
        int cnt = 0,num = 0;
        maxx[0] = 0;
        for(int i = 1;i <= n;i ++){
            if(mx.val < a[i]){
                mx1.val = mx.val; mx1.id = mx.id;
                mx.val = a[i]; mx.id = i; b[cnt++] = a[i];
                num++;
            }
            else if(mx1.val < a[i]){
                mx1.val = a[i]; mx1.id = i;
                v[mx.id].push_back(a[i]);
            }
            maxx[i] = mx.val;  ans[i] = num;
        }
        while(q--){
            scanf("%d%d",&p,&c);
            int sum = 0;
            if(c > a[p]){
                if(c > maxx[p-1]){
                sum ++;  sum += ans[p-1];
                int kk =  upper_bound(b,b+cnt,c) - b;
                if(kk != cnt) sum += cnt-kk;
                }
                else
                    sum = ans[n];
            }
            else if(c == a[p])  sum = ans[n];
            else{
                if(c > maxx[p-1]) sum++,sum += ans[p-1];
                else sum += ans[p-1];
                if(a[p] == maxx[p]){
                     int kk = upper_bound(v[p].begin(),v[p].end(),c)-v[p].begin();
                sum += v[p].size() - kk;
                int kk1 =  upper_bound(b,b+cnt,a[p]) - b;
                if(kk1 != cnt)  sum += cnt-kk1;
                }
                else
                sum = ans[n];
            }
            printf("%d
",sum);
        }
        for(int i = 0;i <= n;i ++){
            maxx[i] = 0; ans[i] = 0; v[i].clear();
        }
        for(int i = 0;i <= cnt;i ++)  b[i] = 0;
    }
    return 0;
}

 

以上是关于hdu 6406 Taotao Picks Apples 锛?018 Multi-University Training Contest 8 1010锛夛紙浜屽垎锛屽墠缂€鍜岋級的主要内容,如果未能解决你的问题,请参考以下文章

hdu 6406Taotao Picks Apples

hdu 6406 Taotao Picks Apples 锛?018 Multi-University Training Contest 8 1010锛夛紙浜屽垎锛屽墠缂€鍜岋級

HDU暑假多校第八场J-Taotao Picks Apples

J. Taotao Picks Apples(st&二分)

hdu-6406-dp+ST表

hdu 6406(思路+数据结构)