比赛的时候抄poj2926的模板,但改不来啊orz

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int dem=; //维数
const int maxxn=;
const double inf=1e200;
struct Point{
double x[dem];
}p[maxxn];
int n;
double minx[<<dem], maxx[<<dem]; double solve(){
int i, j, k, t, tmp=<<dem;
double s, ans=-inf;
for(i=; i<tmp; i++){
minx[i]=inf;
maxx[i]=-inf;
}
for(i=; i<n; i++){
for(j=; j<tmp; j++){
t=j;s=;
for(k=; k<dem; k++){
if(t&)
s+=p[i].x[k];
else s-=p[i].x[k];
t>>=;
}
if(maxx[j]<s)maxx[j]=s;
if(minx[j]>s)minx[j]=s;
}
}
for(i=; i<tmp; i++){
if(maxx[i]-minx[i]>ans)
ans=maxx[i]-minx[i];
}
return ans;
}
int main(){
//freopen("1.txt", "r", stdin);
int i, j;
while(scanf("%d", &n)!=EOF){
for(i=; i<n; i++){
for(j=; j<dem; j++)
scanf("%lf", &p[i].x[j]);
}
printf("%.2f\n", solve());
}
return ;
}

后来看了标程,感觉上面那个算法是瞎搞的,于是改成了正确的姿势:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<ctime>
using namespace std;
#define LL double int T, n, m, K ;
LL A[], B[], Ans,val,x[]; int main()
{
int i, k, S; K=;
cin>>n;
Ans = ;
for (int i = ; i < ; i++)A[i] = B[i] = -1e200; for (i = ; i <= n; i++)
{ for (k = ; k<K; k++)
scanf("%lf", &x[k]);
for (S = ; S< << K; S++)
{
LL Sum = ;
for (k = ; k<K; k++)
Sum += x[k] * ((((S >> k) & ) << ) - );
A[S] = max(A[S], Sum);
}
} for (S = ; S< << K; S++)
{Ans = max(Ans, A[S] + A[( << K) - - S]);
}
printf("%.2lf\n", Ans); }
/*
3
2 5 6 2 1.5
1.2 3 2 5 4
7 5 3 2 5 */

这个程序的二进制操作比较秀,具体来说:

((((S >> k) & 1) << 1) - 1)这一句是if(S的第k位为1)f=1; else f=-1 的缩写(感觉反而更麻烦了233)

A[S] + A[(1 << K) - 1 - S]这一句的意思是把符号相反的一组最优向量加起来。因为(1 << K) - 1 - S与S的0,1 互补,对应向量里+、- 互补。

标程唯一被hack的地方是数组初始化时用了memset,应该全部赋值为-inf

下面给出标程(改了一句)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<ctime>
using namespace std;
#define LL long long int T, n, m, K, val, x[];
LL A[], B[], Ans; int main()
{
int i, k, S;
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d", &n, &m, &K);
Ans = ;
//memset(A, 0, sizeof(A));
//memset(B, 0, sizeof(B));
for (int i = ; i < ; i++)A[i] = B[i] = -1e18;
for (i = ; i <= n; i++)
{
scanf("%d", &val);
for (k = ; k<K; k++)
scanf("%d", &x[k]);
for (S = ; S< << K; S++)
{
LL Sum = val;
for (k = ; k<K; k++)
Sum += x[k] * ((((S >> k) & ) << ) - );
A[S] = max(A[S], Sum);
}
} for (i = ; i <= m; i++)
{
scanf("%d", &val);
for (k = ; k<K; k++)
scanf("%d", &x[k]);
for (S = ; S< << K; S++)
{
LL Sum = val;
for (k = ; k<K; k++)
Sum += x[k] * ((((S >> k) & ) << ) - );
B[S] = max(B[S], Sum);
}
} for (S = ; S< << K; S++)
Ans = max(Ans, A[S] + B[( << K) - - S]);
printf("%lld\n", Ans);
}
} /*
1
2 2 1
0 1
0 2
0 4
0 3 2
2 2 1
0 233
0 666
0 123
0 456
2 2 1
100 0 1000 100 1000 100
100 0 */

