集训队每周一赛2020-03-01(二分排序+冒泡排序+结构体排序)

A 我

POJ 3104

传送门.

It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).

Output

Output a single integer — the minimal possible number of minutes required to dry all clothes.

Sample Input

sample input #1
3
2 3 9
5
sample input #2
3
2 3 6
5

Sample Output

sample output #1
3
sample output #2
2

题解

题意求烘干衣服最少用的时间。二分,mid表示最少时间,若a[i]大于mid,则需要烘干,每分钟烘干水量为k-1,比较烘干总时间与mid,确定二分范围。

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define ll long long
using namespace std;
int n,k,maxx,a[10000000];
int binarysearch(ll mid)
{
	ll i,t,sum=0;
	for(int i=0; i<n; i++)
	{
		if(a[i]>mid)
		{
			t=ceil((a[i]-mid)*1.0/(k-1));
			sum+=t;
		}
	}
	if(sum<=mid)
		return 1;
	else
		return 0;
}
int main()
{
	scanf("%d",&n);
	ll l,r,mid;
	for(int i=0; i<n; i++)
	{
		scanf("%d",&a[i]);
		maxx=max(maxx,a[i]);
	}
	scanf("%d",&k);
	if(k==1)
	{
		printf("%lld\n",maxx);
		return 0;
	}
	l=0;
	r=maxx;
	while(l<r)
	{
		mid=(l+r)/2;
		if(binarysearch(mid))
			r=mid;
		else
			l=mid+1;
	}
	printf("%lld",r);
	return 0;
}

B 想

传送门.

HDU 5540

Master Mind KongMing gave Fei Zhang a secrete master plan stashed in a pocket. The plan instructs how to deploy soldiers on the four corners of the city wall. Unfortunately, when Fei opened the pocket he found there are only four numbers written in dots on a piece of sheet. The numbers form 2×2 matrix, but Fei didn’t know the correct direction to hold the sheet. What a pity!
Given two secrete master plans. The first one is the master’s original plan. The second one is the plan opened by Fei. As KongMing had many pockets to hand out, he might give Fei the wrong pocket. Determine if Fei receives the right pocket.
在这里插入图片描述

Input

The first line of the input gives the number of test cases, T(1≤T≤104). T test cases follow. Each test case contains 4 lines. Each line contains two integers ai0 and ai1 (1≤ai0,ai1≤100). The first two lines stands for the original plan, the 3rd and 4th line stands for the plan Fei opened.

Output

For each test case, output one line containing " Case #x: y", where x is the test case number
(starting from 1) and y is either “POSSIBLE” or “IMPOSSIBLE” (quotes for clarity).

Sample Input

4
1 2
3 4
1 2
3 4
1 2
3 4
3 1
4 2
1 2
3 4
3 2
4 1
1 2
3 4
4 3
2 1

Sample Output

Case #1: POSSIBLE
Case #2: POSSIBLE
Case #3: IMPOSSIBLE
Case #4: POSSIBLE

题解

#include<stdio.h>
int main(){
    int t,cas=1;
    scanf("%d",&t);
    while(t--){
        int a1,b1,c1,d1,a2,b2,c2,d2;
        scanf("%d%d%d%d%d%d%d%d",&a1,&b1,&c1,&d1,&a2,&b2,&c2,&d2);
        if(a2==a1&&b1==b2&&c1==c2){
			printf("Case #%d: POSSIBLE\n",cas++);
            continue;
        }
        if(b2==a1&&a2==c1&&d2==b1){
			printf("Case #%d: POSSIBLE\n",cas++);
            continue;
        }
        if(c2==a1&&d2==c1&&a2==b1){
			printf("Case #%d: POSSIBLE\n",cas++);
            continue;
        }
        if(d2==a1&&c2==b1&&b2==c1){
			printf("Case #%d: POSSIBLE\n",cas++);
            continue;
        }
		printf("Case #%d: IMPOSSIBLE\n",cas++);
    }
    return 0;
}

C 上

CodeForces 1311B

传送门

You are given an array a of length n.
You are also given a set of distinct positions p1,p2,…,pm, where 1≤pi<n. The position pi means that you can swap elements a[pi] and a[pi+1]. You can apply this operation any number of times for each of the given positions.
Your task is to determine if it is possible to sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps.
For example, if a=[3,2,1] and p=[1,2], then we can first swap elements a[2] and a[3] (because position 2 is contained in the given set p). We get the array a=[3,1,2]. Then we swap a[1] and a[2] (position 1 is also contained in p). We get the array a=[1,3,2]. Finally, we swap a[2] and a[3] again and get the array a=[1,2,3], sorted in non-decreasing order.
You can see that if a=[4,1,2,3] and p=[3,2] then you cannot sort the array.
You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤100) — the number of test cases.
Then t test cases follow. The first line of each test case contains two integers n and m (1≤m<n≤100) — the number of elements in a and the number of elements in p. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤100). The third line of the test case contains m integers p1,p2,…,pm (1≤pi<n, all pi are distinct) — the set of positions described in the problem statement.

Output

