poj1828-Monkeys’ Pride

來源:互聯網
上載者:User
 題目大意:

題目給出n個點,求出有多少點滿足沒有橫縱座標同時大於等於這個點的個數。也就是在這所有的點中,這樣的點有多少個?什麼樣的點呢?設這個點(x0,y0),那麼如果不存在(x,y)使得x>=x0並且y>=y0,那麼這就算一個點,求出所有這樣的點。

 

演算法:

我們可以豎杆子,什麼意思,就是如果x相同,那麼按照y從小到大排序。那麼我們會發現x-y座標軸上是一根根杆子,那麼我們從後面開始,第一個點肯定算一個,因為他的x和y都是最大的,沒有誰大過它。  然後往前走,由於是按照x排序,再按照y排序,所以如果前面的y小於當前的y,那麼它肯定不是,因為x一定小於當前的x。所以就相當於找出大於最後一個y的點,算是第二個,然後再把這個y當做最大值,往前繼續找比他大的,如果有,算一個,更新最大值,知道第一個點。

代碼:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef struct Point{int x;int y;};bool cmp(const Point &a, const Point &b){if (a.x == b.x){return a.y < b.y;}elsereturn a.x < b.x;}int main(){Point p[50001];int num;while (scanf("%d", &num),num){for (int i = 0; i < num; i ++){scanf("%d %d", &p[i].x, &p[i].y);}sort(p, p + num, cmp);int ans = 1;int mMax = p[num - 1].y;for (int i = num - 2; i >= 0; i --){if (mMax < p[i].y){mMax = p[i].y;ans ++;}}printf("%d\n",ans);}return 0;}

代碼很簡單,就不做注釋了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.