Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的。。把基础题打完就日常划水了。。。。
A. Arpa's loud Owf and Mehrdad's evil plan
题目大意: 有n个人,每个人有一个后继,求在进行多少次传递后第i 个人跟第j个人对话的同时,第j个人也能跟第i个人对话(允许自己与自己对话)。$1\leq n \leq 100$ 这
题意花了我好多时间啊。。说白了就是要找环咯,把每条环求出来然后看环长度是不是偶数,是的话就除2,然后就个最大公约数就行了。
Code:
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 #define maxn 110 7 typedef long long ll; 8 ll gcd(ll x,ll y){ 9 if (y==0) return x; 10 return gcd(y,x%y); 11 } 12 int a[maxn],b[maxn],n; 13 int main(){ 14 scanf("%d",&n); 15 for (int i=1;i<=n;i++) scanf("%d",a+i); 16 ll ans=1; 17 for (int i=1;i<=n;i++) { 18 memset(b,0,sizeof(b)); 19 int l=1; 20 int t=i; 21 while (!b[t]) { 22 b[t]=l; 23 l++; 24 t=a[t]; 25 } 26 if (b[t]!=1) { 27 printf("-1\n"); 28 return 0; 29 } 30 l--; 31 if (l%2==0) l/=2; 32 ans=ans*1ll*((l)/gcd(ans,l)); 33 } 34 cout<<ans<<endl; 35 return 0; 36 }
View Code
B. Arpa's weak amphitheater and Mehrdad's valuable Hoses
题目大意:给定n群女生,每群女生要么只能邀请一个女生要么就要全部邀请,求在满足重量限制的情况下的最大收益。
分组背包模型,可以直接$O((m+n)w)$解决,令$f[i][j]$为前i组重量为j的最大收益,则每个$f[i]$内就是最好的。
Code:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 using namespace std; 8 #define maxn 1010 9 vector<int> e[maxn]; 10 int w[maxn],b[maxn]; 11 bool bo[maxn]; 12 int f[maxn][maxn]; 13 int num[maxn][maxn]; 14 inline void addedge(int x,int y){ 15 e[x].push_back(y);e[y].push_back(x); 16 } 17 int n,m,W,N; 18 void bfs(int u){ 19 static queue<int> q; 20 q.push(u); 21 bo[u]=1; 22 N++; 23 while (!q.empty()){ 24 u=q.front();q.pop(); 25 num[N][0]++; 26 num[N][num[N][0]]=u; 27 for (int i=0;i<e[u].size();i++){ 28 if (bo[e[u][i]]) continue; 29 q.push(e[u][i]); 30 bo[e[u][i]]=1; 31 } 32 } 33 return ; 34 } 35 int sumb[maxn],sumw[maxn]; 36 int main(){ 37 scanf("%d%d%d",&n,&m,&W); 38 for (int i=1;i<=n;i++) scanf("%d",w+i); 39 for (int j=1;j<=n;j++) scanf("%d",b+j); 40 for (int i=1;i<=m;i++) { 41 int x,y; 42 scanf("%d%d",&x,&y); 43 addedge(x,y); 44 } 45 for (int i=1;i<=n;i++) 46 if (!bo[i]) bfs(i); 47 for (int i=1;i<=N;i++) { 48 for (int j=0;j<=W;j++) f[i][j]=f[i-1][j]; 49 for (int j=1;j<=num[i][0];j++) { 50 for (int k=W;k>=w[num[i][j]];k--) 51 f[i][k]=max(f[i-1][k-w[num[i][j]]]+b[num[i][j]],f[i][k]); 52 sumb[i]+=b[num[i][j]]; 53 sumw[i]+=w[num[i][j]]; 54 } 55 for (int j=W;j>=sumw[i];j--) 56 f[i][j]=max(f[i-1][j-sumw[i]]+sumb[i],f[i][j]); 57 } 58 printf("%d\n",f[N][W]); 59 return 0; 60 }
View Code
C. Arpa’s overnight party and Mehrdad’s silent entering
题目大意:有n对情侣坐在一张圆桌上吃饭,有两种食品,要求每个人和男女朋友不吃同一种东西,同时要求没有连续3个人吃同一种东西。
这题太坑了。。要把条件收束一点才能做。。。把没有连续的3个人吃同一种东西变成1跟2吃不同,3跟4吃不同...,然后就变成一个二分图模型就可以2-set解决了。。。(为什么是个二分图可以自己想一想)
Code:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 #define pb push_back 8 #define maxn 201000 9 vector<int> e[maxn]; 10 int n,x,y; 11 int col[maxn],b[maxn]; 12 int dfs(int x,int y){ 13 col[x]=y; 14 b[x]=1; 15 for (int i=0;i<e[x].size();i++) 16 if (!b[e[x][i]]) 17 dfs(e[x][i],y^1); 18 return 0; 19 } 20 int a[maxn],c[maxn]; 21 int main(){ 22 scanf("%d",&n); 23 for (int i=1;i<=n;i++) { 24 scanf("%d%d",&x,&y); 25 a[i]=x;c[i]=y; 26 e[x].pb(y); 27 e[y].pb(x); 28 } 29 for (int i=1;i<=n;i++) { 30 e[i*2].pb(i*2-1); 31 e[i*2-1].pb(i*2); 32 } 33 for (int i=1;i<=n*2;i++) if (!b[i]) dfs(i,1); 34 for (int i=1;i<=n;i++) printf("%d %d\n",col[a[i]]+1,col[c[i]]+1); 35 return 0; 36 }
View Code
转载于:https://www.cnblogs.com/New-Godess/p/6222807.html
Codeforces Round #383 Div 1题解相关推荐
- Codeforces Round #514 (Div. 2)题解
Codeforces Round #514 (Div. 2)题解 A 喵,直接模拟. B 枚举所有盖章时的,合法的,左上角的位置.能盖的话就盖一下.最后check一下图案是否相等即可 C 一轮一轮的扔 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- 【算法题解】Codeforces Round #817 (Div. 4)题解
文章目录 Codeforces Round #817 (Div. 4)题解 A. Spell Check B. Colourblindness C. Word Game D. Line E. Coun ...
- Codeforces Round #747 (Div. 2)题解
Codeforces Round #747 (Div. 2)题解 (本博客将持续更新以后每场CF div2的题解,喜欢ACM.OI的小伙伴记得点个关注哟) 昨天夜晚刷网络流刷入迷了,渐渐就忘记了我还要 ...
- Codeforces Round #789 (Div. 2)题解
Codeforces Round #789 (Div. 2)题解 A. Tokitsukaze and All Zero Sequence 原题链接 算法标签 贪心 排序 思路 情况一:数组存在零 → ...
- Codeforces Round #748 (Div. 3) 题解 完整A~G
Codeforces Round #748 (Div. 3) 题解 A. Elections 题意 已知竞选中三个候选人的当前得票数 a , b , c a,b,c a,b,c,现在可以增加任何一个人 ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- Codeforces Round #734 (Div. 3) 题解
Hello大家好,今天给大家带来的是 Codeforces Round #734 (Div. 3) 的全题目讲解. 本文链接:https://www.lanqiao.cn/questions/2040 ...
- Codeforces Round #462 (Div. 2)题解
Codeforces Round #462 (Div. 2) B题--我固执的认为1e18是18位数,导致被hack,花了20分钟才检查出这个错误,很僵硬 Codeforces 934C 题意 给定一 ...
- Codeforces Round #441 Div. 2题解
A.直接判断相邻的边是不是最短边 是就来回走 不然就走一条第二的然后再来回走 #include<cstdio> #include<cstring> #include<al ...
最新文章
- JAVA min()_Java min()方法
- python线性表和队列_[笔记]python数据结构之线性表:linkedlist链表,stack栈,queue队列...
- Docker容器压力测试查看CPU权重
- wpf scrollviewer 两个事件相互影响_WPF绘制3D效果显示密集立体集装箱
- hbuilderx的快捷键整理pdf_mac键盘快捷键详解,苹果电脑键盘快捷键图文教程
- 前端大神的离逝,让我们不得不有所反思。
- android 控件发光_如何在android中的按钮周围制作动画/常量发光效果?
- HTML-参考手册: 键盘快捷键
- /etc/fstab修改及mkfs(e2label)相关应用与疑问
- 20191117每日一句 EVERYBODY DIES, BUT NOT EVERYBODY LIVES
- 交换机trunk模式工作原理
- 肺疫面前,段子手出动,但是玩笑也要分场合
- 阿里云ECS云服务器实例重置-更换操作系统
- 05笔趣阁小说爬取--爬取作者所有小说
- @Autowire和@Resource区别
- ZooKeeper基础知识笔记(含3节点伪分布式安装配置流程)
- sinx的不定积分易错点
- 浅谈OA系统与Portal门户的区别
- cesium实现日照阴影模拟效果
- 北京计算机二本学院,北京有哪些二本大学
热门文章
- 网络规划---网络计划图的时间参数计算
- 从命令行安装IIS 7.0
- Directional Light,Ambient,Specular,光照感性认识...
- 0930_C/C++笔试题_12:16道c语言面试【6/7】
- Windows Server 2008 R2的教程36篇!
- git clone 非空目录
- Git提交代码相关命令
- 【代码优化】反射机制的慎用
- 手把手写一个vue3的组件
- c语言程序设计一至六章测验,c语言程序设计1-6章.ppt