B. Prefiquence
算法:双指针
思路:一个l指针指向a字符串的第一个字母 r指针指向b的第一个字母,若l和r指向的字母相同 则双指针往后移动 并更新ans值为l指针 若l和r不相同 则移动r指针至第一个可以和l指针匹配的位置 直到l或者r指针跑到n或m
PS:赛时没想出来 寄
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;using ll = long long;using PLL = pair<ll,ll>;
const ll MAX = 1e18;const ll MIN = -1e18;const ll INF=0x3f3f3f3f;
const ll Q = 2e5+9;const ll MOD = 1e9 + 7;
void solve() {
ll n,m;cin>>n>>m;
string a,b;cin>>a>>b;
a=" "+a;
b=" "+b;
ll lcnt=1,rcnt=1;ll ans=0;
while(lcnt<=n and rcnt<=m) {
if(a[lcnt]==b[rcnt]) {
lcnt++,rcnt++;
ans=lcnt;
}else if(a[lcnt]!=b[rcnt]){
while(a[lcnt]!=b[rcnt] and rcnt<=m){
rcnt++;
}
}
}
cout<<max(ans-1,0LL)<<"\n";
}
int main(){
ios;ll _=1;cin>>_;
while (_--)solve();
return 0;
}
C. Assembly via Remainders
算法:贪心
思路:看数据范围可以得知 取模后的值最多为500 所以即使500个500 也远远小于1e9 所以可以大胆的 从后往前跑 从1e9开始 每次减去要取模的值 肯定符合条件
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;using ll = long long;using PLL = pair<ll,ll>;
const ll MAX = 1e18;const ll MIN = -1e18;const ll INF=0x3f3f3f3f;
const ll Q = 2e5+9;const ll MOD = 1e9 + 7;
ll a[Q];ll ans[Q];
void solve(){
ll n;cin>>n;
for (ll i = 1; i <= n-1; i++)
{
cin>>a[i];
}
ll pre=1e9;
ans[n]=1e9;
for (ll i = n-1; i >= 1; i--)
{
ans[i]=pre-a[i];
pre-=a[i];
}
for (ll i = 1; i <= n; i++)
cout<<ans[i]<<" ";cout<<"\n";
}
int main(){
ios;ll _=1;cin>>_;
while (_--)solve();
return 0;
}
D. Permutation Game
算法:贪心
思路:每个人有一个起始位置 当我们走到每一个点的时候 我们都可以在这个地方停下直到游戏结束 可以肯定的是肯定有一个位置我们走到那个位置之后停下 直到游戏结束我们可以获得的分数最大 我们可以在起始位置停k轮 那我们将获得k*起始位置的分数 移动到下一个位置 我们将会获得 (k-1)*当前位置的分数+我从起始位置走到这里的分数 对于我能走到的每个位置 我们都可以这么做 每次取最大的ans值 看数据范围我们可以得知 k是1e9级别的 我们如果走k次 肯定会超时 但我们会发现 位置的数量只有2e5 这是可以接受的 所以我们可以开map记录一下我走过所有位置 当再次走到这个位置的时候 我们就可以结束循环了
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;using ll = long long;using PLL = pair<ll,ll>;
const ll MAX = 1e18;const ll MIN = -1e18;const ll INF=0x3f3f3f3f;
const ll Q = 2e5+9;const ll MOD = 1e9 + 7;
ll a[Q],p[Q];
void solve(){
ll n,k,b,s;cin>>n>>k>>b>>s;
for (ll i = 1; i <= n; i++)
{
cin>>p[i];
}
for (ll i = 1; i <= n; i++)
{
cin>>a[i];
}
map<ll,ll>mp;
ll lans=0,rans=0;
ll sum=0;
ll now=k;
while(now--){
if(mp[b]) break;
sum+=a[b];
mp[b]++;
lans=max(lans,sum+a[b]*(now));
b=p[b];
}
mp.clear();
now=k;sum=0;
while(now--){
if(mp[s]) break;
sum+=a[s];
mp[s]++;
rans=max(rans,sum+a[s]*(now));
s=p[s];
}
// cout<<lans<<" "<<rans<<"\n";
if(lans==rans) cout<<"Draw\n";
else
if(lans>rans) cout<<"Bodya\n";
else cout<<"Sasha\n";
}
int main(){
ios;ll _=1;cin>>_;
while (_--)solve();
return 0;
}
E. Cells Arrangement
算法:构造
思路:一种验证可行的构造方法 为第一行的前n-2个单元格和最后一行的第二个和最后一个
(需要特判1和2的情况)如图为7的时候
#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;using ll = long long;using PLL = pair<ll,ll>;
const ll MAX = 1e18;const ll MIN = -1e18;const ll INF=0x3f3f3f3f;
const ll Q = 2e5+9;const ll MOD = 1e9 + 7;
void solve(){
ll n;cin>>n;
cout<<1<<" "<<1<<"\n";
cout<<n<<" "<<n<<"\n";
for (ll i = 2; i < n-1; i++)
{
cout<<1<<" "<<i<<"\n";
}
if(n>2)
cout<<n<<" "<<2<<"\n";
cout<<"\n";
}
int main(){
ios;ll _=1;cin>>_;
while (_--)solve();
return 0;
}