[BJWC2011]元素

來源:互聯網
上載者:User

標籤:tor   +=   最大值   mat   inline   cti   turn   line   bool   

[BJWC2011]元素題目大意:

\(n(n\le1000)\)個物品,每個物品有兩個屬性:序號\(a_i(a_i\le10^{18})\)和權值\(b_i(b_i\le10000)\)。現在從中選取若干個物品,使得序號異或和不為\(0\),求權值和最大值。

思路:

按照權值從大到小排序貪心地構造線性基。

原始碼:
#include<cstdio>#include<cctype>#include<algorithm>#include<functional>typedef long long int64;inline int64 getint() {    register char ch;    while(!isdigit(ch=getchar()));    register int64 x=ch^'0';    while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');    return x;}const int N=1000,B=60;struct Node {    int64 id;    int w;    bool operator > (const Node &rhs) const {        return w>rhs.w;    }};Node a[N],b[B];int main() {    const int n=getint();    for(register int i=0;i<n;i++) {        a[i].id=getint();        a[i].w=getint();    }    std::sort(&a[0],&a[n],std::greater<Node>());    for(register int i=0;i<n;i++) {        for(register int j=B-1;j>=0;j--) {            if((a[i].id>>j)&1) {                if(b[j].id==0) {                    b[j]=a[i];                    break;                }                a[i].id^=b[j].id;            }        }    }    int ans=0;    for(register int i=0;i<B;i++) {        ans+=b[i].w;    }    printf("%d\n",ans);    return 0;}

[BJWC2011]元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.