A. Lex String
思路:
双指针模拟
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m,k;
void solve()
{
cin>>n>>m>>k;
string a,b;
cin>>a>>b;
int i=0,j=0;
int cnt1=0,cnt2=0;
sort(all(a));
sort(all(b));
while(i<n&&j<m)
{
if(a[i]<b[j])
{
if(cnt1<k)
{
cnt1++;
cnt2=0;
cout<<a[i++];
}
else
{
cnt1=0;
cnt2++;
cout<<b[j++];
}
}
else
{
if(cnt2<k)
{
cnt1=0;
cout<<b[j++];
cnt2++;
}
else
{
cout<<a[i++];
cnt1++;
cnt2=0;
}
}
}
cout<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
B. Mystic Permutation
如果一个数字不在他的下标位置,则先把他放回自己本身的位置。如果本身就在下标位置,将这些数字进行swap,能与后边的swap字典序最小
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1010;
int a[N];
int c[N];
void solve()
{
cin>>n;
memset(a,0,sizeof a);
memset(c,0,sizeof c);
for(int i=1;i<=n;i++)cin>>a[i];
if(n==1)
{
cout<<-1<<endl;
return;
}
for(int i=1;i<=n;i++)
{
if(a[i]!=i)
{
c[i]=i;
}
else c[i]=0;
}
for(int i=1;i<=n;i++)
if(c[i]==0)
{
if(i<n)c[i]=i+1,c[i+1]=i;
else c[i]=c[i-1],c[i-1]=i;
}
for(int i=1;i<=n;i++)cout<<c[i]<<' ';
cout<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
C. Infected Tree
两种方法,第一种dfs
第二种 树形dp
//dfs
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=3e5+10;
int h[N],e[2*N],ne[N*2],idx;
bool st[N];
int l[N],r[N],w[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int fa)
{
if(h[u]==-1)return 0;
int s=0;
bool f=false;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
if(!f)s+=dfs(j,u),l[u]=j,f=true;
else s+=dfs(j,u),r[u]=j;
}
w[u]+=s;
return s+1;
}
int dfs2(int u)
{
if(h[u]==-1)return 0;
int res=0;
res=max(res,w[r[u]]+dfs2(l[u]));
res=max(res,w[l[u]]+dfs2(r[u]));
return res;
}
void solve()
{
cin>>n;
memset(h,-1,sizeof h);
idx=0;
memset(st,false,sizeof st);
memset(l,0,sizeof l);
memset(r,0,sizeof r);
memset(w,0,sizeof w);
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs(1,-1);
cout<<dfs2(1)<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
//dp
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=3e5+10,M=N<<1;
int h[N],e[M],ne[M],idx;
int dp[N],sz[N];
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int fa)
{
sz[u]=1;
int s1=-1,s2=-1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
dfs(j,u);
sz[u]+=sz[j];
if(s1==-1)s1=j;
else if(s2==-1)s2=j;
}
if(s1==-1)return dp[u]=0;
else if(s2==-1)return dp[u]=sz[s1]-1;
return dp[u]=max(sz[s1]-1+dp[s2],sz[s2]-1+dp[s1]);
}
void solve()
{
memset(h,-1,sizeof h);
idx=0;
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
cout<<dfs(1,-1)<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
D. Lena and Matrix
离所有黑点的最大距离最小,尽量与左上,左下,右上,右下的中间区域。
点i
是我们可以提前预处理的黑点,四个方向取max
然后枚举所有点j
,最后再去max就是最大值最小的点
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1010;
char g[N][N];
void solve()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>g[i][j];
int max1=-1e9,max2=-1e9,max3=-1e9,max4=-1e9;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='B')
{
max1=max(max1,i+j);
max2=max(max2,i-j);
max3=max(max3,-i+j);
max4=max(max4,-i-j);
}
}
}
int resx=0,resy=0,res=1e9;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int t=max({max1-i-j,max2-i+j,max3+i-j,max4+i+j});
if(t<res)res=t,resx=i,resy=j;
}
}
cout<<resx+1<<' '<<resy+1<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}