Codeforces 1421 D. Hexagons(推结论)

这篇博客探讨了一种在二维六边形网格上寻找从原点到目标点最短路径的问题。题目描述了每个方向的步进成本,并提供了多种可能的路径组合。通过转换和分类讨论,博主提出了一种解决方案,通过考虑不同方向的组合来找到最小成本路径。文章包含了具体的代码实现,展示了如何处理这种情况下的路径规划问题。
摘要由CSDN通过智能技术生成

Lindsey Buckingham told Stevie Nicks “Go your own way”. Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.

Consider a hexagonal tiling of the plane as on the picture below.

Nicks wishes to go from the cell marked (0,0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0,0) to (1,1) will take the exact same cost as going from (−2,−1) to (−1,0). The costs are given in the input in the order 𝑐1, 𝑐2, 𝑐3, 𝑐4, 𝑐5, 𝑐6 as in the picture below.

Print the smallest cost of a path from the origin which has coordinates (0,0) to the given cell.

Input
Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). Description of the test cases follows.

The first line of each test case contains two integers 𝑥 and 𝑦 (−109≤𝑥,𝑦≤109) representing the coordinates of the target hexagon.

The second line of each test case contains six integers 𝑐1, 𝑐2, 𝑐3, 𝑐4, 𝑐5, 𝑐6 (1≤𝑐1,𝑐2,𝑐3,𝑐4,𝑐5,𝑐6≤109) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).

Output
For each testcase output the smallest cost of a path from the origin to the given cell.

Example
inputCopy
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
outputCopy
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking 𝑐3 3 times and 𝑐2 once, amounting to 5+5+5+3=18.

题意:
蜂窝形的地图,可以向周围6个方向走,每个方向走的花费不同,求从(0,0)走到(x,y)的最小花费。

思路:
感觉写的很麻烦。
将蜂窝形地图看作为直角坐标轴,则6个方向分别对应上,右上,右,下,左下,左。

先考虑上下左右的方向,那么左右肯定会合并成只有左或者只有右,上下会合并成只有上或只有下。

如果同时考虑右上和左下,那么很明显会合并成左下和右上一个。

考虑方向左,上,右上:
左,上一部分合并成左上,最后只剩下左或者上。
然后左上和右上一部分合并成上,剩下左上和右上一个。

这样结果就成了

  1. 左,上
  2. 左,右上
  3. 上,右上

其他部分合并也可以推出这个结论。

于是就成了,要么是

  1. 左,上
  2. 左,下
  3. 右,上
  4. 右,下
  5. 左下+上下左右中的一个
  6. 右上+上下左右中的一个

然后分类讨论一下即可。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstring>
using namespace std;

typedef long long ll;
const int maxn = 1e3 + 7;
const double eps = 1e-6;

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        ll x,y;scanf("%lld%lld",&x,&y);
        ll c1,c2,c3,c4,c5,c6;
        scanf("%lld%lld%lld%lld%lld%lld",&c1,&c2,&c3,&c4,&c5,&c6);
        swap(x,y);
        ll ans = 0;
        if(x <= 0) {
            ans += -c5 * x;
        } else {
            ans += c2 * x;
        }
        
        if(y <= 0) {
            ans += -c3 * y;
        } else {
            ans += c6 * y;
        }
        
        //c1 c6
        if(x >= 0) {
            ll tmp = c1 * x;
            ll cy = y - x;
            if(cy >= 0) {
                tmp += c6 * cy;
                ans = min(ans,tmp);
            }
        }
        
        //c1 c2
        if(y >= 0) {
            ll tmp = c1 * y;
            ll cx = x - y;
            if(cx >= 0) {
                tmp += cx * c2;
                ans = min(ans,tmp);
            }
        }
        
        //c1 c5
        if(y >= 0) {
            ll tmp = c1 * y;
            ll cx = x - y;
            if(cx <= 0) {
                tmp += -cx * c5;
                ans = min(ans,tmp);
            }
        }
        
        //c1 c3
        if(x >= 0) {
            ll tmp = c1 * x;
            ll cy = y - x;
            if(cy <= 0) {
                tmp += -cy * c3;
                ans = min(ans,tmp);
            }
        }
        
        //c4 c2
        if(y <= 0) {
            ll tmp = -c4 * y;
            ll cx = x - y;
            if(cx >= 0) {
                tmp += cx * c2;
                ans = min(ans,tmp);
            }
        }
        
        //c4 c3
        if(x <= 0) {
            ll tmp = -c4 * x;
            ll cy = y - x;
            if(cy <= 0) {
                tmp += -cy * c3;
                ans = min(ans,tmp);
            }
        }
        
        //c4 c5
        if(y <= 0) {
            ll tmp = -c4 * y;
            ll cx = x - y;
            if(cx <= 0) {
                tmp += -cx * c5;
                ans = min(ans,tmp);
            }
        }
        
        //c4 c6
        if(x <= 0) {
            ll tmp = -c4 * x;
            ll cy = y - x;
            if(cy >= 0) {
                tmp += cy * c6;
                ans = min(ans,tmp);
            }
        }
        
        printf("%lld\n",ans);
    }
    return 0;
}
 
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值