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 ...
随机推荐
- Spring Cloud(五):Hystrix 监控面板【Finchley 版】
Spring Cloud(五):Hystrix 监控面板[Finchley 版] 发表于 2018-04-16 | 更新于 2018-05-10 | 在上一篇 Hystrix 的介绍中,我们提到 ...
- 爬虫1.6-selenium+HeadlessChrome
目录 爬虫-selenium+HeadlessChrome 1. 浏览器处理步骤 2. headless-chrome初体验 3. 实战爬取淘宝镇.街道信息 爬虫-selenium+HeadlessC ...
- LeeCode第一次刷题(两数相加)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- 【转载】Android 内存溢出如何发生的。
[转载]Android 内存溢出如何发生的. 且谈Android内存溢出 前言 关于android的内存溢出在创新文档库中也有不少,网络上也有很多这方面的资料.所以这遍文章不算是正真意义上的创新,仅仅 ...
- STM32单片机是如何启动的?
STM32单片机是如何启动的? STM32中的内存 STM32中的内存包含两块主要区域:flash memory(只读).static ram memory(SRAM,读写).其中,flash mem ...
- opencv打开视频文件出错
使用C#调用mingw的so文件,在C++端使用opencv打开视频.这样的项目完成过了一个,第二次做的时候,发现opencv打开视频文件出错. 首先怀疑是opencv的opencv_ffmpeg24 ...
- POJ 3675 Telescope(简单多边形和圆的面积交)
Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...
- Median Weight Bead(最短路—floyed传递闭包)
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- Python 循环语句和运算符
while 循环 while 条件 : //条件为True时,执行while下带有缩进的语句 语句1 语句2 语句3 for循环 for循环可以用来遍历某一对象(遍历:通俗点说,就是把这个循环中的第一 ...
- 模拟Excel同一列相同值的单元格合并
背景 项目中有一个查询工作量,可以将查询的结果导出到Excel表中.在Excel工具中,有一个合并居中功能,可以将选中的单元格合并成一个大的单元格.现在需要在程序中直接实现查询结果的汇总, 问题分析 ...