Problem C Shopping 闭环贪心

#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;
}

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

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

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

保证di>ci

问你最小距离走多少

往回走一次,再走过去就好了!!!!

时间: 2024-12-07 19:36:02

Problem C Shopping 闭环贪心的相关文章

HDU Hotaru&#39;s problem(Manacher算法+贪心)

manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成,第一段和第二段对称,第一段和第三段一样. 思路:首先利用Manacher算法求出以任意两个相邻元素为中心的回文串长度,用a[i]表示i-1,i为中心的回文串长度的一半, 那么问题就转化成了求最大的x,使得a[i]>=x,a[i+x]>=x,这一步可以贪心来做. 将a[i]从大到小排序(间接排序保留

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

华农oj Problem J: 幻化【贪心/抽屉原理】

Problem J: 幻化 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 18 Solved: 3 [Submit][Status][Web Board] Description 遇见你是我一世的春暖花开, 从此清风明月浩瀚星海. 不论结局,很高兴认识你. 她给了他一个长度为n的整数序列a[],他还给了她另外一个长度为n的整数序列b[],现在他想通过每次交换a[i],a[j]使序列a变成b,但是每次交换的代价是|j-i|. 请问最少的代价是多少呢?

UVa12545 Bits Equalizer (贪心)

链接:http://bak2.vjudge.net/problem/UVA-12545 分析:贪心乱搞. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 5 const int maxn = 100 + 5; 6 7 char s[maxn], t[maxn]; 8 9 int main() { 10 int T; 11 scanf("%d", &T); 12 for (

UVa1149 Bin Packing (贪心)

链接:http://vjudge.net/problem/UVA-1149 分析:贪心的放,先放重的,剩下的容量看能不能放进一个轻的. 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 100000 + 5; 6 7 int a[maxn]; 8 9 int main() { 10 int T; 11 scanf("%d", &T)

POJ 3831 &amp; HDU 3264 Open-air shopping malls(几何)

题目链接: POJ:http://poj.org/problem?id=3831 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3264 Description The city of M is a famous shopping city and its open-air shopping malls are extremely attractive. During the tourist seasons, thousands of people

hdu 3264 Open-air shopping malls(求圆相交的面积,二分)

Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2256    Accepted Submission(s): 837 Problem Description The city of M is a famous shopping city and its open-air shopping

Open-air shopping malls(二分半径,两元交面积)

http://acm.hdu.edu.cn/showproblem.php?pid=3264 Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2139    Accepted Submission(s): 775 Problem Description The city of M is a

hdu 3264 09 宁波 现场 E - Open-air shopping malls 计算几何 二分

Description The city of M is a famous shopping city and its open-air shopping malls are extremely attractive. During the tourist seasons, thousands of people crowded into these shopping malls and enjoy the vary-different shopping. Unfortunately, the