北华校赛

D.价值最大的字符串

在这里插入图片描述

样例输入

2
2
aa
aaa
2
b
c

样例输出

aaa
c

被这道题坑了,其实还是要细心吧。
将字符串反转后,如果两个字符串长度相等,就可以直接比较大小。
如果长度不等,直接取长度最大的即可。
最后还需要把字符串反转过来。

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

string a, b;

int main(void)
{
	int t, n;
	cin >> t;
	while (t--){
		cin >> n;
		for (int i = 0; i < n; i++){
			cin >> b;
			reverse(b.begin(), b.end());
			if (i == 0) a = b;
			if (a.size() == b.size())
				a = max(a, b);
			else if (a.size() < b. size()){
				a = b;
			}
		}
		reverse(a.begin(), a.end());
		cout << a << endl;
	}
	return 0;
}

G.狭路相逢头碰头

在这里插入图片描述

样例输入

3
10 1 5
2 1 2
6 2 4

样例输出

YES
NO
YES

这道题的题目给了一点暗示,只要能够使对方退到最边上的位置即可获胜。
如果两个人之间的距离为偶数,则先手的人必胜,反之后手必胜。

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;

int main(void)
{
	int t, n, a, b;
	cin >> t;
	while (t--){
		cin >> n >> a >> b;
		a = abs(a - b);
		if (a % 2 == 0)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

F.又见序列

在这里插入图片描述

扫描二维码关注公众号,回复: 8609413 查看本文章

样例输入

1
3
1 2 -3

样例输出

2
提示:序列变成-1 2 -3,满足题目条件。

杨总的代码,目前还没搞明白。

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

const int N = 100010;

int n, m;
long long arr[N] = {0};
long long brr[N] = {0};

int main()
{
    long long ans1, ans2;
    scanf("%d",&n);
    while(n --)
    {
        ans1 = 0, ans2 = 0;
        scanf("%d",&m);
        for(int i = 1; i <= m ; i ++){
            scanf("%lld",&arr[i]);
            brr[i] = arr[i];

        }

        for(int i = 1; i <= m; i ++){
            //printf("%lld %lld ",arr[i - 1], arr[i]);
            if(i % 2 == 1){
                if(arr[i - 1] + arr[i] >= 0){
                    ans2 += abs(arr[i - 1] + arr[i]) + 1;
                    arr[i] = -1;
                    //printf("1 ");
                }
                else{
                    arr[i] = arr[i - 1] + arr[i];
                    //printf("2 ");
                }
            }
            else{
                if(arr[i - 1] + arr[i] <= 0){
                    ans2 += abs(arr[i - 1] + arr[i]) + 1;
                    arr[i] = 1;
                    //printf("3 ");
                }
                else{
                    arr[i] = arr[i - 1] + arr[i];
                    //printf("4 ");
                }
            }
            //printf("%lld\n",arr[i]);
        }

        for(int i = 1; i <= m; i ++){
            if(i % 2 == 0){
                if(brr[i - 1] + brr[i] >= 0){
                    ans1 += abs(brr[i - 1] + brr[i]) + 1;
                    brr[i] = -1;
                }
                else{
                    brr[i] = brr[i - 1] + brr[i];
                }
            }
            else{
                if(brr[i - 1] + brr[i] <= 0){
                    ans1 += abs(brr[i - 1] + brr[i]) + 1;
                    brr[i] = 1;
                }
                else{
                    brr[i] = brr[i - 1] + brr[i];
                }
            }
        }
        /*for(int i = 0; i <= m; i ++){
            printf("%lld %lld\n",arr[i],brr[i]);
        }*/

        long long ans3 = min(ans1, ans2);
        printf("%lld\n",ans3);
    }
    return 0;
}

后来自己写的

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;

ll arr[N], a, sum;
ll ans1, ans2;

int main(void)
{
	//ios::sync_with_stdio(false);
	int t, n;
	cin >> t;
	while (t--){
		ans1 = ans2 = 0;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> arr[i];
		}
		
		sum = 0;
		for (int i = 1; i <= n; i++){
			//奇数位正,偶数位负 
			if (i % 2 == 1 && arr[i] + sum <= 0){
				ans1 += (1 - arr[i] - sum);
				sum = 1;//让前缀和等于1 
			}
			else if (i % 2 == 1){
				sum += arr[i];
			} 
			else if (i % 2 == 0 && arr[i] + sum >= 0){
				ans1 += (arr[i] + sum + 1);
				sum = -1;
			}
			else if (i % 2 == 0){
				sum += arr[i];
			} 
		}
		
		sum = 0;
		for (int i = 1; i <= n; i++){
			//奇数位负,偶数位正 
			if (i % 2 == 1 && arr[i] + sum >= 0){
				ans2 += (arr[i] + sum + 1);
				sum = -1;
			}
			else if (i % 2 == 1){
				sum += arr[i];
			}
			else if (i % 2 == 0 && arr[i] + sum <= 0){
				ans2 += 1 - arr[i] - sum;
				sum = 1;
			}
			else if (i % 2 == 0){
				sum += arr[i];
			}
		}
		cout << min(ans1, ans2) << endl;
	}
	return 0;
}

H.骑士游走

在这里插入图片描述

样例输入

1
2 2
1 -2
1 1

样例输出

7

世龙的代码

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<queue>
#include<stack>
#include<map>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef struct{
    int x,y;
}test;
int ch[4][2]={0,1,1,0,-1,0,0,-1};
int maze[405][405];
int vis[405][405];
long long val,n,m;
test arr[160005];
int num;

