Codeforces Round 943 (Div. 3)

53 篇文章 6 订阅
23 篇文章 0 订阅

欢迎关注更多精彩
关注我,学习常用算法与数据结构,一题多解,降维打击。

Codeforces Round 943 (Div. 3) 题解

题目列表:https://codeforces.com/contest/1968

A

https://codeforces.com/contest/1968/problem/A

题目大意

给定一个整数x, 找到y使得 gcd(x, y)+y最大(y<x)。gcd是最大公约数。

代码

直接枚举即可

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

int gcd(int a, int b) {
    if (a % b == 0)return b;
    return gcd(b, a % b);
}

void solve() {
    int t;
    cin >> t;
    while (t--) {
        int a;
        cin >> a;
        int ans = 0;
        int y = 0;
        for (int i = 1; i < a; ++i) {
            int res = gcd(i, a) + i;
            if (res >= ans) {
                ans = res; y = i;
            }
        }
        cout << y << endl;
    }
}


int main() {
    solve();
    return 0;
}
/*

 */

B

https://codeforces.com/contest/1968/problem/B

题目大意

给定01串a,b, 问可以由b的子序列组成a最长前缀是多少。

解析

贪心算法判断。
依次遍历a, 寻找b中第一个相等的字符。

代码

代码中使用了二分查找,多此一举了。



#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;
class KMP {
	vector<int> next;
	string pattern;

public:
	vector<int> makeNext(string pat);
	vector<int> find(string s);
};

// a b a c a b a
// 0 0 1 0 1 2 3

vector<int> KMP::makeNext(string pat){
	pattern = pat;
	next.assign(pat.length(), 0);

	for (int i=1, j=0; i < pattern.length(); i++) {
		for (;j > 0 && pattern[j] != pattern[i];) {
			j = next[j - 1];
		}

		if (pattern[j] == pattern[i]) {
			j++;
		}
		next[i] = j;
	}

	return next;
}

vector<int> KMP::find(string s){
	int j = 0;
	vector<int> res;
	for(int  i=0;i< s.length();++i) {
		char c = s.at(i);
		for (;j > 0 && c != pattern[j];) {
			j = next[j - 1];
		}
		if (c == pattern[j]) {
			j++;
		}
		if (j == pattern.length()) {
			res.push_back(i - pattern.length() + 1);
			j = next[j - 1];
		}
	}

	return res;
}


bool subsequence(string a, string b) {
	int i, j;
	for (i = 0, j = 0; i < a.length() && j < b.length(); ++j) if (a[i] == b[j])++i;
	return i == a.length();
}

void solve() {
    int t;
    cin >> t;
	KMP kmp;
    while (t--) {
		string a, b;
		int l, r;
		cin >> l >> r;
		cin >> a >> b;

		l = 0, r = a.length();

		while (l < r) {
			int mid = (l + r) / 2 + (l+r)%2;
			if (!subsequence(a.substr(0, mid), b)) r = mid - 1;
			else l = mid;
		}

		cout << l << endl;
    }
}


int main() {
    solve();
    return 0;
}
/*
6
5 4
10011
1110
3 3
100
110
1 3
1
111
4 4
1011
1111
3 5
100
11010
3 1
100
0

 */

C

https://codeforces.com/contest/1968/problem/C

题目大意

给定x数组,构造一个a数组
满足 x i = a i % a i − 1 x_i = a_i\%a_{i-1} xi=ai%ai1

解析

假设已知 ai-1, 则ai = xi+n*ai-1。
同时ai>x(i+1)。

代码



#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

void solve() {
    int t;
    cin >> t;
    while (t--) {
        int dv = 1, mv = 0;
        int n;
        cin >> n;
        int a;
        n--;
        while (n--) {
            cin >> a;
            int res = (((a - mv) / dv + 1) * dv + mv);
            cout << res << " ";
            dv = res;
            mv = a;
        }
        cout << a << " ";

        cout << endl;
    }
}