For each test case, print the answer — “YES” (without quotes) if you can sort the initial array in non-decreasing order (a1≤a2≤⋯≤an) using only allowed swaps. Otherwise, print “NO”.

Input

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

Output

YES
NO
YES
YES
NO
YES

题解

冒泡排序,只有p[i]和p[i+1]可以交换顺序,如果a数组无法完成排序,则输出NO,否则输出YES。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[105],p[105],q[105];
int main()
{
	int t,n,m,flag;
	scanf("%d",&t);
	while(t--)
	{
		memset(q,0,sizeof(q));
		flag=0;
		scanf("%d%d",&n,&m);
		for(int i=1; i<=n; i++)
			scanf("%d",&a[i]);
		for(int i=1; i<=m; i++)
		{
			scanf("%d",&p[i]);
			q[p[i]]=1;
		}			
		for(int j=1; j<n; j++)
		{
			for(int i=1; i<n; i++)
			{
				if(a[i]>a[i+1])
				{
					if(q[i])
					{
						swap(a[i],a[i+1]);
					}
					else
					{
						flag=1;
						break;
					}
				}				
			}
			if(flag)
			break;
		}
		if(flag)
		printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

D 学

HDU 5182

传送门

Nowadays we use content of PM2.5 to discribe the quality of air. The lower content of PM2.5 one city have, the better quality of air it have. So we sort the cities according to the content of PM2.5 in asending order.
Sometimes one city’s rank may raise, however the content of PM2.5 of this city may raise too. It means that the quality of this city is not promoted. So this way of sort is not rational. In order to make it reasonable, we come up with a new way to sort the cityes. We order this cities through the diffrence between twice measurement of content of PM2.5 (first measurement – second measurement) in descending order, if there are ties, order them by the second measurement in asending order , if also tie, order them according to the input order.

Input

Multi test cases (about 100), every case contains an integer n which represents there are n cities to be sorted in the first line.
Cities are numbered through 0 to n−1.
In the next n lines each line contains two integers which represent the first and second measurement of content of PM2.5
The ith line describes the information of city i−1
Please process to the end of file.
[Technical Specification]
all integers are in the range [1,100]

Output

For each case, output the cities’ id in one line according to their order.

Sample Input

2
100 1
1 2
3
100 50
3 4
1 2

Sample Output

0 1
0 2 1

题解

#include<stdio.h>
struct s
{
	int first,second,order;
}city[105], tmp;
int main()
{
	int n,i,j;
	while(~scanf("%d",&n))
	{
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&city[i].first, &city[i].second);
			city[i].order=i; 
		}		
		for (i=0;i<n-1;i++)
			for (j=0;j<n-1-i;j++)
			{
				if (city[j].first - city[j].second < city[j+1].first - city[j+1].second) 
				{
					tmp = city[j];
					city[j] = city[j+1];
					city[j+1] = tmp;
				}
				else if (city[j].first - city[j].second == city[j+1].first - city[j+1].second)
				{
					if (city[j].second > city[j+1].second) 
					{
						tmp = city[j];
						city[j] = city[j+1];
						city[j+1] = tmp;
					}
				}
			}		
		for(i=0;i<n-1;i++) printf("%d ",city[i].order);
		printf("%d\n",city[n-1].order);
	}
	return 0;
}

E 啊

HDU 5832

传送门

Two planets named Haha and Xixi in the universe and they were created with the universe beginning.
There is 73 days in Xixi a year and 137 days in Haha a year.
Now you know the days N after Big Bang, you need to answer whether it is the first day in a year about the two planets.

Input

There are several test cases(about 5 huge test cases).
For each test, we have a line with an only integer N(0≤N), the length of N is up to 10000000.

Output

For the i-th test case, output Case #i: , then output “YES” or “NO” for the answer.

Sample Input

10001
0
333

Sample Output

Case #1: YES
Case #2: YES
Case #3: NO

F !

CodeForces 1263D

传送门

One unknown hacker wants to get the admin’s password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator’s office and stole a piece of paper with a list of n passwords — strings, consists of small Latin letters.
Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:
two passwords a and b are equivalent if there is a letter, that exists in both a and b;
two passwords a and b are equivalent if there is a password c from the list, which is equivalent to both a and b.
If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.
For example, if the list contain passwords “a”, “b”, “ab”, “d”, then passwords “a”, “b”, “ab” are equivalent to each other, but the password “d” is not equivalent to any other password from list. In other words, if:
admin’s password is “b”, then you can access to system by using any of this passwords: “a”, “b”, “ab”;
admin’s password is “d”, then you can access to system by using only “d”.
Only one password from the list is the admin’s password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.

Input

The first line contain integer n (1≤n≤2⋅105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings si, with length at most 50 letters. Some of the passwords may be equal.
It is guaranteed that the total length of all passwords does not exceed 106 letters. All of them consist only of lowercase Latin letters.

Output

In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.

Examples

Input
4
a
b
ab
d
Output
2
Input
3
ab
bc
abc
Output
1
Input
codeforces
Output
1

Note

In the second example hacker need to use any of the passwords to access the system.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值