1.18今日总结

上午3个半小时

学习了c++万能头文件:#include<bits/stdc++.h>

学习了sort函数。

1.数组:

int a[10];

for(int i=0;i<=9;i++)

scanf("%d",&a[i]);

sort(a,a+10);

2.自定义函数:

bool compare(int a,int b)//bool前面可以加static

{

return a>b//从大到小排序

}

sort(a,a+10,compare);

3.结构体:

struct node{

int grade;

char name[100];

}

bool compare(node a,node b)

{

return a.grade<b.grade//按照成绩从小到大排序

}

node f[10000];

for(int i=0;i<=9;i++)

scanf("%d%s",&f[i].grade,f[i].name);

sort(f,f+10,compare);

修复公路

题目背景

AA地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车。政府派人修复这些公路。

题目描述

给出A地区的村庄数NN,和公路数MM,公路是双向的。并告诉你每条公路的连着哪两个村庄,并告诉你什么时候能修完这条公路。问最早什么时候任意两个村庄能够通车,即最早什么时候任意两条村庄都存在至少一条修复完成的道路(可以由多条公路连成一条道路)

输入格式

第11行两个正整数N,MN,M

下面MM行,每行33个正整数x, y, tx,y,t,告诉你这条公路连着x,yx,y两个村庄,在时间t时能修复完成这条公路。

输出格式

如果全部公路修复完毕仍然存在两个村庄无法通车,则输出-1−1,否则输出最早什么时候任意两个村庄能够通车。

输入输出样例

输入 #1复制

4 4
1 2 6
1 3 4
1 4 5
4 2 3

输出 #1复制

5

说明/提示

N \le 1000,M \le 100000N≤1000,M≤100000

x \le N,y \le N,t \le 100000x≤N,y≤N,t≤100000

思路:首先可以先按照时间从小到大来排序,修复是同时进行的,最后,在所有村庄的根节点相同的时候取最大的那个时间,即为所求结果,最后进行判断,是否每个节点的根节点是否相同,如果出现不相同的,则所有村庄没有联通,输出-1。

代码实现:

#include<bits/stdc++.h>
int father[1001]; 
using namespace std;
struct node{
	int x;
	int y;
	int t;//时间 
};
static bool compare(node a,node b)//按时间排序 
{
return a.t<b.t;
}
int find_root(int a)
{
	if(father[a]==a)
	return a;
	else
	{
		father[a]=find_root(father[a]);
		return father[a];
	}
}
void link(int a,int b)
{
	if(find_root(a)!=find_root(b))
	{
		father[find_root(b)]=find_root(a);
	}
	return ;
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	node fix[100001]; 
	for(int i=0;i<m;i++)
		scanf("%d%d%d",&fix[i].x,&fix[i].y,&fix[i].t);
	sort(fix,fix+m,compare);//按照时间从小到大排序 
    for(int i=1;i<=n;i++)
    father[i]=i;//定义父节点
    int max=0;
	for(int i=0;i<m;i++)
	{
	 if(find_root(fix[i].x)==find_root(fix[i].y))//比较根节点 
	 continue ;
	 link(fix[i].x,fix[i].y);
	 if(max<fix[i].t)//找最大的时间 
	 max=fix[i].t;
	}
	for(int i=1;i<=n;i++)
	{
		if(find_root(i)!=find_root(1))//判断根节点是否都相同 
		{
		printf("-1");
		return 0;
	}
	}
	printf("%d",max);
}
	
	 

 

下午4个半小时,补了两个补题:

CF12B Correct Solution?

题目描述

One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice's turn, she told the number nn to Bob and said:

—Shuffle the digits in this number in order to obtain the smallest possible number without leading zeroes.

—No problem! — said Bob and immediately gave her an answer.

Alice said a random number, so she doesn't know whether Bob's answer is correct. Help her to find this out, because impatient brother is waiting for the verdict.

输入格式

