hdu6435 Problem J. CSGO标程讲解以及改正标程的一个错误(本来第一个样例过不了2333) 以及 poj2926 五维曼哈顿距离模板
比赛的时候抄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 五维曼哈顿距离模板的更多相关文章
- [hdu6435]Problem J. CSGO
题目大意:给定$n$个$A$类元素和$m$个$B$类元素,每类元素有值$S$和$k$个值$x_0,x_1,\dots,x_{k-1}(k\leqslant 5)$. 要求选出一个$A$类元素$a$和$ ...
- HDU - 6435 Problem J. CSGO (曼哈顿距离变换)
题目大意:有两类武器(主武器和副武器),每类有若干把,每把武器都有一个基础属性S,以及k个附加属性,让你选一把主武器M和一把副武器S,使得最大. 显然后面的和式是一个k维的曼哈顿距离,带绝对值符号不好 ...
- HDU - 6435 Problem J. CSGO 2018 Multi-University Training Contest 10 (二进制枚举+思维)
题意:有N个主武器(MW)和M个副武器(SW),每个武器都有自己的S值,和K个附加属性xi.要选取一对主副武器搭配,搭配后获得的性能由该公式得出: 求获得最大的性能为多少. 分析:由于|xm - xs ...
- FZu Problem 2236 第十四个目标 (线段树 + dp)
题目链接: FZu Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...
- Problem 2236 第十四个目标
Problem 2236 第十四个目标 Accept: 4 Submit: 6Time Limit: 1000 mSec Memory Limit : 32768 KB Problem D ...
- 实验12:Problem J: 动物爱好者
#define null ""是用来将字符串清空的 #define none -1是用来当不存在这种动物时,返回-1. 其实这种做法有点多余,不过好理解一些. Home Web B ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- 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, ...
- Codeforces Gym 100342J Problem J. Triatrip bitset 求三元环的数量
Problem J. TriatripTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/att ...
随机推荐
- [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications
ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Da ...
- C++调用外部应用程序
很多时候,我们的程序需要调用DOS命令,通过Dos命令调用其他程序从而完成所需要完成的功能.比如,调用Dos程序PKZIP完成ZIP包的解压缩,调用SVN完成文件的更新或者上传.但是在程序运行时又要求 ...
- 阿里巴巴CI:CD之分层自动化实践之路
阿里巴巴CI:CD之分层自动化实践之路 2018-05-30 摘自:阿里巴巴CI:CD之分层自动化实践之路 目录 1 自动化 1.1 为什么要做自动化? 1.2 自动化的烦恼 1.3 自动化的追 ...
- Android开发(十五)——ListView中Items的间距margin
ListView中Items没有margin 参考:http://www.cnblogs.com/xitang/p/3677528.html
- mercurial的几个易用性小技巧
其实这两年,能够采用mercurial的项目我都尽量用,甚至有些上游是git的,或者需要托管到公司内gitlab上与别人协作的,我都装上hg-git.无它,只是因为mercurial易用性比git好得 ...
- [转]mysql大表更新sql的优化策略
看了该文章之后,很受启发,mysql在update时,一般也是先select.但注意,在Read Committed隔离级别下,如果没有使用索引,并不会锁住整个表, 还是只锁住满足查询条件的记录而已. ...
- 如何在Excel中提取小数点后面的数字?
Excel中,如果某个单元格中包含一个带小数,要用公式提取该数值小数点后面的数字,例如A1单元格中包含一个数值“59178.68”,在B1单元格中输入下面的公式: =RIGHT(A1,LEN(A1)- ...
- audio video 控制播放和停止
<audio id="audio" src="waring.wav" preload="auto" controls loop> ...
- centos7中端口及服务对应情况(笔记)
25 postfix服务 111 rpcbind.socket服务
- Java知多少(103)网络编程之IP地址和InetAddress类
Java语言的优势之一是Java程序能访问网络资源.Java提供一系列的类支持Java程序访问网络资源. TCP/IP协议和IP地址 为了进行网络通信,通信双方必须遵守通信协议.目前最广泛使用的是TC ...