51nod -1289 . 大鱼吃小鱼(思维)
生活随笔
收集整理的這篇文章主要介紹了
51nod -1289 . 大鱼吃小鱼(思维)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1289?.?大魚吃小魚 題目來(lái)源:?Codility 基準(zhǔn)時(shí)間限制:1?秒 空間限制:65536?KB 分值:?5 有N條魚每條魚的位置及大小均不同,他們沿著X軸游動(dòng),有的向左,有的向右。游動(dòng)的速度是一樣的,兩條魚相遇大魚會(huì)吃掉小魚。從左到右給出每條魚的大小和游動(dòng)的方向(0表示向左,1表示向右)。問足夠長(zhǎng)的時(shí)間之后,能剩下多少條魚? Input 第1行:1個(gè)數(shù)N,表示魚的數(shù)量(1?<=?N?<=?100000)。
第2?-?N?+?1行:每行兩個(gè)數(shù)A[i],?B[i],中間用空格分隔,分別表示魚的大小及游動(dòng)的方向(1?<=?A[i]?<=?10^9,B[i]?=?0?或?1,0表示向左,1表示向右)。 Output 輸出1個(gè)數(shù),表示最終剩下的魚的數(shù)量。 Input 示例 5
4 0
3 1
2 0
1 0
5 0 Output 示例 2
注意隱含條件是從左至右給出,然后用棧從左至右掃一遍就行了。
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <string> #include <set> #include <functional> #include <numeric> #include <sstream> #include <stack> #include <map> #include <queue>#define CL(arr, val) memset(arr, val, sizeof(arr))#define ll long long #define inf 0x7f7f7f7f #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0)#define L(x) (x) << 1 #define R(x) (x) << 1 | 1 #define MID(l, r) (l + r) >> 1 #define Min(x, y) (x) < (y) ? (x) : (y) #define Max(x, y) (x) < (y) ? (y) : (x) #define E(x) (1 << (x)) #define iabs(x) (x) < 0 ? -(x) : (x) #define OUT(x) printf("%I64d\n", x) #define lowbit(x) (x)&(-x) #define Read() freopen("a.txt", "r", stdin) #define Write() freopen("dout.txt", "w", stdout); #define N 100005 using namespace std; int main() {//Read();stack<int>s;int n,a,b,m;scanf("%d",&n);m=n;while(n--){scanf("%d%d",&a,&b);if(b==1) s.push(a);else{while(!s.empty()){if(a>s.top()) {s.pop();m--;}else { m--;break;}}}}printf("%d\n",m);return 0; }
總結(jié)
以上是生活随笔為你收集整理的51nod -1289 . 大鱼吃小鱼(思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小乌龟Tortoisegit官方汉化包
- 下一篇: 图片元素