Codeforces Round 912(Div.2) A-C题解

比赛链接:Dashboard - Codeforces Round 912 (Div. 2) - Codeforces

A题:Halloumi Boxes

题目链接:Problem - A - Codeforces

A题很简单,只需要考虑k是否等于1就可以。因为只要K>1,一定可以将数组重新排序。当k等于1的时候,只需要看数组是否原来就是已经排序好的。

代码如下

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
#define kewu ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
void solve()
{
    int n,k;
    cin>>n>>k;
    vector<int> a(n+1);
    for(int i = 1;i<=n;i++) cin>>a[i];
    if(k==1)
    {
        for(int i = 1;i<=n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                cout<<"NO"<<endl;
                return;
            }
        }
        cout<<"YES"<<endl;
    }
    else cout<<"YES"<<endl;
}
signed main(void)
{
    kewu;
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

B题:StORage room

题目链接:Problem - B - Codeforces

B题就是位运算

我们以题目给的一个样例解释

0 7 7 5 5
7 0 3 2 6
7 3 0 3 7
5 2 3 0 4
5 6 7 4 0

数组大小肯定是5,毋庸置疑。我们看一下第五列,首先第五列的第一个数是a1,a5        位或运算的结果,5的二进制数是101(省略前导零),我们看5的二进制数低位的第二位,是0,那么很明显,a1,a5的二进制在同等位上,也必然是0,什么意思呢,就是a1,a5现在的二进制一定是......xxxxx0x,按着这个思路,只要让每一列位与运算即可。ps:i==j的位置不要位与。

代码展示

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define endl '\n'
using namespace std;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 1010;
int a[N][N];
void solve()
{
    int n;
    cin>>n;
    vector<int> ans(n+1);
    for(int i = 1;i<=n;i++) ans[i] = (1<<30)-1;
    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=n;j++) 
        {
            cin>>a[i][j];
            if(i!=j)
            {
                ans[i]&=a[i][j];
                ans[j]&=a[i][j];
            }
        }
    }
    for(int i = 1;i<=n;i++)
    {
        for(int j = 1;j<=n;j++)
        {
            if(a[i][j]!=(ans[i]|ans[j])&&i!=j)
            {
                cout<<"NO"<<endl;
                return;
            }
        }
    }
    cout<<"YES"<<endl;
    for(int i = 1;i<=n;i++) cout<<ans[i]<<' ';
    cout<<endl;
}
signed main(void)
{
    ios;
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

C题:Theofanis' Nightmare

题目链接:Problem - C - Codeforces

C题就是后缀和,从后往前便利,只要lastpre>=0,ans就可以加上lastpre,好好想想我说的话,还有要特判一下i==1的情况

代码展示

#include <bits/stdc++.h>
#define int long long
#define ll long long
#define endl '\n'
using namespace std;
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
void solve()
{
    int n;
    cin>>n;
    int ans = 0;
    int a[n+2];
    int lastpre[n+2];
    lastpre[n+1]=0;
    for(int i = 1;i<=n;i++) cin>>a[i];
    for(int i = n;i>=1;i--)
    {
        lastpre[i]=lastpre[i+1]+a[i];
        if(lastpre[i]>=0||i==1)
        {
            ans+=lastpre[i];
        }
    }
    cout<<ans<<endl;
}
signed main(void)
{
    ios;
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法好玩头秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值