ACM【2021/9/21】

CF67A Partial Teacher

题目描述

A teacher decides to give toffees to his students. He asks nn students to stand in a queue. Since the teacher is very partial, he follows the following rule to distribute toffees.

He looks at the first two students and gives more toffees to the student having higher marks than the other one. If they have the same marks they get the same number of toffees. The same procedure is followed for each pair of adjacent students starting from the first one to the last one.

It is given that each student receives at least one toffee. You have to find the number of toffees given to each student by the teacher such that the total number of toffees is minimum.

输入格式

The first line of input contains the number of students nn ( 2<=n<=10002<=n<=1000 ). The second line gives ( n-1n−1 ) characters consisting of "L", "R" and "=". For each pair of adjacent students "L" means that the left student has higher marks, "R" means that the right student has higher marks and "=" means that both have equal marks.

输出格式

Output consists of nn integers separated by a space representing the number of toffees each student receives in the queue starting from the first one to the last one.

题意翻译

有n个正整数a1,...,an,给出ai和ai+1的大小关系序列s1...sn−1,si为=,L,R分别表示ai等于、大于、小于ai+1,求满足条件的序列中这n个数的和最小对应的序列

输入输出样例

输入 #1

5
LRLR

输出 #1

2 1 2 1 2

输入 #2

5
=RRR

输出 #2

1 1 2 3 4

题解:

我们用一个a[i] 表示第i个小朋友的糖果,则:

  • 若字符为R,则a[i]=a[i-1]+1;
  • 若字符为L,则a[i]=1,此时如果a[i-1]也是1,那么对应向前推进,a[i-1]+1
  • 若字符为=,则a[i]=a[i-1];
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ull;
int main(){
	int n;
	int a[1005];
	scanf("%d",&n);
	string str;
	cin>>str;
	int num=1;a[0]=1;
	for(int i=0;i<str.size();i++){
		if(str[i]=='=') a[num]=a[num-1];
		if(str[i]=='R') a[num]=a[num-1]+1;
		if(str[i]=='L'){
			a[num]=1;
			if(a[num-1]==1){
				for(int j=i;j>=0;j--){
					if(str[j]=='='){
						a[j]=a[j+1];
					}
					else if(str[j]=='L'&&a[j+1]==a[j]){
						a[j]++;
					}
					else break;
				}
			}
		}
		num++;
	}
	for(int i=0;i<n-1;i++) cout<<a[i]<<' ';
	cout<<a[n-1]<<endl;
}

CF102A Clothes

题目描述

A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.

Overall the shop sells nn clothing items, and exactly mm pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.

输入格式

The first input file line contains integers nn and mm — the total number of clothing items in the shop and the total number of matching pairs of clothing items (

).

Next line contains nn integers a_{i}ai​ ( 1<=a_{i}<=10^{6}1<=ai​<=106 ) — the prices of the clothing items in rubles.

Next mm lines each contain a pair of space-separated integers u_{i}ui​ and v_{i}vi​ ( 1<=u_{i},v_{i}<=n,u_{i}≠v_{i}1<=ui​,vi​<=n,ui​=vi​ ). Each such pair of numbers means that the u_{i}ui​ -th and the v_{i}vi​ -th clothing items match each other. It is guaranteed that in each pair u_{i}ui​ and v_{i}vi​ are distinct and all the unordered pairs (u_{i},v_{i})(ui​,vi​) are different.

输出格式

Print the only number — the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print "-1" (without the quotes).

题意翻译

有 nn 件服装,mm 组匹配关系,每件服装的价格为ai​。

请找出三件两两匹配的服装,求出最小的价格之和。如果找不到这样的三件服装,输出 -1

3≤n≤100,0≤m≤2n(n−1)​,1≤ai​≤10^6。

输入输出样例

输入 #1

3 3
1 2 3
1 2
2 3
3 1

输出 #1

6

输入 #2

3 2
2 3 4
2 3
2 1

输出 #2

-1

输入 #3

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

输出 #3

-1

题解:

这一题我们可以把它简化成是判断三个点之间有无连接的问题,因为数据范围比较小,我们可以使用弗洛伊德模板,因为自身求有无连接,所以不需要求最短路,有一下几点需要注意:

  1. 要双向建边,因为假如 A 和 B 有组合,那么 B 和 A 肯定也有
  2. 弗洛伊德时要去重,因为可能有两点或三个点重复的情况,如:i=j=k
  3. 如果没有答案为 INF 那么要输出 -1

AC代码:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ull;
int sum=INF;
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int mp[105][105];
	int a[100];
	memset(mp,0,sizeof(mp));
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=0;i<m;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		mp[x][y]=1;
		mp[y][x]=1;
	}
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			for(int k=j;k<=n;k++){
				if(mp[i][j]==1&&mp[j][k]==1&&mp[i][k]==1){
					int ans=a[i]+a[j]+a[k];
					sum=min(sum,ans);
				}
			}
		}
	}
	if(sum==INF){
		printf("-1\n");
	}
	else printf("%d\n",sum);
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瘾ิۣۖิۣۖิۣۖิꦿ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值