Test41普转提考试总结

· · 题解

Test41普转提考试总结

Data

Score:40 = 10 + 10 + 20 + 0

Rank: B组P14,全组P30(diandi)

Preface

坐牢了孩子们,成功再次坠机>___< /(ㄒoㄒ)/~~

浙江的题还是太权威太有水平了:(

确实是想不到,跟状态可能有点关系,但大体还是思路问题,根本就没在正解的路上

T1想到二分就起飞了

T2啥都没想也起飞了

T3没有用数字优化还起飞了

T4瞎JB乱写本来就起飞了

啊啊啊o((>ω< ))o要死了

qc真的要死了

救救QC吧

Statements

A. 玩具(toy)

题面
有 t 波小朋友,n 个篮子,每个篮子有 a_i 个玩具熊。每波小朋友有 k_i 人,选择区间 [l_i, r_i] 内连续的一段篮子,要求每个小朋友分到的玩具熊数量相同。若满足要求则加 w_i 分,否则扣 w_i 分。初始分为 s,求最大得分。

输入格式
第一行:t, n
第二行:n 个整数 a_i
接下来 t 行:每行 l_i, r_i, k_i, w_i
最后一行:初始分 s

输出格式
最大得分 w

数据范围

样例输入

Input

2 5
2 1 5 2 4 3
2 5 7 4
1 3 9 10
100

Output

94

Solution

此题因为要满足连续区间是k的倍数,所以我们想到使用前缀和\%k的方式,而非二分.二分只能求到一部分答案,对于特殊的样例构造,二分无法实现

我们使用前缀和\%k的方式,又观察到k_i \leq 100,我们直接暴力枚举所有的k,去查找在区间[l_i,r_i]范围内是否有两个不同的s_i \% k是相等的,如果有,就直接加,否则就减

这里给一组小数据(甚至逆天到考试的二分代码都能通过???) Input:

7 10
1 3 4 2 5 8 6 7 9 11
3 5 1 10
4 9 7 10
1 1 100 10
1 10 11 10
5 6 13 10
6 10 17 10
6 10 18 10
100

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 1e2 + 11;
LL t,n,org;
LL a[N],s[N],ys[N];
vector<int> v[M];
struct node{
    int l,r,k,w;
}g[N];
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("toy.out","w",stdout);
    read(t);read(n);
    for(LL i=1;i<=n;i++) read(a[i]);
    for(LL i=1;i<=n;i++) s[i] = s[i - 1] + a[i];
    for(LL i=1;i<=t;i++){
        read(g[i].l);read(g[i].r);read(g[i].k);read(g[i].w);
        v[g[i].k].emplace_back(i);
    }
    read(org);
    for(int i=0;i<=100;i++){
        if(v[i].size() == 0) continue;
        for(int j=1;j<=n;j++) ys[j] = s[j] % i;
        for(auto x : v[i]){
            int l = g[x].l,r = g[x].r;
            map<int,int> mp;
            mp.clear();
            bool fl = 0;
            for(int k=l-1;k<=r;k++){
                if(mp.find(ys[k]) != mp.end()){
                    fl = 1;
                    break;
                }
                mp[ys[k]] = k;
            }
            if(fl == 1) org += g[x].w;
            else org -= g[x].w;
        }
    }   
    print(org);
    return 0;
}

B. 数列(seq)

题面
给定数列 A = \{a_i\},构造数列 B = \{b_i\} 满足:

输出所有可能的 g。

输入格式
第一行:T(数据组数)
每组数据:

输出格式
每组数据一行,升序输出所有满足条件的 g

样例输入

1
6 3 1
78 69 55 102 233 666

样例输出

1 2 3 4 5 6 11

数据范围 设 a_i 的最大值为 A

测试点编号 n, k, f, A T
1-6 \leq 10 -
7-10 \leq 3 \times 10^3, f = 0 -
11-12 \leq 5 \times 10^3 \leq 3
13-14 \leq 3 \times 10^4 -
15-16 \leq 5 \times 10^4 -
17-18 \leq 5 \times 10^5 -
19-20 \leq 2 \times 10^6 \leq 2

Solution

我们观察到要满足有至少n - f个元素是g的倍数,我们思考如何快速实现这一点

