比赛链接: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;
}