总目录详见https://blog.csdn.net/mrcrack/article/details/84471041
做题原则,找不到测评地址的题不做。2018-11-28
重走长征路---OI每周刷题记录---11月30日 2013
本周共计23题
测评地址:
并查集
1.关押罪犯
2.You are my brother //在线测评地址https://vjudge.net/problem/NBUT-1218
3.冗余关系 //在线测评地址http://codevs.cn/problem/2155/
(约瑟夫问题)
4.数绵羊
dp
5.乘法难题(乘法游戏)
6.最大连续字串和
7.NOIP2007守望者的逃离
8.NOIP2008传纸条
9.NOIP2000方格取数
10.合唱队形
11.马棚问题
贪心
12.配对
13.NOIP2007纪念品分组 //在线测评地址https://www.luogu.org/problemnew/show/P1094
14.NOIP2008排座椅
dfs
15.NOIP2001数的划分
模拟
16.验证数独
17.铁轨
18.NOIP2013表达式求值
19.NOIP2013记数问题
dfs
20.传话
二分图匹配
21.梦幻大PK
22.寻找代表元
贪心/dp
23.田忌赛马
题解:
并查集
1.关押罪犯
2.You are my brother
//You are my brother NBUT - 1218
//在线测评地址https://vjudge.net/problem/NBUT-1218
//样例模拟如下:
//6->5->3->1
//6->4->2
//You are my elder
//7->5->3->1
//7->6->4->2
//You are my brother
//该题思路,有向图,找到根节点,计算各个点的深度,越深表示越年轻。
//样例通过,提交Runtime error,马上想到,cnt,last也要每次初始化为0
//初始化变量出现遗漏,这是多组数据常遇到的问题。
//last=-1,cnt=0;//漏了此行初始化
//修改,提交Wrong answer,什么情况?
//仔细想了想,可能给出的数据是空洞的,也即n对数据中,1-last未必全用上,故找root会出现失误
//以下代码为Wrong answer,要想AC,需用新的算法,另起炉灶。2019-2-5 20:22
//读题出现重大失误,
//样例通过,提交AC。2019-2-5 20:39
//读题出现重大失误,将一个简单问题,编复杂了,之后不得不推倒重来。
//虽然,同是算离祖宗的距离,但明显,能扛过数据的空洞了,
//解释一下,最小值1,最大值10,并不代表1-10中的所用数都会用到,比如只用1,2,3,4,7,10
//以下为AC代码。
#include <stdio.h>
#include <string.h>
int n,f[2010];
int main(){
int i,u,v,a,b,A,B;
while(scanf("%d",&n)!=EOF){
memset(f,0,sizeof(f));
for(i=1;i<=n;i++){
scanf("%d%d",&u,&v);
f[u]=v;
}
a=1,A=0,b=2,B=0;
while(a)a=f[a],A++;
while(b)b=f[b],B++;
if(B<A)printf("You are my elder\n");//深度小,表明离祖宗近,更大辈分
else if(B>A)printf("You are my younger\n");
else printf("You are my brother\n");
}
return 0;
}
//You are my brother NBUT - 1218
//在线测评地址https://vjudge.net/problem/NBUT-1218
//样例模拟如下:
//6->5->3->1
//6->4->2
//You are my elder
//7->5->3->1
//7->6->4->2
//You are my brother
//该题思路,有向图,找到根节点,计算各个点的深度,越深表示越年轻。
//样例通过,提交Runtime error,马上想到,cnt,last也要每次初始化为0
//初始化变量出现遗漏,这是多组数据常遇到的问题。
//last=-1,cnt=0;//漏了此行初始化
//修改,提交Wrong answer,什么情况?
//仔细想了想,可能给出的数据是空洞的,也即n对数据中,1-last未必全用上,故找root会出现失误
//以下代码为Wrong answer,要想AC,需用新的算法,另起炉灶。2019-2-5 20:22
#include <stdio.h>
#include <string.h>
struct node{
int to;
int next;
}e[1010];
int n,f[2010],last,root,h[2010],head[2010],cnt;
int max(int a,int b){
return a>b?a:b;
}
void add_edge(int u,int v){
cnt++,e[cnt].to=v,e[cnt].next=head[u],head[u]=cnt;
}
void dfs_tree(int father){//建树,确定各点深度
int b,u,v;
u=father,b=head[u];
while(b){
v=e[b].to;
h[v]=h[u]+1;
dfs_tree(v);
b=e[b].next;
}
}
int main(){
int i,u,v;
while(scanf("%d",&n)!=EOF){
last=-1,cnt=0;//漏了此行初始化
memset(f,0,sizeof(f)),memset(head,0,sizeof(head));
for(i=1;i<=n;i++){
scanf("%d%d",&u,&v);
last=max(last,max(u,v));
add_edge(v,u);//有向图 父到子
f[u]=v;
}
for(i=1;i<=last;i++)
if(f[i]==0){//找根
root=i;
break;
}
h[root]=1;
dfs_tree(root);
if(h[2]<h[1])printf("You are my elder\n");//深度小,表明离祖宗近,更大辈分
else if(h[2]>h[1])printf("You are my younger\n");
else printf("You are my brother\n");
}
return 0;
}
3.冗余关系
//2155 冗余关系
//在线测评地址http://codevs.cn/problem/2155/
//找有几个爸爸
//样例通过,提交AC。2019-2-5 23:14
#include <stdio.h>
int n,m,f[1010],cnt=0;
int getf(int u){
return f[u]==u?u:f[u]=getf(f[u]);
}
void merge(int u,int v){
int f1=getf(u),f2=getf(v);
if(f1!=f2)f[f2]=f1;
}
int main(){
int i,u,v;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)f[i]=i;
while(n--){
scanf("%d%d",&u,&v);
merge(u,v);
}
for(i=1;i<=m;i++)
if(f[i]==i)cnt++;
printf("%d\n",cnt);
return 0;
}
(约瑟夫问题)
4.数绵羊
dp
5.乘法难题(乘法游戏)
6.最大连续字串和
7.NOIP2007守望者的逃离
8.NOIP2008传纸条
9.NOIP2000方格取数
10.合唱队形
11.马棚问题
贪心
12.配对
13.NOIP2007纪念品分组
//P1094 纪念品分组
//NOIP 2007 普及组
//在线测评地址https://www.luogu.org/problemnew/show/P1094
//思路,因n最大值为30000,快排,自小到大,采用C++中的sort函数
//之后,有序数组,两头开始向中间靠近,进行分组
//样例通过,提交AC。2019-1-20 15:17
#include <cstdio>
#include <algorithm>
#define maxn 30100
using namespace std;
int n,w,p[maxn];
int main(){
int i,j,cnt=0;
scanf("%d%d",&w,&n);
for(i=1;i<=n;i++)scanf("%d",&p[i]);
sort(p+1,p+1+n);
i=1,j=n;
while(i<j){//思路有些累快排
if(p[j]+p[i]<=w)j--,i++,cnt++;//配对
else j--,cnt++;//只有一个
}
if(i==j)cnt++;//i==j时,表示还剩一个,未配对; 若i>j表示全部都已配对
printf("%d\n",cnt);
return 0;
}
14.NOIP2008排座椅
dfs
15.NOIP2001数的划分
模拟
16.验证数独
17.铁轨
18.NOIP2013表达式求值
19.NOIP2013记数问题
dfs
20.传话
二分图匹配
21.梦幻大PK
22.寻找代表元
贪心/dp
23.田忌赛马