Hello 2024 题解

A. Wallet Exchange

cpp 复制代码
#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--){
		ll a,b;
		cin>>a>>b;
		puts((a+b)%2 ?  "Alice":"Bob");
	}
	
	
	return 0;
}

B. Plus-Minus Split

cpp 复制代码
#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (5010)
int n,s[MAXN];
char a[MAXN];
int f[MAXN]={};
int pr[MAXN<<2]={},t[MAXN<<2]={};
int main()
{
//	freopen("B.in","r",stdin);
//	freopen(".out","w",stdout);
	int C=5001;
	int T=read();
	For(kcase,T){
		s[0]=0;
		n=read();
		cin>>(a+1);
		For(i,n) s[i]=s[i-1]+(a[i]=='+'?1:-1);
		pr[C]=0,t[C]=kcase;
		For(i,n) {
			f[i]=f[i-1]+1;
			if(t[C+s[i]]==kcase) {
				f[i]=min(f[i],pr[C+s[i]]);
			}
			if(t[C+s[i]]!=kcase)
				pr[C+s[i]]=f[i];
			gmin(pr[C+s[i]],f[i])
			t[C+s[i]]=kcase;
		}
		cout<<f[n]<<endl;
	}
	
	
	return 0;
}

C. Grouping Increases

给一个数列,不改变相对顺序前提拆成2个数列。

问这2个数列中,相邻且前一个数小于后一个数的数对的最小值

贪心,维护2个数列队尾的值,如果都会增加数对或都不会,则放到队尾数小的那个。不然放不会增加数对的那个。

cpp 复制代码
#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (212345)
int n,m,a[MAXN],d[MAXN],f[MAXN],len=0;

