Codeforces 1272 A-E
Codeforces 1272 A-E
A Three Friends
直接枚举所有情况,共\(3\times 3\times 3=27\)种。
code
#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
int q;
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>q;
while(q--){
int a,b,c;
cin>>a>>b>>c;
int ans=abs(a-b)+abs(b-c)+abs(a-c);
for(int i=-1;i<=1;i++){
for(int j=-1;j<=1;j++){
for(int k=-1;k<=1;k++){
int x=a+i,y=b+j,z=c+k;
ans=min(ans,abs(x-y)+abs(y-z)+abs(x-z));
}
}
}
cout<<ans<<endl;
}
return 0;
}
B Snow Walking Robot
因为是无限大的网格,所以最简单的构造方法是走一个矩形框
注意要特判向任意方向走一步再走回起点的情况,样例中有。
code
#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
int q;
char s[maxn];
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>q;
while(q--){
cin>>s+1;
int n=strlen(s+1);
int L=0,R=0,U=0,D=0;
for(int i=1;i<=n;i++){
if(s[i]=='L') ++L;
else if(s[i]=='R') ++R;
else if(s[i]=='U') ++U;
else ++D;
}
L=R=min(L,R);
U=D=min(U,D);
if(L&&U==0){
cout<<2<<endl;
cout<<"RL"<<endl;
}else if(U&&L==0){
cout<<2<<endl;
cout<<"DU"<<endl;
}else if(U==0||L==0){
cout<<0<<endl;
}else{
cout<<L*2+U*2<<endl;
for(int i=1;i<=L;i++){
cout<<'R';
}
for(int i=1;i<=D;i++){
cout<<'D';
}
for(int i=1;i<=L;i++){
cout<<'L';
}
for(int i=1;i<=D;i++){
cout<<'U';
}
cout<<endl;
}
}
return 0;
}
C Yet Another Broken Keyboard
在字符串s中所有不能打出的字母的位置作为隔板,隔出来的每个子串(设\(len\)为子串的长度)对答案的贡献即为\(len \times (len+1) /2\)。
code
#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e5+10;
int n,k;
char s[maxn],t[30];
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>n>>k;
cin>>s+1;
for(int i=1;i<=k;i++){
char c;
cin>>c;
t[c-'a']=1;
}
ll ans=0,cnt=0;
for(int i=1;i<=n;i++){
if(!t[s[i]-'a']){
ans+=cnt*(cnt+1)/2;
cnt=0;
}else ++cnt;
}
ans+=cnt*(cnt+1)/2;
cout<<ans<<endl;
return 0;
}
D Remove One Element
\(O(n)\)预处理出\(l[i]\)为\(i\)作为终点的最长严格递增子段,\(r[i]\)为\(i\)作为起始点的最长严格递增子段。
- 不删除一个元素,\(ans=max\{l[i]\},(1<=i<=n)\)。
- 删除一个元素,\(ans=max\{l[i-1]+r[i+1]\},(1<i<n\&\&a[i-1]<a[i+1])\)
code
#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e5+10;
int n;
int a[maxn];
int l[maxn],r[maxn];
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>n;
int ans=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>a[i-1]) l[i]=l[i-1]+1;
else l[i]=1;
ans=max(ans,l[i]);
}
r[n]=1;
for(int i=n-1;i>=1;i--){
if(a[i]<a[i+1]) r[i]=r[i+1]+1;
else r[i]=1;
}
for(int i=2;i<n;i++){
if(a[i+1]>a[i-1]) ans=max(ans,l[i-1]+r[i+1]);
}
cout<<ans<<endl;
return 0;
}
E Nearest Opposite Parity
能发现若\(d[i+-a[i]]\)的值已确定,那么\(d[i]=min(d[i],d[i+-a[i]]+1)\)。
跟据这个性质我们以反边建图,所有边的边权都为1,以所有能一步到达奇偶性不同的点的点作为源点来跑最短路就行了(因为所有边权都为1,所以直接bfs也行)。
code
#include<bits/stdc++.h>
#define fi first
#define se second
#define lson l,mid,p<<1
#define rson mid+1,r,p<<1|1
#define pb push_back
#define ll long long
using namespace std;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=2e5+10;
int n;
int a[maxn];
vector<int>g[maxn];
int dis[maxn];
struct ppo{
int v,c;
bool operator <(const ppo &r) const{
return c>r.c;
}
};
int main(){
ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
dis[i]=inf;
if(i-a[i]>=1){
if(a[i-a[i]]%2!=a[i]%2) dis[i]=1;
g[i-a[i]].pb(i);
}
if(i+a[i]<=n){
if(a[i+a[i]]%2!=a[i]%2) dis[i]=1;
g[i+a[i]].pb(i);
}
}
priority_queue<ppo>q;
for(int i=1;i<=n;i++){
if(dis[i]==1) q.push(ppo{i,dis[i]});
}
while(!q.empty()){
int u=q.top().v;q.pop();
for(int x:g[u]){
if(dis[u]+1<dis[x]){
dis[x]=dis[u]+1;
q.push(ppo{x,dis[x]});
}
}
}
for(int i=1;i<=n;i++){
if(dis[i]==inf) cout<<-1<<" ";
else cout<<dis[i]<<" ";
}
return 0;
}
Codeforces 1272 A-E的更多相关文章
- Codeforces Round #605 (Div. 3) E - Nearest Opposite Parity
题目链接:http://codeforces.com/contest/1272/problem/E 题意:给定n,给定n个数a[i],对每个数输出d[i]. 对于每个i,可以移动到i+a[i]和i-a ...
- [CodeForces - 1272D] Remove One Element 【线性dp】
[CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...
- 【cf比赛记录】Codeforces Round #605 (Div. 3)
比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...
- Codeforces Round #605 (Div. 3)
地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) D. Remove One Element(DP)
链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...
- Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard
链接: https://codeforces.com/contest/1272/problem/C 题意: Recently, Norge found a string s=s1s2-sn consi ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- Codeforces Round #605 (Div. 3) A. Three Friends(贪心)
链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...
随机推荐
- javaSE面试题总结
目 录 第一章 初识Java 1 1. Java跨平台原理(字节码文件.虚拟机) 1 2. Java的安全性 1 3. Java三大版本 2 4. Java开发运行过程 2 5. Java开发环境 ...
- asp.net core-2.在vs2017中创建asp.net core应用程序
今天我们用vs2017创建一个asp.net core 的应用程序,打开vs2017 点击:文件—>项目,选择asp.net core web 应用程序 点击确定 红框内就昨天用控制台去创建的应 ...
- (转)从0移植uboot(六) _实现网络功能
ref:https://www.cnblogs.com/xiaojiang1025/p/6500532.html 为uboot添加网卡功能可以让uboot通过tftp下载内核, 方便我们的开发, 对于 ...
- 一、eureka服务端自动配置
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 @EnableEurekaServer开关 eureka是一个c/s架构的服务治理框架, ...
- python打印表格式数据-星号或注释
python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...
- form表单详解
form表单 form是一个复杂的系统标签,其内部又可包含很多的一些输入标签 例如input 输入文本标签 checkbox 多选标签等等 form表单有几个属性我们需要注意一下 1:action属 ...
- 【Zookeeper】 在Java中的操作
一.基本功能演示 1.1 Maven依赖信息 1.2 代码演示 方法说明 1.3 创建Zookeeper节点信息 二.Watcher 2.1 什么是Watcher接口 2.2 Watcher代码 一. ...
- sklearn特征工程
目录 一. 特征工程是什么? 2 ①特征使用方案 3 ②特征获取方案 4 ③特征处理 4 1. 特征清洗 4 2. 数据预处理 4 3. 特 ...
- Linux 01 Liunx系统简介
Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位 ...
- 机器学习(十)—聚类算法(KNN、Kmeans、密度聚类、层次聚类)
聚类算法 任务:将数据集中的样本划分成若干个通常不相交的子集,对特征空间的一种划分. 性能度量:类内相似度高,类间相似度低.两大类:1.有参考标签,外部指标:2.无参照,内部指标. 距离计算:非负性, ...