我们先使用前缀和将所有的元素个数累加起来

我们可以将所有可能的g放在数轴上,对于g \leq k这种情况,因为这时候a_i \bmod g = a_i,故我们直接前缀和暴力枚举元素个数是否大于等于n - f即可

对于g > k的情况,我们这么进行考虑

我们将区间[a_i-k,a_i]放到数轴上,注意到此时g的间隔已经大于所有区间的长度(其实所有区间等长,都是k),我们就可以用查分数组标记所有的区间,然后再在枚举g,此时的g就可以理解为g间的间隔,然后累加每个间隔的值,最后判断是否大于等于n - f

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 2e6 + 11,M = 25;
int t,n,k,f;
int a[N],mp[N],s[N],c[N];
//unordered_map<int,int> mp;
void init(){
    for(int i=1;i<=N-11;i++) mp[i] = s[i] = c[i] = 0;
    return;
}
int main(){
//    freopen("seq.in","r",stdin);
//    freopen("seq.out","w",stdout);
    read(t);
    while(t --){
        read(n);read(k);read(f);
        init();
        for(int i=1;i<=n;i++){
            read(a[i]);
            mp[a[i]] ++;
        }
        for(int i=1;i<=N-11;i++) s[i] = s[i - 1] + mp[i];
        for(int i=1;i<=k+1;i++){
            if(s[N - 11] - s[i - 1] >= n - f){
                print(i);
                putchar(' ');
            }
        }
        for(int i=1;i<=n;i++){
            int l = max(1,a[i] - k),r = a[i];
            c[l] ++,c[r + 1] --;
        }
        for(int i=1;i<=N-11;i++) c[i] += c[i - 1];
        for(int i=k+2;i<=N-11;i++){
            int cnt = 0;
            for(int j=i;j<=N-11;j+=i) cnt += c[j];
            if(cnt >= n - f){
                print(i);
                putchar(' ');
            }
        }
        putchar('\n');
    }
    return 0;
}

C. 文件(file)

题面
有一个 n \times m 的矩阵,每个元素是一个字符串。经过 q 次加密,每次加密是交换两个大小相同的子矩形(左上角分别为 (x_1,y_1) 和 (x_2,y_2),大小为 l \times c)。给出加密操作(倒序),要求按顺序执行这些操作(即解密),输出最终矩阵。

输入格式
第一行:n, m, q
接下来 n 行:每行 m 个字符串
接下来 q 行:每行 x_1, y_1, x_2, y_2, l, c

输出格式
解密后的 n \times m 矩阵

样例输入

4 4 2
a a b b
a a b b
c c d d
c c d d
1 1 3 3 2 2
3 1 1 3 2 2

样例输出

d d c c
d d c c
b b a a
b b a a

数据范围

Solutions

S1.半暴力大法

因为此题时限3s,我们直接考虑暴力枚举所有要交换的矩形的每一个点,但注意string的swap操作的时间复杂度并非O(1),所以对string数组进行编号,使用编号的swap,最大时间复杂度为O(q\times n^2),结果在赌狗WLS的豪赌下,竟然AC了??!!!,其实我TM随便加强点数据(n,q数据拉满,l,c每次都顶死n),这个2B就死透了,这个神仙方法也落幕了

