codeforces-746【思维】【模拟】

本文提供了四道编程竞赛题目的解答思路及代码实现,包括水果搭配、解码还原、电车出行最优策略及交替饮茶问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

A. Compote
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio1: 2: 4. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits — lemons, apples and pears — should be put in the compote as whole fruits.

Your task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0.

Input

The first line contains the positive integer a (1 ≤ a ≤ 1000) — the number of lemons Nikolay has.

The second line contains the positive integer b (1 ≤ b ≤ 1000) — the number of apples Nikolay has.

The third line contains the positive integer c (1 ≤ c ≤ 1000) — the number of pears Nikolay has.

Output

Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote.

Examples
input
2
5
7
output
7
input
4
7
13
output
21
input
2
3
2
output
0
Note

In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1 + 2 + 4 = 7.

In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21.

In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0.


#include<cstdio>
#include<algorithm>
using namespace std;
int a,b,c;
int main()
{
	while(~scanf("%d%d%d",&a,&b,&c))
	{
		int x1=a;
		int x2=b/2;
		int x3=c/4;
		printf("%d\n",min(x1,min(x2,x3))*7);
	}
	return 0;
}

题目链接: 点击打开链接

B. Decoding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contestinfo. If the word consists of single letter, then according to above definition this letter is the median letter.

Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.

You are given an encoding s of some word, your task is to decode it.

Input

The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word.

The second line contains the string s of length n consisting of lowercase English letters — the encoding.

Output

Print the word that Polycarp encoded.

Examples
input
5
logva
output
volga
input
2
no
output
no
input
4
abba
output
baba
Note

In the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter gwhich was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked likelogva.

In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.

In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letterb, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba.


大意:给出一种编码方式,告诉你某单词的编码结果,求原单词

思路:观察可以看出,按编码结果字母的顺序,中间的位置开始:左边一个,右边一个,排完之后就是原单词。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
int n;
char str[2010];
int main()
{
	while(~scanf("%d",&n))
	{
		scanf("%s",str);
		stack<char> l;
		char r[1010];
		bool flag=0;
		char ch;
		if(n&1)
		{
			flag=1;
			ch=str[0];
		}
		int temp=0,num=0;
		for(int i=0;i<n;i++)
		{
			if(i==0&&flag)
				continue;
			temp++;
			if(temp&1)
				l.push(str[i]);
			else
				r[num++]=str[i];
		}
		while(!l.empty())
		{
			printf("%c",l.top());
			l.pop();
		}
		if(flag)
			putchar(ch);
		for(int i=0;i<num;i++)
			putchar(r[i]);
		puts("");
	}
	return 0;
}

题目链接: 点击打开链接

C. Tram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.

Igor is at the point x1. He should reach the point x2. Igor passes 1 meter per t2 seconds.

Your task is to determine the minimum time Igor needs to get from the point x1 to the point x2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x1.

Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per t2 seconds). He can also stand at some point for some time.

Input

The first line contains three integers sx1 and x2 (2 ≤ s ≤ 10000 ≤ x1, x2 ≤ sx1 ≠ x2) — the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.

The second line contains two integers t1 and t2 (1 ≤ t1, t2 ≤ 1000) — the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.

The third line contains two integers p and d (1 ≤ p ≤ s - 1d is either 1 or ) — the position of the tram in the moment Igor came to the point x1 and the direction of the tram at this moment. If , the tram goes in the direction from the point s to the point 0. If d = 1, the tram goes in the direction from the point 0 to the point s.

Output

Print the minimum time in seconds which Igor needs to get from the point x1 to the point x2.

Examples
input
4 2 4
3 4
1 1
output
8
input
5 4 0
1 2
3 1
output
7
Note

In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds.

In the second example Igor can, for example, go towards the point x2 and get to the point 1 in 6 seconds (because he has to pass 3meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1meter in 1 second. Thus, Igor will reach the point x2 in 7 seconds in total.


大意:给出人和电车的起始位置,以及速度,人可以在合适的位置上乘坐电车,求从出发点到终点的最小时间

思路:

1)人在某一时刻可以坐顺风车,那么求人和电车到达 x2 点的时间的最小值即可;

2)人全程都坐不上顺风车,那么只能是人到达 x2 的时间

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int s,x1,x2,t1,t2,p,d;
int main()
{
	while(~scanf("%d%d%d%d%d%d%d",&s,&x1,&x2,&t1,&t2,&p,&d))
	{
		int ans1=0;
		int ans2=abs(x1-x2)*t2;
		bool flag=0;
		while(1)
		{
			if(p==x1)
				flag=1;
			if(p==x2&&flag) // 保证电车是从 x1 开向 x2 
				break;
			if(p==0&&d==-1)
				d=1;
			if(p==s&&d==1)
				d=-1;
			if(d==1)
				p++;
			else
				p--;
			ans1+=t1;
		}
		printf("%d\n",min(ans1,ans2));
	}
	return 0;
}

