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

#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的更多相关文章
- hdu 1576 A/B(拓展欧几里得)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
随机推荐
- Visual Studio下使用jQuery的10个技巧
广泛流行的jQuery是一个开源的,跨浏览器和兼容CSS 3的JavaScript库,你可以用它简化你的JavaScript编码任务和操作(添加,编辑和删除)HTML内容中的DOM元素,本文介绍10个 ...
- PhotoSwipe简介
官方网站http://www.photoswipe.com/源码示例http://github.com/downloads/codecomputerlove/PhotoSwipe/code.photo ...
- 利用StringBuffer向字符串特定的重复子字符串插入数据
public class InsertDetail { public void insertInvoiceDetail(StringBuffer sb, String Label, String ...
- 搭建属于你自己的RAMOS系统 – WIN7+VHD+GURB map方式
前段时间加了一个内存条,将笔记本内存升级到了6G,由于之前用的是32位的win7不可以直接使用6G内存,便装了64位的系统.网上找资源的时候发现,大内存可以使用RamOS,从内存中虚拟出来一个硬盘,然 ...
- Gym 101102B The Little Match Girl(贪心+规律)
这个题目的做法不止一种,solve1:每个数字使用的火柴棒都在2~7之间,而且是连续的,就是2-7之前没有空着的数.这样就有一个结论,在下界为l,上界为r的情况下,假设有n个数,那么火柴棒总数一定在n ...
- SQL语句的优化
1.创建索引 表中数据经常需要用的哪些字段数据进行过滤,则添加该字段的索引,索引相当如一本书的目录,能加快查询数据的速度:同时在新建索引的时候,将需要查询的列,在包含性 列中新增上去,能减少查询语句的 ...
- UEFI安装Win10
启动方式: Legency UEFI 分区表类型: MBR 主引导记录 最多4个主分区 最大支持2.1T硬盘 GPT 基于可扩展固件接口(EFI) 分区数不限 NTFS格式最大支持256T硬盘 安装模 ...
- Ajax的基本请求/响应模型
一.Ajax工作核心 Ajax的核心是JavaScript对象XMLHttpRequest(简称XHR).它是一种支持异步请求的技术.可以通过使用XHR对象向服务器提出请求并处理响应,而不阻塞用户. ...
- [妙味JS基础]JS热身运动
知识点总结 获取ID元素 document.getElementById(' ') 事件:鼠标事件.键盘事件.系统事件.表单事件.自定义事件 onclick onmouseout onmouseove ...
- Java中的Math类的简单实用
System.out.println(Math.PI);//获取PI的值 System.out.println(Math.E);//常量E int min = Math.min(5, 4);//求最小 ...