题意:

许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果.

思路:

用"疑似绝对值"的思想, 维护每种状态下各点的计算值, 插入或删除一个点就更新一次每种状态(用 multiset 或 map 或 priority_queue 实现), 每次求ans时扫一遍最大差值即可.

为了练习STL, 每一个都实现一次.

multiset

/* **********************************************
Author : kuangbin
Created Time: 2013/8/13 18:25:38
File Name : F:\2013ACM练习\2013多校7\1001.cpp
*********************************************** */
//4640MS    14972K
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
int a[60010][10];
multiset<int>mst[1<<5]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int q,k;
while(scanf("%d%d",&q,&k)==2)
{
for(int i = 0;i < (1<<k);i++)
mst[i].clear();
int od,x;
for(int i = 1;i <= q;i++)
{
scanf("%d",&od);
if(od == 0)
{
for(int j = 0;j < k;j++)
scanf("%d",&a[i][j]);
for(int j = 0; j < (1<<k); j++)
{//计算当前点在每种情况下的"疑似绝对值"
int s = 0;
for(int t = 0; t < k;t++)
if(j & (1<<t))
s += a[i][t];
else s -= a[i][t];
mst[j].insert(s);//插入到该种情况下
}
}
else
{
scanf("%d",&x);
for(int j = 0; j < (1<<k); j++)
{//一次操作,插入或删除一个点,都是将这个点对应的所有状态插入每种状态中
int s = 0;//因此,要清除一次操作,就要删除所有状态中的那一个
for(int t = 0; t < k;t++)
if(j & (1<<t))
s += a[x][t];
else s -= a[x][t];
multiset<int>::iterator it = mst[j].find(s);
mst[j].erase(it);
}
}
int ans = 0;
for(int j = 0; j < (1<<k);j++)
{
multiset<int>::iterator it = mst[j].end();
it--;
int t1 = (*it);
it = mst[j].begin();
int t2 = (*it);//用于作差
ans = max(ans,t1-t2);//保留最大值
}
printf("%d\n",ans);
}
}
return 0;
}

map

//8359MS	37928K慢死了

#include <cstdio>
#include <algorithm>
#include <map>
using namespace std; int a[60010][6];
map<int, int> mp[1<<5]; int main()
{
int q,k;
while(scanf("%d %d",&q,&k)==2)
{
for(int i=0;i<1<<k;i++)
mp[i].clear();
int od, x;
for(int i=1;i<=q;i++)
{
scanf("%d",&od);
if(!od)
{
for(int j=0;j<k;j++)
scanf("%d",a[i]+j);
for(int s=0;s<1<<k;s++)
{
int t = 0;
for(int j=0;j<k;j++)
{
if(s & (1<<j)) t += a[i][j];
else t -= a[i][j];
}
mp[s][t]++;
// printf("map[s][t] = %d\n",mp[s][t]);
}
}
else
{
scanf("%d",&x);
for(int s=0;s<1<<k;s++)
{
int t = 0;
for(int j=0;j<k;j++)
{
if(s & (1<<j)) t += a[x][j];
else t -= a[x][j];
}
map<int, int>::iterator it = mp[s].find(t);
mp[s][t]--;
}
}
int ans = 0;
for(int s=0;s<(1<<k);s++)
{
map<int, int>::iterator it = mp[s].end();
it--;
while(it->second==0) it--;
int mx = it->first;///first~~~
it = mp[s].begin();
while(it->second==0) it++;
int mi = it->first;
ans = max(ans, mx - mi);
// printf("mx = %d, mi = %d\n",mx,mi);
}
printf("%d\n",ans);
}
}
}

priority_queue

优先队列只能返回队首元素,因此需要两个队列分别求最大最小值.

对于已删除的元素, 无法直接删除, 可以做标记, 碰到已删除的元素时直接pop掉就行了.

因此入队的就不能仅仅是一个值(前两个有find功能, 不需要额外标号), 而应该是一个记录key和value的结构体.

//2218MS	36748K
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; int a[6];
bool vis[60005];
typedef struct ascending_node
{
int id,t;
bool operator<(const ascending_node& a) const
{
return t > a.t;
}
}anode;
typedef struct descending_node
{
int id,t;
bool operator<(const descending_node& a) const
{
return t < a.t;
}
}dnode;
/* 2812MS 30224K
priority_queue<anode> apq[1<<5];
priority_queue<dnode> dpq[1<<5];
int main()
{
int q,k;
while(scanf("%d %d",&q,&k)==2)
{
for(int i=0;i<1<<k;i++)
{
while(!apq[i].empty()) apq[i].pop();
while(!dpq[i].empty()) dpq[i].pop();
}*/
/**/
int main()
{
int q,k;
while(scanf("%d %d",&q,&k)==2)
{
priority_queue<anode> apq[1<<5];
priority_queue<dnode> dpq[1<<5];/**/
anode t1;
dnode t2;
memset(vis,false,sizeof(vis));
int od, x;
for(int i=1;i<=q;i++)
{
scanf("%d",&od);
if(!od)
{
for(int j=0;j<k;j++)
scanf("%d",a+j);
for(int s=0;s<1<<k;s++)
{
int t = 0;
for(int j=0;j<k;j++)
{
if(s & (1<<j)) t += a[j];
else t -= a[j];
}
t1.t = t2.t = t;
t1.id = t2.id = i;
apq[s].push(t1);
dpq[s].push(t2);
// printf("map[s][t] = %d\n",mp[s][t]);
}
}
else
{
scanf("%d",&x);
vis[x] = true;
}
int ans = 0;
for(int s=0;s<(1<<k);s++)
{
while(1)
{
t1 = apq[s].top();
if(!vis[t1.id]) break;
apq[s].pop();
}
while(1)
{
t2 = dpq[s].top();
if(!vis[t2.id]) break;
dpq[s].pop();
}
ans = max(ans, t2.t-t1.t);
}
printf("%d\n",ans);
}
}
}

[HDU 4666]Hyperspace[最远曼哈顿距离][STL]的更多相关文章

  1. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  2. HDU 4666 最远曼哈顿距离

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...

  3. poj 2926:Requirements(最远曼哈顿距离,入门题)

    Requirements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3908   Accepted: 1318 Desc ...

  4. HDU 4666 Hyperspace (最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  6. 多校联赛7 1001 hdu 4666(最远哈曼顿距离+优先队列)

    吐个糟,尼玛今天被虐成狗了,一题都没搞出来,这题搞了N久居然还是搞不出来,一直TLE,最后还是参考别人代码才领悟的,思路就这么简单, 就是不会转弯,看着模板却不会改,艹,真怀疑自己是不是个笨蛋题意:求 ...

  7. 2018 Multi-University Training Contest 10 CSGO(HDU - 6435)(最远曼哈顿距离)

    有 n 种主武器,m 种副武器.每种武器有一个基础分数k种属性值 X[i] . 选出一种主武器 mw 和一种副武器 sw,使得两种武器的分数和 + 每个属性的差值尽量大.(参考下面的式子) 多维的最远 ...

  8. POJ-2926 Requirements 最远曼哈顿距离

    题目链接:http://poj.org/problem?id=2926 题意:求5维空间的点集中的最远曼哈顿距离.. 降维处理,推荐2009武森<浅谈信息学竞赛中的“0”和“1”>以及&l ...

  9. Codeforces 491B. New York Hotel 最远曼哈顿距离

    最远曼哈顿距离有两个性质: 1: 对每一个点(x,y)  分别计算  +x+y , -x+y , x-y , -x-y 然后统计每种组合的最大值就能够了, 不会对结果产生影响 2: 去掉绝对值 , 设 ...

随机推荐

  1. 认识jeecms开源项目

    1. JEECMS源代码基本结构及相关技术简介: 参考:http://blog.csdn.net/caozhenyu/article/details/47005623

  2. IO之读入文件

    整个java.io包中最重要的就是5个类和一个接口,5个类指的是File,OutputStream,InputStream,Reader,Writer:一个接口是Serializable. 在整个io ...

  3. git配置ssh

    $ git config --global user.name "yourname"$ git config --global user.email "youremail ...

  4. php数组使用技巧及操作总结

    数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是一些小结,借此记之,便于以后鉴之. 1. 数组定义 数组的定义使用 array()方式定义,可以定义空数组:<?ph ...

  5. poj 3348 Cows 求凸包面积

    题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include ...

  6. 利用python进行数据分析之数据加载存储与文件格式

    在开始学习之前,我们需要安装pandas模块.由于我安装的python的版本是2.7,故我们在https://pypi.python.org/pypi/pandas/0.16.2/#downloads ...

  7. ubuntu中查找软件的安装位置

    ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get install安装.但是安装后的软件在哪个位置呢?这点跟windows环境下安装软件的路径选择不一样.ubuntu中可 ...

  8. 8_Times_Tables

    8 // // ViewController.swift // Times Tables // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...

  9. vc++实现avi文件的操作

    为了对avi进行读写,微软提供了一套API,总共50个函数,他们的用途主要有两类,一个是avi文件的操作,一类是数据流streams的操作. 1.打开和关闭文件 AVIFileOpen ,AVIFil ...

  10. MinGW gcc 生成动态链接库 dll 的一些问题汇总 (补充)

    我以前写过一个小短文,介绍MinGW gcc 生成动态链接库 dll 的一些问题.当时写的并不全面.近期又遇到写新的问题.这里记录一下,做个补充. 通常情况下,dll 中的函数假设採用 _stdcal ...