int main() {
    solve();
    return 0;
}
/*
5
4
2 4 1
3
1 1
6
4 2 5 1 2
2
500
3
1 5

3 5 4 9
2 5 11
5 14 16 5 11 24
501 500
2 7 5

 */

D

https://codeforces.com/contest/1968/problem/D

题目大意

给定a数组和p数组。
现在需要玩一个游戏,得分越高越好。

得分规则如下,
如果当前位置为x则得分加ax分。
然后可以选择停在原来位置或者移动到px。
游戏进行k轮。
现在给出 "Bodya"和"Sasha"的初始位置,用最佳策略谁可以赢。

解析

一个直观的感觉就是想找到一个比较大的值就停着不走了。
那么最多把所有位置走一遍。
反过来想,最后肯定会停在某个地方不动。

那么我们可以枚举最后停在了哪个地方,该地方肯定是使得我们获利最大,所以是想尽快达到那个地方。那么之前经过的地方只得一次分数,最后剩下的次数都是最后停的地方得分。

代码

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

typedef long long lld;


lld getScore(vector<int>& p, vector<lld>& a, int s, lld k) {
    //cout << s << endl;
    vector<lld> scores;
    for (int i = s;;) {
        scores.push_back(a[i]);
        if (scores.size() > 1)scores[scores.size() - 1] += scores[scores.size() - 2];
        //cout << a[i] << " | " <<scores.back()<<", ";
        i = p[i];
        if (i == s)break;
    }

    //cout << "\n----" << endl;
    lld sum = scores[0]*k;
    for (int i = 1; i < scores.size() && i<k; ++i) {
        lld t = scores[i - 1] + (k - i) * (scores[i] - scores[i - 1]);
        //cout << t << " ";
        sum = max(sum, t);
    }
    //cout << endl;
    return sum;
}

void solve() {
    int t;
    cin >> t;
    while (t--) {
        int n, k, pb, ps;
        cin >> n >> k >> pb >> ps;
        pb--, ps--;
        vector<int> p(n, 0);
        vector<lld> a(n, 0);

        for (int i = 0; i < n; ++i) {
            cin >> p[i];
            p[i]--;
        }
        for (int i = 0; i < n; ++i)cin >> a[i];

        lld scoreb = getScore(p, a, pb, k);
        lld scores = getScore(p, a, ps, k);

        /*
        "Bodya" if Bodya wins the game.
        "Sasha" if Sasha wins the game.
        "Draw" if the players have the same score.
        */
        //cout << scoreb << ", " << scores << endl;
        if (scoreb > scores) cout << "Bodya";
        else if (scoreb < scores) cout << "Sasha";
        else cout << "Draw";
        cout << endl;
    }
}


int main() {
    solve();
    return 0;
}
/*
10
4 2 3 2
4 1 2 3
7 2 5 6
10 8 2 10
3 1 4 5 2 7 8 10 6 9
5 10 5 1 3 7 10 15 4 3
2 1000000000 1 2
1 2
4 4
8 10 4 1
5 1 4 3 2 8 6 7
1 1 2 1 2 100 101 102
5 1 2 5
1 2 4 5 3
4 6 9 4 2
4 2 3 1
4 1 3 2
6 8 5 3
6 9 5 4
6 1 3 5 2 4
6 9 8 9 5 10
4 8 4 2
2 3 4 1
5 2 8 7
4 2 3 1
4 1 3 2
6 8 5 3
2 1000000000 1 2
1 2
1000000000 2

 */

E

https://codeforces.com/contest/1968/problem/E

题目大意

有一个n*n的棋盘,现在需要在上面放上n颗棋子。
H为所有棋子两两之间的曼哈顿距离集合,使集合最大。

解析

对一个nn的棋盘,最大曼哈顿距离是确定的,即2(n-1)。
那所有距离就是0,1,2… 2*(n-1)。

假设n*n棋盘已经做到了所有距离 , 则长度n+1的棋盘只要在之前基础上,(n+1, n+1)放一个棋子就可以了。

n<3时特殊处理。

代码



#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

