Codeforces Gym 100803C Shopping 贪心好题

本文介绍了一种解决特定条件下寻找最短行走路径的问题。在一个包含N个店铺的直线上,给定一系列访问顺序限制,文章提供了一个有效的贪心算法解决方案,确保在满足所有限制条件的同时,计算出从入口到出口的最小行走距离。

题目链接:这里
Description

Your friend will enjoy shopping. She will walk through a mall along a straight street, where N
individual shops (numbered from 1 to N) are aligned at regular intervals. Each shop has one
door and is located at the one side of the street. The distances between the doors of the adjacent
shops are the same length, i.e. a unit length. Starting shopping at the entrance of the mall, she
visits shops in order to purchase goods. She has to go to the exit of the mall after shopping.
She requires some restrictions on visiting order of shops. Each of the restrictions indicates that
she shall visit a shop before visiting another shop. For example, when she wants to buy a nice
dress before choosing heels, she shall visit a boutique before visiting a shoe store. When the
boutique is farther than the shoe store, she must pass the shoe store before visiting the boutique,
and go back to the shoe store after visiting the boutique.

If only the order of the visiting shops satisfies all the restrictions, she can visit other shops in
any order she likes.

Write a program to determine the minimum required walking length for her to move from the
entrance to the exit.

Assume that the position of the door of the shop numbered k is k units far from the entrance,
where the position of the exit is N + 1 units far from the entrance.
Input

The input consists of a single test case.

N m
c1 d1
.
.
.
cm dm

The first line contains two integers N and m, where N (1 ≤ N ≤ 1000) is the number of shops,
and m (0 ≤ m ≤ 500) is the number of restrictions. Each of the next m lines contains two
integers ci and di (1 ≤ ci < di ≤ N) indicating the i-th restriction on the visiting order, where
she must visit the shop numbered ci after she visits the shop numbered di (i = 1, … , m).
There are no pair of j and k that satisfy cj = ck and dj = dk.
Output

Output the minimum required walking length for her to move from the entrance to the exit.
You should omit the length of her walk in the insides of shops.
Sample Input

10 3

3 7

8 9

2 5
Sample Output

23

题意:
在一条街上有1-n个店,你一开始在0这个位置,你需要访问每个店,并且最后到n+1这个点

然后有m个限制,就给你ci,di

表示你去ci这个店之前,你必须先到di这个点才行

保证di>ci

问你最小距离走多少

解法:
贪心好题。

我们走的话,就走闭环就好了

闭环是什么?这个区间的[l,r]中,l点是被限制的,并且这个区间的最大的限制为r。

那么我们就往回走一次,再走过去就好了

这个贪心显然是正确的。因为这样可以保证重复经过的路最少。

//CF gym 100803C

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1200;
int fa[maxn];

int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n+1; i++) fa[i] = i;
    for(int i = 1; i <= m; i++){
        int x, y;
        scanf("%d%d", &x, &y);
        fa[x] = max(fa[x], y);
    }
    long long ans = n+1;
    int l = 1, r = 1;
    for(int i = 1; i <= n; ){
        r = fa[i];
        int now = i;
        while(now <= r){
            r = max(r, fa[now]);
            now++;
        }
        ans += 2*(r-i);
        i = now;
    }
    printf("%lld\n", ans);
    return 0;
}
Codeforces Gym 101630 是一场编程竞赛,通常包含多个算法挑战问。这些问往往涉及数据结构、算法设计、数学建模等多个方面,旨在测试参赛者的编程能力和解决问的能力。 以下是一些可能出现在 Codeforces Gym 101630 中的目类型及解决方案概述: ### 目类型 1. **动态规划(DP)** 动态规划是编程竞赛中常见的型之一。问通常要求找到某种最优解,例如最小路径和、最长递增子序列等。解决这类问的关键在于状态定义和转移方程的设计[^1]。 2. **图论** 图论问包括最短路径、最小生成树、网络流等。例如,Dijkstra 算法用于求解单源最短路径问,而 Kruskal 或 Prim 算法则常用于最小生成树问[^1]。 3. **字符串处理** 字符串问可能涉及模式匹配、后缀数组、自动机等高级技巧。KMP 算法和 Trie 树是解决此类问的常用工具[^1]。 4. **数论与组合数学** 这类问通常需要对质数、模运算、排列组合等有深入的理解。例如,快速幂算法可以用来高效计算大数的模幂运算[^1]。 5. **几何** 几何问可能涉及点、线、多边形的计算,如判断点是否在多边形内部、计算两个圆的交点等。向量运算和坐标变换是解决几何问的基础[^1]。 ### 解决方案示例 #### 示例问:动态规划 - 最长递增子序列 ```python def longest_increasing_subsequence(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return max(dp) # 示例输入 nums = [10, 9, 2, 5, 3, 7, 101, 18] print(longest_increasing_subsequence(nums)) # 输出: 4 ``` #### 示例问:图论 - Dijkstra 算法 ```python import heapq def dijkstra(graph, start): distances = {node: float('infinity') for node in graph} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) if current_distance > distances[current_node]: continue for neighbor, weight in graph[current_node].items(): distance = current_distance + weight if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue, (distance, neighbor)) return distances # 示例输入 graph = { 'A': {'B': 1, 'C': 4}, 'B': {'A': 1, 'C': 2, 'D': 5}, 'C': {'A': 4, 'B': 2, 'D': 1}, 'D': {'B': 5, 'C': 1} } start = 'A' print(dijkstra(graph, start)) # 输出: {'A': 0, 'B': 1, 'C': 3, 'D': 4} ``` #### 示例问:字符串处理 - KMP 算法 ```python def kmp_failure_function(pattern): m = len(pattern) lps = [0] * m length = 0 # length of the previous longest prefix suffix i = 1 while i < m: if pattern[i] == pattern[length]: length += 1 lps[i] = length i += 1 else: if length != 0: length = lps[length - 1] else: lps[i] = 0 i += 1 return lps def kmp_search(text, pattern): n = len(text) m = len(pattern) lps = kmp_failure_function(pattern) i = 0 # index for text j = 0 # index for pattern while i < n: if pattern[j] == text[i]: i += 1 j += 1 if j == m: print("Pattern found at index", i - j) j = lps[j - 1] elif i < n and pattern[j] != text[i]: if j != 0: j = lps[j - 1] else: i += 1 # 示例输入 text = "ABABDABACDABABCABAB" pattern = "ABABCABAB" kmp_search(text, pattern) # 输出: Pattern found at index 10 ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值