Codeforces Round 986 (Div. 2)

Codeforces Round 986 (Div. 2) 总结

A

按题意模拟即可,因为 n,a,b 很小,可以多循环几遍来判断。只循环十遍的吃罚时 qwq。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=1;
int n,a,b;
string s;
void solve()
{
    cin>>n>>a>>b;
    cin>>s;
    int x=0,y=0;
    for(int j=0;j<100;j++)
    for(int i=0;i<n;i++)
    {
        if(s[i]=='N') y++;
        else if(s[i]=='E') x++;
        else if(s[i]=='S') y--;
        else x--;
        if(x==a&&y==b) 
        {
            cout<<"Yes\n";
            return;
        }
    }
    cout<<"No\n";
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif 
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

B

有点烦人的题,要分类讨论清楚。将一次操作中 x 变为 y 称为 x 移动到 y

  • b=0,就是全是 c,讨论一下 c 的值。
    • c=n1 时,可以将 n1c 移动到 [0,n2],代价为 n1
    • c=n2 时,将 n2c 移动到 [0,n3],再将一个 c 移动到 n1。代价为 n1
    • c<n2 时,无论怎么操作,能移动到最大的数为 c+1,所以不合法。
    • c>n1 时,就要将 nc 移动到 [0,n1],代价为 n
  • b>0,那么就是要将大于 n1 的值都移动到 [0,n1],代价就是大于 n1 的数的个数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=1;
ll n,b,c;
void solve()
{
    cin>>n>>b>>c;
    if(!b)
    {
        if(c<n-2) cout<<-1<<'\n';
        else 
        {
            if(c<=n-1) cout<<n-1<<'\n';
            else cout<<n<<'\n';
        }
        return ;
    }
    if(c>=n)
    {
        cout<<n<<'\n';
        return ;
    }
    ll k=max(0ll,n-1-c)/b+1;
    cout<<n-k<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif 
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}


C

首先切蛋糕时,一但大于 v 就会将其切开,最后将其中一些相邻的合并,使得块数刚好是 m+1,合并出来的就是答案。所以答案肯定是中间的某一段的和。

bi 为切到 i 为止,最多能分出多少块大于 v 的蛋糕。再从后往前,设 ci 为切 [i,n] 最多能分出多少块。对于每个 ci 找到一个 bn,使得 ci+bj=m,且 j 尽可能小。也就是说,将 [1,j][i,n] 的部分给别人,[j+1,i1] 的部分留给自己。用 map 记录 bi 最早出现的位置,复杂度为 O(n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,m;
ll v;
ll a[N],b[N],c[N],pre[N];
void solve()
{
    cin>>n>>m>>v;
    for(int i=1;i<=n;i++) cin>>a[i],pre[i]=pre[i-1]+a[i];
    map<int,int> H;
    ll x=0;
    H[0]=0;
    for(int i=1;i<=n;i++)
    {
        b[i]=b[i-1];
        if(x<v) x+=a[i];
        if(x>=v) x=0,b[i]++;
        if(H.count(b[i])==0) H[b[i]]=i;
    }
    if(b[n]<m) 
    {
        cout<<-1<<'\n';
        return ;
    }
    c[n+1]=0,x=0;
    ll ans=pre[n]-pre[H[m]];
    for(int i=n;i>=1;i--)
    {
        c[i]=c[i+1];
        if(x<v) x+=a[i];
        if(x>=v) x=0,c[i]++;
        int j=H[m-c[i]];
        ans=max(ans,pre[i-1]-pre[j]);
    }
    cout<<ans<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif 
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

posted @   zhouruoheng  阅读(63)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示