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 ...
随机推荐
- Python 安装mysqldb
切换目录: cd /usr/local/src/ 一.下载资源 wget http://sourceforge.net/projects/mysql-python/files/mysql-python ...
- [Spring MVC] - 表单提交
Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...
- HackerRank "Minimum Penalty Path"
It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...
- vim 学习日志(4):多窗口使用技巧
原文地址: http://blog.csdn.net/devil_2009/article/details/7006113 vim多窗口使用技巧 1.打开多个窗口打开多个窗口的命令以下几个:横向切割窗 ...
- AnyCAD .Net SDK 用户手册 v2013.1
AnyCAD .Net SDK 用户手册 v2013.1 1. 简介 AnyCAD .Net SDK为.Net4.0开发者提供简单易用的三维建模和三维可视化的API.SDK主要由三维建模的API和可视 ...
- AX2012 referencegroup
用referencegroup 1.添加EDT,refrecid 2.修改autoidentification 3. 可以直接拖refrecid字段出来,就是referencegroup了
- IOS 关于扬声器和听话筒的设置 ----自己试验过的,可以达到扩音器和听筒播放的效果
今下午项目中使用到了 扬声器和听筒的设置,我项目中是这样的,有一个聊天设置,聊天设置有一个使用扬声器 播放声音的设置. 这个设置是,当你打开那个开关的话,你在聊天中都可以根据你的使用来任意的播放声音, ...
- python序列化: json & pickle & shelve 模块
一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...
- lintcode-【简单题】链表求和
题目: 你有两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. 样例: 给出两个链表 3- ...
- NHibernate系列文章十七:NHibernate Session管理(附程序下载)
摘要 NHibernate的Session的管理涉及到NHibernate的两个最重要的对象ISessionFactory和ISession.ISessionFactory的生成非常消耗资源,通常都在 ...