Codeforces Round #539 (Div. 2) 题解
Codeforces Round #539 (Div. 2)
题目链接:https://codeforces.com/contest/1113
A. Sasha and His Trip
题意:
n个城市,城市分布在一条直线上且按升序排序,现在有个人开车从一号城市出发,车的油箱容量为v。
在每个城市都可以买油,但价格不一样:第i个城市买1单位的油花费i元。问最终从1到n花费的最少为多少。
题解:
贪心即可,尽量在前面的城市买油,最后一鼓作气到n号城市。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n,v;
int main(){
cin>>n>>v;
int fir=,las=n-v;
int ans=v;
if(n-<=v){
cout<<n-;
return ;
}
for(int i=;i<=las;i++){
ans+=i;
}
cout<<ans;
return ;
}
B. Sasha and Magnetic Machines
题意:
给出n个数,现在选定i,j,然后取x满足aj%x==0,之后让aj=aj/x,ai=ai*x。这个操作最多一次,问最后所有数的和最小为多少。
题解:
由于这里ai<=100,并且n也最多1e4,所以可以直接考虑暴力。但这里暴力的是,取到一个x的时候,应该始终就是让最小的那个ai来乘x,这应该是显而易见的。
具体见代码吧:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+;
int a[N];
int n;
int main(){
scanf("%d",&n);
int sum = ;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
int ans = sum;
int mn = *min_element(a+,a+n+);
sum-=mn;
for(int i=;i<=n;i++){
for(int j=;j<=a[i];j++){
if(a[i]%j!=) continue ;
ans=min(ans,sum-a[i]+a[i]/j+mn*j);
}
}
cout<<ans;
return ;
}
C. Sasha and a Bit of Relax
题意:
给出n个数,然后让你找出满足条件的区间个数:区间长度为偶数且区间左部分的异或和等于右部分的异或和。
题解:
根据题中条件可以知道,那整个区间的异或和是为0的,然后根据异或前缀和进一步可知:假设那个区间为[l,r],那么prel-1=prer,这里prei为1~i的异或前缀和。并且还可以观察到,这里的l-1以及r是同奇偶的。
然后做法也大致清晰了,找出在奇偶位置异或前缀和为x的个数,假设我们目前只看奇数位置,有n个,那么这时对答案的贡献为C(n,2),也即是n*(n-1)/2;偶数也同样。
只是需要注意的是,当前缀异或和为0并且当前为偶数位置时,这也算一种情况。
具体代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[][<<];
int main(){
int n;
cin>>n;
vector <ll> a(n+);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
ll sum=,ans=;
cnt[][]=;
for(int i=;i<=n;i++){
sum^=a[i];
ans+=cnt[i%][sum];
cnt[i%][sum]++;
}
cout<<ans;
return ;
}
D. Sasha and One More Name
题意:
给出一个回文串,然后让你用最少的次数分割为几个部分,之后可以拼接出一个和原来不同的回文串。如果不能就输出"Impossible",否则输出最少次数。
题解:
不能的情况很好判断。
在判断可行性过后,可以证明次数最多只需要两次,考虑从两边同时往中间走的切法就行了。
也就是说,我们只需要判断只切一次是否可行。由于字符串的长度只有5000,O(n^2)暴力即可。
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = ;
string s;
int l;
bool is(string ss){
int f=;
for(int i=;i<l;i++){
int j=l-i-;
if(ss[i]!=ss[j]) f=;
}
return f&&ss!=s;
}
int main(){
cin>>s;
l=s.length();
int f=;
for(int i=;i<l;i++){
if(l& && i==l/) continue ;
if(s[i]!=s[]) f=;
}
if(f){
puts("Impossible");
return ;
}
f=;
for(int i=;i<l;i++){
string t1=s.substr(,i),t2=s.substr(i,l);
t2+=t1;
if(is(t2))f=;
}
if(f) cout<<<<endl;
else cout<<<<endl;
return ;
}
Codeforces Round #539 (Div. 2) 题解的更多相关文章
- Codeforces Round #539 (Div. 2) - D. Sasha and One More Name(思维)
Problem Codeforces Round #539 (Div. 2) - D. Sasha and One More Name Time Limit: 1000 mSec Problem ...
- Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)
Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #539 (Div. 2)
Codeforces Round #539 (Div. 2) A - Sasha and His Trip #include<bits/stdc++.h> #include<iost ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
随机推荐
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
- django request bug
bug描述:django请求request接收数据时,如果参数中包含分号时,会导致分号后面的消息丢失. 比如前台js调用代码 $.post('/get_params', { "A" ...
- 【python】scrapy相关
目前scrapy还不支持python3,python2.7与python3.5共存时安装scrapy后,执行scrapy后报错 Traceback (most recent call last): F ...
- JavaScript初探系列之Ajax应用
一 什么是Ajax Ajax是(Asynchronous JavaScript And XML)是异步的JavaScript和xml.也就是异步请求更新技术.Ajax是一种对现有技术的一种新的应用,不 ...
- <Effective C++>读书摘要--Resource Management<一>
1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphi ...
- 百度安卓sdk开发
一 key问题 1 在百度地图api控制台申请key的流程主要用到了app包,开发工具的开发sha1和发布sha1值,这2个值的获取就非常关键了. 一般来说我们都是在windows上开发安卓,使用an ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺-1
各个成员在 Alpha 阶段认领的任务 成员 Alpha 阶段认领的任务 肖世松 编写界面设计代码 杨泽斌 服务器连接与配置 叶文柠 数据库连接与配置 谢庆圆 编写功能板块代码 林伟航 编写功能板块代 ...
- 理解BitSet
先来看几道面试题: 1.统计40亿个数据中没有出现的数据,将40亿个不同数据进行排序. 2.现在有1千万个随机数,随机数的范围在1到1亿之间,要求写出一种算法,将1到1亿之间没有在随机数中的数求出来. ...
- 【Linux】- CentOS查看IP
1.查询命令: ip addr 显示如图: 可以看到ens33没有inet这个属性,那么就没办法通过IP远程连接. 2.设置配置文件: vi /etc/sysconfig/network-script ...
- 内存交换空间(swap)的构建
一.使用物理分区构建swap 1.先进行分区的行为. [root@iZ255cppmtxZ ~]# fdisk /dev/xvdb Welcome to fdisk (util-linux ). Ch ...