The first line contains one integer nn ( 0<=n<=10^{9}0<=n<=109 ) without leading zeroes. The second lines contains one integer mm ( 0<=m<=10^{9}0<=m<=109 ) — Bob's answer, possibly with leading zeroes.

输出格式

Print OK if Bob's answer is correct and WRONG_ANSWER otherwise.

题意翻译

题目描述(舍弃各种乱七八糟的故事)

给定一个数NN,要求把NN的各个数位打乱,要求组成一个可能的,最小的数(最小数有可能含有前导00)。现在已经有一个“最小数”,请你判断这个“最小数”是不是最小数。

第一行输入n不含前导0。
第二行输入的假定的最小数可能含有前导0。 题目要求排序后的最小数不含前导0。

输入格式

两行。 第一行是给定的数NN。 第二行是假定的NN重组的“最小数”。

输出格式

一行。 如果输入的最小数就是NN重组的最小数,输出“OK”。 否则输出“WRONG_ANSWER”。

Translated by LiM_817

输入输出样例

输入 #1复制

3310
1033

输出 #1复制

OK

输入 #2复制

4
5

输出 #2复制

WRONG_ANSWER

这个题之前我不是用字符数组来输得,结果可以出,但是过不了。

思路:先用两个字符数组来输出,用strlen函数来检测字符长度,先匹配两个字符长度是否相同。

然后用两个数组来接受字符,然后用sort函数对第一个数组排序,之后在判断是否第一个为0,如果是0,找到不是0的那一位,然后两个进行交换,最后比对两个数组即可。

代码实现:
 

#include<bits/stdc++.h> 
using namespace std;
int main()
{
	char a[1000];
	char b[1000];
	int c[1000];
	int d[1000];
	gets(a);
	gets(b);
	int n=strlen(a);
	int m=strlen(b);
	if(n!=m)
{
		printf("WRONG_ANSWER");
	return 0;
	} 
	for(int i=0;i<n;i++)
		c[i]=a[i]-'0';
	for(int i=0;i<m;i++)
	    d[i]=b[i]-'0';
	    sort(c,c+n);
	 
	int cnt=0;
	while(c[cnt]==0&&cnt<=n)
	{
		cnt++;
	}
	int t;
	t=c[0];
	c[0]=c[cnt];
	c[cnt]=t;  
	for(int i=0;i<n;i++)
	{
		if(c[i]!=d[i])
		{
		printf("WRONG_ANSWER");
	    return 0;
	}
	}
	printf("OK");
	return 0; 
 } 

 CF30B Codeforces World Finals

题目描述

The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that after it the brightest minds will become his subordinates, and the toughest part of conquering the world will be completed.

The final round of the Codeforces World Finals 20YY is scheduled for DDDD . MMMM . YYYY , where DDDD is the day of the round, MMMM is the month and YYYY are the last two digits of the year. Bob is lucky to be the first finalist form Berland. But there is one problem: according to the rules of the competition, all participants must be at least 18 years old at the moment of the finals. Bob was born on BDBD . BMBM . BYBY . This date is recorded in his passport, the copy of which he has already mailed to the organizers. But Bob learned that in different countries the way, in which the dates are written, differs. For example, in the US the month is written first, then the day and finally the year. Bob wonders if it is possible to rearrange the numbers in his date of birth so that he will be at least 18 years old on the day DDDD . MMMM . YYYY . He can always tell that in his motherland dates are written differently. Help him.

According to another strange rule, eligible participant must be born in the same century as the date of the finals. If the day of the finals is participant's 18-th birthday, he is allowed to participate.

As we are considering only the years from 20012001 to 20992099 for the year of the finals, use the following rule: the year is leap if it's number is divisible by four.

输入格式

The first line contains the date DDDD . MMMM . YYYY , the second line contains the date BDBD . BMBM . BYBY . It is guaranteed that both dates are correct, and YYYY and BYBY are always in [01;99][01;99] .

It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date.

输出格式

If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the DDDD . MMMM . YYYY , output YES. In the other case, output NO.

Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits.

题意翻译

题意描述

