2017.3.23 考试小结

7 篇文章 0 订阅
4 篇文章 0 订阅
    今天下午神犇以来机房就说考试,吓得还在调数据结构的我一脸懵逼。
    主要是这段时间一直在码一堆神奇的数据结构体,一天到晚都在调代码,脑子都快成浆糊了......
    开始考试,第一题还好,就是一道简单的数据结构。也没出什么事,到第二题,当时不知道怎么在算时间复杂度,把线段树的时间复杂度算成了O(n*logn)的,想到的正解也就没敢往上码,还在那里纠结了很久,到最后考试快结束的时候,还是码的暴力,哪想到这道题就一个点,连部分分都没有(GG)直接爆0;后来在码第三题的时候,看到还是自己写过博客的矩形树一下子就高兴了,果断码完。结果测的时候T掉了,得的分好像比暴力选手多一个点吧(哭晕在厕所...),不过到现在我也没搞懂为什么矩形树会T,不过倒是想到了小矩形,重复查询多次,可以直接查询一次就记录下来,后来重复的话就直接输答案了(看到题和数据范围你自然就明白了),然后就加了一句话,在OJ上就A了...
    总的来说呢,这次考试还是有很多不细心和想得不够到位的地方,以后还是要多练下题,在考场上调整一下做题时间的分配(比如,把码得来的码完后,暴力也打了,还有时间就应该再去看一下有把握A的题,在优化一下时间,空间,把能拿到的分都尽力拿完)

说的差不多就是这些了,下面就来看一下题吧。


Task1:原子核研究

题目描述:

最近物理学家正在研究一些新的原子,称为X族。他们发现该族中的元素非常相近,但是其质量都不相同。质量越接近的越容易相互转化。现在,物理学家已经发明了一种设备,可以对X族的元素来进行以下三种操作:
1.generate M 产生质量为M的一个元素,如果已经存在,则该操作不产生作用。
2.romove M 销掉质量为M的元素,如果不存在质量为M的元素,则该操作不产生作用。
3.query 查询X族中质量最接近的两种元素的质量差的绝对值。如果此时X族中的元素个数小于2,则输出-1.
现在,请你写一个程序来模拟该设备的操作。

输入:

输入包含多组测试数据,第一行一个整数t,表示测试数据的组数。对于每一组数据,第一行是一个整数n,表示有n条指令。接下来n行,每行表示一条指令,如上所述。M是一个正整数,不超过100000.

输出:

对于每组测试数据,对于其中的每一个查询操作,都要输出一个结果。注意:每一组测试数据后要输出一个空行。

样例输入:

1
12
generate 1
remove 2
generate 1
query
generate 7
query
generate 10
query
generate 5
query
remove 7
query

样例输出:

-1
6
3
2
4

解题思路:

这道题我看到的第一思路就是平衡树的模板题,在每个节点维护一下以该节点为根的子树的最小值(最左下的那个点的值)和最大值(最右下的那个点的值),以及最小差就行了。因为每次插入和删除都会及时更新所以查询就直接在总的根节点找,这样就达到了O(1)的查询。

Code:

#include<cstdio>
#include<climits>
#include<algorithm>
#include<cstring>
using namespace std;

inline void Read(int &Ret){
    char ch; int flg = 1;
    while(ch = getchar(), ch < '0' || ch > '9')
        if(ch == '-') flg = -1;
    Ret = ch - '0';
    while(ch = getchar(), ch >= '0' && ch <= '9')
        Ret = Ret * 10 + ch - '0';
    ungetc(ch, stdin); Ret *= flg;
}

const int MAXN = 100000;
const int INF = INT_MAX;

inline int MIN(int a, int b, int c, int d){return min(min(a, b), min(c, d));}

struct AVL_Tree{
    int rt, cnt, siz;
    bool flg[MAXN + 5];
    struct node{
        int v, mw, lv, rv;
        int h, lson, rson;
    }T[MAXN + 5];

    AVL_Tree(){}

