1. 程式人生 > >CF變紅之路_Codeforces Round #401 (Div. 2)

CF變紅之路_Codeforces Round #401 (Div. 2)

A

很好的思維題, 初始位置就三個,然後列舉發現週期都為6

操作次數+位置變化
操作次數 0 1 2 3 4 5 
初始位置分別是0, 1, 2時的變化
    0 1 2 2 1 0
    1 0 0 1 2 2
    2 2 1 0 0 1 

很明顯發現規律,打表即可 程式碼

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1000 + 10, inf = 0x3f3f3f3f;

int n, idx;

int ans[] = {0, 1, 2, 2, 1, 0};

void solve() {
    int d = n % 6
; for(int i = 0; i < 3; i++) { int t = (d - i*2 + 6) % 6; if(ans[t] == idx) { cout << i << endl; break; } } } int main() { //freopen("input.txt", "r", stdin); while(cin >> n >> idx) { solve(); } return
0; }

B

給出兩串數字,每串數字中數字順序不固定,找到某個順序使得上面數字按位大於下面數字的次數最少,輸出該次數ans1。然後在輸出下面數字按位大於上面數字的次數最多的次數ans2。

#include <bits/stdc++.h>
using namespace std;

int a[1010];
int b[1010];

int main() {
    //freopen("input.txt", "r", stdin);
    int n;
    cin  >> n;
    getchar();
    for(int i = 0; i < n; i++) {
        char
c = getchar(); a[i] = c - '0'; } getchar(); for(int i = 0; i < n; i++) { char c = getchar(); b[i] = c - '0'; } sort(a, a+n); sort(b, b+n); int i = n-1, j = n-1, ans2 = 0; while(i >= 0 && j >= 0) { if(b[j] > a[i]) { i--, j--, ans2++; } else i--; } i = 0, j = 0; int ans1 = 0; while(i < n && j < n) { if(a[i] <= b[j]) { i++, j++; } else { j++; } } ans1 = n - i; cout << ans1 << endl; cout << ans2 << endl; return 0; }

c(好題)

這道題很好,預處理思想,那個n*m <= 100000的條件也是極好的,二維陣列是開不了了,這能開1e5的一維陣列,邊讀入邊處理,這個寫法我還是看一個大神的部落格才學會的。 v[j]記錄第j列上一行的讀入資料,cd[j] = x記錄著第j列第x行到讀入的這一行滿足非遞減的條件,然後d[i] = x說明x 到 i這些行滿足非遞減的條件。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int v[maxn], d[maxn], cd[maxn];
int n, m;

int main() {
    // freopen("input.txt", "r", stdin);
    cin >> n >> m;
    int tmp;
    for(int i = 1; i <= n; i++) {
        d[i] = i;
        for(int j = 1; j <= m; j++) {
            cin >> tmp;
            if(tmp < v[j])  cd[j] = i;
            v[j] = tmp;
            if(cd[j] < d[i]) d[i] = cd[j];
        }
    }
    int q, l, r;
    cin >> q;
    while(cin >> l >> r) {
        if(d[r] <= l) {
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
    }
    return 0;
}