官方题解:

考虑去掉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的更多相关文章

  1. hdu5798 Stabilization

    温习一下多校的题目 这题主要抓住一点,亦或值的贡献是固定的 所以按位搜索即可 #include<bits/stdc++.h> using namespace std; typedef lo ...

随机推荐

  1. pushViewController:animated:的问题

    1.在AppDelegate.m中: 2.在SecondViewController.h中: 3.在FirstViewController.m中: 4.在SecondViewController.m中 ...

  2. Trades FZU - 2281 (大数+贪心)

    This is a very easy problem. ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n ...

  3. ObservableCollection 类

    假设您正在创建 Windows 窗体应用程序,并且已将 DataGridView 控件绑定到标准 List(Of Customer) 数据结构.您希望能够使网格中的项目与基础数据源中的值保持同步.也就 ...

  4. 耗子学Python了(2)__Python开发“Hello World”

    一:开发工具 在网上看到的用的开发工具Aptana Studio,我下载的是Aptana_Studio_3_Setup_3.6.1.exe,在安装的过程中啊,出现了各种问题,然后安装后了也出现打不开的 ...

  5. Lucene6.6添加索引数据时字符个数超限,字符数不能超过BYTE_BLOCK_SIZE=32766

    最近发现Lucene6.6版本添加索引数据字符数超过32766时,出现报错,而Lucene4.6版本中则未出现这一问题,原因如下: 概述:         添加索引数据时,对于分词字段,分词后的Ter ...

  6. 解决Sublime Install Package的There are no packages available for install问题(channel_v3.json)

    Sublime版本 Sublime Text 3.1.1 Build 3176 自己也尝试了很多次,所以这一解决办法仅是可能解决你的问题 一.解决简要描述 造成的原因大致是 无法通过request去获 ...

  7. spoj104 highways 生成树计数(矩阵树定理)

    https://blog.csdn.net/zhaoruixiang1111/article/details/79185927 为了学一个矩阵树定理 从行列式开始学(就当提前学线代了.. 论文生成树的 ...

  8. ZOJ3229 Shoot the Bullet [未AC]

    Time Limit: 2 Seconds      Memory Limit: 32768 KB      Special Judge Gensokyo is a world which exist ...

  9. 【bzoj】1717 [Usaco2006 Dec]Milk Patterns 产奶的模式

    [算法]后缀数组 [题解]后缀数组 由于m太大,先离散化. 然后处理SA和LCP. 最后用单调队列处理即可. 注意实际上队列头尾长度限制是K-1. 删队尾不要删过头 i≥K才能开始统计答案. #inc ...

  10. springboot:Spring boot中mongodb的使用(山东数漫江湖)

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...