王屎山代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
inline void read(string &s){
    s = "";
    char c = getchar();
    while(c == ' ' || c == '\n' || c == '\r') c = getchar(); 
    while(c != ' ' && c != '\n' && c != '\r' && c != EOF){
        s += c;
        c = getchar();
    }
}
inline void print(const string &s){
    for(char c : s) putchar(c);
}
const int N = 1e3 + 11,M = 25;
int n,m,q,cnt;
string g[N * N];
int a[N][N];
map<PII,PII> pos;
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("file.out","w",stdout);
    read(n);read(m);read(q);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            read(g[++ cnt]);
            a[i][j] = cnt;
        }
    }
    while(q --){
        int x1,y1,x2,y2,l,c;
        read(x1);read(y1);read(x2);read(y2);read(l);read(c);
        for(int i=x1,i2=x2;i<x1+l;i++,i2++){
            for(int j=y1,j2=y2;j<y1+c;j++,j2++){
                swap(a[i][j],a[i2][j2]);
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            print(g[a[i][j]]);
            putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}

S2.正解

还是Cdo大佬权威,这个做法还是太NB太快了

我们分析矩形交换的实际含义

我们发现两个矩形的交换就是断开与原来上下左右四个方向的边框并重新连上新位置的四个边框,因为我们并不能实际的交换位置,所以只能寻找和四周的关系,于是我们就可以使用链表(最好是手写的)来实现这个题目,对于每个点,我们并不需要上下左右四个方向的连接,只需要与右方和下方衔接即可

我们枚举矩形的四个边,将边上的点交换所指向的位置即可,注意边界的处理!

实现:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
inline void read(string &s){
    s = "";
    char c = getchar();
    while(c == ' ' || c == '\n' || c == '\r') c = getchar(); 
    while(c != ' ' && c != '\n' && c != '\r' && c != EOF){
        s += c;
        c = getchar();
    }
}
inline void print(const string &s){
    for(char c : s) putchar(c);
}
const int N = 1e3 + 11,M = 2e6 + 11;
int n,m,q,cnt;
string g[N][N];
struct node{
    int x,y,r,d;
}a[M];
int find_pos(int x,int y){
    int pos = 1;
    for(int i=1;i<x;i++) pos = a[pos].d;
    for(int i=1;i<y;i++) pos = a[pos].r;
    return pos;
}
int main(){
//    freopen("ex_0.in","r",stdin);
//    freopen("file.out","w",stdout);
    read(n);read(m);read(q);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) read(g[i][j]); 
    }
    for(int i=0;i<=n+1;i++){
        for(int j=0;j<=m+1;j++){
            a[++ cnt].x = i;
            a[cnt].y = j;
            a[cnt].d = cnt + m + 2;
            a[cnt].r = cnt + 1;
        }
    }
    while(q --){
        int xl,yl,xr,yr,l,c;
        read(xl);read(yl);read(xr);read(yr);read(l);read(c);
        int rg1 = find_pos(xl,yl),rg2 = find_pos(xr,yr),tp1 = rg1,tp2 = rg2;
        for(int i=1;i<=l;i++){
            tp1 = a[tp1].d;
            tp2 = a[tp2].d;
            int x = a[tp1].r,y = a[tp2].r;
            a[tp1].r = y;
            a[tp2].r = x;
        }
        for(int i=1;i<=c;i++){
            tp1 = a[tp1].r;
            tp2 = a[tp2].r;
            int x = a[tp1].d,y = a[tp2].d;
            a[tp1].d = y;
            a[tp2].d = x;
        }
        tp1 = rg1,tp2 = rg2;
        for(int i=1;i<=c;i++){
            tp1 = a[tp1].r;
            tp2 = a[tp2].r;
            int x = a[tp1].d,y = a[tp2].d;
            a[tp1].d = y;
            a[tp2].d = x;
        }
        for(int i=1;i<=l;i++){
            tp1 = a[tp1].d;
            tp2 = a[tp2].d;
            int x = a[tp1].r,y = a[tp2].r;
            a[tp1].r = y;
            a[tp2].r = x;
        }
    }
    int st = a[a[1].d].r;
    for(int i=1;i<=n;i++){
        int p = st;
        for(int j=1;j<=m;j++){
            print(g[a[p].x][a[p].y]);
            p = a[p].r;
            putchar(' ');
        }
        st = a[st].d;
        putchar('\n');
    }
    return 0;
}

D. 排列(permutation)

题面
有一个长度为 n 的排列丢失了,已知 m 个三元组 (a_i, b_i, c_i) 表示 b_i 在 a_i 和 c_i 之间。要求构造一个排列,满足至少 \lceil \frac{m}{2} \rceil 个三元组。

输入格式
第一行:n, m
接下来 m 行:每行 a_i, b_i, c_i

输出格式
一行 n 个整数,表示构造的排列

数据范围

**数据范围** **注意:本题采用捆绑测试** | 子任务 | 条件 | 分值 | | ------ | ----------- | ----- | | 1 | $n \leq 8$ | 10 分 | | 2 | $n \leq 12$ | 15 分 | | 3 | $m \leq 12$ | 15 分 | | 4 | 无特殊限制 | 60 分 | 对于所有数据:$n, m \leq 10^5

