蚂蚁感冒
来源:互联网 发布:结构化数据 编辑:程序博客网 时间:2024/04/27 15:32
nyoj990 和蓝桥杯都有这道题。
由于数据很水,模拟应该也能过。
主要说一下O(n)算法,两只蚂蚁相遇可以看做互相穿过,那么向右走的蚂蚁数量和向右走的蚂蚁数量都不会变。假设第一只感冒的蚂蚁向右走,那么在它右边向左走的蚂蚁都会感冒,如果它的右边存在向左走的蚂蚁,那么它的左边向右走的蚂蚁也会和他们相遇,总的感冒蚂蚁就算出来了。
AC代码
#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10#define inf 0x3f3f3f3f#define PI pair<int, int> typedef long long LL;const int maxn = 50 + 5;int pos[maxn];bool cmp(int a, int b) {return abs(a) < abs(b); }int main() {int n;while(scanf("%d", &n) == 1) {for(int i = 0; i < n; ++i) scanf("%d", &pos[i]);int fir = pos[0]; //第一只蚂蚁的位置 sort(pos, pos+n, cmp);int b = 0, c = 0;int tpos;for(int i = 0; i < n; ++i) {if(pos[i] == fir) {tpos = i;break;}if(pos[i] > 0) ++b;}for(int i = tpos+1; i < n; ++i) {if(pos[i] < 0) ++c;}int ans = 1;if(fir < 0) { //向左 ans += b;if(b) ans += c;}else if(fir > 0) { //向右 ans += c;if(c) ans += b;}printf("%d\n", ans); }return 0;}
如有不当之处欢迎指出!
0 0
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 蚂蚁感冒
- 使用腾讯云Ubuntu服务器搭建MySQL+Tomcat+jdk
- unity总结四
- Oracle数据库基础知识点
- Android退出应用最优雅的方式
- 经典门限秘密共享方案
- 蚂蚁感冒
- 【linux】--初识Linux
- 有关opencv的学习(16)—图像的膨胀和腐蚀(2)
- webView移动H5适配问题
- 【C++模板】C++声明模板时能否用struct关键字代替class或者typename
- (转)Android getWidth和getMeasuredWidth的正解
- css 符号样式 ()
- async
- android-webView使用问题