    inline void pushup(int u){
        T[u].h = max(T[T[u].lson].h, T[T[u].rson].h) + 1;
        int tmp1, tmp2, tmp3, tmp4;
        tmp1 = tmp2 = tmp3 = tmp4 = INF;
        if(T[u].lson) T[u].lv = T[T[u].lson].lv, tmp1 = T[T[u].lson].mw, tmp3 = T[u].v - T[T[u].lson].rv;
        else T[u].lv = T[u].v;
        if(T[u].rson) T[u].rv = T[T[u].rson].rv, tmp2 = T[T[u].rson].mw, tmp4 = T[T[u].rson].lv - T[u].v;
        else T[u].rv = T[u].v;
        T[u].mw = MIN(tmp1, tmp2, tmp3, tmp4);
    }

    inline int Right(int u){
        int tmp = T[u].lson;
        T[u].lson = T[tmp].rson;
        T[tmp].rson = u;
        pushup(u);
        pushup(tmp);
        return tmp;
    }

    inline int Left(int u){
        int tmp = T[u].rson;
        T[u].rson = T[tmp].lson;
        T[tmp].lson = u;
        pushup(u);
        pushup(tmp);
        return tmp;
    }

    inline int Left_Right(int u){
        T[u].lson = Left(T[u].lson);
        return Right(u);
    }

    inline int Right_Left(int u){
        T[u].rson = Right(T[u].rson);
        return Left(u);
    }

    inline void maintain(int &u){
        pushup(u);
        if(T[T[u].lson].h == T[T[u].rson].h + 2){
            int tmp =  T[u].lson;
            if(T[T[tmp].lson].h == T[T[u].rson].h + 1) u = Right(u);
            else if(T[T[tmp].rson].h == T[T[u].rson].h + 1) u = Left_Right(u);
        }
        else if(T[T[u].lson].h + 2 == T[T[u].rson].h){
            int tmp = T[u].rson;
            if(T[T[tmp].rson].h == T[T[u].lson].h + 1) u = Left(u);
            else if(T[T[tmp].lson].h == T[T[u].lson].h + 1) u = Right_Left(u);
        }
        pushup(u);
    }

    int Insert(int u, int v){
        if(!u){
            cnt ++;
            T[cnt].lv = T[cnt].rv = T[cnt].v = v;
            T[cnt].h = 1;
            T[cnt].mw = INF;
            return cnt;
        }
        if(v < T[u].v) T[u].lson = Insert(T[u].lson, v);
        else if(v > T[u].v) T[u].rson = Insert(T[u].rson, v);
        maintain(u);
        return u;
    }

    inline int Find_maxu(int u){
        while(T[u].rson)
            u = T[u].rson;
        return T[u].v;
    }

    int del(int u, int v){
        if(T[u].v == v){
            if(!T[u].lson || !T[u].rson) return T[u].lson + T[u].rson;
            else{
                int tmp = Find_maxu(T[u].lson);
                T[u].v = tmp;
                T[u].lson = del(T[u].lson, tmp);
                maintain(u);
                return u;
            }
        }
        if(v < T[u].v) T[u].lson = del(T[u].lson, v);
        else T[u].rson = del(T[u].rson, v);
        maintain(u);
        return u;
    }

    void INSERT(int v){
        if(flg[v]) return;
        flg[v] = 1; siz ++;
        rt = Insert(rt,v);
    }

    void DELETE(int v){
        if(!flg[v]) return;
        flg[v] = 0; siz --;
        rt = del(rt, v);
    }

    void QUERY(){
        if(siz < 2) printf("-1\n");
        else printf("%d\n",T[rt].mw);
    }

    void CLEAR(){
        memset(T, 0, sizeof T);
        memset(flg, 0, sizeof flg);
        siz = rt = cnt = 0;
    }

}mytr;

