老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution
对没错下面的代码全部是python 3(除了E的那个multiset)
题目链接:https://codeforces.com/contest/1157
A. Reachable Numbers
按位算贡献,一位数的贡献直接算即可
n=int(input())
ans=0
while (n>=10):
tmp=n%10
tmp=10-tmp
ans+=tmp
n+=tmp
while (n>0) and (n%10==0):
n//=10
ans+=9
print(ans)
B. Long Number
贪心,肯定是优先变最高位
import sys
import os
n=int(input())
a=input()
f=list(map(int,input().split()))
#print(f)
ans=""
pos=0
while (pos<n):
now=ord(a[pos])-48
if (a[pos]>=chr(f[now-1]+48)):
ans=ans+a[pos]
pos+=1
else:
while (pos<n) and (a[pos]<=chr(f[now-1]+48)):
ans=ans+chr(f[now-1]+48)
pos+=1
if (pos<n) :
now=ord(a[pos])-48
print(pos)
for i in range (pos,n):
ans=ans+a[i]
pos=n
break
print(ans)
C1&C2 Increasing Subsequence
建立两个指针\(l\)和\(r\),同时记录上一次的答案,暴力扫描即可
注意\(a_l==a_r\)时很明显决策是只会从一端开始取数,直接做完
n=int(input())
ans=[]
a=list(map(int,input().split()))
l=-0
r=n-1
now=0
while l<=r:
#print(l,r,now,a[l],a[r])
if a[l]<a[r] and a[l]>now:
now=a[l]
l+=1
ans.append('L')
elif a[r]<a[l] and a[r]>now:
now=a[r]
r-=1
ans.append('R')
elif a[r]<a[l] and a[l]>now:
now=a[l]
l+=1
ans.append('L')
elif a[l]<a[r] and a[r]>now:
now=a[r]
r-=1
ans.append('R')
elif a[l]==a[r]:
#print(l,r,now)
if a[l]<=now:
break
nowl=now
nowr=now
pos=l
cnt1=0
cnt2=0
while pos<=r and a[pos]>nowl:
nowl=a[pos]
pos+=1
cnt1+=1
pos=r
while l<=pos and a[pos]>nowr:
nowr=a[pos]
pos-=1
cnt2+=1
if (cnt1>cnt2):
for i in range(0,cnt1):
ans.append('L')
else:
for i in range(0,cnt2):
ans.append('R')
break
else:
break
print(len(ans))
for i in ans:
print(i,end="")
D.N Problems During K Days
人民群众喜闻乐见的构造题
先把这个序列设定为\([x,x+1,\cdots,x+k-1]\)之后再进行调整
对于每一个位置\(i\)考虑在\([i,n]\)中所有的数都加上某个相同的数,这个可以直接得到,同时要和\(a_{i-1}*2-a_i\)取\(min\)
最后看\(n\)是否还有剩余
import sys
n,k=map(int,input().split())
a=[0]*(k+2)
if k*(k+1)>n*2:
print("NO")
sys.exit()
for i in range(1,k+1):
a[i]=i
n-=k*(k+1)//2
rest=n//k
n-=rest*k
a[1]+=rest
for i in range(2,k+1):
a[i]=a[i-1]+1
rest=n//(k-i+1)
tmp=min(rest,a[i-1]*2-a[i])
a[i]+=tmp
n-=(k-i+1)*tmp
if n>0:
print("NO")
sys.exit()
print("YES")
for i in range(1,k+1):
print(a[i],end=" ")
E. Minimum Array
简单题
对于\(a_i\),你期望找到一个\(b_i\),使得\(a_i+b_i=n\),如果找不到,那你就希望找一个大于\(b_i\)的又和\(b_i\)最接近的
这不是lower_bound这是什么
把\(b\)丢到multiset中,每次对\(a_i\)lower_bound一下
当然万一找不到的话还是要找最小的\(b_i\),比较一下即可
使用c++实现
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define maxd 1000000007
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
int n,a[200200],b[200200];
multiset<int> s;
int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
int main()
{
n=read();
rep(i,1,n) a[i]=read();
rep(i,1,n) {b[i]=read();s.insert(b[i]);}
multiset<int>::iterator it;
rep(i,1,n)
{
int tmp=(n-a[i])%n;
it=s.lower_bound(tmp);
if (it==s.end()) it--;
int st=*(s.begin()),now=*it;
if ((now+a[i])%n>(st+a[i])%n) now=st;
s.erase(s.lower_bound(now));
printf("%d ",(now+a[i])%n);
}
return 0;
}
F. Maximum Balanced Circle
又是构造题
首先你选的数一定是在某一个连续区间内的数,这样你就解决了\(|a_i-a_{i+1}|\leq 1\)
然后就是\(|a_1-a_k|\leq 1\),你可以把所有的\(a_i\)排成一个先递增后递减的形式(就像山峰一样)
然后出现次数为1的数要么出现在山脚(\(a_1\)),要么出现在山顶(\(max\{a_i\}\))
然后就做完啦
n=int(input())
a=list(map(int,input().split()))
cnt=[0]*(200005)
for i in range(0,n):
cnt[a[i]]+=1
ans=0
ansl=0
ansr=0
l=1
N=200001
while (l<=N):
if cnt[l]==0:
l+=1
else:
r=l
now=0
while (r<=N):
if cnt[r]==0:
r-=1
break
elif cnt[r]==1 and l!=r:
now+=cnt[r]
break
else:
now+=cnt[r]
r+=1
if now>ans:
ansl=l
ansr=r
ans=now
#print(l,r,now)
if l==r:
l=r+1
else:
l=r
seq=[]
for i in range(ansl,ansr+1):
seq.append(i)
cnt[i]-=1
for i in range(ansr,ansl-1,-1):
while cnt[i]>0:
seq.append(i)
cnt[i]-=1
print(len(seq))
for i in seq:
print(i,end=" ")
G. Inverse of Rows and Columns
对没错还是构造题
考虑一个合法方案一定至少符合下列两个条件之一:
1)第一行全部为\(0\) 2)最后一行全部为\(1\)
我们枚举当前满足哪一个条件,那么根据当前行中每一个数的情况我们就确定了当前列的翻转情况
接下来我们枚举行,找到中间的断点(由0变成1的地方),以此确定行的反转情况
最后暴力取出所有书\(check\)即可
import sys
n=0
m=0
sq=[[0]*(310)]*(310)
ansx=[]
ansy=[]
tmp=[[0]*(310)]*(310)
def chk():
val=[]
for i in range(0,n):
for j in range(0,m):
val.append(tmp[i][j])
Len=len(val)
for i in range(1,Len):
if val[i-1]>val[i]:
return False
return True
n,m=map(int,input().split())
for i in range(0,n):
sq[i]=list(map(int,input().split()))
for i in range(0,n):
tmp[i]=list(sq[i])
for i in range(0,m):
if tmp[0][i]==1:
ansy.append(1)
for j in range (0,n):
tmp[j][i]=1-tmp[j][i]
else:
ansy.append(0)
op=0
for i in range(0,n):
flag0=0
flag1=0
for j in range(0,m):
if tmp[i][j]==0:
flag0=1
else:
flag1=1
if flag0==1 and flag1==1:
op=1
if tmp[i][0]==1:
ansx.append(1)
for j in range (0,m):
tmp[i][j]=1-tmp[i][j]
else:
ansx.append(0)
elif flag0==1:
if op==0:
ansx.append(0)
elif op==1:
ansx.append(1)
for j in range(0,m):
tmp[i][j]=1-tmp[i][j]
elif flag1==1:
if op==0:
ansx.append(1)
for j in range(0,m):
tmp[i][j]=1-tmp[i][j]
else:
ansx.append(0)
if chk():
print("YES")
for i in range(0,n):
print(ansx[i],end="")
print()
for i in range(0,m):
print(ansy[i],end="")
sys.exit()
for i in range(0,n):
tmp[i]=list(sq[i])
ansx=[]
ansy=[]
for i in range(0,m):
if (tmp[n-1][i]==1):
ansy.append(0)
else:
ansy.append(1)
for j in range(0,n):
tmp[j][i]=1-tmp[j][i]
op=0
for i in range(0,n):
flag0=0
flag1=0
for j in range(0,m):
if tmp[i][j]==1:
flag1=1
else:
flag0=1
if flag1==1 and flag0==1:
op=1
if tmp[i][0]==1:
ansx.append(1)
for j in range(0,m):
tmp[i][j]=1-tmp[i][j]
else:
ansx.append(0)
elif flag0==1:
if op==0:
ansx.append(0)
else:
ansx.append(1)
for j in range(0,m):
tmp[i][j]=1-tmp[i][j]
elif flag1==1:
if op==1:
ansx.append(0)
else:
ansx.append(1)
for j in range(0,m):
tmp[i][j]=1-tmp[i][j]
if chk():
print("YES")
for i in range(0,n):
print(ansx[i],end="")
print()
for i in range(0,m):
print(ansy[i],end="")
sys.exit()
print("NO")
老年OIer的Python实践记—— Codeforces Round #555 (Div. 3) solution的更多相关文章
- Codeforces Round #578 (Div. 2) Solution
Problem A Hotelier 直接模拟即可~~ 复杂度是$O(10 \times n)$ # include<bits/stdc++.h> using namespace std; ...
- Codeforces Round #555 (Div. 3) AB
A: http://codeforces.com/contest/1157/problem/A 题意:每次加到10的整数倍之后,去掉后面的0,问最多有多少种可能. #include <io ...
- Codeforces Round #545 (Div. 1) Solution
人生第一场Div. 1 结果因为想D想太久不晓得Floyd判环法.C不会拆点.E想了个奇奇怪怪的set+堆+一堆乱七八糟的标记的贼难写的做法滚粗了qwq靠手速上分qwqqq A. Skyscraper ...
- Codeforces Round 500 (Div 2) Solution
从这里开始 题目地址 瞎扯 Problem A Piles With Stones Problem B And Problem C Photo of The Sky Problem D Chemica ...
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- CodeForces Round #555 Div.3
A. Reachable Numbers 代码: #include <bits/stdc++.h> using namespace std; ; int N; set<int> ...
- Codeforces Round #555 (Div. 3) E. Minimum Array
题意:b数组可以自由排序,c[i]=(a[i]+b[i])%n. 题目中要求c数组的字典序是最小的.那么我们需要尽量满足前面的c[i],才能使字典序最小. 我们知道a[i]和b[i]都是[0,n-1] ...
- Codeforces Round #555 (Div. 3)[1157]题解
不得不说这场div3是真的出的好,算得上是从我开始打开始最有趣的一场div3.因为自己的号全都蓝了,然后就把不经常打比赛的dreagonm的号借来打这场,然后...比赛结束rank11(帮dreago ...
- Codeforces Round #525 (Div. 2) Solution
A. Ehab and another construction problem Water. #include <bits/stdc++.h> using namespace std; ...
随机推荐
- Easyui 合并单元格
onMyLoadSuccessText: function () { $(".datagrid-row").mouseover(function () { var titlestr ...
- C#连接基于Java开发IM——Openfire
Openfire简介 Openfire 是开源的.基于可拓展通讯和表示协议(XMPP).采用Java编程语言开发的实时协作服务器.Openfire的效率很高,单台服务器可支持上万并发用户. ...
- nginx系列10:通过upstream模块选择上游服务器和负载均衡策略round-robin
upstream模块的使用方法 1,使用upstream和server指令来选择上游服务器 这两个指令的语法如下图: 示例: 2,对上游服务使用keepalive长连接 负载均衡策略round-rob ...
- citrix外企,鸡鸣寺,玄武湖环跑,七牛云笔试
笔记汇总,计算机网络笔记汇总 昨天下江宁,经历了人生第一场f2f面试,外企,citrix思杰:(准备好久都英文介绍没用上sad) 全程一个半小时,最后被面试官夸是面过人中,对计算机网络理解最多的(希望 ...
- 数据库原理剖析 - 序列1 - B+树
本文节选自<软件架构设计:大型网站技术架构与业务架构融合之道>第6.3章节. 作者微信公众号: 架构之道与术.进入后,可以加入书友群,与作者和其他读者进行深入讨论.也可以在京东.天猫上购买 ...
- Tomcat设置cmd窗口的title属性
说明:官网下载tomcat之后,双击bin目录下的startup.bat文件,即可运行tomcat:linux下面运行startup.sh. 但是如果测试服务器上面搭建了多个项目,则启动之后窗口一样, ...
- vmware 14 新安装centos7 没法联网
vmware14 刚安装好centos7后,想下载安装一些软件发现无法联网,于是就百度了一下.下面 记录下解决方法. 1 确报主机能上网. 2 设置虚拟机网络适配器 3 设置虚拟机网卡 4 修改cen ...
- selenium之表格的定位
浏览器网页常常会包含各类表格,自动化测试工程师可能会经常操作表格中的行,列以及某些特定的单元格,因此熟练掌握表格的定位方法是自动化测试实施过程中必要的技能. 被测试网页的HTML代码 <!DOC ...
- 记录display:table的使用
兼容性:不兼容IE7 1.左右对齐 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 微信内无法自动跳转外部浏览器打开H5分享链接的解决办法
很多情况下我们用微信分享转发H5链接的时候,都无法在微信内打开,即使开始能打开,过一段时间就会被拦截,拦截后再打开微信会提示 “已停止访问该网址” ,那么导致这个情况的因素有哪些呢,主要有以下四点 1 ...