typedef long long lld;

void solve() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i) {
            if (i < 2) cout << 1 << " " << (i + 1) << endl;
            else cout << (i + 1) << " " << (i + 1) << endl;
        }

        if (t)cout << endl;
    }
}


int main() {
    solve();
    return 0;
}
/*


 */

F

https://codeforces.com/contest/1968/problem/F

题目大意

对于1个数组,如果该可以分成k段(k>1),且每段的异或和都相等,则称为有趣数组。

现在给定一个数组a,问区间a[l…r]是否有趣。

解析

对于1个数组(长度大于1)来说,如果异或和为0,a1^ a2 ^ a3…^an=0那么显然可以分成2段
a2 ^ a3…^an=a1, 就是一个有趣数组。

如果a1^ a2 ^ a3…^an=x !=0, 则如果是一个有趣串,就要使得每一段的异或和为x, 更进一步只要分成三段即可。因为如果大于3段,可以把其中偶数段异或成0。

那么对于3段来说,只要查找到前后两段异或和为k的段就可以判断为有趣串。

在这里插入图片描述

所以问题就转化为一个查找问题。

先把所有异或前缀(后缀)和对应位置存储起来,用二分查找就可以快速找到。

代码

 
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

typedef long long lld;
const int N = 1e6+10;
int arr[N];
int arrright[N];

void solve() {
    int t;
    cin >> t;
    while (t--) {
        int q, n;
        cin >> n >> q;
        map<int, vector<int> > left, right;
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> arr[i];
            sum ^= arr[i];
            left[sum].push_back(i);
        }

        for (int i = 0; i < n; ++i) {
            right[sum].push_back(i);
            arrright[i] = sum;
            sum ^= arr[i];
            if (i)arr[i] ^= arr[i - 1];
        }

        while (q--) {
            int l, r;
            cin >> l >> r;
            l--, r--;
            sum = arr[r];
            if (l)sum ^= arr[l - 1];
            if (sum == 0 && r > l) {
                cout << "YES" << endl;
                continue;
            }
            if (left[arr[r]].empty() || right[arrright[l]].empty()) {
                cout << "NO" << endl;
                continue;
            }
            //查找最左边
            auto itleft = lower_bound(left[arr[r]].begin(), left[arr[r]].end(), l);

            // 查找最右边
            auto itright = upper_bound(right[arrright[l]].begin(), right[arrright[l]].end(), r);
           /* cout << itright - right[arrright[l]].begin() << endl;
            cout << right[arrright[l]].size() << endl;
            cout << *(itright - 1) << endl;*/
            if (itleft != left[arr[r]].end() && itright!= right[arrright[l]].begin() && *itleft < *(itright-1)) {
                cout << "YES" << endl;
            }
            else cout << "NO" << endl;
        }

    }
}


int main() {
    solve();
    return 0;
}
/*
1 2 3 4 5 6 7 8 9 10 11
0 0 1 0 0 1 0 1 1 0  1
0 0 1 1 1 0 0 1 0 0 1
1 1 0 0 1 0 0 0 1 1 1


1
11 1
0 0 1 0 0 1 0 1 1 0  1
6 9




 */

G1

https://codeforces.com/contest/1968/problem/G1

题目大意

给定一个字符串s和数字k,问将字符串分成连续k段,公共前缀最长是多少。

题目分析

最长前缀数组
可以通过二分枚举长度l,然后查找s[0…l]在s中出现的次数,利用公共前缀数组实现每次判断为O(n), 整体复杂度为nlog(n)。

代码



#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <assert.h>

using namespace std;

typedef long long lld;