hdu6435 Problem J. CSGO标程讲解以及改正标程的一个错误(本来第一个样例过不了2333) 以及 poj2926 五维曼哈顿距离模板的更多相关文章

  1. [hdu6435]Problem J. CSGO

    题目大意:给定$n$个$A$类元素和$m$个$B$类元素,每类元素有值$S$和$k$个值$x_0,x_1,\dots,x_{k-1}(k\leqslant 5)$. 要求选出一个$A$类元素$a$和$ ...

  2. HDU - 6435 Problem J. CSGO (曼哈顿距离变换)

    题目大意:有两类武器(主武器和副武器),每类有若干把,每把武器都有一个基础属性S,以及k个附加属性,让你选一把主武器M和一把副武器S,使得最大. 显然后面的和式是一个k维的曼哈顿距离,带绝对值符号不好 ...

  3. HDU - 6435 Problem J. CSGO 2018 Multi-University Training Contest 10 (二进制枚举+思维)

    题意:有N个主武器(MW)和M个副武器(SW),每个武器都有自己的S值,和K个附加属性xi.要选取一对主副武器搭配,搭配后获得的性能由该公式得出: 求获得最大的性能为多少. 分析:由于|xm - xs ...

  4. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

  5. Problem 2236 第十四个目标

    Problem 2236 第十四个目标 Accept: 4    Submit: 6Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem D ...

  6. 实验12:Problem J: 动物爱好者

    #define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...

  7. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  8. The Ninth Hunan Collegiate Programming Contest (2013) Problem J

    Problem J Joking with Fermat's Last Theorem Fermat's Last Theorem: no three positive integers a, b, ...

  9. Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量

    Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...

随机推荐

  1. Effective Java 第三版——56. 为所有已公开的API元素编写文档注释

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  2. malloc()參数为0的情况

    以下的代码片段输出是什么?为什么? char *ptr; ))==NULL) puts("Got a null pointer"); else puts("Got a v ...

  3. EXP-00056: ORACLE error 12154 encountered

    今天用EXP命令导Oracle数据库,出现如下错误信息: [oracle@yingxiang-testServer1 ~]$ exp imgpf/imgpf@orcl file=/prlife/db/ ...

  4. Thinkphp的S缓存用法!

    /节点列表 public function node(){ if(!$node = S('node_list')){ $field = array('id', 'name', 'title', 'pi ...

  5. Oracle11.2.0.4 RAC GI ORA-15003: diskgroup "XXXX" already mounted in another lock name space

    最新文章:Virson‘s Blog 安装GI,在执行root.sh时报错: Disk Group CRSDG creation failed with the following message: ...

  6. eBGP&iBGP 总结

    3.4 BGP 原文地址:http://mp.weixin.qq.com/s?src=3&timestamp=1500043305&ver=1&signature=XwiIVV ...

  7. Java8学习笔记(四)--接口增强

    增强点 静态方法 public interface InterfacePlus { void run(); static Date createDate(){ return new Date(); } ...

  8. 【Mac brew】代理安装brew insall

    http_proxy=dev-proxy.**.**:8080 https_proxy=dev-proxy.**.**:8080  brew install npm

  9. 什么是跨域访问,JSON&JSONP

    http://blog.csdn.net/notechsolution/article/details/50394391 更详细的讲解,关于同源策略等: http://www.cnblogs.com/ ...

  10. [algorithm] Dijkstra双栈算法表达式求值算法

    一.原理 Dijkstra所做的一个算法,双栈求值,用两个栈(一个保存运算符,一个用于保存操作数), 表达式由括号,运算符和操作数组成. (1).将操作数压入操作数栈 (2).将运算符压入运算符栈: ...