Codeforces 1549A 题目链接:点击这里传送
题意:
给出一个质数,要求你构造出两个数使这个质数与这两个数的模相同
思路:
质数必定是奇数,那么一个为2一个为n-1,模必然为1
#include<bits/stdc++.h>
using namespace std;
int t,n;
map <int,int> mp;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
cout<<2<<" "<<n-1<<endl;
}
return 0;
}
Codeforces 1549B 题目链接:点击这里传送
题意:
一共有n个棋子,他们要尽可能多的走到终点。如果他们前方没有敌方棋子,他们只能往前走;如果有,他们只能往左前方或右前方走。问最后能走到终点的棋子有几个。
思路:
按照他给的意思模拟就行了,没有任何思考量。
#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
string s1,s2;
int t,n;
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
cin>>s1>>s2;
int ans=0;
for(int i=0;i<s1.length();i++)
{
if(s1[i]=='1')
{
if(i-1>=0&&s2[i-1]=='1')
{
s2[i-1]='0';
ans++;
// cout<<s1<<" "<<s2<<endl;
continue;
}
if(i+1<n&&s2[i+1]=='1')
{
s2[i+1]='0';
ans++;
// cout<<s1<<" "<<s2<<endl;
continue;
}
}
else
{
if(s2[i]=='1')
{
s2[i]='0';
ans++;
// cout<<s1<<" "<<s2<<endl;
continue;
}
}
//cout<<s1<<" "<<s2<<endl;
}
cout<<ans<<endl;
}
return 0;
}
Codeforces 1549C 题目链接:点击这里传送
题意:
你要维护一个朋友圈。一个朋友圈中能力值最弱的人会自杀,而且自杀会持续下去。你可以给某个人加朋友,也可以删除某段友谊,对于每次查询,你需要输出场上还活着的人数。一个人的能力值就是他的编号。
思路:
首先在每次查询过后人是会复活的。之前每次的加边删边操作对后面是有影响的。注意了,题目没说清楚。
结论一: 在一轮查询过后,场上不会有任何朋友关系(每个人的编号都是独特的)
结论二: 在一轮查询过后,只有各自朋友圈中最强的那个人才能活下去。
于是我们就维护一个统计数组,统计的是在第i个人的朋友圈中比其高的有几个,只有cnt[i]为0时才能最后存活下来。
#include<bits/stdc++.h>
using namespace std;
int t, m, n,k;
#define MAXN 200005
int cnt[MAXN];
int ans;//死的人
void solve(int l,int r)
{
if (l > r)
{
cnt[r]++;
if (cnt[r] == 1) ans++;
}
else if (l < r)
{
cnt[l]++;
if (cnt[l] == 1) ans++;
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int l, r;
cin >> l >> r;
solve(l, r);
}
cin >> k;
while (k--)
{
int f;
cin >> f;
if (f == 1)//加边
{
int l, r;
cin >> l >> r;
solve(l, r);
}
else if (f == 2)//减边
{
int l, r;
cin >> l >> r;
if (l > r)
{
cnt[r]--;
if (cnt[r] == 0) ans--;
}
else if (l < r)
{
cnt[l]--;
if (cnt[l] == 0) ans--;
}
}
else cout << n - ans << endl;
}
return 0;
}
Codeforces 1549D 题目链接:点击这里传送
题意:
找出一段连续区间,使得他们模某个数所得的余数都相同。求出这个区间的最大长度。
思路:
依旧还是线段树+尺取。
以两个相邻元素之间的差值构造一个新序列。题目就转变成了所有区间内任意两数gcd大于1的最大区间长度。用线段树来维护区间的gcd,用尺取法的思路先确定右端点再右移左端点的方法确定区间长度。
#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define ll long long
int t, n;
ll ans;
ll a[MAXN];//两个相邻元素的差值
ll gcd(ll x, ll y)
{
if (x < y) swap(x, y);
while (y)
{
ll temp = x % y;
x = y;
y = temp;
}
return x;
}
struct segtreenode
{
int l, r;
ll val;//gcd
}tree[MAXN * 4];
void pushup(int rt) { tree[rt].val = gcd(tree[rt * 2].val, tree[rt * 2 + 1].val); }
void build(int rt, int l, int r)
{
tree[rt].l = l;
tree[rt].r = r;
if (l == r)
{
tree[rt].val = a[l];
return;
}
int mid = (l + r) / 2;
if (mid >= l) build(rt * 2, l, mid);
if (mid < r) build(rt * 2 + 1, mid + 1, r);
pushup(rt);
}
ll query(int rt, int l, int r)
{
if (tree[rt].l > r || tree[rt].r < l) return 0;//
if (tree[rt].l >= l && tree[rt].r <= r) return tree[rt].val;
return gcd(query(rt * 2, l, r), query(rt * 2 + 1, l, r));
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while (t--)
{
ans = 1;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i - 1] = abs(a[i] - a[i - 1]);
}
if (n == 1)
{
cout << 1 << endl;
continue;
}
build(1, 1, n);
ll j = 1;
for (ll i = 1; i <= n - 1; i++)
{
while (j <= i && query(1, j, i) == 1) j++;//尺取法
ans = max(ans, i - j + 2);
}
cout << ans << endl;
}
return 0;
}