vector<int> Zfunc(string & str) {
	int n = str.size();
	vector<int>z(n);
	int l = 0, r = 0;
	for (int i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i + z[i] < n && str[z[i]] == str[i + z[i]]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}

int getCnt(vector<int> Z, int len) {
	int cnt = 1;
	for (int i = len; i < Z.size();) {
		if (Z[i] >= len) {
			cnt++;
			i += len;
		}
		else i++;
	}
	return cnt;
}

void solve() {
	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		string s;
		cin >> s;
		auto Z = Zfunc(s);
		/*for (int z : Z)cout << z << " ";
		cout << endl;*/
		int left = 0, right = s.length() / l;
		while (left < right) {
			int mid = (left + right) / 2 + (left + right) % 2;
			if (getCnt(Z, mid) >= r) {
				left = mid;
			}
			else {
				right = mid - 1;
			}
		}

		cout << left << endl;

	}
}


int main() {
	solve();
	return 0;
}
/*
7
3 3 3
aba
3 3 3
aaa
7 2 2
abacaba
9 4 4
abababcab
10 1 1
codeforces
9 3 3
abafababa
5 3 3
zpozp


2
10 1 1
aaaaaaaaaa
10 1 1
abcdaabcda


 */

G2

https://codeforces.com/contest/1968/problem/G2

题目大意

该题是上一题的加强版本。

给定一个字符串s和数字l,r,问将字符串分成连续k段(k=l,l+1,l+2,…,r),公共前缀最长是多少。

题目分析

先求出k=1到s.length()的所有答案,用数组ans表示。

可以分段考虑,对于k<=sqrt(n), 可以按照题目一的做法,总复杂度为sqrt(n) n log(n)。

对于k>sqrt(n), 那么分段后的长度不会大于sqrt(n), 可以枚举l 从1到sqrt(n) 计算出最多可以分成多少段k,则ans[k]=l。

保险起见,最后做一遍最大值比较,ans[i-1]=max(ans[i-1], ans[i])。

代码


#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long lld;


vector<int> Zfunc(string & str) {
	int n = str.size();
	vector<int>z(n);
	int l = 0, r = 0;
	for (int i = 1; i < n; i++) {
		if (i <= r) {
			z[i] = min(r - i + 1, z[i - l]);
		}
		while (i + z[i] < n && str[z[i]] == str[i + z[i]]) {
			z[i]++;
		}
		if (i + z[i] - 1 > r) {
			l = i;
			r = i + z[i] - 1;
		}
	}
	return z;
}

int getCnt(vector<int>& Z, int len) {
	int cnt = 1;
	for (int i = len; i < Z.size();) {
		if (Z[i] >= len) {
			cnt++;
			i += len;
		}
		else i++;
	}
	return cnt;
}

int getFix(vector<int >& Z, string &s, int k) {
	int left = 0, right = s.length() / k;
	while (left < right) {
		int mid = (left + right) / 2 + (left + right) % 2;
		if (getCnt(Z, mid) >= k) {
			left = mid;
		}
		else {
			right = mid - 1;
		}
	}

	return left;
}

void solve() {
	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		string s;
		cin >> s;
		auto Z = Zfunc(s);
		vector<int> ans(s.length() + 1, 0);
		int sl = sqrt(s.length()) + 0.5;
		for (int i = 1; i <= sl; ++i) {
			ans[i] = max(ans[i], getFix(Z, s, i));
			int k = getCnt(Z, i);
			ans[k] = max(ans[k], i);
		}

		for (int i = s.length()-1; i > 0; --i) {
			ans[i] = max(ans[i + 1], ans[i]);
		}


		for (; l <= r; ++l) {
			cout << ans[l] << " ";
		}
		cout << endl;
	}
}


int main() {
	solve();
	return 0;
}
/*
7
3 3 3
aba
3 3 3
aaa
7 2 2
abacaba
9 4 4
abababcab
10 1 1
codeforces
9 3 3
abafababa
5 3 3
zpozp


2
10 1 1
aaaaaaaaaa
10 1 1
abcdaabcda



7
3 1 3
aba
3 2 3
aaa
7 1 5
abacaba
9 1 6
abababcab
10 1 10
aaaaaaawac
9 1 9
abafababa
7 2 7
vvzvvvv

 */


本人码农,希望通过自己的分享,让大家更容易学懂计算机知识。创作不易,帮忙点击公众号的链接。

  • 30
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值