红酒哥比赛A题:题目链接:点击这里传送
题意:
给出n个非递减的数字,要求你改变某一个数字的某一位,使其不构成非递减序列。如果不能输出Impossible
思路:
就模拟,枚举相邻的两个数,如果前一个数的第一位变成9或者后一个数的第一位变成1(个位数变成0)仍然合法,继续枚举直到遍历完所有数字。
#include<bits/stdc++.h>
using namespace std;
#define MAXN 105
#define ll long long
int n;
ll a[MAXN];
string s[MAXN];
ll len[MAXN];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
s[i] = to_string(a[i]);
len[i] = (long long)to_string(a[i]).length();
}
int f = 0;
for (int i = 1; i < n; i++)
{
if (f == 1) break;
if (len[i] < len[i + 1]) continue;
char temp = s[i][0];
s[i][0] = '9';
if (s[i] > s[i + 1])
{
f = 1;
continue;
}
s[i][0] = temp;//不行的话换回来
temp = s[i + 1][0];
if (len[i + 1] == 1) s[i + 1] = "0";
else s[i + 1][0] = '1';
if (s[i] > s[i + 1])
{
f = 1;
continue;
}
s[i + 1][0] = temp;//不行的话换回来
}
if(f==0)
cout << "impossible";
else
{
for (int i = 1; i <= n; i++) cout << s[i] << " ";
}
return 0;
}
红酒哥比赛L题 题目链接:点击这里传送
这道题没啥意义,不讲了
#include<bits/stdc++.h>
using namespace std;
string s1, s2;
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> s1 >> s2;
int ans1 = 0;
int ans2 = 0;
int ans = 0;
for (int i = 0; i < s1.length(); i++) if (s1[i] == '(') ans1++;
for (int i = 0; i < s2.length(); i++) if (s2[i] == '(') ans2++;
ans = ans1 * ans2;
if (ans == 0)
{
cout << 0;
return 0;
}
for (int i = 1; i <= ans; i++) cout << "S(";
cout << "0";
for (int i = 1; i <= ans; i++) cout << ")";
return 0;
}
洛谷P2574 线段树例题:点击这里传送
题意:
算法题,还是中文的,不用翻译真爽
思路:
每次翻转,1的个数就相当于上回合0的个数,这是pushup的内容。
奇数次翻转1的个数变化,偶数次翻转1的个数不变化,这是pushdown的内容
#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
struct segtreenode
{
int l, r, len;
int val, lazy;
}tree[MAXN*4];
int a[MAXN];
string s;
int n,m;
void pushup(int rt) { tree[rt].val = 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; tree[rt].len = r - l + 1;
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);
}
void pushdown(int rt)
{
if (tree[rt].lazy%2==1)
{
tree[rt * 2 + 1].lazy += tree[rt].lazy;
tree[rt * 2].lazy += tree[rt].lazy;
tree[rt * 2].val = tree[rt * 2].len - tree[rt * 2].val;
tree[rt * 2 + 1].val = tree[rt * 2 + 1].len - tree[rt * 2 + 1].val;
tree[rt].lazy = 0;
}
else if (tree[rt].lazy % 2 == 0)
{
tree[rt * 2 + 1].lazy += tree[rt].lazy;
tree[rt * 2].lazy += tree[rt].lazy;
tree[rt].lazy = 0;
}
}
void update(int rt, int l, int r)
{
if (tree[rt].l > r || tree[rt].r < l) return;
if (tree[rt].l >= l && tree[rt].r <= r)
{
tree[rt].lazy += 1;
tree[rt].val = tree[rt].len - tree[rt].val;
return;
}
pushdown(rt);
update(rt * 2, l, r);
update(rt * 2 + 1, l, r);
pushup(rt);
}
int 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;
pushdown(rt);
return 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 >> n >> m >> s;
for (int i = 0; i < s.length(); i++)
{
if (s[i] == '0') a[i + 1] = 0;
if (s[i] == '1') a[i + 1] = 1;
}
build(1, 1, n);
while (m--)
{
int f,l,r;
cin >> f;
cin >> l >> r;
if (f == 0)
{
update(1, l, r);
}
else if (f == 1)
{
cout << query(1, l, r) << endl;
}
}
return 0;
}
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;
}