MM編程俱樂(lè)部 (線段樹(shù))

MM編程俱樂(lè)部

ACM is popular in HDU. Many girls want to learn more about programming skills in ACM. As one of assistances of Lcy, Yifenfei is now busy preparing a new club called “MM Programming Club”. Of Course, He will be the leader of the club, and teach these girls patiently. After the news posted around the campus, as many as N girls are determined to take part in the club. However, the numbers of members are limited; Yifenfei will only select K of them. It is quite a difficult problem. Here is a list of all information about N girls. Each of them has intelligence value and prettiness value. He also wants these K members such that the difference of intelligence between any two of them must not be greater than MAXK (If K = 1, the difference is zero). Now he wants to maximize the Sum of these K girls’ prettiness value.

Input

Many test case, please process to end of file. Each test first contains three integers N(1 <= N <= 200), K(1 <= K <= N), MAXK(1 <= MAXK <= 500). Then N lines follow. Each line contains two integers S, T (1 <= S, T <= 500). S represents the intelligence value, and T represents the prettiness value.

Output

If he can’t succeed in selecting K girls, print “-1”. Otherwise, Print the maximum the Sum of these K girls’ prettiness value.

Sample Input

2 1 0
1 2
2 3
2 2 0
1 2
2 3
2 2 1
1 2
2 3

Sample Output

3
-1
5

題目的大概意思是在n個(gè)女孩中取k個(gè)女孩在滿足她們之間的智商差小于一個(gè)MAXK的情況下蘸炸,使她們的顏值和最大躬络。
前面第一眼看到就是DFS+剪枝,時(shí)間復(fù)雜度是O(n!)會(huì)TLE搭儒。
另一種是采取貪心的思想洗鸵,將女孩按智商排序,然后遍歷所有智商差為k的區(qū)間仗嗦,區(qū)間按顏值后取前k個(gè)和sum,然后取所有區(qū)間的sum中最大的一個(gè)甘凭。這種方法的時(shí)間復(fù)雜度是O(n^2*logn)稀拐。

//由于本人比較懶
//這里貼出的是楊同學(xué)的代碼
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=10000+5;
struct node{
    int w,v;
}stu[maxn];
bool cmp1(node x,node y)
{
    return x.w<y.w;
}
bool cmp2(node x,node y)
{
    return x.v>y.v;
}
int n,k,limit,res,ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n>>k>>limit)
    {
        res=-1;
        for(int i=1;i<=n;i++)
        cin>>stu[i].w>>stu[i].v;
        sort(stu+1,stu+n+1,cmp1);
        for(int i=1;i<=n;i++)
        {
            ans=stu[i].v;
            int w=stu[i].w;
            vector<node> tmp;
            for(int j=i+1;stu[j].w-w<=limit&&j<=n;j++)
                tmp.push_back(stu[j]);
            if(tmp.size()<k-1) continue;
            sort(tmp.begin(),tmp.end(),cmp2);
            for(int m=0;m<k-1;m++)
                ans+=tmp[m].v;
            res=max(res,ans);
        }
        cout<<res<<endl;
    }
    return 0;
}

