NOIP2023 T1-T4题目解析
bsjsaikou10 · · 个人记录
NOIP2023题解
T1 : 词典
解析 :
没什么好说的,绝世水题,甚至直接排序跑带个老哥都能过
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e3 + 4;
int n,m;
int ans[MAXN],a[MAXN][MAXN],a2[MAXN][MAXN],a3[MAXN][MAXN];
string s[MAXN],s2[MAXN],s3[MAXN];
int main(){
// freopen("dict.in","r",stdin);
// freopen("dict.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> s[i];
ans[i] = 1;
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < m; j++){
a[i][j] = (int)s[i][j];
a2[i][j] = a[i][j];
a3[i][j] = a[i][j];
}
}
for(int i = 1; i <= n; i++){
sort(a2[i],a2[i] + m,greater<int>());
sort(a3[i],a3[i] + m);
}
for(int i = 1; i <= n; i++){
for(int j = 0; j < m; j++){
s2[i] += (char)a2[i][j];
s3[i] += (char)a3[i][j];
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(j == i){
continue;
}
if(s3[i] >= s2[j]){
ans[i] = 0;
}
}
}
for(int i = 1; i <= n; i++){
cout << ans[i];
}
return 0;
}
/*
4 7
abandon
bananaa
baannaa
notnotn
*/
T2 : 三值逻辑
解析 :
1.看到这种赋值关系,考虑建立图论关系,也就是说我们需要建立一些虚点(包括TFU这种常量点),然后相反关系的点连一条边权为-1的边,相等关系的边边权为1,然后找矛盾即可,这样我们就确立了这道题的大方向,但是如果以为这么简单就完了,因为我们发现每一个点会被赋值多次,也就是每赋值一次后面连边的贡献都是不同的,所以我们需要对每一次赋值都重新建立一个赋值的点就可以了,然后考虑找矛盾,发现我们建出来的这一坨东西是一个基环树森林,注意到如果某一棵基环树上只要有一个是U,那么整颗树都是U,然后注意到如果一个环里面边权之积为-1,那肯定是U,然后跑基环树找环即可.
2.我们还是考虑建立图论关系,与上文做法不同的是我们这次建立的是一坨并查集森林,也就是说我们开一个两倍的并查集,其中n个存相同关系,另外n个存相反关系,这里有一个巧妙方法(在第一篇题解看到的这种赋值方式orz),也就是注意到0 = -0,所以我们可以直接令U = 0,然后令T = 1e5 + 1,F = -(1e5 + 1),这样之后我们可以把变量之间的关系具像化出来,这样之后还是建立T,F,U这些常量点,然后对于给定的操作,直接进行连边(形如fa[x] = y),代指的是x被y赋值,若建立相反关系的边则为fa[x] = -y即可,这样建立完之后我们每个点的操作路线就清楚了,然后考虑什么时候一个点才是U,这个比较显然,对于一个变量x来说,当且仅当它的根节点是U,或者它连向了-x,那么我们就只需要对每个点进行判断,具体的,只需要对每个点做一次类似并查集的跳父亲即可.
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1e5 + 5;
const int T = 1e5 + 1;
const int F = -(1e5 + 1);
const int U = 0;
int n,m;
int fa[MAXN * 3];
int vis[MAXN * 3];
inline int dfs(int u){
int res;
if(u == T || u == F || u == U){ //若跳到常量了那直接就有结果了
res = u;
}
else if(vis[n - u]){ //若u连向了-u
res = U;
}
else if(vis[n + u]){ //这个给res随便赋一个T或者F即可,若不判这个会挂掉
res = T;
}
else{
if(u < 0){ //因为节点编号可能小于零,需要特判
if(u == -fa[-u]){
res = u;
}
else{
vis[n + u] = 1;
res = dfs(-fa[-u]);
vis[n + u] = 0;
}
}
else{
if(u == fa[u]){
res = u;
}
else{
vis[n + u] = 1;
res = fa[u] = dfs(fa[u]);
vis[n + u] = 0;
}
}
}
return res;
}
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
vis[i] = 0;
fa[i] = i;
}
for(int i = 1; i <= m; i++){
char opt;
cin >> opt;
if(opt == 'T'){
int x;
cin >> x;
fa[x] = T;
}
if(opt == 'F'){
int x;
cin >> x;
fa[x] = F;
}
if(opt == 'U'){
int x;
cin >> x;
fa[x] = U;
}
if(opt == '+'){
int u,v;
cin >> u >> v;
fa[u] = fa[v];
}
if(opt == '-'){
int u,v;
cin >> u >> v;
fa[u] = -fa[v];
}
}
int res = 0;
for(int i = 1; i <= n; i++){
if(dfs(i) == U){ //对每个节点进行判断
res++;
}
}
cout << res << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int c,T;
cin >> c >> T;
while(T--){
solve();
}
}
T3 : 双序列拓展
解析:
一眼邪恶的性质题,这种题一定要按照 "暴力 -> 特殊性质 -> 正解" 的思路来思考,因为根据出题者的引导来做题时相对容易的.
首先最先来看暴力怎么做,考虑我们去一个个填这两个拓展序列,我们先假定X[1] < Y[1],因为反过来本质是一样的,那么我们可以考虑一个O(nm)的暴力dp,发现它拓展的过程本质上是选X序列的一个i,填若干个进入拓展序列,选Y序列的一个j,填若干个进入拓展序列,然后i与j本质上是两个指针,这样我们就有一个状态
我们现在学会了暴力,那么来考虑特殊性质 : 最大最小值唯一且一定在最后一个,我们再去观察这个转移方程,发现它很像一个方格走路的问题,也就是我们把问题转换为,我们现在有一个矩阵A,
好了,我们现在学会了这个特殊性质之后,其实正解也就浮出水面了,我们发现若最大最小值不在最后,那么那一行全1与那一列全1一定把这个矩形分割成4部分,也就是相当于两个特殊性质拼起来,所以我们只需要跑两次递归就可以了
#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXN = 5e5 + 5;
int c,n,m,q;
int x[MAXN],y[MAXN];
typedef struct Node{
int posx,posn,vmax,vmin;
}Node;
Node prex[MAXN],prey[MAXN];
Node sufx[MAXN],sufy[MAXN];
bool dfs(int l,int r){
if(l == 1 || r == 1){
return 1;
}
if(prex[l - 1].vmin < prey[r - 1].vmin){
return dfs(prex[l - 1].posn,r);
}
if(prey[r - 1].vmax > prex[l - 1].vmax){
return dfs(l,prey[r - 1].posx);
}
return 0;
}
bool dfs2(int l,int r,int lim1,int lim2){//注意第二次递归要用到后缀的最值,因为我们的边界变成左边界跟上边界了
if(l == lim1 || r == lim2){
return 1;
}
if(sufx[l + 1].vmin < sufy[r + 1].vmin){
return dfs2(sufx[l + 1].posn,r,lim1,lim2);
}
if(sufy[r + 1].vmax > sufx[l + 1].vmax){
return dfs2(l,sufy[r + 1].posx,lim1,lim2);
}
return 0;
}
bool check(int qx[],int qy[],int n,int m){
if(qx[1] >= qy[1]){
return 0;
}
for(int i = 0; i < MAXN; i++){
prex[i].posn = prex[i].posx = 0;
prex[i].vmax = -1e9;
prex[i].vmin = 1e9;
sufx[i].posn = sufx[i].posx = 0;
sufx[i].vmax = -1e9;
sufx[i].vmin = 1e9;
}
for(int i = 0; i < MAXN; i++){
prey[i].posn = prey[i].posx = 0;
prey[i].vmax = -1e9;
prey[i].vmin = 1e9;
sufy[i].posn = sufy[i].posx = 0;
sufy[i].vmax = -1e9;
sufy[i].vmin = 1e9;
}
for(int i = 1; i <= n; i++){
prex[i] = prex[i - 1];
if(prex[i].vmax < qx[i]){
prex[i].posx = i;
prex[i].vmax = qx[i];
}
if(prex[i].vmin > qx[i]){
prex[i].posn = i;
prex[i].vmin = qx[i];
}
}
for(int i = n; i >= 1; i--){
sufx[i] = sufx[i + 1];
if(sufx[i].vmax < qx[i]){
sufx[i].posx = i;
sufx[i].vmax = qx[i];
}
if(sufx[i].vmin > qx[i]){
sufx[i].posn = i;
sufx[i].vmin = qx[i];
}
}
for(int i = 1; i <= m; i++){
prey[i] = prey[i - 1];
if(prey[i].vmax < qy[i]){
prey[i].posx = i;
prey[i].vmax = qy[i];
}
if(prey[i].vmin > qy[i]){
prey[i].posn = i;
prey[i].vmin = qy[i];
}
}
for(int i = m; i >= 1; i--){
sufy[i] = sufy[i + 1];
if(sufy[i].vmax < qy[i]){
sufy[i].posx = i;
sufy[i].vmax = qy[i];
}
if(sufy[i].vmin > qy[i]){
sufy[i].posn = i;
sufy[i].vmin = qy[i];
}
}
int posi = prex[n].posn;
int posj = prey[m].posx;
if(prex[n].vmin >= prey[m].vmin || prex[n].vmax >= prey[m].vmax){
return 0;
}
bool fl = (dfs(posi,posj) & dfs2(posi,posj,n,m));
return fl;
}
int qx[MAXN],qy[MAXN];
void solve(){
int c,n,m,q;
cin >> c >> n >> m >> q;
for(int i = 1; i <= n; i++){
cin >> x[i];
}
for(int i = 1; i <= m; i++){
cin >> y[i];
}
cout << (check(x,y,n,m) | check(y,x,m,n));
while(q--){
int kx,ky;
cin >> kx >> ky;
for(int i = 1; i <= n; i++){
qx[i] = x[i];
}
for(int i = 1; i <= m; i++){
qy[i] = y[i];
}
for(int i = 1; i <= kx; i++){
int px,vx;
cin >> px >> vx;
qx[px] = vx;
}
for(int i = 1; i <= ky; i++){
int py,vy;
cin >> py >> vy;
qy[py] = vy;
}
cout << (check(qx,qy,n,m) | check(qy,qx,m,n));
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1,c;
//cin >> c >> T;
while(T--){
solve();
}
}
T4 : 天天爱打卡
解析 :
警告,本题极其卡常,对于我这种人傻常数大的选手需要极其注意卡常
首先有一个很显然的dp,
然后我们分析一下这个方程,我们发现我们只要打卡,一定不是闲的没事干去打卡,我们一定是去连续打卡一段区间来获得某些奖励,也就是说只有我们奖励的这些区间端点有用,所以我们考虑离散化,这样之后我们只需要枚举端点即可,但是我们发现,当我们枚举到一个端点,这一个端点的奖励就必须加上,但是我们发现每一次奖励都是对一段区间有影响的,所以我们需要区间加,而且我们转移还需要.....区间最大值.....,等等,区间加,区间最大值,这不就是妥妥的线段树吗,所以我们只需要套上线段树的板子就做完了,需要注意一下本题极其卡常,不加inline会挂到56分/fn
#include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
#include<algorithm>
#define int long long
using namespace std;
#define LOCAL
using namespace std;
namespace ly
{
namespace IO
{
#ifndef LOCAL
#define SIZE (1<<20)
char in[SIZE],out[SIZE],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,SIZE,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(p3=out,1,SIZE,stdout))
#define putchar(ch) (p3==out+SIZE&&flush(),*p3++=(ch))
class Flush{public:~Flush(){flush();}}_;
#endif
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool flag=1)
{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
flag?putchar('\n'):putchar(' ');
}
#ifndef LOCAL
#undef SIZE
#undef getchar
#undef putchar
#undef flush
#endif
}
}using namespace ly::IO;
const int MAXN = 4e5 + 5;
int n,m,k,d;
typedef struct Node{
int l,r;
long long v;
bool operator < (const Node& b){
return r < b.r;
}
}Node;
Node clg[MAXN];
typedef struct TR{
int l,r;
long long tag,v;
}TR;
TR tr[MAXN << 2];
inline int ls(int pos){
return pos << 1;
}
inline int rs(int pos){
return pos << 1 | 1;
}
inline void build(int pos,int l,int r){
tr[pos].l = l;
tr[pos].r = r;
tr[pos].v = 0;
tr[pos].tag = 0;
if(l == r){
return;
}
int mid = (l + r) >> 1;
build(ls(pos),l,mid);
build(rs(pos),mid + 1,r);
}
inline void push_up(int pos){
tr[pos].v = max(tr[ls(pos)].v,tr[rs(pos)].v);
}
inline void push_down(int pos){
tr[ls(pos)].v = tr[ls(pos)].v + tr[pos].tag;
tr[rs(pos)].v = tr[rs(pos)].v + tr[pos].tag;
tr[ls(pos)].tag = tr[ls(pos)].tag + tr[pos].tag;
tr[rs(pos)].tag = tr[rs(pos)].tag + tr[pos].tag;
tr[pos].tag = 0;
}
inline void update(int pos,int ql,int qr,int k){
int l = tr[pos].l;
int r = tr[pos].r;
if(l >= ql && r <= qr){
tr[pos].v = tr[pos].v + k;
tr[pos].tag = tr[pos].tag + k;
return;
}
int mid = (l + r) >> 1;
push_down(pos);
if(ql <= mid){
update(ls(pos),ql,qr,k);
}
if(qr > mid){
update(rs(pos),ql,qr,k);
}
push_up(pos);
}
inline long long query(int pos,int ql,int qr){
int l = tr[pos].l;
int r = tr[pos].r;
if(l >= ql && r <= qr){
return tr[pos].v;
}
int mid = (l + r) >> 1;
long long res = -1e18;
push_down(pos);
if(ql <= mid){
res = max(res,query(ls(pos),ql,qr));
}
if(qr > mid){
res = max(res,query(rs(pos),ql,qr));
}
push_up(pos);
return res;
}
int t[MAXN];
inline void solve(){
//int cnt = 0;
read(n),read(m),read(k),read(d);
for(int i = 1; i <= m; i++){
int x,y,z;
read(x),read(y),read(z);
clg[i] = {x - y,x,z};
t[(i << 1) - 1] = x - y;
t[i << 1] = x;
}
sort(t + 1,t + 1 + 2 * m);
int len = unique(t + 1,t + 1 + 2 * m) - t - 1;
for(int i = 1; i <= m; i++){
clg[i].l = lower_bound(t + 1,t + 1 + len,clg[i].l) - t;
clg[i].r = lower_bound(t + 1,t + 1 + len,clg[i].r) - t;
}
sort(clg + 1,clg + 1 + m);
build(1,0,len);
long long res = 0;
int p = 1,u = 0;
for(int i = 1; i <= len; i++){
update(1,i,i,res + d * t[i]);
while(p <= m && clg[p].r == i){
update(1,0,clg[p].l,clg[p].v);
p++;
}
for(; u < i && t[u] < t[i] - k; u++);
res = max(res,query(1,u,i - 1) - d * t[i]);
}
write(res);
}
signed main(){
int T = 1,c;
read(c),read(T);
while(T--){
solve();
}
}