第一次打小白周赛,对于小白还是怪友好的。如果你也是算法小白的话,可以一起来感受acfun!!!
A
等价于询问数组是否满足单调递增
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
int n;cin >> n;
vector<int>v (n + 1);
for(int i = 1;i <= n;i++)
{
cin >> v[i];
}
for(int i = 1;i <= n;i++)
{
if(v[i] <= v[i - 1])
{
cout << "NO" << '\n';
return;
}
}
cout << "YES" << '\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int _ = 1;cin >> _;
while(_--) solve();
return 0;
}
B & C
为C的easy版本 所以直接看hard版本吧
对于给定x进行二进制拆分,如果有不超过两位为1,则该数是合法的
对于合法的x,输出下一个合法的数字,否则输出二进制中1的个数
首先需要了解下lowbit
int lowbit(int x)
{
return x & -x;
}
返回的是二进制下第一位为1的数,比如lowbit(6) 返回的为2
那么我们可以每次都减去lowbit(x),直到x为0。这样我们就可以得到一共有多少位1,和每一位1在十进制所表示的数字。
对于合法的数字:如果只有一位1,那么只需加上1
如果有两位,那么根据二进制的特性,直接加上最小位所代表的数字即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int lowbit(int x)
{
return x & -x;
}
void solve()
{
int x;cin >> x;
int temp = x;
vector<int>v;
int cnt = 0;
while(cnt != x)
{
cnt = lowbit(x);
x -= cnt;
v.push_back(cnt);
}
sort(begin(v) , end(v));
//for(auto x : v) cout << x << ' ';
if(v.size() == 2) cout << temp + 1 << '\n';
else if(v.size() <= 3) cout << temp + lowbit(temp) << '\n';
else cout << v.size() - 1<< '\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int _ = 1;
cin >> _;
while(_--) solve();
return 0;
}
D
为A的hard版本 ,此时我们可以删去至多一个数字,并且可以对进行+1操作
那么对于整个数组我们可以记录下每个数字出现的次数,如果整个数字已经出现过了,我们需要对该数字进行+1操作(至多一次),然后求出修改过的数组的最长上升子序列,如果该序列长度为或者
,则满足条件
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
map<int , int>mp;
void solve()
{
mp.clear();
vector<int>st;
int n;cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
if(mp[a[i]] != 0)a[i]++;
mp[a[i]]++;
}
//for(int i = 1;i <= n;i++) cout << a[i] << ' ';
//cout << '\n';
st.push_back(a[1]);
for(int i=1;i<= n;i++)
{
if(a[i] > st.back())
st.push_back(a[i]);
else *lower_bound(st.begin(),st.end(),a[i]) = a[i];
}
if(st.size() == n || st.size() == n - 1) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
int main() {
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
int _ = 1;cin >> _;
while(_--) solve();
return 0;
}
E
赛时想出偏暴力的code,结果超时了
后来发现最大为20,那么我们可以利用mod的特性,用数组
来记录答案。
记录下标,
表示mod之后的值。
转移方程的时候,枚举0 ~ k-1。对于每个点 加上 上个位置mod的值即可
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 510;
int a[N][N] , dp[N][N][30];
const int P = 998244353;
void solve()
{
int n , k;cin >> n >> k;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++) cin >> a[i][j];
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++)
{
if(i == 1 && j == 1) dp[i][j][a[i][j] % k] = 1;
for(int x = 0;x < 20;x++)
{
dp[i + 1][j][a[i + 1][j] * x % k] = ( dp[i + 1][j][a[i + 1][j] * x % k] % P + dp[i][j][x] % P) % P;
dp[i][j + 1][a[i][j + 1] * x % k] = ( dp[i][j + 1][a[i][j + 1] * x % k] % P + dp[i][j][x] % P) % P;
}
}
cout << dp[n][n][0] % P << '\n';
}
signed main()
{
cin.tie(0)->sync_with_stdio(false);
int _ = 1;
//cin >> _;
while(_--) solve();
return 0;
}