int main(){
    int T, n, v;
    char ord[15];
    Read(T);
    while(T --){
        Read(n);
        while(n --){
            scanf("%s",ord);
            switch(ord[0]){
                case 'g': Read(v), mytr.INSERT(v); break;
                case 'r': Read(v), mytr.DELETE(v); break;
                default: mytr.QUERY();
            }
        }
        putchar(10);
        mytr.CLEAR();
    }
    return 0;
}

Task2: 球星

题目描述:

给出球星们的能力值、年份、名字,有很多个查询,每个查询给出一个年份的范围,求出这个范围里能力值从高到低排列的前11名球员,如果能力值相同则按年份从低到高排,如果年份仍然相同,则按名字的字典序排。如果不足11个球员,就用XXX代替输出凑够11行。

输入:

第一行包含1个整数N(1<=N<=50000),表示球星的总数,接下来N行,每行描述了1个球星(year,name,ability)。
保证没有两个人的名字相同。
接下来有一个整数R,表示有R个查询。接下来R行,每行描述了一个产寻,每个查询包含2个整数(x,y),表示从第x年到第y年
(0<=year<=1000000000)
(name不超过15个字母)
(0<=ability<=1000000000)
(0<=x<=y<=1000000000)

输出:

对于每组数据,按上面描述的顺序输出最靠前的11个球员的名字,每个球员占一行。如果不足11行,则用XXX代替,凑够11行。每组数据后都有一个空行。

样例输入:

5
1 a 1
2 b 2
2 c 6
5 e 50
5 d 50
2
1 2
5 5

样例输出:

c
b
a
XXX
XXX
XXX
XXX
XXX
XXX
XXX
XXX

d
e
XXX
XXX
XXX
XXX
XXX
XXX
XXX
XXX
XXX

解题思路:

其实就是一个简单的线段树,每个节点存当前区间里的前11名球员,因为每个点都存的只有11个,所以合并成大区间的时候直接暴力合并就行了,时间复杂度也只有O(11*4*n),然后单次查询也是O(11*logn)的(考试的时候不知道怎么的就算成了O(nlogn)的单次查询了)

Code:

#include<cstdio>
#include<cstring>
#include<vector>
#include<climits>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 50000;
const int INF = INT_MAX;

inline void Read(int &Ret){
    char ch; int flg = 1;
    while(ch = getchar(), ch < '0' || ch > '9')
        if(ch == '-') flg = -1;
    Ret = ch - '0';
    while(ch = getchar(), ch >= '0' && ch <= '9')
        Ret = Ret * 10 + ch - '0';
    Ret *= flg;
}

struct PLAYER{
    int v, year, name;
    bool operator < (const PLAYER &p) const{
        if(v != p.v) return v > p.v;
        if(year != p.year) return year  < p.year;
        return name < p.name;
    }
};

struct sr_name{
    char name[20];
    int id;
    bool operator < (const sr_name &p) const{
        return strcmp(name, p.name) < 0;
    }
};

struct TREE{
    int l, r;
    PLAYER star[12];
};

PLAYER player[MAXN + 5], Tmp, cpy[12], ans[12];
TREE T[MAXN * 4];
int year[MAXN + 5], year_cnt;
sr_name names[MAXN + 5];
vector <PLAYER> years[MAXN + 5];

#define lson u << 1
#define rson u << 1 | 1

inline void Merge(PLAYER *u, PLAYER *ls, PLAYER *rs){
    int lt = 0, rt = 0;
    for(int i = 0; i < 11; ++ i){
        if(ls[lt] < rs[rt]){
            u[i] = ls[lt];
            lt ++;
        }
        else{
            u[i] = rs[rt];
            rt ++;
        }
    }
}

void build(int u, int l, int r){
    T[u].l = l; T[u].r = r;
    if(l == r){
        sort(years[l].begin(), years[l].end());
        int len = min((int)years[l].size(), 11);
        for(int i = 0; i < len; ++ i)
            T[u].star[i] = years[l][i];
        for(int i = len; i < 11; ++ i)
            T[u].star[i] = Tmp;
        return;
    }
    int mid = (l + r) >> 1;
    build(lson, l, mid);
    build(rson, mid + 1, r);
    Merge(T[u].star, T[lson].star, T[rson].star);
}

