A Gregor and Cryptography
题目大意
给定一个质数n,求任意两个数a, b,使得n % a == n % b
主要思路
由于给定的数是质数,质数一定是奇数,那么n % 2一定为1,那么b为多少时n % b也等于1呢,答案是n - 1
AC代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while(t--) {
int p;
cin >> p;
cout << "2 " << p-1 << "\n";
}
return 0;
}
B Gregor and the Pawn Game
题目大意
给定一个字符串s, 一个字符串t,每个字符串都由0和1组成,1代表有棋子,0代表没棋子,t的棋子向前走到达s,当t中的棋子向前走到s无棋子或者左前方或右前方有s的棋子时才能向前走,求t中能到达s的棋子数目
主要思路
按顺序贪心即可,先判断棋子能不能向前走,在判断能不能向左走,再判断能不能向右走即可
AC代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef long long ll;
const int N = 200010;
int n, a[N], s[N];
bool st[N];
signed main(void)
{
int T;
scanf("%lld", &T);
while(T--)
{
memset(st, 0, sizeof(st));
cin >> n;
string t1, t2;
cin >> t1 >> t2;
int ans = 0;
for(int i = 0; i < n; i++)
{
if(t2[i] == '1')
{
if(t1[i] == '0' && !st[i]) ans++, t1[i] = '1', st[i] = true;
else if(i - 1 >= 0 && t1[i - 1] == '1' && !st[i - 1])
{
t1[i - 1] = '0';
ans++;
st[i - 1] = true;
}
else if(i + 1 < n && t1[i + 1] == '1' && !st[i + 1])
{
t1[i + 1] = '0';
ans++;
st[i + 1] = true;
}
//debug(ans)debug(t1)
}
}
cout << ans << endl;
}
return 0;
}
C Web of Lies
题目大意
给定n个点,权值分别为1~n,初始时给定n条边,每一条边上权值低的会"死",有3种操作,加一条边,减一条边,询问在当前边的条件下活的人数
主要思路
由于每一条边权值低的会死,所以我们用一个数组存下每条边死的次数,同时维护一个人数即可
AC代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef long long ll;
const int N = 200010;
int st[N];
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
T = 1;
while(T--)
{
int n, m;
cin >> n >> m;
int num = n;
for(int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
if(st[min(a, b)] == 0) num--;
st[min(a, b)]++;
}
int q;
cin >> q;
while(q--)
{
int t;
cin >> t;
if(t == 3) cout << num << endl;
if(t == 1)
{
int a, b;
cin >> a >> b;
if(st[min(a, b)] == 0) num--;
st[min(a, b)]++;
}
if(t == 2)
{
int a, b;
cin >> a >> b;
st[min(a, b)]--;
if(st[min(a, b)] == 0) num++;
}
}
}
return 0;
}
D Integers Have Friends
题目大意
给定一个长度为n的数组,求一个数组的最大长度,使得该数组%m的值相同,m >= 2
主要思路
由于取余某一个数相同,则会想到差分,我们从第二个数开始对后面每一个数做差分,求得的差分数组每一个数一定%m == 0,也就是这个差分数组的每一个数都是m的倍数,也就是这个差分数组的gcd == m。
我们先计算差分数组,然后用st表或线段树维护区间gcd,然后再二分判断最大长度即可。
AC代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define debug(a) cout << #a << " = " << a << endl;
#define x first
#define y second
typedef long long ll;
const int N = 200010, M = 18;
int n, a[N];
int b[N];
int w[N], f[N][M];
double lg[N];
void init()
{
for(int j = 0; j < M; j++)
{
for(int i = 1; i + (1 << j) - 1 < n; i++)
{
if(!j) f[i][j] = b[i];
else f[i][j] = __gcd(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
}
}
int query(int l, int r)
{
int len = r - l + 1;
int k = lg[len] / lg[2];
return __gcd(f[l][k], f[r - (1 << k) + 1][k]);
}
bool check(int mid)
{
for(int i = 1; i + mid - 1 < n; i++)
{
if(query(i, i + mid - 1) >= 2) return true;
}
return false;
}
signed main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
for(int i = 1; i < N; i++)
{
lg[i] = log(i);
}
int T;
cin >> T;
while(T--)
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
for(int i = 1; i < n; i++) b[i] = abs(a[i + 1] - a[i]);
init();
int l = 0, r = n;//注意l的取值
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l + 1 << "\n";
}
return 0;
}
本文精选了四道算法竞赛题目并提供了详细的解题思路及AC代码。包括质数取余特性、棋子移动策略、图论中节点存活计数及数组差分与区间GCD求解等核心内容。
1656

被折叠的 条评论
为什么被折叠?



