juruo5e59OI Round1 题解

· · 个人记录

VzisOI Round1 juruo5e59OI Round1 题解

个人评价: ||T1|T2|T3|T4|T5|T6| |:-|:-|:-|:-|:-|:-|:-| |难度|黄|绿|橙|黄|橙|黄|

Fun Facts:\ 比赛中的模数全都是 99824353。好笑吧(\ 怒怒怒原来全是 florr 背景的题来着。

发现原题不要声张,我是故意的(

T1:

做滑动窗口。

为了维护窗口内 sfr 子序列的数量,我们还要维护 s,f,r,sf,fr 的数量(均为子序列)。

以从右端加入 r 为例:

删除时做类似的操作。

#include<bits/stdc++.h>
#define mod 99824353
#define int long long
using namespace std;
string a;
int s,f,r,sf,fr,sfr,ans;
void wk(){s=(s+mod)%mod,f=(f+mod)%mod,r=(f+mod)%mod,sf=(sf+mod)%mod,fr=(fr+mod)%mod,sfr=(sfr+mod)%mod,ans=(ans+mod)%mod;}
//wk是封装取模不用鸟它
signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>a;int n=a.size();
  a+=a;
  for(int i=0;i<n;i++){
    if(a[i]=='s')s++;
    if(a[i]=='f')f++,sf+=s;
    if(a[i]=='r')r++,fr+=f,sfr+=sf;
    wk();
  }
  for(int i=0;i<n;i++){
    ans+=sfr;
    if(a[i]=='s')s--,sf-=f,sfr-=fr;
    if(a[i]=='f')f--,fr-=r;
    if(a[i]=='r')r--;
    wk();
    int x=i+n;
    if(a[x]=='s')s++;
    if(a[x]=='f')f++,sf+=s;
    if(a[x]=='r')r++,fr+=f,sfr+=sf;
    wk();
  }
  cout<<ans;
  return 0;
}

T2:

直接二分受到的最大伤害,然后 ban 掉伤害过大的点跑最短路。

Fun Facts:本题是该比赛码量顶梁柱,但顶了个寂寞,只有 1.4k。

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
int n,m,f,s,t;
struct node{int to,w,nxt;};
node e[200010];int cnt;
int hed[200010],k[200010];
int dis[200010];
bool vis[200010]; 
priority_queue<pii,vector<pii>,greater<pii> > q;
void ad(int u,int v,int w){
    ++cnt;
    e[cnt].to=v,e[cnt].w=w;
    e[cnt].nxt=hed[u];
    hed[u]=cnt;
}
bool check(int x){
    if(k[s]>x)return 0;
    while(!q.empty())q.pop();
    for(int i=1;i<=n;i++)
        vis[i]=(k[i]<=x),//ban点
        dis[i]=1e12;
    dis[s]=0;
    q.push({0,s});
    while(!q.empty()){
        int u=q.top().second;
        int x=q.top().first;
        q.pop();
        if(x>dis[u]||!vis[u])continue;
        for(int i=hed[u];i;i=e[i].nxt){
            int v=e[i].to,w=e[i].w;
            if(dis[v]>dis[u]+w)
                dis[v]=dis[u]+w,
                q.push({dis[v],v});
        }
    }
    return dis[t]<=f;
}
void solve(){
    memset(hed,0,sizeof(hed));
    cnt=0;
    cin>>n>>m>>f>>s>>t;
    int maxf=0;
    for(int i=1;i<=n;i++)
        cin>>k[i],
        maxf=max(maxf,k[i]);
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        ad(u,v,w);
        ad(v,u,w);
    }
    int l=max(k[s],k[t])-1,r=maxf;
    while(l<r-1){
        int mid=(l+r+1)/2;
        if(check(mid))r=mid;
        else l=mid;
    }
    if(!check(r))cout<<"CaiJiuDuoLian"<<endl;//若 l 取到最大仍然无法 check 成功则不可行
    else cout<<r<<endl;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;cin>>t;
    while(t--)solve();
    return 0;
}

T3:

套路:两个胡萝卜相撞相当于互相穿过。还有方向?不用鸟它。

然后你就会了。不开 long long 见祖宗。

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,h,w;
    cin>>n>>h>>w;
    int maxn=0;
    for(int i=1;i<=n;i++){
        int t,f;
        cin>>t>>f;
        if(f<=2)maxn=max(maxn,t+h);
        else maxn=max(maxn,t+w);
    }
    cout<<maxn;
    return 0;
}

T4:

对于要干的怪,血量越大必然要越先干掉,且选择要干的怪血量水平要尽可能低,于是考虑按血量从小到大排序然后选择某一个前缀,在其中倒序解决。

若如此解决,做两次前缀和之后完美符合需求:

a_1,2a_1+a_2,3a_1+2a_2+a_3,\dots

于是解决前 i 个怪消耗的最少精力就是两次前缀和之后第 i 个数的值,最后二分就可以。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[1000010];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)a[i]+=a[i-1];
    for(int i=1;i<=n;i++)a[i]+=a[i-1];
    for(int i=1;i<=q;i++){
        int t;cin>>t;
        int l=0,r=n;
        while(l<r){
            int mid=(l+r+1)/2;
            if(a[mid]>t)r=mid-1;
            else l=mid;
        }
        cout<<l<<'\n';
    }
    return 0;
}

T5:

这题我们可以打表看出系数的规律。

对于位置 i 的数,左端点在它前面可以使它被加 i 次,右端点在它后面可以使它被加 n-i+1 次,乘起来就是这个数对应的系数。

#include<bits/stdc++.h>
#define int long long
#define mod 99824353
using namespace std;
int a[100005];
void solve(){
    int n,f=0;cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i],a[i]%=mod;
    for(int i=1;i<=n;i++)
        f+=(((a[i]*i)%mod)*((n-i+1)%mod))%mod,f%=mod;
    cout<<f<<endl;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;cin>>t;
    while(t--)solve();
    return 0;
}

T6:

注意到根据题目,WWW...WA 可以逐次转变为 ACCC...C。然后跑个循环边做边统计该位置前的 W 数量,然后就做完了。

有几个 educational 的细节(对的对的样例里都没有):

Fun Facts:这题也许可以不用费马小定理逆元,直接从 0 到 99824352 枚举逆元结果(我没试过)

Another Fact:这题所有数据点 N 都取到了最大值,所以每个 task 里 inv_N 是固定的。所以上面的想法真的可做

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=99824353;
string s;
int qpow(int a,int b){
  int r=1;
  while(b){
    if(b&1)r=r*a%mod;
    a=a*a%mod;
    b>>=1;
  }
  return r;
}
signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>s;
  int n=s.size();
  int cnt=0,sum=0,cc=0,acb=0;
  for(int i=0;i<n;i++)
    cc+=(s[i]=='C');
  int invn=qpow(n,mod-2);
  for(int i=0;i<n;i++){
    int t=cnt+cc-(s[i]=='C');
    if(i&&!(s[i-1]=='W'&&s[i]=='C'))sum+=cnt;
    acb=(acb+t*invn%mod)%mod;
    if(s[i]=='W')cnt++;
    else cnt=0;
  }
  cout<<(n+sum)%mod<<' '<<acb<<endl;
}