UVALive - 7000 Present Problem 简单模拟

别看了,每一列保存 排序,upper_bound 降复杂度

慢慢写吧


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>

using namespace std;

typedef long long ll;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;

int n, m, len;
vector<int> a[maxn];
int ans[maxn];

void init() {

    scanf("%d %d %d", &n, &m, &len);
    for(int i = 0; i <= n; ++i)
        a[i].clear();
    for(int i = 0; i < m; ++i) {
        int r, c;
        scanf("%d %d", &r, &c);
        a[r].push_back(c);
    }
    for(int i = 0; i < n; ++i) {
        sort(a[i].begin(), a[i].end());
        //cout << " i   " << i << "  = ";
        //for(int j = 0; j < a[i].size(); ++j)
        //    cout << a[i][j] << " ";
        //cout << endl;
    }

}

void solve() {

    if(n == 1) {
        cout << 0 << endl;
        return;
    }

    for(int i = 0; i < n; ++i) {
        ans[i] = i;
        int pos = 0;
        while(1) {
            //cout << ans[i] << " ++= " << pos << endl;
            if(ans[i] == 0) {
                int t = upper_bound(a[0].begin(), a[0].end(), pos) - a[0].begin();
                if(t >= a[0].size()) break;
                else {
                    pos = a[0][t];
                    ans[i] = 1;
                }
            } else if(ans[i] == n-1) {
                int t = upper_bound(a[n-2].begin(), a[n-2].end(), pos) - a[n-2].begin();
                if(t >= a[n-2].size()) break;
                else {
                    //cout << t << " " << a[n-2][t] << " ==== 23333333" << endl;
                    pos = a[n-2][t];
                    ans[i] = ans[i]-1;
                }
            } else {
                int t1 = upper_bound(a[ans[i]-1].begin(), a[ans[i]-1].end(), pos) - a[ans[i]-1].begin();
                int t2 = upper_bound(a[ans[i]].begin(), a[ans[i]].end(), pos) - a[ans[i]].begin();


                if(t1 < a[ans[i]-1].size() && t2 < a[ans[i]].size()) {
                    if(a[ans[i]-1][t1] < a[ans[i]][t2]) {pos = a[ans[i]-1][t1];
                        ans[i] = ans[i]-1;
                    }
                    else {//cout << a[ans[i]-1][t1] << " ------------------------------ " << a[ans[i]][t2] << endl;
                        pos = a[ans[i]][t2];
                        ans[i] = ans[i]+1;
                        //cout << ans[i] << " +++ 4654765 " << pos << endl;
                    }
                    //cout << " 2333 " << endl;
                }
                else if(t1 < a[ans[i]-1].size()) {

                    pos = a[ans[i]-1][t1];
                    ans[i] = ans[i]-1;  //cout << " ++++++++++++++++++++++++ " << endl;
                }
                else if(t2 < a[ans[i]].size()) {pos = a[ans[i]][t2];
                    ans[i] = ans[i]+1;   //cout << " +++++++==============+++++++++++ " << endl;
                }
                else {
                    break;
                }
            }
        }
        //cout << ans[i] << endl;
        printf("%d\n", ans[i]);
    }

}

int main() {

    //int b[4] = {0,1,1,2};
    //int tt = upper_bound(b,b+4, 5) - b;
    //cout << tt << endl;
    int T;
    scanf("%d", &T);
    while(T--) {
        init();
        solve();
    }

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值