CODEFORCES 704 div2 題解(e待補)

 

 題目ios

 

 A數組

題意:給四個正整數p,a,b,c  。有三位游泳的人分別在0,a ,2a,3a....;0,b,2b,3b....;0,c,2c,3c.... 的時間來到岸邊,另外一人將會在P的時間時來到岸邊,問p要等多久才能等到至少一我的。spa

思路:若是p能夠整除a,b,c中的某一個則答案爲0;不然答案是n -(p%n)的最小值, n爲a,b,c。code

#include<iostream>
typedef long long ll;
#define rep(a,n) for(a=1;a<=n;++a)
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    ll p, a, b, c, t;
    cin >> t;
    while (t--) {
        cin >> p >> a >> b >> c;
        if (p % a == 0 || p % b == 0 || p % c == 0)cout << 0 << endl;
        else cout << min(min(a - p % a, b - p % b), c - p % c) << endl;
    }
    return 0;
}

 

 

  Bblog

 

題意:只可意會,不可傳言(實際上是不會,多附一張圖ci

 

 思路:總之咱們要讓大的儘量在前面,因爲每一個數只會用一次,咱們能夠想辦法把用過的數標記下來,同時咱們還應該知道每一個數在數組的下標,而後從後開始遍歷;字符串

#include<iostream>
typedef long long ll;
#define rep(a,n) for(a=1;a<=n;++a)
const  ll maxn = 1e5 + 5;
using namespace std;
int sz[maxn], ans[maxn];
struct p {
    int index;//儲存數在sz數組中的下標
    bool us = false;//判斷這個數是否被用過
}po[maxn];
int main() {
    ios::sync_with_stdio(false);
    ll t, n, i, j, temp;
    cin >> t;
    while (t--) {
        cin >> n;
        rep(i,n)po[i].us = false;
        temp = n;
        int cnt = 0, cou = 0;
        rep(i,n) {
            cin >> sz[i];
            po[sz[i]].index = i;
        }
        for (i = n; i > 0; i--) {
            if (!po[i].us) {
                for (j = po[i].index; j <= temp; ++j) {
                    ans[cou++] = sz[j];
                    po[sz[j]].us = true;
                }
                temp = po[i].index - 1;//更新temp
            }
        }
        cout << ans[0];
        for (i = 1; i < cou; ++i)cout << " " << ans[i];
        cout << endl;
    }
    return 0;
}

 

 

  

 

  Cit

 

題意:兩個字符串a,b,其中b是a的子串,求最大的寬度,寬度的定義如題io

思路:很容易想到貪心。從前日後遍歷一遍,獲得可能的狀況中b中全部字母都映射在a中的最前面,儲存在pre;從後遍歷一遍,獲得映射在最後面的結果,儲存在last中;ast

隨後不斷更新,over~

 

#include<iostream>
#include<algorithm>
typedef long long ll;
#define rep(a,n) for(a=1;a<=n;++a)
const  ll maxn = 2e5 + 5;
using namespace std;
char a[maxn], b[maxn];
int pre[maxn], last[maxn];
int main() {
   ios::sync_with_stdio(false);
int n, m, i, j = 1, ans=0; cin >> n >> m; rep(i, n)cin >> a[i]; rep(i, m)cin >> b[i]; for (i = 1; j <= m && i <= n; ++i) if (a[i] == b[j]) pre[j++] = i; for (i = n,j=m; j > 0 && i > 0; i--)if (a[i] == b[j])last[j--] = i; rep(i, m-1)ans = max(ans, last[i + 1] - pre[i]); cout << ans << endl; return 0; }

 D

題意: 構造兩個二進制的數,兩個數均含有a個0,b個1,它們的差(二進制形式)中含有k個0。PS:全部數中均不可有前導零。

思路:首先特判一下k==0||a==0||b==1的狀況,而後考慮通常狀況。

對於11xxxxxx0xxx  這樣的狀況,二者相減等於001111111000  (x爲0或者1且相等);這就是構造的方式,除了k>a+b-2的時候均可以構造;

      10xxxxxx1xxx                                                                                              代碼已經呼之欲出了(雖然可是,寫的仍是差點心態崩了

 

 

#include<iostream>
#include<algorithm>
typedef long long ll;
const ll mod = 1000000007;
const ll maxn = 2e5 + 5;
const ll inf = 0x3f3f3f3f;
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    ll t, n, i, j, a, temp, k, res, flag, b, c;
    cin >> a >> b >> k;
    if (k == 0) {
        cout << "YES" << endl;
        for (i = 1; i <= b; ++i)cout << 1;
        for (i = 1; i <= a; ++i)cout << 0;
        cout << endl;
        for (i = 1; i <= b; ++i)cout << 1;
        for (i = 1; i <= a; ++i)cout << 0;
        cout << endl;
    }
    else {
        if (a == 0 || b == 1||k>a+b-2)cout << "NO" << endl;
        else {
            cout << "YES" << endl;
            cout << 11;
            int s0 = a - 1, s1 = b - 2;
            for (i = 3; i < 2 + k; ++i) {
                if (s0) {
                    cout << 0;
                    s0--;
                }
                else if (s1) {
                    cout << 1;
                    s1--;
                }
            }
            cout << 0;
            while (1) {
                if (s0 > 0) {
                    cout << 0;
                    s0--;
                }
                else break;
            }
            while (1) {
                if (s1 > 0) {
                    cout << 1;
                    s1--;
                }
                else break;
            }
            cout << endl;
            s0 = a - 1, s1 = b - 2;
            cout << 10;
            for (i = 3; i < 2 + k; ++i) {
                if (s0>0) {
                    cout << 0;
                    s0--;
                }
                else if (s1 > 0) {
                    cout << 1;
                    s1--;
                }
            }
            cout << 1;
            while (1) {
                if (s0 > 0) {
                    cout << 0;
                    s0--;
                }
                else break;
            }
            while (1) {
                if (s1 > 0) {
                    cout << 1;
                    s1--;
                }
                else break;
            }
            cout << endl;
        }
    }
    return 0;
}

 E 待補;

相關文章
相關標籤/搜索