B.Broken Pad
题意:给出两个串a,b ,求最少操作次数将a-->b....懒得翻译了。
分析:就跟着题目造呗,分两种情况,
一是保留a串不变,将a的每一位与b的每一位判断,
并记录反转的次数:二是将a全部变成0,
再将a的每一位与b的每一位判断,并记录反转的次数;
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+5;
int t;
int main()
{
cin>>t;
while (t--){
string a,b;
cin>>a>>b;
int len=(int)a.size();
int f[maxn]={0},s[maxn]={0},cnt=0,tot=0;
bool flag=false;
for(int i=0;i<len;i++){
if(a[i]!=b[i]&&!flag)
f[++cnt]=i+1, flag=!flag;
else if(a[i]==b[i]&&flag)
f[++cnt]=i+1,flag=!flag;
}
flag=false;
s[++tot]=0;
for(int i=0;i<len;i++){
if(b[i]!='0'&&!flag)
s[++tot]=i+1, flag=!flag;
else if(b[i]=='0'&&flag)
s[++tot]=i+1, flag=!flag;
}
if(cnt<tot)
for(int i=1;i<=cnt;i++)
printf("%d ",f[i]);
else
for(int i=1;i<=tot;i++)
printf("%d ",s[i]);
printf("\n");
}
return 0;
}
C .Cook Steak
[ l , r ]
分析:有一个标记pos表示当前的温度,首先取第一个区间的 l 值,pos=l,然后
比较 abs(l[i+1]-pos)与abs(r[i+1]-pos) ,去最小值即可
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+5;
int t,n;
int main()
{
cin >> t;
int l[maxn],r[maxn];
while (t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&l[i],&r[i]);
ll ans=l[1];
int pos=l[1];
for(int i=2;i<=n;i++){
if(pos>=l[i]&&pos<=r[i])
continue;
if(abs(l[i]-pos)>=abs(r[i]-pos)){
ans+=abs(r[i]-pos);
pos=r[i];
}
else if(abs(l[i]-pos)<=abs(r[i]-pos)){
ans+=abs(l[i]-pos);
pos=l[i];
}
}
ans+=n;
printf("%lld\n",ans);
}
}
F. Flag Scramble Competition
求文章中出现频率最高的字母,不就是e 吗
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+5;
int t;
int main()
{
cout<<'e'<<endl;
return 0;
}
I. Isolated Pointset
三点可以构成一个等腰三角形
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
int a[100001];
int b[100001];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
if(n>=3)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}```
H.Happy Time is Always Short
线段树维护区间最值
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+5;
int n,m,fa[maxn],k,t;
struct Node{
int l,r,Max,Tag;
int mid(){
return (l+r)>>1;
}
}node[maxn<<2];
void Pushup(int rt){
node[rt].Max=max(node[rt<<1].Max,node[rt<<1|1].Max);
}
void Pushdown(int rt){
if(node[rt].Tag){
node[rt<<1].Tag=1;
node[rt<<1|1].Tag=1;
node[rt<<1].Max=0;
node[rt<<1|1].Max=0;
node[rt].Tag=0;
}
}
void BuildTree(int rt,int l,int r){
node[rt].l=l,node[rt].r=r,node[rt].Max=0,node[rt].Tag=0;
if(l==r){
node[rt].Max=fa[++k];
return;
}
int mid=node[rt].mid();
BuildTree(lson);
BuildTree(rson);
Pushup(rt);
}
void UpdateTree(int tag,int rt,int l,int r){
if(node[rt].l==l&&node[rt].r==r){
node[rt].Tag=1;
node[rt].Max=0;
return;
}
if(node[rt].l==node[rt].r) return;
Pushdown(rt);
int mid=node[rt].mid();
if(mid>=r) UpdateTree(tag,rt<<1,l,r);
else if(l>mid) UpdateTree(tag,rt<<1|1,l,r);
else UpdateTree(tag,lson),UpdateTree(tag,rson);
Pushup(rt);
}
int QueryTree(int rt,int l,int r){
if(node[rt].l==l&&node[rt].r==r) return node[rt].Max;
Pushup(rt);
int Max=-INF;
int mid=node[rt].mid();
if(mid>=r) Max=max(Max,QueryTree(rt<<1,l,r));
else if(l>mid) Max=max(Max,QueryTree(rt<<1|1,l,r));
else Max=max(Max,QueryTree(lson)),Max=max(Max,QueryTree(rson));
return Max;
}
int main()
{
scanf("%d",&t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &fa[i]);
k = 0;
BuildTree(1, 1, n);
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
UpdateTree(1, 1, l, r);
printf("%d\n", QueryTree(1, 1, n));
}
}
return 0;
}