Problem - 1924A - Codeforces(构造)
要判断s字符串是否满足是所有前k个字符的子数组
则需要把s分段,每一段都包含前k个字符
如果段数>=n长度,即满足
否则,找最后一段不满足的字符
构造一个不满足的字符串
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;void solve()
{set<char> s;int n, k, m;int cnt = 0;cin >> n >> k >> m;string str,ans;cin >> str;for(auto x:str){s.insert(x);if(s.size()==k){s.clear();cnt++;ans += x;}}if(ans.size()<n){cout << "NO\n";char ch;for (char i = 'a'; i <= 'z';i++){if(!s.count(i)){ch = i;break;}}while(ans.size()<n){ans += ch;}cout << ans << endl;}else{cout << "YES\n";}
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}
Problem - 1547E - Codeforces(dp)(1500)
从前往后推一遍
从后往前推一遍
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=3e5+10;
LL a[N], t[N],dp[N];
LL inf = 1e18;void solve()
{int n, k;cin >> n >> k;for (int i = 0; i < k;i++){cin >> a[i];}for (int i = 0; i <= n;i++){dp[i] = inf;}for (int i = 0; i < k; i++){cin >> t[i];dp[a[i]] = t[i];}for (int i = 2; i <= n;i++){dp[i] = min(dp[i], dp[i - 1] + 1);}for (int i = n - 1; i >= 1;i--){dp[i] = min(dp[i], dp[i + 1] + 1);}for (int i = 1; i <= n;i++){cout << dp[i] << " ";}cout << endl;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}
Problem - 1976C - Codeforces(贪心)(1600)
贪心,求出n,m+1和n+1,m
然后遍历减去
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
int a[N], b[N];void solve()
{int n, m;cin >> n >> m;for (int i = 0; i < n + m + 1;i++)cin >> a[i];for (int i = 0; i < n + m + 1;i++)cin >> b[i];LL ca1 = n, cb1 = m + 1, ca2 = n + 1, cb2 = m;LL ans1 = 0, ans2 = 0;for (int i = 0; i < n + m + 1;i++){if(a[i]>b[i]){if(ca1>0){ans1 += a[i];ca1--;}else{ans1 += b[i];cb1--;}if(ca2>0){ans2 += a[i];ca2--;}else{ans2 += b[i];cb2--;}}else{if (cb1 > 0){ans1 += b[i];cb1--;}else{ans1 += a[i];ca1--;}if (cb2 > 0){ans2 += b[i];cb2--;}else{ans2 += a[i];ca2--;}}}int ca = n, cb = m;for (int i = 0; i < n + m + 1;i++){if(ca>0&&cb>0){if(a[i]>b[i]){cout << ans2 - a[i] << " ";ca--;}else{ cout << ans1 - b[i] << " ";cb--;}}else{if(ca==0){cout << ans1 - b[i] << " ";}elsecout << ans2 - a[i] << " ";}}cout << endl;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}
Problem - 1811E - Codeforces(妙)(思维题)(1500)
初看以为是复杂的数位dp+二分
看了题解发现只要把十进制转化成9进制
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
int x,a[50];
int m[12] = {0, 1, 2, 3, 5, 6, 7, 8, 9};void solve()
{LL n;x = 0;cin >> n;while(n){a[++x] = m[n % 9];n /= 9;}for (int i = x; i >= 1;i--){cout << a[i];}cout << endl;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}