排序+逆向思維 ACdream 1205 Disappeared Block

來源:互聯網
上載者:User

標籤:

 

題目傳送門

 

 1 /* 2     從大到小排序,逆向思維,從最後開始考慮,無後向性 3     每找到一個沒被淹沒的,對它左右的樓層查詢是否它是孤立的,若是++,若不是-- 4     複雜度 O(n + m),還以為 O(n^2)嚇得寫了一半就不寫了 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm>10 #include <map>11 #include <set>12 #include <string>13 using namespace std;14 15 const int MAXN = 1e6 + 10;16 const int INF = 0x3f3f3f3f;17 struct Hight18 {19     int val, id;20 }h[MAXN];21 int q[MAXN];22 int vis[MAXN];23 int ans[MAXN];24 25 bool cmp(Hight a, Hight b)26 {27     return a.val > b.val;28 }29 30 int main(void)        //ACdream 1205 Disappeared Block31 {32     //freopen ("J.in", "r", stdin);33 34     int t;35     int n, m;36 37     scanf ("%d", &t);38     int cas = 0;39     while (t--)40     {41         memset (vis, 0, sizeof (vis));42         scanf ("%d%d", &n, &m);43         for (int i=1; i<=n; ++i)44         {45             scanf ("%d", &h[i].val);46             h[i].id = i;47         }48         for (int i=1; i<=m; ++i)    scanf ("%d", &q[i]);49 50         sort (h+1, h+1+n, cmp);51 52         int cnt = 0;    int res = 0;53         for (int i=1, j=m; j>=1; --j)54         {55             for (; i<=n; ++i)56             {57                 if (h[i].val <= q[j])    break;58                 int pos = h[i].id;59                 vis[h[i].id] = 1;60                 if (!vis[pos-1] && !vis[pos+1])    res++;61                 if (vis[pos-1] && vis[pos+1])    res--;62             }63             ans[j] = res;64         }65         printf ("Case #%d: ", ++cas);66         for (int i=1; i<=m; ++i)    printf ("%d%c", ans[i], (i==m) ? ‘\n‘ : ‘ ‘);67     }68     69 70     return 0;71 }72 73 /*74 Case #1: 1 1 2 75 Case #2: 1 2 1 1 076 */

 

排序+逆向思維 ACdream 1205 Disappeared Block

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.