Codeforces Round #750 (Div. 2)
Codeforces Round #750 (Div. 2)
A. Luntik and Concerts
思路分析:
- 首先我们可以肯定的是a,b,c都大于等于1,所以我们先让它们自己抵消自己,最后a,c只有三种情况。
- a = 1, c = 1 如果只有奇数个b,我们取一个b * 2 + a抵消c,否则就拿两个b放到一个数组,另外一个放1个a,1个c。
- a = 0, c = 1 不能抵消。
- a = 0, c = 0 如果是偶数个b,那么我们把b分成两份,否则,我们就先取出一个b和一对a,c抵消,剩下的就和a = 1, c = 1,b为偶数一样了。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t;
cin >> t;
while (t--)
{
ll a, b, c;
cin >> a >> b >> c;
a %= 2;
c %= 2;
if (a == 0 && c == 0 || (a == 1 && c == 1))
{
cout << 0 << endl;
}
else
cout << 1 << endl;
}
return 0;
}
B. Luntik and Subsequences
思路分析:
- 考虑1和0这两个元素,如果有n个0的话我们对于每个0都有选与不选,所以答案就是\(2^n\),如果有m个1的话我们至少要选一个1,答案就是\(C_m^1\),乘法即可。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 100;
int a[maxn];
map<ll, ll> cnt;
ll qpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)
ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
cnt.clear();
long long sum = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum += a[i];
cnt[a[i]]++;
}
cout << cnt[1] * qpow(2ll, cnt[0]) << endl;
}
return 0;
}
C. Grandma Capa Knits a Scarf
思路分析:
- 首先我们肯定的是要删去的字符肯定是首次出现的不对称的两个字符(如果是其他的那么在第一个位置就不对称了)。
- 然后就是如何求删除次数,我在这里采用的是双指针做法,一个在最左段,一个在最右段,如果相等,那么就指针移动,否则就删去和当前字符一样的字符,如果两端都不是当前字符,那么不可能回文,注意删去之后指针的移动即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn];
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
scanf("%d", &n);
scanf("%s", s + 1);
int ans = 0x3f3f3f3f;
int l = 1, r = n;
char ch[3];
int cnt = 0;
while (l < r)
{
if (s[l] == s[r])
{
l++;
r--;
}
else
{
ch[++cnt] = s[l];
ch[++cnt] = s[r];
break;
}
}
for (int i = 1; i <= 2; i++)
{
char c = ch[i];
int l = 1, r = n;
bool flag = 1;
int cnt = 0;
while (l < r)
{
if (s[l] == s[r])
{
l++;
r--;
}
else if (s[l] == c)
{
l++;
cnt++;
}
else if (s[r] == c)
{
r--;
cnt++;
}
else
{
flag = 0;
break;
}
}
if (flag)
{
ans = min(ans, cnt);
}
}
if (ans == 0x3f3f3f3f)
cout << -1 << endl;
else
cout << ans << endl;
}
return 0;
}
D. Vupsen, Pupsen and 0
思路分析:
- 题目要求的是\(\sum_{i = 1}^n{a_i\times b_i} = 0\),我们可以这样想,我们把两个数两两匹配例如:\(a_i = 5, a_{i+1} = 4\) 我们就可以令\(b_i = 4, b_{i+1} = -5\),依次类推。
- 那么每次都能两两匹配的话\(n\)必须是偶数,所以还要讨论\(n\)为奇数的时候,我们可以直接把前三项取出来,让这三项相加为\(0\),那么就有三种情况。
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;
ll a[maxn];
ll sum;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
sum = 0;
ll n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum += a[i];
}
if (n % 2 == 0)
{
for (int i = 1; i <= n; i += 2)
{
cout << a[i + 1] << ' ' << -a[i] << ' ';
}
cout << endl;
}
else if (n % 2 == 1)
{
//不可能三项加起来都为0,所以保证了有答案
//推一下就好了
if (a[3] + a[1] != 0)
cout << a[2] << ' ' << -(a[3] + a[1]) << ' ' << a[2] << ' ';
else if (a[1] + a[2] != 0)
{
cout << a[3] << ' ' << a[3] << ' ' << -(a[2] + a[1]) << ' ';
}
else if (a[3] + a[2] != 0)
{
cout << -(a[2] + a[3]) << ' ' << a[1] << ' ' << a[1] << ' ';
}
for (int i = 4; i <= n; i += 2)
{
cout << a[i + 1] << ' ' << -a[i] << ' ';
}
cout << endl;
}
}
return 0;
}
F1. Korney Korneevich and XOR (easy version)
思路分析:
- 考虑dp,dp[i]表示得到i这个数的子序列最后一位(递增子序列)的最小值,具体细节看代码注释。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int dp[1001];
int a[maxn];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
memset(dp, 0x3f3f3f3f, sizeof(dp));
//先让dp[i]默认为最大
dp[0] = 0;
//dp[0] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1000; j >= 0; j--)
{
//更新一下子序列最后一位的最小值
if (dp[j] < a[i])
{
dp[j ^ a[i]] = min(dp[j ^ a[i]], a[i]);
}
//只要子序列最后一位的最小值小于当前这个数就可以状态转移,因为保证子序列是递增的。
}
}
int cnt = 0;
for (int i = 0; i <= 1000; i++)
{
if (dp[i] != 0x3f3f3f3f)
cnt++;
}
cout << cnt << endl;
for (int i = 0; i <= 1000; i++)
{
if (dp[i] != 0x3f3f3f3f)
cout << i << ' ';
}
cout << endl;
return 0;
}
F2. Korney Korneevich and XOR (hard version)
思路分析:
- 如果这题和上题一样的做法的话会T掉,所以要想一下另外一个算法。
- 这里的dp值其实就是得到的异或值中的子序列中最后一个值的最先出现的位置。
- 如果能加入到这个子序列的话,那么这个数必须要是在这个子序列中最后一个值出现的位置之后而且值小于它。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 13;
int f[maxn];
const int N = 1e6 + 7;
int main()
{
int n;
cin >> n;
vector<int> a(n + 1), g[5005];
for (int i = 1; i <= n; i++)
{
cin >> a[i];
g[a[i]].emplace_back(i);
}
for (int i = 1; i < maxn; i++)
{
f[i] = N;
}
for (int i = 1; i <= 5000; i++)
{
for (int j = 0; j < maxn; j++)
{
auto pos = upper_bound(g[i].begin(), g[i].end(), f[j]);
if (pos != g[i].end())
{
f[i ^ j] = min((*pos), f[i ^ j]);
}
}
}
vector<int> ans;
for (int i = 0; i < maxn; ++i)
if (f[i] != N)
ans.emplace_back(i);
cout << ans.size() << '\n';
for (auto i : ans)
cout << i << ' ';
return 0;
}
Codeforces Round #750 (Div. 2)的更多相关文章
- Codeforces Round #750 (Div. 2) E. Pchelyonok and Segments
传送门 题目大意: 给一个序列,可以在这个序列中从左至右选若干个段,第i段的长度为i,对于任意的段i,段内元素和S[i]<S[i+1],求在该序列中最多可以选出几段. 思路:设dp[i][j]为 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
随机推荐
- AWS扩容EC2实例根空间
文章原文 aws 端操作 先在EC2 实例中选中磁盘 然后打开跟设备 修改大小后保存 ec2 端操作 lsblk 查看当前设备的磁盘编号 df -T -H 查看扩容前的空间大小并确定磁盘格式 grow ...
- Python3正则表达式学习笔记
学习前准备:导入re模块 import re 一.re的核心函数 1 - re.compile(pattern[, flags]) 编译正则表达式,速度快 2 - re.match(pattern, ...
- 区间DP的瞎扯淡
写在前面连个引言都不加就直接开1. 区间DP状态常见模板: f[i][j]常常表示第i个到第j个这个区间内达到题目要求,所需要的最小值(最大值) 如: 1. [石子合并](https://www.lu ...
- VS Code 搭建stm32开发环境
MCU免费开发环境 一般芯片厂家会提供各种开发IDE方案,通常其中就包括其自家的集成IDE,如: 意法半导体 STM32CubeIDE NXP Codewarrior TI CCS 另外也可以用ecl ...
- Linux常用命令 - more命令详解
21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 每次显 ...
- 常见shell脚本测试题 for/while语句
1.计算从1到100所有整数的和2.提示用户输入一个小于100的整数,并计算从1到该数之间所有整数的和3.求从1到100所有整数的偶数和.奇数和4.执行脚本输入用户名,若该用户存在,输出提示该用户已存 ...
- golang isPowerOfTwo判断是否是2的幂
iota.go strconv包 func isPowerOfTwo(x int) bool { return x & (x -1) } 了解n&(n-1)的作用如下: n& ...
- 图论---DFS
图论---DFS 1. 图的遍历 在理解DFS算法之前,我们首先需要对什么是遍历进行了解,遍历的概念就是:从某一个点出发(一般是首或尾),依次将数据结构中的每一个数据访问且只访问一遍. 2. DFS简 ...
- python语言介绍及安装
Python语言简介 Python是什么语言 Python是一种解释型的.可移植的.开源的脚本. 什么是计算机编程 计算机程序:为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合 如何 ...
- Django学习day02随堂笔记
每日测验 """ 今日考题 1.谈谈你对web框架的认识,简述web框架请求流程 2.python三大主流web框架的区别 3.安装django需要注意的事项有哪些(最少 ...