样例输入

5 4
1 2 3
2 3 4
3 4 5
4 5 1

样例输出

1 4 3 2 5

Solution

我们思考这道题时,发现此题有SPJ,但问题不大

首先可以枚举所有可能的排列情况,再逐一检查是否满足条件,满足就输出,时间复杂度O(n!\times n),可以拿到15pts

我们就不多bb了,这题部分分还很多,我们直奔主题

反正我做这道题时考场上是想到了和差分约束的相关东西,因为如果这道题明显规定了a_i,b_i,c_i的先后顺序,我们就可以直接确定建图,跑拓扑排序了

但,没这么简单,对于此题,a_i和c_i没有先后顺序,没有谁位高权重,所以没法直接建图,但明显对于这么逆天的数据,又不可能是二分

对于我们刚刚想到的词-----拓扑排序

OK!恭喜你找到此题正解

我们来看如何进行建图

这样进行建图,我们保证在1被扔进队列时,2也入队,直接将3的入度减2即可

这样保证1和2先后顺序相同

我们进行答案统计时要将三元组的最后一个元素归位时才记录答案,不然会重复,出现矛盾

在统计答案时,我们要把答案从两边往中间记录,这样可以保证先确定两边位置,再确定中间位置,在拓扑到每个点时,去枚举每个包含这个点的三元组,去计算放在当前的最左边还是最右边符合的方案数更多,选择方案数较多的一边记录即可,这样也符合拓扑序,我们就可以边拓扑便记录答案,就更方便.因为每次符合的三元组数量都比不符合的多,就一定可以满足大于等于\frac{m}{2}的的限制条件

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef __int128 SLL;//super_long_long
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int>> PIP;
typedef pair<LL,LL> PLL;
template <typename T> inline void read(T &x){
    x = 0;
    LL f = 1;
    char c = getchar();
    for(; !isdigit(c); c=getchar()) if(c == '-') f = -1;
    for(; isdigit(c); c=getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    x *= f;
}
template <typename T> inline void print(T x){
    if(x < 0) x = -x,putchar('-');
    if(x > 9) print(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e5 + 11,M = 25;
int n,m,l,r;
struct node{
    int x,y,z;
}a[N];
int iD[N],ans[N],pos[N];
bool st[N];
vector<node> ed[N],md[N];
void add(int u){
    int cntl = 0,cntr = 0;
    for(auto x : ed[u]){
        if(pos[x.x] == 0 && pos[x.y] > 0){
            if(pos[x.y] < l) cntr ++;
            else cntl ++;
        }
    }
    for(auto x : md[u]){
        if(pos[x.x] == 0 && pos[x.y] > 0){
            if(pos[x.y] < l) cntl ++;
            else cntr ++;
        }
        if(pos[x.y] == 0 && pos[x.x] > 0){
            if(pos[x.x] < l) cntl ++;
            else cntr ++;
        }
    }
    if(cntl >= cntr){
        pos[u] = l;
        ans[l] = u;
        l ++;
    }else{
        pos[u] = r;
        ans[r] = u;
        r --;
    }
    return;
}
void toposort(){
    queue<int> q;
    for(int i=1;i<=n;i++){
        if(iD[i] == 0) q.push(i);
    }
    while(q.size()){
        int t = q.front();
        q.pop();
        add(t);
        for(auto x : ed[t]){
            if(st[x.z] == 1) continue;
            st[x.z] = 1;
            iD[x.x] -= 2;
            if(iD[x.x] == 0) q.push(x.x);
        }
    }
}
int main(){
    // freopen("permutation.in","r",stdin);
    // freopen("permutation.out","w",stdout);
    read(n);read(m);
    l = 1,r = n;
    for(int i=1;i<=m;i++){
        read(a[i].x);read(a[i].y);read(a[i].z);
        iD[a[i].y] += 2;
        ed[a[i].x].emplace_back((node){a[i].y,a[i].z,i});
        ed[a[i].z].emplace_back((node){a[i].y,a[i].x,i});
        md[a[i].y].emplace_back((node){a[i].x,a[i].z,i});
    }
    toposort();
    for(int i=1;i<=n;i++){
        print(ans[i]);
        putchar(' ');
    }
    return 0;
}