F. 蚂蚁

来源:互联网 发布:不懂电工能学plc编程吗 编辑:程序博客网 时间:2024/04/29 22:35

F. 蚂蚁

Time limit per test: 0.5 seconds

Time limit all tests: 5.0 seconds

Memory limit: 256 megabytes

Accept / Submit: 112 / 336

水平线上有 N  只蚂蚁,每只蚂蚁的位置及大小均不同。他们沿着 X 轴爬行,有的向左,有的向右,爬行的速度是一样的,两只蚂蚁相遇大一点的会吃掉小的。

现在从左到右给出每只蚂蚁的大小和爬行的方向(0  表示向左,1  表示向右)。问足够长的时间之后,能剩下多少只蚂蚁?

Input

第 1 行:一个整数 N ,表示蚂蚁的数量(1N10 5 ) 

第 2 到 N+1  行:每行两个数 A i ,B i  (1A i 10 9 B i {0,1}) ,中间用空格分隔,分别表示蚂蚁的大小及爬
行的方向,B i =0  表示向左,B i =1  表示向右。

对于 3/8 的数据,存在 x  满足:所有坐标比 x  小的蚂蚁向左爬、坐标比 x  大的蚂蚁向右爬;或者所有坐标比 x  小的蚂蚁向右爬、坐标比 x  大的蚂蚁向左爬。

Output

输出最终剩下的蚂蚁的数量。

Examples

Input
54 03 12 01 05 0
Output

2

思路:

这个题目真的不难,刚开始被那个3/8数据吓的不知所措,其实到最后我也觉得没什么用,最多就用来判断输入是否合法。

我的思想主要用到了stack,有点像南阳理工数据结构里的括号匹配。

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>#include<stack>using namespace std;typedef struct mayi{    int w;    int flag;}mayi;int main(){    int n;    mayi zhandin;    while(scanf("%d",&n)!=EOF)   {  stack<mayi>mys;      for(int i=1;i<=n;i++)      {          int x,flag;          scanf("%d%d",&x,&flag);          if(mys.empty()) {mayi a={x,flag}; mys.push(a);}          else          {              zhandin=mys.top();              if(zhandin.flag==0) {mayi b={x,flag};mys.push(b);}              else              {                 if(flag==0)                 {                     while(zhandin.w<x&&flag!=zhandin.flag&&!mys.empty())                     {                         mys.pop();                       if(!mys.empty()) zhandin=mys.top();                     }                     if(mys.empty()||zhandin.flag==flag)                     {    mayi c={x,flag};                          mys.push(c);                    }                 }                 else                 {                     mayi d={x,flag};                     mys.push(d);                 }              }          }      }      printf("%d\n",mys.size());}}


原创粉丝点击