这题貌似是蓝桥杯的一题改了个题面。
就是模拟啦,应该有比我的更简洁的方法。
我的方法是把所有的人(蚂蚁)按位置排完序以后从左往右看,每次有一个向左走的,就会把最左边的t出,这个变成向右中,同时,从左端到此位置的人都会相遇一遍,处理一下就好了。
不废话了,直接上代码
/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std; typedef struct People {
bool sad;
int x;
People (int xx = , bool s = false) {
sad = s;
x = xx;
}
} People; bool inline operator<(const People& p1, const People& p2) {
return abs(p1.x) < abs(p2.x);
} const int MAXP = ;
People pe[MAXP];
int N; void run() {
People *start = pe;
People *end = &pe[N - ];
while (start < end && (*start).x < ) {
start++;
}
while (start < end) {
People *p = start;
while (p <= end && (*p).x > ) {
p++;
}
if (p > end) {
break;
}
(*p).x = - (*p).x;
while (--p >= start) {
(*p).sad = (*(p + )).sad = (*p).sad or (*(p + )).sad;
}
start++;
}
} int main() {
int x;
while (scanf("%d", &N) == ) {
memset(pe, , sizeof(pe));
pe[].sad = true;
for (int i = ; i < N; i++) {
scanf("%d", &pe[i].x);
}
sort(pe, pe + N);
run();
int ans = ;
for (int i = ; i < N; i++) {
ans += pe[i].sad;
}
printf("%d\n", ans);
}
return ;
}