第三種方法是用線段樹(shù)維護(hù),建一顆(1丹弱,T)的的線段樹(shù)德撬,維護(hù)區(qū)間內(nèi)的女孩個(gè)數(shù)與顏值和,先將所有女孩按智商排序躲胳,然后遍歷更新到線段樹(shù)中去蜓洪,如果樹(shù)中人數(shù)等于k或者是智商差值大于MAXK,一個(gè)個(gè)減去最左邊的女孩直到滿足要求坯苹,取所有當(dāng)樹(shù)中人數(shù)等于k的顏值和隆檀,也就是MAX{tree[1].sum}。首先一次排序的時(shí)間復(fù)雜度是nlogn粹湃,后面的遍歷每次樹(shù)的更新操作是logT恐仑,時(shí)間復(fù)雜度是nlogT,由于T>n所以總的時(shí)間復(fù)雜度就是nlogT。

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
struct node{
    int s,t;
    bool operator < (const node & e) const {
            return s<e.s;
        }
};
struct seg{
    int l,r,mid;
    int s;
    int num;
};
seg tree[505<<2];
node n_[205];
int ri;
void build(int k,int l,int r){
    tree[k].l=l;
    tree[k].r=r;
    tree[k].mid=(l+r)>>1;
    tree[k].s=tree[k].num=0;
    if(l==r)return;
    build(k<<1,l,tree[k].mid);
    build(k<<1|1,tree[k].mid+1,r);
}
void pushup(int k){
    tree[k].s=tree[k<<1].s+tree[k<<1|1].s;
}
void add(int k,int x){
    tree[k].num++;
    if(tree[k].l==tree[k].r){
        tree[k].s+=x;
        return;
    }
    if(x<=tree[k].mid)add(k<<1,x);
    else add(k<<1|1,x);
    pushup(k);
}
void sub(int k,int x){
    tree[k].num--;
    if(tree[k].l==tree[k].r){
        tree[k].s-=x;
        return;
    }
    if(x<=tree[k].mid)sub(k<<1,x);
    else sub(k<<1|1,x);
    pushup(k);
}
int sum(int k,int x){
   
    if(!x)return 0;
    if(tree[k].l==tree[k].r){
        return x*tree[k].l;
    }
    if(x<=tree[k<<1|1].num)return sum(k<<1|1,x);
    else return sum(k<<1,x-tree[k<<1|1].num)+tree[k<<1|1].s;
}
int main(){
    int n,k,d;
    while (scanf("%d%d%d",&n,&k,&d)!=EOF){
       for(int i=1;i<=n;i++){
          scanf("%d%d",&n_[i].s,&n_[i].t);
       }
       sort(n_+1,n_+1+n);
        ri=1;
        build(1,1,500);
        int res=-1;
        for(int i=1;i<=n;i++){
            add(1,n_[i].t);
            while (ri<=i&&n_[i].s-n_[ri].s>d){
                sub(1,n_[ri].t);
                ri++;
            }
            if(tree[1].num>=k){
                res=max(res,sum(1,k));
            }
        }
        printf("%d\n",res);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末为鳄,一起剝皮案震驚了整個(gè)濱河市裳仆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌孤钦,老刑警劉巖歧斟,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異偏形,居然都是意外死亡静袖,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)壳猜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)勾徽,“玉大人,你說(shuō)我怎么就攤上這事〈悖” “怎么了畅姊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)吹由。 經(jīng)常有香客問(wèn)我若未,道長(zhǎng),這世上最難降的妖魔是什么倾鲫? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任粗合,我火速辦了婚禮,結(jié)果婚禮上乌昔,老公的妹妹穿的比我還像新娘隙疚。我一直安慰自己,他們只是感情好磕道,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布供屉。 她就那樣靜靜地躺著,像睡著了一般溺蕉。 火紅的嫁衣襯著肌膚如雪伶丐。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,482評(píng)論 1 302
  • 那天疯特,我揣著相機(jī)與錄音哗魂,去河邊找鬼。 笑死漓雅,一個(gè)胖子當(dāng)著我的面吹牛录别,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播故硅,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼庶灿,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了吃衅?” 一聲冷哼從身側(cè)響起往踢,我...
    開(kāi)封第一講書(shū)人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎徘层,沒(méi)想到半個(gè)月后峻呕,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡趣效,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年瘦癌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片跷敬。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡讯私,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情斤寇,我是刑警寧澤桶癣,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站娘锁,受9級(jí)特大地震影響牙寞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜莫秆,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一间雀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧镊屎,春花似錦惹挟、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至党巾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間霜医,已是汗流浹背齿拂。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留肴敛,地道東北人署海。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像医男,于是被迫代替她去往敵國(guó)和親砸狞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354