Codeforces Round #383 _python作死系列
A. Arpa’s hard exam and Mehrdad’s naive cheat
题意求1378的n次方的最后一位,懒的写循环节 瞎快速幂
py3 int和LL 合并为int了
def q_(x):
a = 8
ans = 1
while(x>0):
if(x&1):
ans = ans*a%10
a = a*a%10
x = x//2
print(ans%10) c =int(input())
q_(c)
B. Arpa’s obvious problem and Mehrdad’s terrible solution
ZZ题 读题浪费了很长时间
C. Arpa's loud Owf and Mehrdad's evil plan
找环 不合法的情况肯定是度为单数的点 然后 DFS就ok 偶数/2 求lcm
def gcd(x,y):
return x if y==0 else gcd(y,x%y)
def lcm(x,y):
return x//gcd(x,y)*y
def dfs(x,y):
if(vis[x]):
return y
vis[x] = 1
return dfs(c[x-1],y+1)
n =int(input())
c = []
for x in input().split():
c.append(int(x))
vis = [0]*(n+1)
ind = [0]*(n+1)
for i in range(n):
ind[i+1]+=1
ind[c[i]]+=1
mk = 1
for i in range(n):
if(ind[i+1]&1):
mk = 0
ans = 1
if(mk):
for i in range(n):
if(vis[i+1]==0):
val = dfs(i+1,0)
if(val%2==0):
val//=2
ans = lcm(ans,val)
print(ans)
else:
print("-1");
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
约妹纸,要么约集合,要么约集合中的一个(可用list优化) 并查集加01背包
但是问题来了...这个题做了整整五个小时没A................... TLE到死 python的多重循环比C++慢百倍....怪不得DE这种题一般没python的出现....
优化了range 继续TLE 直到现在 我放弃了
虽然红了两页 但是学到了不少....蛮阿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 x
else:
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 = 0
V = 0
E = 0
for k in range(n):
if(find(k)==find(i)):
q[cnt] = k
cnt += 1
V += 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[],a[],b[],f[],q[];
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=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n;i++) scanf("%d",&b[i]);
for(i=;i<=m;i++)
{
scanf("%d%d",&r1,&r2);
j=find(r1); k=find(r2);
if(j!=k)fa[k]=j;
}
for(i=;i<=n;i++)
if(fa[i]==)
{
cnt=; int ww=,bb=;
for(j=;j<=n;j++) if(find(j)==i)
{
q[++cnt]=j; ww+=a[j]; bb+=b[j];
}
for(j=w;j>=;j--)
{
//f[i][j]=f[i-1][j];
for(k=;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 ;
}
AC代码
待补
E.
F.
Codeforces Round #383 _python作死系列的更多相关文章
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...
- 01背包dp+并查集 Codeforces Round #383 (Div. 2)
http://codeforces.com/contest/742/problem/D 题目大意:有n个人,每个人有重量wi和魅力值bi.然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和 ...
- Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)
题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...
随机推荐
- Alpha版本——Postmortem会议
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 设想和目标 1.我们的软件要解决什么 ...
- SpringMVC学习系列(6) 之 数据验证
在系列(4).(5)中我们展示了如何绑定数据,绑定完数据之后如何确保我们得到的数据的正确性?这就是我们本篇要说的内容 —> 数据验证. 这里我们采用Hibernate-validator来进行验 ...
- MSClass 和setInterval 的并发,ajax定时有采集信息滚动显示
setTimeout 用于延时器,只执行一次. setInterval:用于多次执行. //****************************************** 项目中引用到jquer ...
- bzoj1513: [POI2006]Tet-Tetris 3D
Description Task: Tetris 3D "Tetris" 游戏的作者决定做一个新的游戏, 一个三维的版本, 在里面很多立方体落在平面板,一个立方体开始落下直到碰上一 ...
- 在Linux中永久设置Anaconda环境变量的方法
[感谢:http://www.codesec.net/view/459539.html] 如果在安装Anaconda的过程中没有将安装路径添加到系统环境变量中,需要在安装后手工添加: 1.在终端输入$ ...
- Zerojudge解题心得
我进入娄山中学已经有1年多了,也就是说我学习编程也有1年多了,在这一年多的时间中,我已经对编程有了初步的了解.其实只要抓住平时的空闲时间加以利用,哪怕每个星期就做那么三四题,经过了一段时间沉淀,也会有 ...
- (String) 压缩String
e.g. aaabbcccc 返回a3b2c4 public static String compressString(String str) { StringBuilder sb=new S ...
- python os.system()返回值判断
最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...
- lua中的协程
lua中的协程和线程类似: 1. 协程拥有自己的独立的栈,局部变量,和指令: 2. 所有协程都可以共享全局变量: 3. 协程不能像线程那样并行执行,协程之间需要相互协调执行,同一个时刻只能运行一个协程 ...
- [js]识别浏览器及版本
var userAgent = navigator.userAgent.toLowerCase();window.jQuery.browser = { version: (userAgent.m ...