void GET_TOP(int u, int l, int r){
    if(r < T[u].l || T[u].r < l) return;
    if(l <= T[u].l && T[u].r <= r){
        Merge(cpy, ans, T[u].star);
        for(int i = 0; i < 11; ++ i)
            ans[i] = cpy[i];
        return;
    }
    GET_TOP(lson, l, r);
    GET_TOP(rson, l, r);
}

inline void Get_Top11(int l, int r){
    if(l > year[year_cnt] || l > r || r < year[1]){
        for(int i = 0; i < 11; ++ i)
            printf("XXX\n");
        return;
    }
    l = lower_bound(year + 1, year + year_cnt + 1, l) - year;
    if(r > year[year_cnt]) r = year_cnt;
    else{
        int tmp_r = r;
        r = lower_bound(year + 1, year + year_cnt + 1, r) - year;
        if(year[r] > tmp_r) r --;
    }
    for(int i = 0; i < 11; ++ i)
        ans[i] = Tmp;
    GET_TOP(1, l, r);
    for(int i = 0; i < 11; ++ i)
        puts(names[ans[i].name].name);
}

int main(){
    int n, m;
    Read(n);
    for(int i = 1; i <= n; ++ i){
        Read(year[i]);
        player[i].year = year[i];
        scanf("%s",names[i].name);
        names[i].id = i;
        Read(player[i].v);
    }

    sort(names + 1, names + n + 1);
    sort(year + 1, year + n + 1);
    year_cnt = unique(year + 1, year + n + 1) - year - 1;

    strcpy(names[n + 1].name, "XXX");
    Tmp.v = -INF; Tmp.name = n + 1;

    for(int i = 1; i <= n; ++ i){
        player[names[i].id].name = i;
        player[i].year = lower_bound(year + 1, year + 1 + year_cnt, player[i].year) - year;
    }

    for(int i = 1; i <= n; ++ i)
        years[player[i].year].push_back(player[i]);

    build(1, 1, year_cnt);

    Read(m);
    int L, R;
    while(m --){
        Read(L); Read(R);
        Get_Top11(L, R);
        putchar(10);
    }
    return 0;
}

Task3: 跳跃

题目描述:

公园中有许多木桩,每个木桩都有一个高度,活泼的小 z 同学喜欢从一个木桩跳到另 一个木桩上,zn 说太危险了,于是 z 同学让 zn 算出每一次跳跃的危险系数。小z 每一次跳 跃的范围是一个 k ∗ k 的矩形,危险系数为这个矩形内最高的木桩高度减去最小的。

输入:

第一行三个整数 n(木桩为 n ∗ n 的矩阵)、k、b(小 zz 准备跳跃的次数) 接下来为 n ∗ n 的矩阵,描述木桩的高度。
接下来 b 行;每行两个整数 x, y(表示 z 同学跳跃的范围的左上角为第 x 行第 y列), 保证跳跃范围不超过矩阵的范围。

输出:

对于每个x和y,输出当前跳跃的危险系数(每次都要换行)

数据范围:

对于30%的数据 0 < k ≤ n ≤ 250,0 < b ≤ 100
对于100%的数据 0 < k ≤ n ≤ 250,0 < b ≤ 1000000

样例输入:

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

样例输出:

5

解题思路:

这就是一道二维线段树,或是矩形树的裸题没什么好多说的,直接上代码。

Code:

#include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;

const int MAXN = 250;
const int MAXM = MAXN * MAXN * 16;
const int INF = INT_MAX;

int arr[MAXN + 5][MAXN + 5], ans[MAXN + 5][MAXN + 5];

#define son1 (u << 2) - 2
#define son2 (u << 2) - 1
#define son3 (u << 2)
#define son4 (u << 2) + 1


