方法太厉害了....看了官方题解的做法....然后...想了很久很久才知道他想表达什么....

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
char c = getchar(); while(!isdigit(c)) c = getchar();
int x = ;
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
return x;
} int T,n;
LL a[],f[][],ans1,ans2;
int d[]; void dfs(int s,LL b, LL sum)
{
if(s==-)
{
if(sum<ans1) ans1=sum,ans2=b;
else if(sum==ans1) ans2=min(ans2,b);
return;
} for(d[s]=;d[s]<=;d[s]++)
{
LL t=sum; t=t+f[s][s];
for(int j=;j>s;j--)
{
if((d[j]+d[s])%==) t=t+f[j][s];
else t=t-f[j][s];
}
dfs(s-,b|(d[s]*(<<s)),t);
}
} int main()
{
scanf("%d",&T); while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%lld",&a[i]);
memset(f,,sizeof f); ans1=, ans2=;
for(int i=;i<=n;i++)
{
ans1=ans1+abs(a[i]-a[i-]);
if(a[i]==a[i-]) continue;
int h=-;
for(int j=;j>=;j--)
{
int x1=,x2=;
if(a[i]&(<<j)) x1=; if(a[i-]&(<<j)) x2=;
if(x1==x2) continue; h=j; break;
}
int x=max(a[i],a[i-]),y=min(a[i],a[i-]);
for(int j=h;j>=;j--)
{
int x1=,x2=;
if(x&(<<j)) x1=; if(y&(<<j)) x2=;
f[h][j]=f[h][j]+(x1-x2)*(<<j);
}
}
dfs(,,);
printf("%lld %lld\n",ans2,ans1);
}
return ;
}

HDU 5798 Stabilization的更多相关文章

  1. hdu 1576 A/B(拓展欧几里得)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

随机推荐

  1. php常用数学函数

    php常用数学函数1. bcadd 任意精度数的相加2. bcsub 任意精度数的减法3. bcmul 乘法, bcdiv除法 4. bcmod 取余数. (比%功能更强大)5. bcpow 幂函数运 ...

  2. Mybatis(一)

     jdbc开发 1)优点:简单易学,上手快,非常灵活构建SQL,效率高 2)缺点:代码繁琐,难以写出高质量的代码(例如:资源的释放,SQL注入安全性等) 开发者既要写业务逻辑,又要写对象的创建和销毁, ...

  3. hive-1.2.1安装步骤

    一.Hive安装和配置 1.先决条件 已经安装好hadoop-2.4.1,hbase-1.0.0. 2.下载Hive安装包 当前Hive可到apache官网下载,选择的是hive-1.2.1.运行: ...

  4. 二十五、oracle pl/sql进阶--控制结构(分支,循环,控制)

    一.pl/sql的进阶--控制结构在任何计算机语言(c,java,pascal)都有各种控制语句(条件语句,循环结构,顺序控制结构...),在pl/sql中也存在这样的控制结构.在本部分学习完成后,希 ...

  5. 面试题:实现一个方法clone;可以对js五种数据类型进行值复制

    //先来方法的代码function clone(obj) { var copy; switch(typeof obj){ case 'number': case 'string': case 'boo ...

  6. mac地址和ip地址要同时存在么?

    刚再整理笔记的时候,突然想到了一个问题,网络中为什么要同时存在mac地址和ip地址呢?那现在就来随便扯扯吧. 这个问题其实是可以分成两个问题的:Q1:如果只有mac地址,没有ip地址可以么? 众所周知 ...

  7. Struts2第四天

    Struts2第四天 昨天: 自定义的拦截器:继续methodFilterInterceptor,可以指定哪些方法需要拦截或者不拦截. Intercepters(配置拦截器),intercepter( ...

  8. IIS的虚拟目录和子应用程序

    一.虚拟目录     虚拟目录是指在发布的网站下建立一个虚拟子目录,指定一个固定的物理路径做为站点的应用路径.     1. 虚拟目录与父级站点共用一个应用程序池,网站Test是在D盘下建立了的虚拟目 ...

  9. JavaScript功能规划的基本语法总结

    1.迭代数组 1)map 方法会迭代数组中的每一个元素,并根据回调函数来处理每一个元素,最后返回一个新数组.注意,这个方法不会改变原始数组 //例子 var oldArray = [1,2,3,4,5 ...

  10. c# socket传输struct类型

    data结构体类型 public struct datas    { public string test1; public string test2;    } //socket服务器端 publi ...