第五场周赛题解

admin 发布于 2024-12-16 1354 字 452 次阅读


A.构造序列(签到)_

void solve()
{
    int n,m;
    cin>>n>>m;
    if(n==m||abs(n-m)==1) cout<<n+m<<endl;
    else cout<<2*min(n,m)+1<<endl;
}

B.小红的好数(签到)

#include <bits/stdc++.h>
using namespace std;
signed main()
{
    string s;
    cin>>s;
    if(s.size()==2&&s[0]==s[1]) cout<<"Yes"<<endl;    
    else cout<<"No"<<endl;
}

C.小A的糖果

贪心,尽量往后面拿,拿前面的对后面的没影响

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n, x;
    cin >> n >> x;
    vector<long long>a(n + 1);
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        if (a[i] + a[i + 1] > x) {
            ans += a[i] + a[i + 1] - x;
            if (a[i + 1] - (a[i] + a[i + 1] - x) >= 0) {
                a[i + 1] -= (a[i] + a[i + 1] - x);
            }
            else {
                a[i] += a[i + 1];
                a[i] -= (a[i] + a[i + 1] - x);
                a[i + 1] = 0;
            }
        }
    }
    cout << ans << endl;
}

D.数学问题

答案是唯一的,只要另两个数的和是偶数,我就能成立

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    int n;
    int a,b,c;
    cin>>n;
    while(n--){
        cin>>a>>b>>c;
        if((b+c)%2==0) cout<<"1 ";
        else cout<<"0 ";
        if((a+c)%2==0) cout<<"1 ";
        else cout<<"0 ";
        if((b+a)%2==0) cout<<"1 ";
        else cout<<"0 ";
        cout<<endl;
    }
    return 0;
}

E.小陆学长玩原神

不能只逮着数组中最初的最大值一直砍,他血量变少就不是最大的了,所以对k取模,都到一刀能砍死的地步,在从大到小排序,输出即可

#include<bits/stdc++.h>
using namespace std;

bool cmp(array<int,2> a,array<int,2> b){
    if(a[0]!=b[0])return a[0]>b[0];
    return a[1]<b[1];
}

void solve(){
    int n,k;
    cin>>n>>k;
    vector<array<int,2>>a(n+1);
    for(int i=1;i<=n;i++)cin>>a[i][0],a[i][0]=(a[i][0]-1)%k+1,a[i][1]=i;
    sort(a.begin()+1,a.end(),cmp);
    for(int i=1;i<=n;i++){
        cout<<a[i][1]<<" ";
    }
    cout<<endl;
}

signed main(){
    ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)solve();
    return 0;
}

F.小红的矩阵行走

#include <bits/stdc++.h>
using namespace std;
void solve()
{
    int n,m;
    cin>>n>>m;
    vector<vector<int>>a(n+1,vector<int>(m+1)),vis(n+1,vector<int>(m+1));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    vis[1][1]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]==a[1][1]&&(vis[i-1][j]||vis[i][j-1]))//只要我上面或者左边的能走到并且我    的值等于a[1][1]即可走到
            {
                vis[i][j]=1;
             }
    if(vis[n][m]) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}
signed main(){
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	int t;
	t = 1;
	cin >> t;
	while (t--)
		solve();
}

G.简单博弈游戏

题解讲的很详细

https://www.luogu.com.cn/problem/solution/CF1860C

#include <bits/stdc++.h>
using namespace std;
void solve()
{
	int n;
	cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n;i++) cin >> a[i];
	int minn = 1e18;
	int ans = 1e18;
	int sum=0;
	for (int i = 1; i <= n;i++)
	{
		if(a[i]<minn)
			minn = a[i];
		else 
		{
			if(a[i]<ans)
				ans = a[i], sum++;
		}
	}
	cout << sum << endl;
}
signed main(){
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	int t;
	t = 1;
// 	cin >> t;
	while (t--)
		solve();
}

H.动态前缀和问题

线段树模板,数据水了,不然都过不了,自己去学,链接在下面

https://www.luogu.com.cn/problem/P3372

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N=1e5+5;
int a[N],b[N],c[N];
struct node
{
	int l,r;
	int date,add;
}t[N*4+2];
void build(int f,int l,int r)
{
	t[f].l = l, t[f].r =r;
	if(l==r) { t[f].date=a[l] ;return; }
	int mid=l+r>>1;
	build(f<<1,l,mid);
	build(f<<1|1,mid+1,r);
	t[f].date=t[f<<1].date+t[f<<1|1].date;
}
void lazy(int f)
{
	if(t[f].add)
	{
		t[f<<1].date+=t[f].add*(t[f<<1].r-t[f<<1].l+1);
		t[f<<1|1].date+=t[f].add*(t[f<<1|1].r-t[f<<1|1].l+1);
		t[f<<1].add+=t[f].add;
		t[f<<1|1].add+=t[f].add;
		t[f].add=0;
	}
}
int ask(int f,int l,int r)
{
	if(l<=t[f].l&&r>=t[f].r) return t[f].date;
	lazy(f);
	int mid=t[f].l+t[f].r>>1;
	int sum=0;
	if(l<=mid) sum=sum+ask(f<<1,l,r);
	if(r>mid) sum=sum+ask(f<<1|1,l,r);
	return sum;
}
void change(int f,int l,int r,int v)
{
	if(l<=t[f].l&&r>=t[f].r) 
	{
		t[f].date+=v*(t[f].r-t[f].l+1);
		t[f].add+=v;
		return;
	}
	lazy(f);
	int mid=t[f].l+t[f].r>>1;
	if(l<=mid) change(f<<1,l,r,v);
	if(r>mid) change(f<<1|1,l,r,v);
	t[f].date=t[f<<1].date+t[f<<1|1].date;
}
void solve()
{	
    int n,m;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,n);
    cin>>m;
    while(m--)
    {
		int ff;
		cin>>ff;
		if(ff==1)
		{
			int x,z;
			cin>>x>>z;
			change(1,x,x,z);
		}
		else{
			int x,y;
			cin>>x>>y;
			int ss=ask(1,x,y);
			cout<<ss<<endl;
		}
	}
}
signed main()
{
    int tt;
    tt=1;
    //cin>>tt;
    while(tt--)
    solve();
}

最后更新于 2024-12-16