int main()
{
//	freopen("C.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		scanf("%d",&n);
		For(i,n) scanf("%d",&a[i]);
		int t1=1e9,t2=1e9,ans=0;
		For(i,n) {
			if(t1<t2) swap(t1,t2);
			if(t2>=a[i]) t2=a[i];
			else if(t1>=a[i]) t1=a[i];
			else {
				++ans;
				t2=a[i];
			}
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

D. 01 Tree

给一个树,所有非叶子节点均有左右2个子节点,边权分别0和1(可以对调)。

已知所有叶子节点的按dfs序排列后的到根的最短路径长。

问是否合法?

dfs_order = []

function dfs(v):

if v is leaf:

append v to the back of dfs_order

else:

dfs(left child of v)

dfs(right child of v)

dfs(root)

考虑每次将一个节点改为左右2节点,等价于最后的序列中拿出 a a a 换成 { a , a + 1 } \{ a,a+1\} {a,a+1} 或 { a + 1 , a } \{ a+1,a\} {a+1,a}

因此每次把数列中相邻的最大值和次大值删除,模拟这个过程,看是否会变为只有1个0的数列

cpp 复制代码
#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (212345)
int a[MAXN];
set<pair<int,int> > S;
pair<int,int> b[MAXN];
int cmp(pi a,pi b){
	if(a.fi^b.fi) return a.fi<b.fi;return a.se>b.se;
}
bool ck(){
	S.clear();
	int n=read();
	For(i,n) a[i]=read();
//	int mx=max_element(a+1,a+1+n);
	For(i,n) S.insert(mp(i,a[i]));
	S.insert(mp(0,-1000));
	S.insert(mp(n+1,-1000));
	For(i,n) b[i]=mp(a[i],i);
	sort(b+1,b+1+n,cmp);
	ForD(i,n) {
		auto x=b[i];
//		cout<<x.fi<<' '<<x.se<<endl;
		int v=x.fi,id=x.se;
		auto it2=S.upper_bound(mp(id,1e9));
		auto it1=it2;it1--;
		auto it0=it1;it0--;
		int l=it0->se,r=it2->se;
//		cout<<l<<' '<<r<<endl;
		if(v==0) {
			if(SI(S)!=3) return 0;
		}
		if(l==v-1||r==v-1) {
			S.erase(it1);continue;
		}else if(r==v) {
			S.erase(it1);continue;
		}else if(v==0 && l==-1000 && r==-1000){
			S.erase(it1);continue;
		}else return 0;
	}
	return 1;
}
int main()
{
//	freopen("D.in","r",stdin);
//	freopen(".out","w",stdout);
	int T=read();
	while(T--) {
		puts(ck()?  "YES":"NO");
	}
	
	
	return 0;
}

F1. Wine Factory (Easy Version)

n n n个塔排成一列,第 i i i个塔有 a i a_i ai liters 水,且能把 b i b_i bi liters 水变成酒,前 n − 1 n-1 n−1个塔能向 编号 + 1 编号+1 编号+1的塔运送 c i c_i ci水。

问最多获得的酒量。

F1题中 c i = + ∞ c_i=+\infty ci=+∞

线段树, x x i , y y i , z z i xx_i,yy_i,zz_i xxi,yyi,zzi分别表示,节点 i i i做完剩多少酒,剩多少水,还有多少水变酒的额度没用。

cpp 复制代码
#include<bits/stdc++.h> 
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline ll read()
{
	ll x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
#define MAXN (512345+10)
ll xx[MAXN<<2],yy[MAXN<<2],zz[MAXN<<2];
ll a[MAXN],b[MAXN],c[MAXN];
void pushUp(int o) {
	ll delta=min(yy[Lson],zz[Rson]);
	xx[o]=xx[Lson]+xx[Rson]+delta;
	yy[o]=yy[Lson]+yy[Rson]-delta;
	zz[o]=zz[Lson]+zz[Rson]-delta;
}
void build(int l,int r,int o) {
	if (l==r) {
		xx[o]=min(a[l],b[l]);
		yy[o]=a[l]-min(a[l],b[l]);
		zz[o]=max(b[l]-a[l],0ll);
		return;
	}
	int m=(l+r)>>1;
	build(l,m,Lson);
	build(m+1,r,Rson);
	pushUp(o);
}
void update(int l,int r,int o,int L) {
	if (l==r) { //l==r==L==R
		xx[o]=min(a[l],b[l]);
		yy[o]=a[l]-min(a[l],b[l]);
		zz[o]=max(b[l]-a[l],0ll);
		return;
	}
	int m=(l+r)>>1;
	if (L<=m) update(l,m,Lson,L);
	else update(m+1,r,Rson,L);
	pushUp(o);
}
int main()
{
//	freopen("F.in","r",stdin);
//	freopen(".out","w",stdout);
	
	
	int n=read(),m=read();
	For(i,n) a[i]=read();
	For(i,n) b[i]=read();
	For(i,n-1) read();
	
	build(1,n,1);
	while(m--) {
		int p=read();
		a[p]=read(),b[p]=read();read();
		update(1,n,1,p);
		cout<<xx[1]<<endl;
	}	
	
	return 0;
}
相关推荐
乐悠小码4 分钟前
数据结构------队列(Java语言描述)
java·开发语言·数据结构·链表·队列
2的n次方_8 分钟前
二维费用背包问题
java·算法·动态规划
ROC_bird..23 分钟前
STL - vector的使用和模拟实现
开发语言·c++
机器视觉知识推荐、就业指导23 分钟前
C++中的栈(Stack)和堆(Heap)
c++
simple_ssn37 分钟前
【C语言刷力扣】1502.判断能否形成等差数列
c语言·算法·leetcode
寂静山林1 小时前
UVa 11855 Buzzwords
算法
Curry_Math1 小时前
LeetCode 热题100之技巧关卡
算法·leetcode
ahadee1 小时前
蓝桥杯每日真题 - 第10天
c语言·vscode·算法·蓝桥杯
军训猫猫头2 小时前
35.矩阵格式的一到一百数字 C语言
c语言·算法
Mr_Xuhhh3 小时前
递归搜索与回溯算法
c语言·开发语言·c++·算法·github