题目链接: 点击打开链接

D. Green and Black Tea
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly ntea bags, a of them are green and b are black.

Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.

Input

The first line contains four integers nka and b (1 ≤ k ≤ n ≤ 1050 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed thata + b = n.

Output

If it is impossible to drink n cups of tea, print "NO" (without quotes).

Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.

If there are multiple answers, print any of them.

Examples
input
5 1 3 2
output
GBGBG
input
7 2 2 5
output
BBGBGBB
input
4 3 4 0
output
NO

大意:一个人有两种茶,但是他喝一种茶最多连续 k 次,输出一种合法的喝茶方案

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,k,a,b;
char A='G',B='B';
int pos[100010]; // 表示第 i 个位置之后喝另一种茶的次数 
int main()
{
	while(~scanf("%d%d%d%d",&n,&k,&a,&b))
	{
		if(a<b)
		{
			swap(a,b);
			swap(A,B);
		}
		int x=a/k;
		if(a%k==0)
			x--;
		if(x>b) // 最少要换 x 次茶喝 
		{
			puts("NO");
			continue;
		}
		memset(pos,0,sizeof(pos)); 
		for(int i=k;i<a;i+=k) // 保证要换 x 次茶喝 
		{
			b--;
			pos[i]++;
		}
		for(int i=1;i<=a;i++)
		{
			while(b&&pos[i]<k)
			{
				b--;
				pos[i]++;
			}
		}
		while(b--) // b 不为 0,就在前面输出 
			putchar(B);
		for(int i=1;i<=a;i++)
		{
			putchar(A);
			for(int j=1;j<=pos[i];j++)
				putchar(B);
		}
		puts("");
	}
	return 0;
}




### 关于 Codeforces 平台上与威尔逊定理相关的编程问题及解决方案 #### 威尔逊定理简介 威尔逊定理是一个重要的数论结论,它表明:如果 $p$ 是一个质数,则 $(p-1)! \equiv -1 \ (\text{mod}\ p)$。换句话说,$(p-1)! + 1$ 可被 $p$ 整除[^5]。这一性质在许多涉及质数检测和大数运算的算法竞赛题目中有广泛的应用。 --- #### 经典题目解析与实现 以下是几道典型的 Codeforces 题目以及它们基于威尔逊定理的解决方法: --- ##### **题目一:Codeforces 1295D** 该题的核心在于判断给定区间内有多少个数满足某种特殊条件,而这些条件可以通过威尔逊定理简化为对阶乘取模的结果分析[^6]。 ###### 解决方案 为了高效地处理大规模数据,我们可以预先计算出所有可能需要用到的阶乘值及其对应的模意义下的结果。具体代码如下所示: ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; ll factorial_mod(ll n, ll p) { if (n >= p) return 0; ll result = 1; for (ll i = 1; i <= n; ++i) { result = (result * i) % p; } return result; } bool is_prime_wilson(ll p) { if (factorial_mod(p - 1, p) != p - 1) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); ll l, r; cin >> l >> r; int count = 0; for (ll i = max(l, 2LL); i <= r; ++i) { if (is_prime_wilson(i)) { count++; } } cout << count << "\n"; } ``` 这段程序首先定义了一个用于计算阶乘并对指定素数取模的功能函数 `factorial_mod`,接着借助这个工具实现了依据威尔逊定理判定某个自然数是否为素数的逻辑[^6]。 --- ##### **题目二:Prime Number Theorem Application** 虽然这不是一道具体的 Codeforces 题目名称,但它代表了一类常见的挑战场景——即如何利用包括但不限于威尔逊在内的各种经典数学理论去优化复杂度较高的枚举过程[^7]。 ###### 实现细节 当面临需要频繁验证多个候选数字是否属于素数集合的任务时,单纯依靠试除法往往难以达到理想的时间效率标准。此时引入诸如埃拉托斯特尼筛法或者更高级别的线性筛技术固然是一种不错的选择;然而,在某些特定条件下(例如仅需关心少量极大数值的情况),直接调用威尔逊定理反而能带来意想不到的优势。 示例代码片段展示如下: ```python def wilsons_theorem_test(n): from math import factorial if n < 2: return False elif pow(factorial(n - 1), 1, n) == n - 1: return True else: return False # Example Usage print(wilsons_theorem_test(11)) # Output: True ``` 在这里我们采用了 Python 内置库中的高精度整数运算能力来模拟整个流程,尽管如此仍需注意实际比赛中应尽量避免因过度依赖此类特性而导致性能瓶颈的发生[^7]。 --- #### 总结 综上所述,通过对威尔逊定理深入理解并灵活运用到不同类型的算法设计当中,不仅可以帮助参赛者更好地应对那些表面上看似棘手但实际上蕴含深刻规律可循的问题,而且还有助于培养更加严谨缜密的思维习惯! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值