zoukankan      html  css  js  c++  java
  • 1276 岛屿的数量 思维题

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1276

    首先,对每个询问拍个序,那么就相当于有一条水平线慢慢升上去一样。

    对原数组也拍个序,就可以知道每一个询问会删除多少个点,和点的位置在哪里。

    现在要看的就是,删除了这一个点,岛屿的数量会怎么变化呢?

    假如我们已经处理出,当前这个询问,删除的点的id是,vc[0...k],并且vc[]递增。

    1、假如删除了这一个点,同时它的左右都删除了,那么ans--

    2、假如删除了这一个点,同时它的左右都没删除,那么ans++

    3、其他情况,ans不变

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    const int maxn = 50000 + 20;
    struct node {
        int val, id;
        bool operator < (const struct node & rhs) const {
            if (val != rhs.val) return val < rhs.val;
            return id < rhs.id;
        }
    }a[maxn], query[maxn];
    int ans[maxn];
    vector<int>vc;
    bool del[maxn];
    void calc(int id, int &now) {
        if (del[id - 1] && del[id + 1]) now--;
        if (!del[id - 1] && !del[id + 1]) now++;
    }
    void work() {
        int n, q;
        cin >> n >> q;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i].val;
            a[i].id = i;
        }
        for (int i = 1; i <= q; ++i) {
            cin >> query[i].val;
            query[i].id = i;
        }
        sort(a + 1, a + 1 + n);
        sort(query + 1, query + 1 + q);
        int to = 1, now = 1;
        del[0] = true;
        del[n + 1] = true;
        for (int i = 1; i <= q; ++i) {
            vc.clear();
            while (to <= n && query[i].val >= a[to].val) {
                vc.push_back(a[to].id);
                to++;
            }
            sort(vc.begin(), vc.end());
            for (int j = 0; j < vc.size(); ++j) {
                del[vc[j]] = true;
                calc(vc[j], now);
            }
            ans[query[i].id] = now;
        }
        for (int i = 1; i <= q; ++i) {
            cout << ans[i] << endl;
        }
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    第四十七讲 ASP.NET实例编程(六)
    第四十四讲 ASP.NET实例编程(三)
    第四十一讲 ASP.NET消息处理(二)
    第四十三讲 ASP.NET实例编程(二)
    第四十二讲 ASP.NET实例编程(一)
    第四十六讲 ASP.NET实例编程(五)
    第四十八讲 ASP.NET实战编程(一)
    第四十讲 ASP.NET消息处理(一)
    第四十五讲 ASP.NET实例编程(四)
    第三十九 ASP.NET编码
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6380355.html
Copyright © 2011-2022 走看看