C - Avoid K Palindrome 2 (atcoder.jp)
思路:
罗列出排列的每一种情况,再根据题目要求进行判断
代码:
void solve()
{
ll n, k;
cin >> n >> k;
string s;
vector<char>a;
cin >> s;
for (int i = 0; i < n; i++)a.push_back(s[i]);
sort(a.begin(), a.end());
bool ok, flag;
ll ans = 0;
while (true)
{
ok = true;
for (int i = 0; i <= n - k; i++)
{
flag = true;
for (int j = 0; j < k; j++)
{
if (a[i + j] != a[i + 1 + k - j])
{
flag = false;
}
}
if (flag)ok = false;
}
if (ok)ans++;
if(!next_permutation(a.begin(),a.end()))break;
}
cout << ans << endl;
return;
}
D - Palindromic Number (atcoder.jp)
思路其实很简单,根据需要的i-th进行判断即可
#define _CRT_SECURE_NO_WARNINGS 1
//------ 棘手大学 世界第一 ------//
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
using namespace std;
//gcd最大公约数,lcm最小公倍数
typedef long long ll;
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
const int N = 2e6 + 10;
long long sum[60], a[60];
long long n;
int ans[1010], len, b[1010];
ll pow(int x) {
long long res = 1;
for (int i = 1; i <= x; i++)
res *= 10;
return res;
}
int main() {
IOS;
for (int i = 1; i <= 37; i++) {
int mi = ((i + 1) / 2 - 1);
a[i] = 1LL * 9 * pow(mi);
sum[i] = sum[i - 1] + a[i];
}
cin >> n;
if (n <= 1)
{
cout << "0" << endl;
return 0;
}
n -= 1;
int pos = lower_bound(sum + 1, sum + 38, n) - sum;
if (n == sum[pos - 1])pos--;
while (n) {
b[++len] = n % 10;
n /= 10;
}
for (int i = 1, j = len; i <= len; i++, j--)
ans[i] = b[j];
ans[1]--; ans[len]++;
if (ans[1] == 0 && ans[2] == 0)ans[2] = 9;
int now = len;
while (ans[now] >= 10) {
ans[now - 1] += ans[now] / 10;
ans[now] %= 10;
now--;
}
int l = len, r = len + 1;
if (pos % 2 == 1)l--;
while (l >= 1) {
ans[r] = ans[l];
l--; r++;
}
l = 1, r--;
while (ans[l] == 0)
l++;
while (ans[r] == 0)
r--;
for (int i = l; i <= r; i++)
cout << ans[i];
}
Problem - A - Codeforces
思路:
由题意可知两者每次肯定都会做出最佳选择(即选取最大),所以我们只需要判断最大值是奇数还是偶数即可(因为是Alice先手,所以奇数A必胜,否则B胜)
代码:
//该段代码经过修改仅提供思路,不能AC
void solve()
{
ll a[N];
ll n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[n];
mp[a[n]]++;
}
sort(a + 1, a + 1 + n, cmp);
ll flag = 1;
for (auto k : a)
{
if (mp[a[k]] % 2 != 0)
{
flag = 0;
}
}
if (flag)cout << "NO" << endl;
else
cout << "NO" << endl;
return;
}
Problem - B - Codeforces
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdbool.h>
#define N 2000010
typedef long long ll;
ll n, t, x, y;
ll dp[N];
void solve() {
scanf("%lld %lld %lld", &n, &x, &y);
for (ll i = 1; i <= n; i++) {
if (i >= y && i <= x) {
dp[i] = 1;
}
}
for (ll i = y - 1; i >= 1; i -= 2) {
dp[i] = -1;
if (i - 1 >= 1) {
dp[i - 1] = 1;
}
}
for (ll i = x + 1; i <= n; i += 2) {
dp[i] = -1;
if (i + 1 <= n) {
dp[i + 1] = 1;
}
}
for (ll i = 1; i <= n; i++)
printf("%lld ", dp[i]);
printf("\n");
}
int main() {
scanf("%lld", &t);
while (t--) {
solve();
}
return 0;
}
Problem - C - Codeforces
代码:
void solve() {
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
ll res = 0;
for (int i = 0; i < 2; ++i) {
vector<bool> vis(n + 1);
ll ma = 0;
for (ll& x : a) {
if (vis[x])
ma = max(ma, x);
vis[x] = true;
res += x;
x = ma;
}
}
for (int i = 0; i < n; ++i)
res += (n - i) * a[i];
cout << res << endl;
}
C-嘤嘤不想买东西喵_牛客周赛 Round 49(重现赛) (nowcoder.com)
本质就是求最大连续子段和
代码:
#define _CRT_SECURE_NO_WARNINGS 1
//------ 棘手大学 世界第一 ------//
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
using namespace std;
//gcd最大公约数,lcm最小公倍数
typedef long long ll;
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr);
priority_queue<int, vector<int>, greater<int>> pq;
map<int, int>mp;
const int N = 2e6 + 10;
int mod(string a, ll b)//高精度a除以单精度b
{
ll n = 0;
for (int i = 0; i < a.size(); i++) n = (n * 10 + (a[i] - '0')) % b; //求出余数
return n;
}
string gcd(string a, ll b) // 欧几里得算法
{
while (b != 0)
{
ll temp = b;
b = mod(a, b);
a = to_string(temp);
}
return a;
}
int gcd1(int a, int b)//a、b不可以为0(很快)
{
while (b ^= a ^= b ^= a %= b);
return a;
}
int gcd2(int a, int b)//a、b可以为0(很快)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
int lcm1(int a, int b) {
return a * b / gcd1(a, b);
}
bool cmp(ll x, ll y)
{
return x > y;
}
ll a[N], dp[N];
void solve()
{
ll n, x;
cin >> n >> x;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] -= x;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
dp[i] = max(a[i], dp[i - 1] + a[i]);
}
for (int i = 1; i <= n; i++)
{
ans = max(ans, dp[i]);
}
cout << ans << endl;
return;
}
int main()
{
IOS;
solve();
return 0;
}
D-嘤嘤不想求异或喵_牛客周赛 Round 49(重现赛) (nowcoder.com)
求区间异或和(以后直接套用模板算了)
代码:
ll xor_prefix(ll n) {
switch (n % 4) {
case 0: return n;
case 1: return 1;
case 2: return n + 1;
case 3: return 0;
}
return 0;
}
int xor_range(ll L, ll R) {//区间所有整数异或和
return xor_prefix(R) ^ xor_prefix(L - 1);
}