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 ...
随机推荐
- java使用POI操作XWPFDocument中的XWPFParagraph(段落)对象的属性略解
我用的是office word 2016版 创建文本对象 XWPFDocument docxDocument = new XWPFDocument(); 创建段落对象 XWPFParagraph pa ...
- 洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
P3065 [USACO12DEC]第一!First! 题目链接:https://www.luogu.org/problemnew/show/P3065 题目描述 Bessie一直在研究字符串.她发现 ...
- POJ 1753 BFS
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44450 Accepted: 19085 Descr ...
- Spring Security 集成CAS实现单点登录
参考:http://elim.iteye.com/blog/2270446 众所周知,Cas是对单点登录的一种实现.本文假设读者已经了解了Cas的原理及其使用,这些内容在本文将不会讨论.Cas有Ser ...
- 【Luogu】P3930 SAC E#1 - 一道大水题 Knight
[题目]洛谷10月月赛R1 提高组 [题意]给定n*n棋盘和<=16个棋子,给几个棋子种类和攻击范围,现我方只有一马,求能否吃王. [算法]状压+BFS [题解]16种棋子中,马不能吃马,直接处 ...
- python学习笔记(九)之字符串
定义字符串 >>> mystring = 'Hello Python' >>> name = str('Mountain') >>> mystri ...
- 多github帐号的SSH key切换
我有两个github帐号,一个是个人所用,一个是为公司项目所用.如果是单用户(single-user),很方便,默认拿id_rsa与你的github服务器的公钥对比:如果是多用户(multi-user ...
- 今天安装了arch,感觉不错,这速度可以
虽然没有想想中的那么那么快,不过已经可以了 总结一下遇到的问题以及i自己安装的软件 1.u盘硬盘不能自动挂载 安装gvfs 2.不能读写挂载 安装ntfs-3g 3.时间不对 照wiki上的说 #ln ...
- DIV+CSS左右列高度自适应问题
其实解决DIV+CSS左右两列高度自适应的方法就是要注意两点:一是在最外层加上overflow:hidden,然后在左边列加上margin-bottom:-9999px;padding-bottom: ...
- css 背景透明,文字不透明
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...