关于 Codeforces 的网站 king Copa 经常被报道,使得它在要使用网站进行训练和比赛的人之间迅速流行开来。最近, Copa 明白,要征服世界,他需要组织世界 Codeforces 锦标赛。他希望在这次比赛之后之后,最聪明的人将成为被挑选出来成为他的下属,然后征服世界最艰难的部分将会完成。

Codeforces 世界总决赛的最后一轮定于 YYYY 年 MMMM 月 DDDD 日举行,其中 DDDD 是当天的日期, MMMM 是当月的月份, YYYY 是当年的年份的最后两位。Bob 很幸运地能成为来自 Berland 的一名决赛选手。但有一个问题:根据比赛规则,所有参赛者在决赛时必须年满 1818 岁。 Bob 出生于 BYBY 年, BMBM 月,BDBD 日。这个日期记录在他的护照上,他的护照复印件已经寄给了组织者。但是 Bob 了解到,在不同的国家,日期的书写方式是不同的。例如,在美国,先写月份,然后写日期,最后写年份。

鲍勃想知道是否有可能重新排列他出生日期的数字,以便他在 YYYY 年, MMMM 月, DDDD 日那天至少 1818 岁。他看出,在他的祖国,日期写的顺序不一样。请帮帮他。 根据另一个奇怪的规则,合格的参赛者必须与决赛日期出生在同一个世纪。如果决赛当天刚好是参赛者的 1818 岁生日,则他可以参加。

因为我们只考虑从 20012001 年到 20992099 年的决赛年份,所以使用以下规则:如果年份的数字可以被 44 整除,那么年份就是闰年。

输入格式:

第一行包括三个数字 DD,MM,YYDD,MM,YY ,第二行包括三个数字 BD,BM,BYBD,BM,BY ,数据保证两个日期的正确性,并且 BYBY 和 YYYY 保证在 [ 01 ,99 ][01,99] 中。

输出格式:

如果可能通过重新排列出生日期的顺序,让 Bob 在比赛当天至少 1818 岁,则输出 YES 。如果不能,则输出 NO。

输入输出样例

输入 #1复制

01.01.98
01.01.80

输出 #1复制

YES

输入 #2复制

20.10.20
10.02.30

输出 #2复制

NO

输入 #3复制

28.02.74
28.02.64

输出 #3复制

思路:把所有的情况举例出来,月份,年份,日期所满足的条件,然后将年份日期月份交换位置(年,月,日),(年,日,月),(月,日,年)等情况,判断是否有一种满足条件即可。

代码实现:

#include<stdio.h>	
int DD,MM,YY;
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //表示不是闰年的月份天数 
int pd(int x,int y,int z)//x为日,y为月,z为年 
{
    if(z<1||z>99)//年份在1-99之间 
    return 0;
    if(y<1||y>12)//月份在1-12之间 
    return 0;
    if(x<1||(x>day[y]&&z%4!=0))//不是闰年的情况 
    return 0;
    if(z%4==0)//是闰年的情况 
{
	 if(y==2&&x>29)//2月份大于29天 
	 return 0;
	 if(y!=2&&x>day[y])
	 return 0;
}
    if(YY-z>18||(YY-z==18&&MM-y>0)||(YY-z==18&&MM-y==0&&DD-x>=0))//满足条件的情况 
    return 1;
    else
    return 0;
}
int main()
{
     scanf("%d.%d.%d",&DD,&MM,&YY);
    int BD,BM,BY;
    scanf("%d.%d.%d",&BD,&BM,&BY);
    if(pd(BD,BM,BY)==1||pd(BD,BY,BM)==1||pd(BY,BD,BM)==1||pd(BY,BM,BD)==1||pd(BM,BD,BY)==1||pd(BM,BY,BD)==1)//穷举法 
    printf("YES");
    else
    printf("NO");
}

 之后学长讲了二叉树,听懂了,看懂了,但是自己写代码就不懂了。

并查集还比较简单。

今日总结:今天学习时间利用到位,明天要加油学习二叉树。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值