唉。赛上wa了十来发没能过的水题。。 发个博客纪念一下
题意: 不同的位置会随机出现出的 一个地方可能会出现多个吃的 初始在0 每次去最近的 如果两边距离一样去 之前移动方向的那个
分析: 没啥就是一个multiset或者map模拟的水题 有个trick就是 - - 有可能会被呆在原地的多个吃的 隐藏的修改了方向然后就跪了
数据还是水了 题目里的数据只有一个方向的 如果不判断等于pre也可以过 但是等于nxt的数据是有的
结论: 论仔细思考 然后写出优美代码的重要性!
trick: 这个题目数据里没有 但是也能帮你找到wa点吧
1 20 13 0 4 1 0 3 1 0 3 1 0 1 0 5 0 20 1 1 1 1
代码:
1. 修改wa之后AC的代码
// // Created by TaoSama on 2015-09-12 // Copyright (c) 2015 TaoSama. All rights reserved. // //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <set> #include <vector> using namespace std; #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; int n, q; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int kase = 0; while(t--) { scanf("%d%d", &n, &q); multiset<int> s; multiset<int>::iterator lef, rig; int cur = 0, dir = 1; long long ans = 0; while(q--) { // printf("q: %d\n", q); int op, x; scanf("%d", &op); if(op == 0) { scanf("%d", &x); s.insert(x); } else { if(s.size() == 0) continue; rig = s.lower_bound(cur); lef = rig; int pre = -INF, nxt = INF; if(rig != s.end()) nxt = *rig; if(lef != s.begin()) pre = *(--lef); int dif1 = cur - pre, dif2 = nxt - cur; // printf("%d %d %d dir: %d\n", pre, cur, nxt, dir); ans += min(dif1, dif2); if(dif1 < dif2) { if(cur != pre) { dir = 0; cur = pre; } s.erase(lef); } else if(dif1 > dif2) { if(cur != nxt) { dir = 1; cur = nxt; } s.erase(rig); } else { if(dir) cur = nxt, s.erase(rig); else cur = pre, s.erase(lef); } } } printf("Case %d: %I64d\n", ++kase, ans); } return 0; }
// // Created by TaoSama on 2015-09-12 // Copyright (c) 2015 TaoSama. All rights reserved. // //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <set> #include <vector> using namespace std; #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; int n, q; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("out.txt","w",stdout); #endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); int kase = 0; while(t--) { scanf("%d%d", &n, &q); multiset<int> s; multiset<int>::iterator iter; int cur = 0, dir = 1; long long ans = 0; while(q--) { // printf("q: %d\n", q); int op, x; scanf("%d", &op); if(op == 0) { scanf("%d", &x); s.insert(x); } else { if(s.size() == 0) continue; iter = s.lower_bound(cur); if(iter == s.end()) x = *(--iter); else if(iter == s.begin()) x = *iter; else { int r = *iter, l = *(--iter); if(cur - l > r - cur) x = r; else if(cur - l < r - cur) x = l; else x = dir ? r : l; } if(x > cur) dir = 1; else if(x < cur) dir = 0; ans += abs(x - cur); cur = x; s.erase(s.find(x)); } } printf("Case %d: %I64d\n", ++kase, ans); } return 0; }