Problem Statement

     Manao is playing a new game called Reflections. The goal of the game is transferring an artifact in 3-dimensional space from point (0, 0, 0) to point (X, Y, Z). There are two types of moves in the game:

1) The player can move the artifact by 1 unit in the direction of any coordinate axis. So, using this type of move, from point (a, b, c) the artifact can be moved to points (a + 1, b, c), (a - 1, b, c), (a, b + 1, c), (a, b - 1, c), (a, b, c + 1) or (a, b, c - 1).

2) The player can reflect the artifact against any one of the given planes. The reflection works as follows: the artifact disappears at its position before the reflection and appears on the other side of the plane at such a place that the line connecting the old and new positions is perpendicular to the plane and the old and new positions are equidistant from the plane. Reflection against each particular plane can be performed at most once during the game.

The planes are given as vector <int>s mirrorXmirrorY and mirrorZmirrorX[i] corresponds to a plane perpendicular to the X axis with coordinate X = mirrorX[i]mirrorY and mirrorZcontain planes perpendicular to the Y and Z axis in the same fashion. The target position is given in vector <int> finalPositionfinalPosition contains 3 elements, wherefinalPosition[0] is X, finalPosition[1] is Y and finalPosition[2] is Z.

Return the minimum possible number of moves which Manao will need to transfer the artifact.

Definition

    
Class: Reflections
Method: minimumMoves
Parameters: vector <int>, vector <int>, vector <int>, vector <int>
Returns: long long
Method signature: long long minimumMoves(vector <int> mirrorX, vector <int> mirrorY, vector <int> mirrorZ, vector <int> finalPosition)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Constraints

- finalPosition will contain exactly 3 elements.
- Each element of finalPosition will be between -1,000,000,000 and 1,000,000,000, inclusive.
- mirrorX will contain between 0 and 20 elements, inclusive.
- Each element of mirrorX will be between -1,000,000,000 and 1,000,000,000, inclusive.
- All elements of mirrorX will be distinct.
- mirrorY will contain between 0 and 20 elements, inclusive.
- Each element of mirrorY will be between -1,000,000,000 and 1,000,000,000, inclusive.
- All elements of mirrorY will be distinct.
- mirrorZ will contain between 0 and 20 elements, inclusive.
- Each element of mirrorZ will be between -1,000,000,000 and 1,000,000,000, inclusive.
- All elements of mirrorZ will be distinct.

Examples

0)  
    
{2}
{}
{}
{3, 0, 1}
Returns: 3
Manao can reflect the artifact against the only given plane, making it appear at (4, 0, 0). Afterwards, he can transfer it into the target position by two moves of the first type.
1)  
    
{-5, 1, 4, 2, 3, 6, -6}
{}
{}
{9, 0, 0}
Returns: 2
A possible solution will be moving the artifact to (-1, 0, 0) and then reflecting it against the plane represented by mirrorX[2].
2)  
    
{5, 8}
{}
{}
{4, 0, 0}
Returns: 4
If a reflection against a particular plane was allowed more than once, Manao could transfer the artifact in only three moves.
3)  
    
{5}
{5}
{1, 2, 3}
{10, 12, -1}
Returns: 5
The planes perpendicular to the Z axis are of no use. After performing the reflections against the other two planes, Manao gets the artifact to point (10, 10, 0). Three more moves of the first type are required then.
4)  
    
{8, -3, 21}
{4, 5}
{-7, -2, -1, 7, 14}
{40, -4, 31}
Returns: 10
 

题意大致是:在三维空间中,从原点走到(x,y,z),每次可以向六个方向中的一个方向走一步,或者沿一面镜子对称,问至少行动多少次可以到达终点。

每面镜子都是垂直于x轴、y轴、z轴中的一条轴的平面,且垂直于一条轴的镜子数最多为20。每面镜子最多被用来对称一次。

题解:

代码来自TopCoder Wiki。

若直接进行移动,则只会改变三维中的一维,做对称也一样,所以三维可以分开讨论。

因为镜像操作只是进行对称,所以直接移动可以放在最后进行。这样,我们只要搜索按哪种顺序选用了哪些镜子,将得到的坐标与终点做差,即为在这个维度直接移动的次数。

直接枚举显然会超时,我们可以考虑优化。

考虑垂直于x轴的镜子的操作:若点坐标为a,镜子坐标为b,则对称后点坐标为2*b-a。

经过一系列对称,最终点坐标展开为(2*b[n]-2*b[n-1]+2*[b-2]-......-2*b[2]+2*b[1]-a)或(2*b[n]-2*b[n-1]+2*[b-2]-......+2*b[2]-2*b[1]+a)。

即某面被选用镜子操作的贡献只与选用顺序序号的奇偶性有关。

可以枚举n面奇数序号的镜子与n面偶数序号的镜子,原坐标贡献为正;或枚举n面奇数序号的镜子与n+1面偶数序号的镜子,原坐标贡献为负。

可是这样枚举依旧会TLE。

我们可以采用折半搜索,就不会TLE了,可是十分不好写。

事实上,我们可以分别枚举奇数序号镜子的集合与偶数序号镜子的集合,而不用考虑重复的情况。对于重复的情况,可以看做是一面镜子用了两次(效果等同于不使用),可以正常处理,但一定不是最优解。

枚举好后,用类似于折半搜索的做法合并即可。

