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的更多相关文章

  1. 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 ...

  2. [CodeForces - 1272D] Remove One Element 【线性dp】

    [CodeForces - 1272D] Remove One Element [线性dp] 标签:题解 codeforces题解 dp 线性dp 题目描述 Time limit 2000 ms Me ...

  3. 【cf比赛记录】Codeforces Round #605 (Div. 3)

    比赛传送门 Div3真的是暴力杯,比div2还暴力吧(这不是明摆的嘛),所以对我这种一根筋的挺麻烦的,比如A题就自己没转过头来浪费了很久,后来才醒悟过来了.然后这次竟然还上分了...... A题:爆搜 ...

  4. Codeforces Round #605 (Div. 3)

    地址:http://codeforces.com/contest/1272 A. Three Friends 仔细读题能够发现|a-b| + |a-c| + |b-c| = |R-L|*2 (其中L ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)

    链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...

  9. Codeforces Round #605 (Div. 3) A. Three Friends(贪心)

    链接: https://codeforces.com/contest/1272/problem/A 题意: outputstandard output Three friends are going ...

随机推荐

  1. JS 06 bom 框窗_页面_定时任务

    BOM(Broswer Object Model) 凡是 window 的属性和方法,均可以省略“window.” 方法: 框窗 1.警告框 window.alert("msg") ...

  2. 数据仓库之抽取数据:通过bcp命令行导入数据

    原文:数据仓库之抽取数据:通过bcp命令行导入数据 在做数据仓库时,最重要的就是ETL的开发,而在ETL开发中的第一步,就是要从原OLTP系统中抽取数据到过渡区中,再对这个过渡区中的数据进行转换,最后 ...

  3. CentOS 系统 MySQL 5.7 开启远程连接

    CentOS 系统安装好 MySQL 后,默认情况下不支持用户通过非本机连接上数据库服务器,下面是解决方法: 1.在控制台执行 mysql -u root -p 系统提示输入数据库 root 用户的密 ...

  4. Word文档转PDF方法探索

    最近的项目中需要将Word转换为PDF文件,找了很多方法和组件,最后找到了一些方法,和大家分享. 一.使用微软官方自带转换方法 好处是写法方便,官方支持,缺点是需要在服务器上安装office,而且要配 ...

  5. Lua table直接索引VS缓存索引性能测试小示例

    local p = {} p.t = {} p.t.p = {} p.t.p.t = {} p.t.p.t.p = {} p.t.p.t.p.t = {} p.t.p.t.p.t.p = {} p.t ...

  6. sql 给相同属性的数据排序

    UPDATE b SET OrderIndex = a.OrderIndex FROM ( SELECT RTRIM(ROW_NUMBER() OVER ( PARTITION BY [ItemID] ...

  7. c#创建目录和文件夹,数据写入并生成txt文件

    c#创建目录: // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径.System.Diagnostics.Pro ...

  8. CLR ATL

    前段时间,帮人改了个项目,里面明明感觉是MFC,但是却调用C#的类函数,用的都是gcnew指针,凭借着对C#的熟悉,一点一点的实验,终于帮人把程序改好了,但是却不知道到底是什么东西,C#和MFC的混合 ...

  9. Vue 一些用法

    v-model : 数据绑定(多数用于表单元素) ps:同时v-model支持双向数据绑定v-for : 用于元素遍历v-on:事件名称=“方法名” (事件绑定)ps: methods:用于绑定 v- ...

  10. NLP传统基础(2)---LDA主题模型---学习文档主题的概率分布(文本分类/聚类)

    一.简介 https://cloud.tencent.com/developer/article/1058777 1.LDA是一种主题模型 作用:可以将每篇文档的主题以概率分布的形式给出[给定一篇文档 ...