• 【算法学习笔记】78. STL二分的练习 下标映射的处理技巧 SJTU OJ 1053 二哥的内存


    水题也要优化效率嘛

    1.用两个数组单独记录下标的更新

    2.用STL中lower_bound来进行二分查找.

    要注意lower_bound的返回值意义 是大于等于val的第一个,所以返回值要进行判断才可以利用

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    
    struct Point
    {
        int x;
        int y;
        int z;
    };
    
    int n;
    const int MAXN = 100000;
    //int data[MAXN][MAXN];
    Point points[MAXN];
    int curx[MAXN];
    int cury[MAXN];
    
    void init(){
        cin>>n;
        for (int i = 0; i < n; ++i){
            //int x,y,z;
            //scanf("%d %d %d",&x,&y,&z);
            //data[x][y] = z;
            scanf("%d %d %d",&points[i].x,&points[i].y,&points[i].z);
        }
        
        for (int i = 0; i < MAXN; ++i)
            curx[i]=i;
        for (int i = 0; i < MAXN; ++i)
            cury[i]=i;
        
    }
    
    bool cmpPoint(const Point& a,const Point& b){
        if(a.x!=b.x){
            return a.x<b.x;
        }else
            return a.y<b.y;
    }
    
    
    int find(int x,int y){
        Point tofind;
        tofind.x = x;
        tofind.y = y;
        Point* f = lower_bound(points,points+n,tofind,cmpPoint);
        //注意lower_bound的返回值是大于等于val的第一个 不一定正好是它
        if(f != points+n and f->x==x and f->y==y)
            return f->z;
        return 0;
    }
    
    void exe(){
        int m;
        cin>>m;
        sort(points,points+n,cmpPoint);
        for (int i = 0; i < m; ++i)
        {
            int op,x,y;
            scanf("%d %d %d",&op,&x,&y);
            if(op==0){
                swap(curx[x],curx[y]);
            }else if(op==1)
                swap(cury[x],cury[y]);
            else{
                //printf("%d
    ", data[curx[x]][cury[y]]);
                printf("%d
    ", find(curx[x],cury[y]));
            }
        }
    }
    
    
    int main(int argc, char const *argv[])
    {
        init();
        exe();
        return 0;
    }
  • 相关阅读:
    jQuery EasyUI API 中文文档
    easyui datagrid使用
    Jquery EasyUI的添加,修改,删除,查询等基本操作介绍
    JQueryEasyUI datagrid框架的基本使用
    使用easyUI 创建复杂的toolbar到datagrid
    转换和删除重复命令tr
    格式化文本数据抽取工具awk
    流编辑器sed
    查找文本工具grep
    查找文件工具find
  • 原文地址:https://www.cnblogs.com/yuchenlin/p/sjtu_oj_1053.html
Copyright © 2020-2023  润新知