代码:

 class Reflections
{
public:
long long solve(vector<int>& M, int P)
{
int N = M.size();
// Compute the sum of each subset.
vector<long long> V[];
for(int i = ; i < << N; i++)
{
long long sum = ;
for(int j = ; j < N; j++) if(i & << j) sum += M[j] * 2ll;
V[__builtin_popcount(i)].push_back(sum);
}
// For each subset find the subset of the same or one lesser size that puts us closest to our target.
long long ret = abs(P);
for(int i = ; i <= (N + ) / ; i++)
{
sort(V[i].begin(), V[i].end());
for(int j = ; j < V[i].size(); j++)
{
// Find subsets of equal size that put us close to P.
int pos = upper_bound(V[i].begin(), V[i].end(), V[i][j] - P) - V[i].begin();
if(pos < V[i].size()) ret = min(ret, * i + abs(P - V[i][j] + V[i][pos]));
if(pos > ) ret = min(ret, * i + abs(P - V[i][j] + V[i][pos - ])); // Find subsets of one lesser size that put us close to P.
if(!i) continue;
pos = upper_bound(V[i - ].begin(), V[i - ].end(), V[i][j] - P) - V[i - ].begin();
if(pos < V[i - ].size()) ret = min(ret, * i - + abs(P - V[i][j] + V[i - ][pos]));
if(pos > ) ret = min(ret, * i - + abs(P - V[i][j] + V[i - ][pos - ]));
}
}
return ret;
}
long long minimumMoves(vector<int> X, vector<int> Y, vector<int> Z, vector<int> P)
{
return solve(X, P[]) + solve(Y, P[]) + solve(Z, P[]);
}
};

TopCoder[SRM513 DIV 1]:Reflections(1000)的更多相关文章

  1. TopCoder[SRM513 DIV 1]:PerfectMemory(500)

    Problem Statement      You might have played the game called Memoria. In this game, there is a board ...

  2. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  3. TopCoder SRM 558 Div 1 - Problem 1000 SurroundingGame

    传送门:https://284914869.github.io/AEoj/558.html 题目简述  一个人在一个n * m棋盘上玩游戏,想要占领一个格子有两个方法: 在这个格子放一个棋子.  这个 ...

  4. TopCoder SRM 566 Div 1 - Problem 1000 FencingPenguins

    传送门:https://284914869.github.io/AEoj/566.html 题目简述: 平面上有中心在原点,一个点在(r,0)处的正n边形的n个顶点.平面上还有m个企鹅,每个企鹅有一个 ...

  5. TopCoder SRM 561 Div 1 - Problem 1000 Orienteering

    传送门:https://284914869.github.io/AEoj/561.html 题目简述: 题外话: 刚开始看题没看到|C|<=300.以为|C|^2能做,码了好久,但始终解决不了一 ...

  6. TopCoder SRM 582 Div 1 - Problem 1000 SemiPerfectPower

    首先我们可以把答案差分,那么我们只需要求出\(1\)~\(x\)范围内的满足条件的数即可. 题目要求的应该是这个东西的个数: \(l \leq a*b^c \leq r(1 \le a < b) ...

  7. Topcoder SRM584 DIV 2 500

    #include <set> #include <iostream> #include <string> #include <vector> using ...

  8. Topcoder SRM583 DIV 2 250

    #include <string> #include <iostream> using namespace std; class SwappingDigits { public ...

  9. 【补解体报告】topcoder 634 DIV 2

    A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...

随机推荐

  1. 《代码大全2》读书笔记 Week2

    <代码大全2>第四.五章 第四章“关键的‘构建’决策”主要有以下三要点:1.每种编程语言都有优点和缺点,程序员应根据需要选择编程语言,尽量选择熟悉的语言以提高生产效率.作为一种表达工具,编 ...

  2. 如何实现qq消息轰炸

    1.新建一个文本文档复制以下代码 Set WshShell = WScript.CreateObject("Wscript.Shell")WshShell.AppActivate& ...

  3. Samza基本概念

  4. 15_TLB中的G属性

    > TLB 是为了增加访问内存的效率 即 如果 是 29 9 12 分页 请求数据 可能需要访问 4次内存:为了解决这个问题:出现了 TLB (虚拟地址到物理地址的转换关系),如果目标地址在TL ...

  5. TS-接口

    接口 TS的核心原则之一是对值所具有的结构进行类型检测 接口初探 function printLabel(labelledObj: { label: string }) { console.log(l ...

  6. vscode编程nodejs初始安装

    nodejs官网 http://nodejs.cn/ 1.安装nodejs,记得安装时勾选配置路径 在cmd中输入node,进去node环境即为安装成功. 2.安装vscode,并安装插件node e ...

  7. winfrom设置webBrowser框架默认的IE内核版本

    要实现设置webBrowser框架默认的IE内核版本的功能需要三个方法 1:修改注册表信息来兼容当前程序 /// <summary> /// 修改注册表信息来兼容当前程序 /// /// ...

  8. leetcode-159周赛-5230-缀点成线

    自己的提交: class Solution: def checkStraightLine(self, coordinates: List[List[int]]) -> bool: if not ...

  9. 使用Git 上传文件到云端(版本库)

    第一步:本地初始化Git版本库 git init 第二步:链接码云(云端) git remote add orgin "你的远程仓库地址"(复制链接后结尾是.git,如果没有记得加 ...

  10. php数组长度怎么获取

    我们可以将元素添加到数组或从数组中删除元素,那么如果我们想要知道数组中存在的元素的总长度或总数,我们就可以使用count() 或sizeof函数. 下面我们就通过简单的示例,给大家介绍php获取数组长 ...