hdu 4831 Scenic Popularity(模拟)

题目链接:hdu 4831 Scenic Popularity

题目大意:略。

解题思路:对于休闲区g[i][0]和g[i][1]记录的是最近的两个景点的id(只有一个最近的话g[i][1]为0),对于景点来说,g[i][0]为-1(表示该id对应的是景点),g[i][1]为该景点的热度值.主要就是模拟,注意一些细节就可以了。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;
const int N = 100005;
const int INF = 0x3f3f3f3f;
int n, pos[N], g[N][2];

inline int dis (int a, int b) {
    return abs(pos[a]-pos[b]);
}

inline void cat (int x, int mv) {
    if (mv == n+1)
        return;

    int& tmp = g[x][0];

    if (tmp == 0 || dis(x, mv) < dis(x, tmp)) {
        g[x][0] = mv;
    } else if (dis(x, mv) == dis(x, tmp)) {
        g[x][1] = mv;
    }
}

void init () {
    scanf("%d", &n);
    memset(g, 0, sizeof(g));
    memset(pos, 0, sizeof(pos));

    int mv = 0, val;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &pos[i], &val);

        if (val) {
            mv = i;
            g[i][0] = -1;
            g[i][1] = val;
        } else {
            g[i][0] = mv;
        }
    }

    mv = n+1;
    for (int i = n; i; i--) {
        if (g[i][0] < 0) {
            mv = i;
        } else {
            cat(i, mv);
        }
    }
}

int find (int x) {
    if (x == 0)
        return 0;

    if (g[x][0] < 0)
        return g[x][1];
    else
        return max(find(g[x][0]), find(g[x][1]));
}

int query (int k) {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (find(i) <= k)
            ans++;
    }
    return ans;
}

void solve () {
    int m, a, b;
    char str[5];
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%s", str);
        if (str[0] == ‘Q‘) {
            scanf("%d", &a);
            printf("%d\n", query(a));
        } else {
            scanf("%d%d", &a, &b);
            g[a+1][1] = b;
        }
    }
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int i = 1; i <= cas; i++) {
        init();
        printf("Case #%d:\n", i);
        solve();
    }
    return 0;
}

hdu 4831 Scenic Popularity(模拟),布布扣,bubuko.com

时间: 2024-11-04 20:27:37

hdu 4831 Scenic Popularity(模拟)的相关文章

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 4831 Scenic Popularity

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 117    Accepted Submission(s): 25 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊在

hdu4831 Scenic Popularity(线段树)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4831 题目大概意思就是有多个风景区和休息区,每个风景区有热度,休息区的热度与最接近的分景区的热度相同,题目要求求出小于等于给定热度值的风景区和休息区的个数.显然如果直接暴力的话,复杂度为O(TKN),达到10^9次方数量级,复杂度过高,对于这种问答的题目,最一般的思路其实是线段树,线段树更改和查询的时间复杂度均为O(logn),所以如果用线段树的话,这道题目的复杂度为O(TKlogH),达到10^5

hdu 1175 连连看(模拟循环队列)

连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18149    Accepted Submission(s): 4741 Problem Description "连连看"相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条

HDU 4608 I-number--简单模拟

I-number Time Limit: 5000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The I-number of x is defined to be an integer y, which satisfied the the conditions below: 1.  y>x; 2.  the sum of each digit of y(under base 10) is the multiple of 10; 3.  among all

hdu 3125 Slash(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3125 Problem Description The American English slash (/) is a punctuation mark. In the early modern period, in the Fraktur script, which was widespread through Europe in the Middle Ages, one slash(/) repr

hdu 4858 项目管理(vector模拟)

# include <stdio.h> # include <algorithm> # include <string.h> # include <vector> # define N 100005 using namespace std; vector<int>g[N]; int node[N]; int slove(int x) { int sum=0,i; for(i=0;i<g[x].size();i++) { sum+=node[

hdu 4941 STL HASH 模拟

http://acm.hdu.edu.cn/showproblem.php?pid=4941 比赛的时候现学的map的find...以前都是用下标做的,但是map用下标查询的话,如果查询的元素不存在,会插入一个新的元素. 贴一个map查找元素找到和找不到的模板 map<pair<int,int>,int>::iterator it=poshash.find(tmppos);//pair<int,int>poshash; int pp; if(it == poshash.

HDU 4022 Bombing STL 模拟题

手动模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define N 10100 #define inf 1000000010 map<