Codeforces Round #383 _python作死系列
A. Arpa’s hard exam and Mehrdad’s naive cheat
題意求1378的n次方的最后一位,懶的寫循環(huán)節(jié) 瞎快速冪
py3 int和LL 合并為int了
1 def q_(x): 2 a = 8 3 ans = 1 4 while(x>0): 5 if(x&1): 6 ans = ans*a%10 7 a = a*a%10 8 x = x//2 9 print(ans%10) 10 11 c =int(input()) 12 q_(c) View Code?
B. Arpa’s obvious problem and Mehrdad’s terrible solution
ZZ題? 讀題浪費了很長時間
C. Arpa's loud Owf and Mehrdad's evil plan
找環(huán)? 不合法的情況肯定是度為單數(shù)的點 然后 DFS就ok? 偶數(shù)/2 求lcm
1 def gcd(x,y): 2 return x if y==0 else gcd(y,x%y) 3 def lcm(x,y): 4 return x//gcd(x,y)*y 5 def dfs(x,y): 6 if(vis[x]): 7 return y 8 vis[x] = 1 9 return dfs(c[x-1],y+1) 10 n =int(input()) 11 c = [] 12 for x in input().split(): 13 c.append(int(x)) 14 vis = [0]*(n+1) 15 ind = [0]*(n+1) 16 for i in range(n): 17 ind[i+1]+=1 18 ind[c[i]]+=1 19 mk = 1 20 for i in range(n): 21 if(ind[i+1]&1): 22 mk = 0 23 ans = 1 24 if(mk): 25 for i in range(n): 26 if(vis[i+1]==0): 27 val = dfs(i+1,0) 28 if(val%2==0): 29 val//=2 30 ans = lcm(ans,val) 31 print(ans) 32 else: 33 print("-1"); View Code?
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
約妹紙,要么約集合,要么約集合中的一個(可用list優(yōu)化) 并查集加01背包
但是問題來了...這個題做了整整五個小時沒A...................? TLE到死???????? python的多重循環(huán)比C++慢百倍....怪不得DE這種題一般沒python的出現(xiàn)....?
優(yōu)化了range? 繼續(xù)TLE? 直到現(xiàn)在? 我放棄了?
雖然紅了兩頁? 但是學(xué)到了不少....蠻阿Q的...
n, m, w = map(int, input().split()) W = [] B = [] W = list(map(int, input().split())) B = list(map(int, input().split())) par = [0]*(n+10) for i in range(0, n):par[i] = i def find(x):if(x == par[x]):return xelse:par[x] = find(par[x])return par[x] def unite(x, y):x = find(x)y = find(y)if(x != y):par[x] = y for i in range(m):x, y = map(int, input().split())unite(x-1, y-1) dp = [0]*(w+10) q = [0]*(n+10) for i in range(n):if(i==find(i)):cnt = 0V = 0E = 0for k in range(n):if(find(k)==find(i)):q[cnt] = kcnt += 1V += W[k]E += B[k]for j in range(w,-1,-1):if(j>=V):dp[j] = max(dp[j],dp[j-V]+E)for k in range(cnt):if(j>=W[q[k]]):dp[j] = max(dp[j],dp[j-W[q[k]]]+B[q[k]]) print(dp[w]) 理論AC代碼后來去CF上拿了份代碼? 把python翻譯成C++?? 就AC了
#include<cstdio> #include<algorithm> using namespace std; int n,m,w,cnt; int fa[1100],a[1100],b[1100],f[1100],q[1100]; int find(int x) {if(!fa[x]) return x;fa[x]=find(fa[x]);return fa[x]; } int main() {int i,r1,r2,j,k;scanf("%d%d%d",&n,&m,&w);for(i=1;i<=n;i++) scanf("%d",&a[i]);for(i=1;i<=n;i++) scanf("%d",&b[i]);for(i=1;i<=m;i++){scanf("%d%d",&r1,&r2);j=find(r1); k=find(r2);if(j!=k)fa[k]=j;}for(i=1;i<=n;i++) if(fa[i]==0){cnt=0; int ww=0,bb=0;for(j=1;j<=n;j++) if(find(j)==i) {q[++cnt]=j; ww+=a[j]; bb+=b[j];}for(j=w;j>=0;j--) {//f[i][j]=f[i-1][j];for(k=1;k<=cnt;k++) if(j>=a[q[k]]) f[j]=max(f[j],f[j-a[q[k]]]+b[q[k]]);if(j>=ww) f[j]=max(f[j],f[j-ww]+bb);}} printf("%d",f[w]);return 0; } AC代碼待補
E.
F.
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/Geek-xiyang/p/6145092.html
總結(jié)
以上是生活随笔為你收集整理的Codeforces Round #383 _python作死系列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本充电显示未充电怎么办啊 笔记本电脑
- 下一篇: 联想做系统怎么启动u盘安装系统 联想电脑