bool mycmp(test t1,test t2){
    if(maze[t1.x][t1.y]>maze[t2.x][t2.y]) return 1;
    else return 0;
}

void bfs(test s){
    queue<test> q;
    vis[s.x][s.y]=1;
    q.push(s);
    while(!q.empty()){
        test now,next;
        now=q.front();
        for(int i=0;i<4;i++){
            next.x=now.x+ch[i][0];
            next.y=now.y+ch[i][1];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]==0&&maze[next.x][next.y]<=0){
                arr[num]=next;
                vis[next.x][next.y]=1;
                num++;
            }
            else if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]==0&&maze[next.x][next.y]>0){
                val+=maze[next.x][next.y];
                vis[next.x][next.y]=1;
                q.push(next);
            }
        }
        q.pop();
    }
}

void bfs1(test s){
    queue<test> q;
    q.push(s);
    while(!q.empty()){
        test now,next;
        now=q.front();
        for(int i=0;i<4;i++){
            next.x=now.x+ch[i][0];
            next.y=now.y+ch[i][1];
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&vis[next.x][next.y]==0){
                if(maze[next.x][next.y]>0){
                    val+=maze[next.x][next.y];
                    vis[next.x][next.y]=1;
                    q.push(next);
                }
                else{
                    if(val>=(-1)*(maze[next.x][next.y])){
                        val+=(-2)*(maze[next.x][next.y]);
                        vis[next.x][next.y]=1;
                        q.push(next);
                    }
                }
            }
        }
        q.pop();
    }
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf("%d",&maze[i][j]);
            }
        }
        if(maze[0][0]<0){
            cout<<0<<endl;
            continue;
        }
        num=0;
        val=maze[0][0];
        test st;
        st.x=0;
        st.y=0;
        bfs(st);
        sort(arr,arr+num,mycmp);
        for(int i=0;i<num;i++){
            if(val>=(-1)*(maze[arr[i].x][arr[i].y])){
                val+=(-2)*(maze[arr[i].x][arr[i].y]);
                bfs1(arr[i]);
            }
        }
        if(vis[n-1][m-1]==1) cout<<val<<endl;
        else cout<<0<<endl;
	}
	return 0;
}

I.热爱生活

在这里插入图片描述

样例输入

3
6
1 1 2 1 2 1
6
1 2 1 2 1 1
6
1 2 3 1 2 3

样例输出

1 2
4 1
0 3
链接:https://blog.csdn.net/monochrome00/article/details/81782617?tdsourcetag=s_pctim_aiomsg

这道题用到了KMP的next数组,还是不太理解。

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1e6 + 5; 

int a[N], next[N];
int t, n;

void get_next()
{
	for (int i = 2, j = 0; i <= n; i++){
		while (j > 0 && a[i] != a[j + 1])
			j = next[j];
		if (a[i] == a[j + 1])
			j++;
		next[i] = j;
	}
}

int main(void)
{
	ios::sync_with_stdio(false);
	cin >> t;
	while (t--){
		cin >> n;
		for (int i = n; i >= 1; i--)
			cin >> a[i];
		get_next();
		int an1 = n, ak = n;
		for (int i = 1; i <= n; i++){//枚举 
			int k = i - next[i], n1 = n - i;
			if (k + n1 < ak + an1) 
				ak = k, an1 = n1;
	        else if (k + n1 == ak + an1 && k < ak) 
				an1 = n1, ak = k;
	    }
        cout << an1 << " " << ak << endl;
	}
	return 0;
}

J.数糖果

在这里插入图片描述

样例输入

1
5 2
1 2 2 3 1

样例输入

30

K.圣诞节的礼物

在这里插入图片描述
在这里插入图片描述

样例输入

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

样例输出

6

提示

保证不会有同样的关系多次出现的情况,并且不会出现自己送给自己礼物的情况,并且保证
不会出现无限循环送礼物的情况。

硕哥说是拓扑排序下的bfs,咱也不知道

发布了162 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43772166/article/details/103435879