inline void Read(int &Ret){
    char ch; int flg = 1;
    while(ch = getchar(), ch < '0' || ch > '9')
        if(ch == '-') flg = -1;
    Ret = ch - '0';
    while(ch = getchar(), ch >= '0' && ch <= '9')
        Ret = Ret * 10 + ch - '0';
    ungetc(ch, stdin); Ret *= flg;
}

inline int MAX(int a, int b, int c, int d){
    return max(a, max(b, max(c, d)));
}

inline int MIN(int a, int b, int c, int d){
    return min(a, min(b, min(c, d)));
}

struct node{
    int x1, y1, x2, y2;
    int ma, mi;
}T[MAXM];

void pushup(int u){
    T[u].ma = MAX(T[son1].ma, T[son2].ma, T[son3].ma, T[son4].ma);
    T[u].mi = MIN(T[son1].mi, T[son2].mi, T[son3].mi, T[son4].mi);
}

void build(int u, int x1, int x2, int y1, int y2){
    T[u].x1 = x1; T[u].x2 = x2;
    T[u].y1 = y1; T[u].y2 = y2;
    if(x1 == x2 && y1 == y2){
        T[u].ma = T[u].mi = arr[x1][y1];
        return;
    }
    int midx = (x1 + x2) >> 1;
    int midy = (y1 + y2) >> 1;
    if(x1 == x2){
        build(son1, x1, x1, y1, midy);
        build(son3, x1, x1, midy + 1, y2);
    }
    else if(y1 == y2){
        build(son1, x1, midx, y1, y1);
        build(son2, midx + 1, x2, y1, y1);
    }
    else{
        build(son1, x1, midx, y1, midy);
        build(son2, midx + 1, x2, y1, midy);
        build(son3, x1, midx, midy + 1, y2);
        build(son4, midx + 1, x2, midy + 1, y2);
    }
    pushup(u);
}

int getmax(int u, int x1, int x2, int y1, int y2){
    if(x2 < T[u].x1 || T[u].x2 < x1 || y2 < T[u].y1 || T[u].y2 < y1)
        return -INF;
    if(x1 <= T[u].x1 && T[u].x2 <= x2 && y1 <= T[u].y1 && T[u].y2 <= y2)
        return T[u].ma;
    int ret;
    ret = getmax(son1, x1, x2, y1, y2);
    ret = max(ret, getmax(son2, x1, x2, y1, y2));
    ret = max(ret, getmax(son3, x1, x2, y1, y2));
    ret = max(ret, getmax(son4, x1, x2, y1, y2));
    return ret;
}

int getmin(int u, int x1, int x2, int y1, int y2){
    if(x2 < T[u].x1 || T[u].x2 < x1 || y2 < T[u].y1 || T[u].y2 < y1)
        return INF;
    if(x1 <= T[u].x1 && T[u].x2 <= x2 && y1 <= T[u].y1 && T[u].y2 <= y2)
        return T[u].mi;
    int ret;
    ret = getmin(son1, x1, x2, y1, y2);
    ret = min(ret, getmin(son2, x1, x2, y1, y2));
    ret = min(ret, getmin(son3, x1, x2, y1, y2));
    ret = min(ret, getmin(son4, x1, x2, y1, y2));
    return ret;
}

int main(){
    for(int i = 1; i < MAXM; ++ i)
        T[i].ma = -INF, T[i].mi = INF;
    int n, k, b, x, y, tmp1, tmp2;
    Read(n); Read(k); Read(b);
    k --;
    for(int i = 1; i <= n; ++ i)
        for(int j = 1; j <= n; ++ j)
            Read(arr[i][j]);
    build(1, 1, n, 1, n);
    while(b --){
        Read(x); Read(y);
        if(ans[x][y]){
            printf("%d\n",ans[x][y]);
            continue;
        }
        tmp1 = getmax(1, x, x + k, y, y + k);
        tmp2 = getmin(1, x, x + k, y, y + k);
        ans[x][y] = tmp1 - tmp2;
        printf("%d\n",tmp1 - tmp2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值