[日常水题 3.17] codeforces 946 A - C

A. Partition

  • greedy
  • 一组数,任意分成两个部分 .求这两个部的最大差。
#include <bits/stdc++.h>  
//#define LOCAL_DEFINE
using namespace std;

int main(void) {
    ios::sync_with_stdio(false);cin.tie(0);       
#ifdef LOCAL_DEFINE    
    freopen("input.txt", "rt", stdin);    
#endif     

    int t, n;
    int v1 = 0, v2 = 0;
    cin >> t;
    while(t--) {
        cin >> n;
        if(n >= 0) v1 += n;
        else v2 += n;
    }
    cout << v1 - v2 << endl;



#ifdef LOCAL_DEFINE    
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";    
#endif 
        return 0;
}

B. Weird Subtraction Process

  • number theory
  • 描述见题意,写三个函数,注意一些小优化就好。
#include <bits/stdc++.h>
//#define LOCAL_DEFINE
using namespace std;

typedef unsigned long long ull;
ull a, b;
void step1();
void step2();
void step3();

void step1() {
    if (!a || !b) 
        return;
    else
        step2();
}
void step2() {
    if(a >= 2 * b) {
        ull t = a / (2 * b);
        a = a - t * (2 * b);
        step1();
    }
    else 
        step3();
}
void step3() {
    if(b >= 2 * a) {
         ull t = b / ( 2 * a);
         b = b - t * (2 * a);
         step1();
    }
    else 
        return;
}
int main(void) {
    ios::sync_with_stdio(false);cin.tie(0);
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    cin >> a >> b;
    step1();
    cout << a << " " << b << endl;

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
        return 0;
}

C. String Transformation

  • greedy
  • 描述 问你能不能把给定串,变成另外一个串,使得“abcdefghijklmnopqrstuvwxyz”是它的子序列,操作是对原串任意字符做 +1 操作,不限次数。
#include <bits/stdc++.h>
//#define LOCAL_DEFINE
using namespace std;
string s; 
char ch = 'a';

int main(void) {
    ios::sync_with_stdio(false);cin.tie(0);
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif
    cin >> s;
    for(int i = 0; i < (int)s.size(); ++i) {
        if(s[i] == ch || s[i]  <  ch) {
            if(s[i] < ch) s[i] = s[i] + (ch - s[i]);
            ch = ch + 1;
        }
        if(ch == 'z' + 1) puts(s.c_str()), exit(0);
    }
    puts("-1");

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值