• [POI2014]Beads


    题目大意:
      有$n(nleq10^6)$种颜色,第$i$种颜色有$c_i(sum c_ileq10^6)$个,指定第一个颜色为$a$,最后一个颜色为$b$,问对于一个长度为$m=sum c_i$的序列,是否能构造出一个染色方案满足相邻的颜色不相同。如果能,试构造出一种方案。

    思路:
      贪心。如果序列中有多个元素,开头结尾颜色相同而这种颜色只有一个,则显然不存在合法方案。每次选取当前数量最多的不同于前一个颜色的颜色,如果有一样多的,就尽量取和最后一个位置的颜色相同的颜色。如果没有可以取的元素或者倒数第二个元素颜色只能选和最后一个元素相同的颜色就说明不存在合法方案。洛谷和BZOJ随便AC,然而SZKOpuł上怎么卡常都是T。

    正常代码:

     1 #include<queue>
     2 #include<cstdio>
     3 #include<cctype>
     4 #include<algorithm>
     5 inline int getint() {
     6     register char ch;
     7     while(!isdigit(ch=getchar()));
     8     register int x=ch^'0';
     9     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
    10     return x;
    11 }
    12 const int N=1e6+1;
    13 int a[N],c[N];
    14 std::priority_queue<std::pair<int,int> > q;
    15 int main() {
    16     const int n=getint();
    17     a[1]=getint(),a[2]=getint();
    18     int m=0;
    19     for(register int i=1;i<=n;i++) {
    20         m+=c[i]=getint();
    21     }
    22     std::swap(a[2],a[m]);
    23     c[a[1]]--;
    24     if(m!=1) c[a[m]]--;
    25     if(c[a[1]]<0) {
    26         putchar('0');
    27         return 0;
    28     }
    29     for(register int i=1;i<=n;i++) {
    30         if(c[i]) q.push(std::make_pair(c[i],i==a[m]?i+n:i));
    31     }
    32     for(register int i=2;i<m;i++) {
    33         int x=q.top().second%n;q.pop();
    34         if(!x) x+=n;
    35         if(x==a[i-1]) {
    36             if(q.empty()) {
    37                 putchar('0');
    38                 return 0;
    39             }
    40             int y=q.top().second%n;q.pop();
    41             if(!y) y+=n;
    42             q.push(std::make_pair(c[x],x==a[m]?x+n:x));
    43             a[i]=y;
    44             if(--c[y]) q.push(std::make_pair(c[y],y==a[m]?y+n:y));
    45         } else {
    46             a[i]=x;
    47             if(--c[x]) q.push(std::make_pair(c[x],x==a[m]?x+n:x));
    48         }
    49     }
    50     if(a[m-1]==a[m]) {
    51         putchar('0');
    52         return 0;
    53     }
    54     for(register int i=1;i<=m;i++) {
    55         printf("%d%c",a[i]," 
    "[i==m]);
    56     }
    57     return 0;
    58 }
    View Code

    卡常代码:

      1 #pragma GCC optimize(3)
      2 #pragma GCC optimize("unroll-loops")
      3 #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
      4 #include<cstdio>
      5 #include<climits>
      6 #include<algorithm>
      7 #include<sys/mman.h>
      8 #include<sys/stat.h>
      9 typedef long long int64;
     10 class BufferedInputStream {
     11     private:
     12         char *buf,*p;
     13         int size;
     14     public:
     15         BufferedInputStream() {
     16             register int fd=fileno(stdin);
     17             struct stat sb;
     18             fstat(fd,&sb);
     19             size=sb.st_size;
     20             p=buf=reinterpret_cast<char*>(mmap(0,size,PROT_READ,MAP_PRIVATE,fileno(stdin),0));
     21         }
     22         char getchar() {
     23             return (p==buf+size||*p==EOF)?EOF:*p++;
     24         }
     25 };
     26 BufferedInputStream in;
     27 inline int getint() {
     28     register char ch;
     29     while(!__builtin_isdigit(ch=in.getchar()));
     30     register int x=ch^'0';
     31     while(__builtin_isdigit(ch=in.getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
     32     return x;
     33 }
     34 const int N=1e6+1;
     35 int a[N],c[N];
     36 class priority_queue {
     37     #define _left <<1
     38     #define _right <<1|1
     39     #define _par >>1
     40     private:
     41         int size;
     42         int64 val[N];
     43     public:
     44         priority_queue() {
     45             val[0]=LLONG_MAX;
     46         }
     47         void push(const int64 &x) {
     48             register int p=++size;
     49             for(;val[p _par]<x;p=p _par) {
     50                 val[p]=val[p _par];
     51             }
     52             val[p]=x;
     53         }
     54         int64 top() const {
     55             return val[1];
     56         }
     57         void pop() {
     58             register int p=1;
     59             const int64 x=val[size--];
     60             for(;p _left<=size&&(x<val[p _left]||x<val[p _right]);p=val[p _left]>val[p _right]?p _left:p _right) {
     61                 val[p]=std::max(val[p _left],val[p _right]);
     62             }
     63             val[p]=x;
     64         }
     65         bool empty() const {
     66             return !size;
     67         }
     68     #undef _left
     69     #undef _right
     70     #undef _par
     71 };
     72 inline int64 make_pair(const int &x,const int &y) {
     73     return (int64)x<<32|y;
     74 }
     75 inline int second(const int &x) {
     76     return x&0x7fffffff;
     77 }
     78 priority_queue q;
     79 int main() {
     80     const int n=getint();
     81     a[1]=getint(),a[2]=getint();
     82     int m=0;
     83     for(register int i=1;i<=n;i++) {
     84         m+=c[i]=getint();
     85     }
     86     std::swap(a[2],a[m]);
     87     c[a[1]]--;
     88     if(__builtin_expect(m!=1,1)) c[a[m]]--;
     89     if(__builtin_expect(c[a[1]]<0,0)) {
     90         __builtin_puts("0");
     91         __builtin_exit(0);
     92     }
     93     for(register int i=1;i<=n;i++) {
     94         if(__builtin_expect(c[i],1)) q.push(make_pair(c[i],i==a[m]?i+n:i));
     95     }
     96     for(register int i=2;i<m;i++) {
     97         int x=second(q.top());q.pop();
     98         if(__builtin_expect(x>n,0)) x-=n;
     99         if(__builtin_expect(x==a[i-1],0)) {
    100             if(__builtin_expect(q.empty(),0)) {
    101                 __builtin_puts("0");
    102                 __builtin_exit(0);
    103             }
    104             int y=second(q.top());q.pop();
    105             if(__builtin_expect(y>n,0)) y-=n;
    106             q.push(make_pair(c[x],x==a[m]?x+n:x));
    107             if(__builtin_expect(--c[a[i]=y],1)) q.push(make_pair(c[y],y==a[m]?y+n:y));
    108         } else {
    109             if(__builtin_expect(--c[a[i]=x],1)) q.push(make_pair(c[x],x==a[m]?x+n:x));
    110         }
    111     }
    112     if(__builtin_expect(a[m-1]==a[m],0)) {
    113         __builtin_puts("0");
    114         __builtin_exit(0);
    115     }
    116     for(register int i=1;i<=m;i++) {
    117         __builtin_printf("%d%c",a[i]," 
    "[i==m]);
    118     }
    119     __builtin_exit(0);
    120 }
    View Code
  • 相关阅读:
    Mycat 安全设置
    基于 HA 机制的 Mycat 高可用
    mycat中间件进行MySQL数据表的水平拆分
    mycat中间件进行MySQL数据库的垂直拆分
    mycat中间件进行MySQL的数据读写分离
    dubbo环境搭建--Windows
    分布式应用架构的发展演变RPC
    Java新特性--方法引用
    Java新特性-stream流
    Java新特性-四大函数式接口
  • 原文地址:https://www.cnblogs.com/skylee03/p/8659946.html
Copyright © 2020-2023  润新知