其他分享
首页 > 其他分享> > HDU--Paint Pearls--思维构造

HDU--Paint Pearls--思维构造

作者:互联网

 

Paint Pearls

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3728    Accepted Submission(s): 1239


 

Problem Description

Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help.

In each operation, he selects some continuous pearls and all these pearls will be painted to their target colors. When he paints a string which has k different target colors, Lee will cost k2 points.

Now, Lee wants to cost as few as possible to get his ideal string. You should tell him the minimal cost.

 

 

Input

There are multiple test cases. Please process till EOF.

For each test case, the first line contains an integer n(1 ≤ n ≤ 5×104), indicating the number of pearls. The second line contains a1,a2,...,an (1 ≤ ai ≤ 109) indicating the target color of each pearl.

 

 

Output

For each test case, output the minimal cost in a line.

 

 

Sample Input


 

3 1 3 3 10 3 4 2 4 4 2 4 3 2 2

 

 

Sample Output


 

2 7

 

 

Source

2014 ACM/ICPC Asia Regional Xi'an Online

 

 

Recommend

hujie

 

例如N=12  0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 可以划分为多个区间,这多个区间是对称的。每个数字i会对应一个数字j。起点规律为

\\ 1\rightarrow 2\\3\rightarrow 12    \Rightarrow 2^2-2-1=1      2^4-12-1=3  所以找到第一个2的x次方>rear的位置-1即是head。每段数字可能有多个规律对应子串,找到他们对应的头部和尾部就好了。

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 100000+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
ll t[36];
void getM()
{
    t[0]=1;
    rep(i,1,22)
    {
        t[i]=t[i-1]*2;
    }
}
int a[maxn];
int mmp[maxn];
int main()
{
    int n;
    getM();
    while(scanf("%d",&n)!=EOF)
    {
        rep(i,0,n)
        {
            scanf("%d",&a[i]);
        }
        int rear=n;
        int head=0;
        while(rear>=0)
        {
            rep(i,0,20)
            {
                if(t[i]>rear)
                {
                    head=t[i]-rear-1;
                    break;
                }
            }
            for(int i=0;i<(rear-head+1)/2;i++)
            {
                mmp[head+i]=rear-i;
                mmp[rear-i]=head+i;
            }
            if(rear==head)
            {
                mmp[rear]=head;
            }
            rear=head-1;
        }
        ll ans=0;
        rep(i,0,n)
        {
            ans+=a[i]^mmp[a[i]];
        }
        printf("%lld\n",ans);
        rep(i,0,n)
        {
            if(i!=n)printf("%d ",mmp[a[i]]);
            else printf("%d\n",mmp[a[i]]);
        }
    }
    return 0;
}

 

标签:容器,HDU,STL,int,Pearls,Paint,pearls,include,rear
来源: https://blog.csdn.net/lanshan1111/article/details/100134018