codeforces 339A. Helpful Maths,B. Xenia and Ringroad

 A. Helpful Maths

题目链接: codeforces 339A

题意:

   给个字符串,要求将其中的数字从小到大排序

题解:

   简单模拟

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n, m;
	string s;
	while(cin >> s){
		int a[105], j = 0;
		for(int i = 0; i < s.size(); i++){
			if(s[i] - '0' == 1 || s[i] - '0' == 2 || s[i] - '0' == 3){
				a[j] = s[i]-'0';
				j++;
			}
		}
		sort(a, a+j);
		cout << a[0];
		for(int i = 1; i < j; i++){
			cout << "+" << a[i];
		}
		cout << endl;
	}
	return 0;
}

B. Xenia and Ringroad

题目链接:codeforces 339B

题意:

   输入 n和m ,第 i 个点的任务只能在 ai 的位置完成,而且只能按顺序完成,往后走一步的花费为 1,求总花费。

4 3

3 2 3

样例说明,总共有4个点,3个任务, 第一个任务只能在 第三个 点完成,第二个任务只能在 第二个 点完成,第三个任务只能在第三个点完成,不能先做第二个任务在完成第一个任务按顺序进行.

输出6, 从第一个点开始  1 → 2 → 3 (完成第一个任务)→ 4 → 1 → 2(完成第二个任务) → 3(完成第三个任务).

题解:

    明显,后一个比前一个的值小,那么它得再走一圈,然后加上最后一个位置

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int main(){
	int a[maxn];
	ll ans = 0, n, m;
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> a[i];
	}
	for(int i = 2; i <= m; i++){
		if(a[i] < a[i-1]){
			ans++;
		}
	}
	ans = ans * n + a[m] - 1;
	cout << ans << endl;
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值