Codeforces Round #601 (Div. 2)

Codeforces Round #601 (Div. 2)

A. Changing Volume

  • 思路:水题

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int t;
ll a, b, ans;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t -- ){
        cin >> a >> b;
        if (a == b)
            cout << "0\n";
        else{
            ll tmp = abs(a - b);
            ans = tmp / 5 + tmp % 5 / 2 + tmp % 5 % 2;
            cout << ans << "\n";
        }
    }
    return 0;
}

B. Fridge Lockers

  • 思路:贪心

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1010;

int t, n, m, ans, res;

struct A{
    int a, id;
}a[N];

inline bool cmp(const A &a1, const A &a2){
    return a1.a < a2.a;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t -- ){
        ans = 0;
        cin >> n >> m;
        res = m - n;
        for (int i = 1; i <= n; i ++ ){
            cin >> a[i].a;
            a[i].id = i;
        }
        if (m < n || n == 2){
            cout << "-1\n";
            continue;
        }
        sort(a + 1, a + n + 1, cmp);
        for (int i = 1; i <= n; i ++ )
            ans += a[i].a;
        ans *= 2;
        for (int i = 1; i <= res; i ++ )
            ans += a[1].a + a[2].a;
        cout << ans << "\n";
        for (int i = 1; i < n; i ++ )
            cout << a[i].id << " " << a[i + 1].id << "\n";
        cout << a[n].id << " " << a[1].id << "\n";
        for (int i = 1; i <= res; i ++ )
            cout << a[1].id << " " << a[2].id << "\n";
    }
    return 0;
}

C. League of Leesins

  • 思路:每次更新起点 与起点有关系的点只有两个 然后把与起点有关的点从集合中删除 重新找起点

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;

int n, tot, q1, q2, q3, beg, en;
int vis[N], ans[N];
bool flag;
set<int> st[N];

void dfs(int x, int cnt){
    if (cnt == n - 1)
        return ;
    for (auto it = st[x].begin(); it != st[x].end(); it ++ )
        if (vis[*it] != 2)
            st[*it].erase(x);
    for (auto it = st[x].begin(); it != st[x].end(); it ++ ){
        if (!vis[*it] && st[*it].size() == 2){
            ans[ ++ tot ] = *it;
            vis[*it] = 1;
            dfs(*it, cnt + 1);
        }
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n - 2; i ++ ){
        cin >> q1 >> q2 >> q3;
        st[q1].insert(q2), st[q1].insert(q3);
        st[q2].insert(q1), st[q2].insert(q3);
        st[q3].insert(q1), st[q3].insert(q2);
    }
    for (int i = 1; i <= n; i ++ ){
        if (!flag && st[i].size() == 2){
            beg = i;
            flag = true;
        }
        else if (st[i].size() == 2){
            en = i;
            break;
        }
    }
    ans[ ++ tot ] = beg;
    vis[beg] = 1;
    for (auto it = st[en].begin(); it != st[en].end(); it ++ )
        vis[*it] = 2;
    dfs(beg, 1);
    for (auto it = st[en].begin(); it != st[en].end(); it ++ )
        if (vis[*it] == 2 && st[*it].size() == 4)
            ans[ ++ tot ] = *it;
    for (auto it = st[en].begin(); it != st[en].end(); it ++ )
        if (vis[*it] == 2 && st[*it].size() == 3){
            ans[ ++ tot ] = *it;
            ans[ ++ tot ] = en;
            break;
        }
    for (int i = 1; i <= tot; i ++ )
        cout << ans[i] << " ";
    return 0;
}

D. Feeding Chicken

  • 思路:类似于蛇形构造 考场降智题意读复杂了

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#define debug(x) cout << #x << ": " << x << "\n"
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 110;

int t, r, c, k, cnt, tot, x, y, xx;
char mp[N][N];
vector<char> ch;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    for (char i = '0'; i <= '9'; i ++ )
        ch.push_back(i);
    for (char i = 'A'; i <= 'Z'; i ++ )
        ch.push_back(i);
    for (char i = 'a'; i <= 'z'; i ++ )
        ch.push_back(i);
    cin >> t;
    while (t -- ){
        cin >> r >> c >> k;
        cnt = 0, tot = 0;
        for (int i = 1; i <= r; i ++ ){
            for (int j = 1; j <= c; j ++ ){
                cin >> mp[i][j];
                if (mp[i][j] == 'R')
                    cnt ++ ;
            }
        }
        x = cnt / k, y = cnt % k;
        xx = x;
        if (y){
            xx ++ ;
            y -- ;
        }
        for (int i = 1; i <= r; i ++ ){
            if (i & 1){
                for (int j = 1; j <= c; j ++ ){
                    if (mp[i][j] == 'R'){
                        xx -- ;
                        cnt -- ;
                    }
                    mp[i][j] = ch[tot];
                    if (!cnt){
                        mp[i][j] = ch[tot];
                        continue;
                    }
                    if (!xx){
                        if (y){
                            xx = x + 1;
                            y -- ;
                        }
                        else
                            xx = x;
                        tot ++ ;
                    }
                }
            }
            else{
                for (int j = c; j >= 1; j -- ){
                    if (mp[i][j] == 'R'){
                        xx -- ;
                        cnt -- ;
                    }
                    mp[i][j] = ch[tot];
                    if (!cnt){
                        mp[i][j] = ch[tot];
                        continue;
                    }
                    if (!xx){
                        if (y){
                            xx = x + 1;
                            y -- ;
                        }
                        else
                            xx = x;
                        tot ++ ;
                    }
                }
            }
        }
        for (int i = 1; i <= r; i ++ ){
            for (int j = 1; j <= c; j ++ )
                cout << mp[i][j];
            cout << "\n";
        }
    }
    return 0;
}

E2. Send Boxes to Alice (Hard Version)

  • 思路:参考dalao的思路 枚举sum的质因数x 对于每个x 计算\(a_i\)的前缀和\(sum_i\) 因为要求\(a_i % x = 0\) 所以\(sum_i % x = 0\) 如果\(sum_i % x != 0\) 对余数凑成x 每次取花费最少的然后算总和取最小值

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e6 + 10;
const ll INF = 1e18;

int n;
int a[N];
ll sum, ans;

void calc(ll x){
    ll cnt = 0, now = 0;
    for (int i = 1; i <= n; i ++ ){
        now = (now + a[i]) % x;
        cnt += min(now, x - now);
    }
    ans = min(ans, cnt);
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ans = INF;
    cin >> n;
    for (int i = 1; i <= n; i ++ ){
        cin >> a[i];
        sum += a[i];
    }
    if (sum == 0){
        cout << "0\n";
        return 0;
    }
    if (sum == 1){
        cout << "-1\n";
        return 0;
    }
    for (ll i = 2; i * i <= sum; i ++ ){
        if (sum % i == 0){
            calc(i);
            while (sum % i == 0)
                sum /= i;
        }
    }
    if (sum > 1)
        calc(sum);
    cout << ans << "\n";
    return 0;
}
posted @ 2019-11-22 09:09  Misuchii  阅读(138)  评论(0编辑  收藏  举报