Codeforces Round #310 (Div. 1) C Case of Chocolate

思路:对于每个点而言、只与它相邻的两个点有关系、所以可以用stl或者线段树来找到它的相邻点、

代码:187ms(开挂之后貌似是最快的- -)

#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;

const int N = 200000 + 1;

int x[N], y[N], t[N];

//适用于正负整数
template <class T>
inline bool scan_d(T &ret) {
   char c; int sgn;
   if(c = getchar(),c == EOF) return 0; //EOF
   while(c != '-' && (c < '0' || c > '9')) c = getchar();
   sgn = (c == '-') ? -1 : 1;
   ret = (c == '-') ? 0 : (c - '0');
   while(c = getchar(),c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
   ret *= sgn;
   return 1;
}

inline void out(int x) {
   if(x > 9) out(x / 10);
   putchar(x % 10 + '0');
}

int main() {
    int n, q;
    scanf("%d%d", &n, &q);
    x[q] = y[q] = 0;
    map <int, int> mp;
    mp[0] = q;
    mp[n + 1] = q;
    for (int i = 0; i < q; ++ i) {
        char buffer[2];
        scan_d(x[i]);
        scan_d(y[i]);
        scanf("%s",buffer);
        t[i] = *buffer == 'U';
        map <int, int>::iterator iterator;
        iterator = mp.lower_bound(x[i]);
        if (x[i] == iterator->first) {
            puts("0"); continue;
        }
        if (!t[i]) iterator --;
        int j = iterator->second;
        mp[x[i]] = i;
        if (t[i]) {
            out(y[i] - y[j]);
            putchar('\n');
            y[i] = y[j];
        } else {
            out(x[i] - x[j]);
            putchar('\n');
            x[i] = x[j];
        }
    }
    return 0;
}
时间: 2024-08-11 03:30:44

Codeforces Round #310 (Div. 1) C Case of Chocolate的相关文章

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C..但是挺简单的..只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护.每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

题目传送门 1 /* 2 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 3 构造:先求出使第1个指向0要多少步,按照这个次数之后的能否满足要求 4 题目读的好累:( 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #i

Round 310(Div.1) B. Case of Fugitive

Round 310(Div.1) B. Case of Fugitive Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands

codeforces Round #310(Div.1) 题解

嘴巴选手真爽,一不用打代码二不用掉Rating三还可以打杂.... 感觉这套题不难,但是被出题人出瞎了... 555A. Case of Matryoshkas 题目大意:给定n个大小从1到n的套娃,初始套成k坨,每次你可以选择两个操作: 1.选择一个不在任何其他套娃里的套娃,将里面的套娃取出来(要求原先里面有套娃) 2.选择一个不再任何其他套娃里的套娃,将一个套娃塞进去(要求原先里面没有套娃) 求将所有套娃合并的最小操作次数 如果一个套娃x初始在最里面,或者满足大小为1...x?1的套娃都在它

Codeforces Round #310 (Div. 1)——A水——Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元组[ll,rr]排序(ll相同时再rr),长度x排序(升序).一个全局优先队列pq(rr小的顶部).for循环,对每个x,将ll比它小的放入优先队列pq,如果pq仍为空,说明这块桥用不上,不为空,看top的rr是否大于x,如果大于,这块桥就能用上,并且给当前的top一定是可行的. 乱码: #pragma co

Codeforces Round #310 (Div. 2)

A. Case of the Zeros and Ones 题目大意: 给出一个只含0和1的字符串,当是0和1相邻的话,这两个字符就可以删除,问最后不能删除的字符有多少个? 解题思路: 只需要分别统计出来0和1的个数,然后相减之后的绝对值就是答案. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdl