1. 程式人生 > >[日常水題 3.17] codeforces 946 A

[日常水題 3.17] codeforces 946 A

  • 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; }
  • 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; }
  • 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;
}