hdu5798
官方题解:
考虑去掉abs符号,发现只有相邻两个数的最高位被影响了才会影响abs的符号,所以可以按照最高位不一样的位置分类,之后考虑朴素枚举x从0到2^20,每次的复杂度是O(400),无法通过,考虑优化,第一种方法是用DFS来进行枚举,第二种则是加入记忆化
用dfs枚举简单一点
#include<bits/stdc++.h> using namespace std;
typedef long long ll;
int d[],n,mx,ansx;
ll anss,c[][];
void dfs(int i,int x,ll s)
{
if (s>anss) return;
if (i>mx)
{
if (s<anss||(s==anss&&x<ansx))
{
anss=s;
ansx=x;
}
return;
}
for (d[i]=; d[i]<=; d[i]++)
{
ll ns=s+c[i][i];
for (int j=; j<i; j++)
if (d[i]^d[j]) ns-=c[i][j];
else ns+=c[i][j];
dfs(i+,x+d[i]*(<<i),ns);
}
} int work(int a,int b,int h)
{
if (a<b) swap(a,b);
for (int i=h; i>=; i--)
c[h][i]+=((a>>i&)-(b>>i&))<<i;
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d",&n);
int a,b;
scanf("%d",&a);
memset(c,,sizeof(c));
mx=;
for (int i=; i<n; i++)
{
scanf("%d",&b);
int h=;
while (h>=&&!((a>>h&)^(b>>h&))) h--;
work(a,b,h);
a=b;
}
while (mx>=&&!c[mx][mx]) mx--;
anss=1e18; ansx=;
dfs(,,);
printf("%d %lld\n",ansx,anss);
}
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int d[],n,mx,ansx;
ll anss,c[][];
void dfs(int i,int x,ll s)
{
if (s>anss) return;
if (i>mx)
{
if (s<anss||(s==anss&&x<ansx))
{
anss=s;
ansx=x;
}
return;
}
for (d[i]=; d[i]<=; d[i]++)
{
ll ns=s+c[i][i];
for (int j=; j<i; j++)
if (d[i]^d[j]) ns-=c[i][j];
else ns+=c[i][j];
dfs(i+,x+d[i]*(<<i),ns);
}
} int work(int a,int b,int h)
{
if (a<b) swap(a,b);
for (int i=h; i>=; i--)
c[h][i]+=((a>>i&)-(b>>i&))<<i;
} int main()
{
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%d",&n);
int a,b;
scanf("%d",&a);
memset(c,,sizeof(c));
mx=;
for (int i=; i<n; i++)
{
scanf("%d",&b);
int h=;
while (h>=&&!((a>>h&)^(b>>h&))) h--;
work(a,b,h);
a=b;
}
while (mx>=&&!c[mx][mx]) mx--;
anss=1e18; ansx=;
dfs(,,);
printf("%d %lld\n",ansx,anss);
}
}
hdu5798的更多相关文章
- hdu5798 Stabilization
温习一下多校的题目 这题主要抓住一点,亦或值的贡献是固定的 所以按位搜索即可 #include<bits/stdc++.h> using namespace std; typedef lo ...
随机推荐
- pg_basebackup: invalid tar block header size
问题: 在使用pg_basebackup搭建备节点时,由于pg_basebackup本身使用的是int整型来保存传输的数据大小,当传输的数据大于4G的话,整数就会溢出,进而报出:pg_baseback ...
- Codeforces Round #340 (Div. 2) A
A. Elephant time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 【题解】最大公约数之和 V3 51nod 1237 杜教筛
题目传送门 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 数学题真是做的又爽又痛苦,爽在于只要推出来公式基本上就 ...
- bzoj 1520 [POI2006]Szk-Schools 费用流
[POI2006]Szk-Schools Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 743 Solved: 381[Submit][Status][ ...
- html 5 新特性
现在html 5技术是最新的html标准,掌握html 5已经变得非常重要,以下是我查看相关资料后对html 5 的新特性的总结,方便大家对比学习.html 5的新特性1.取消了一些过时的html 4 ...
- Spring @Async开启异步任务
1. 开启异步 @SpringBootApplication @EnableAsync //开启异步任务 public class Application { @Bean(name="pro ...
- HDU 5869 Different GCD Subarray Query 树状数组+离线
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
- LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合
题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...
- 2015/8/9 到家了,学完了CodeCademy的Python
昨天坐了20多个小时的硬座回家.发现在网络信号差的火车上也是学习的好地方.如果你的手机电量不足的话,带上两本书简直是绝配.我在火车上阅读了两百多页的内容,并没有多大的疲累,那样无聊的环境里面能看书学习 ...
- RDLC - 后台代码直接导出Excel/PDF/Word格式
最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...