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 ...
随机推荐
- 【RandomString】- 随机字符串
RandomString 随机字符串的用法
- 简单的图片滑动效果插件 jQuery.iocnSlider.js
近几日在制作一个客户引导页面,其中有一个图片展示而且带滑动的效果.好久没练手了,索性自己写一个插件吧. 依据设计原型,需要满足两套分辨率下图片不同的尺寸,所以在css中使用了media query的相 ...
- Django创建App报错
在django下创建APP项目时遇到的坑 python manage.py startapp app01 报错内容如下: 解决:找到报错中的文件夹151行删除items(),)中的逗号即可 在命令行下 ...
- LeetCode 104——二叉树中的最大深度
1. 题目 2. 解答 如果根节点为空,直接返回 0.如果根节点非空,递归得到其左右子树的深度,树的深度就为左右子树深度的最大值加 1. /** * Definition for a binary t ...
- springmvc项目,浏览器报404错误的问题
问题描述: 建立了web工程,配置pom.xml,web.xml,编写controller类,在spring-mvc-servlet.xml文件中指定开启注解和扫描的包位置<mvc:annota ...
- selenium识别登录验证码---基于python实现
本文主要是通过PIL+pytesseract+Tesseract-OCR实现验证码的识别 其中PIL为Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PI ...
- POJ 1873 The Fortified Forest(枚举+凸包)
Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...
- Rescue(BFS时间最短 另开数组或优先队列)
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...
- android入门 — ListView的优化
ListView的运行效率是比较低的,因为在getView()中每次都会将整个布局重新加载一遍,当ListView快速滚动的时候就会成为性能瓶颈. 调用View中的findViewById()方法获取 ...
- 创建、编译、执行 java程序
java源文件(.java)——Java字节码文件(.class)——在java虚拟机上执行 其他语言很多是编译后执行,所以无法跨平台