老年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; ...
随机推荐
- Javascript中双等号(==)隐性转换机制
在Javascript中判断相等关系有双等号(==)和三等号(===)两种.其中双等号(==)是值相等,而三等号(===)是严格相等(值及类型是否完全相等). 因此有几个常识知识: 1.对于strin ...
- 编程心法 之 怎么选择合适的IDE
一般情况下,使用IDE进行开发可以极大的提高开发效率 最佳选择 如果语言是GNU开源的则Eclipse,因为Eclipse就是开源的 例如C和C++这样的底层语言并且经典的语言,基于GNU的语言,推荐 ...
- 非常易于理解‘类'与'对象’ 间 属性 引用关系,暨《Python 中的引用和类属性的初步理解》读后感
关键字:名称,名称空间,引用,指针,指针类型的指针(即指向指针的指针) 我读完后的理解总结: 1. 我们知道,python中的变量的赋值操作,变量其实就是一个名称name,赋值就是将name引用到一个 ...
- js之制作简易红绿灯
HTML代码: 在一个div容器内,设置3个span <body> <div id="i1"> <span class="light red ...
- java 线程池 ---- newFixedThreadPool()
class MyThread implements Runnable{ private int index; public MyThread(int index){ this.index = inde ...
- spring javaconfig druidsource
package dataConfig; import java.sql.SQLException; import org.springframework.context.annotation.Bean ...
- C# ToString()日期格式
C# ToString()日期格式 ToString:2016/9/27 0:00:00ToString("yyyy/MM/dd"):2016/09/27ToString(&quo ...
- C#多线程和线程池 【转】
1.概念 1.0 线程的和进程的关系以及优缺点 windows系统是一个多线程的操作系统.一个程序至少有一个进程,一个进程至少有一个线程.进程是线程的容器,一个C#客户端程序开始于一个单独的线程,C ...
- mssql sqlserver 分组排序函数row_number、rank、dense_rank用法简介及说明
在实际的项目开发中,我们经常使用分组函数,对组内数据进行群组后,然后进行组内排序:如:1:取出一个客户一段时间内,最大订单数的行记录2: 取出一个客户一段时间内,最后一次销售记录的行记录——————— ...
- windows10安装mysql-8.0.13(zip安装)
安装环境说明 系统版本:windows10 mysql版本:mysql-8.0.13-winx64.zip 下载地址:http://mirrors.163.com/mysql/Downloads/My ...