CodeForces雜題/1月訓練

1.#490 div3

A. Mishka and Contest

 

解題思路:

水題,從左到右掃,再從右到左掃,無需在此特判,只需要在最後特判即可。

#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
using namespace std;
deque<int> q;
bool f;
int n;
int k;
const int maxn = 105;
int a[maxn];
int main() {
    cin >> n >> k;
    int ans = 0;
    int i = 0;
    for(i = 0; i < n ; i++) {
        cin >> a[i];
    }
    i = 0;
    while(i < n && a[i]<= k) {
        ans++;
        i++;
    }
    int j = n - 1;
    while(j >= 0 && a[j] <= k) {
        ans++;
        j--;
    }
    ans > n ? printf("%d\n",ans/2):printf("%d\n",ans);
}

 

B. Reversing Encryption

 

題意:翻轉字符串中的字母。

解題思路:如果單純模擬的話還挺費時費力的。需要用到一個函數:

reverse
#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int n;
const int maxn = 105;
char s[maxn];
int main() {
    cin >> n;
    cin >> s;
    for(int i = 1; i <= n;i++) {
        if(n%i == 0){
            reverse(s,s+i);
        }
    }
    cout << s << endl;
}

 

C. Alphabetic Removals

 

 

題意:根據字典序按要求去除字符。

解題思路:模擬

#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int n, k;
const int maxn = 4e5+5;
char s1;
struct edge{
    char s;
    int pos;
    int f;
}point1[maxn],point2[maxn];
bool cmp(edge a,edge b) {
    if(a.s==b.s) return a.pos < b.pos;
    return a.s<=b.s;
}
int main() {
    cin >> n >> k;
    if(k>=n) cout <<endl;
    else {
        for (int i = 0; i < n; i++) {
            cin >> s1;
            point1[i].s = s1;
            point1[i].pos = i;
            point1[i].f = 1;
            point2[i] = point1[i];
        }
        sort(point2, point2 + n,cmp);
        for (int i = 0; i < k; i++) {
            int cnt = point2[i].pos;
            point1[cnt].f = 0;
        }
        for(int i = 0; i < n; i++) {
            if(point1[i].f)
                cout <<point1[i].s;
        }
        cout << endl;
    }
}

 

CodeForces 1234 div3

C. Pipes

 

好題目!

解題思路:DP判斷,其實只有兩種管道,1和2,且由於只有兩行,因此只需要考慮1和2直接的銜接即可。

#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int T;
const int maxn = 2e5 + 5;
char pipes[2][maxn];
int main() {
    cin >> T;
    while(T--) {
        int n;
        cin >> n;
        for(int i = 0; i < 2;i++) {
            for(int j = 0; j < n; j++){
                cin >> pipes[i][j];
            }
        }
        int now = 0;
        int j;
        for(j = 0; j < n; j++) {
            if(now == 0 && pipes[now][j] > '2') {
                if(pipes[1][j] <= '2') break;
                else now = 1;
            } else if(now == 1 && pipes[now][j] > '2') {
                if(pipes[0][j] <= '2') break;
                else now = 0;
            }
        }
        if(now == 1 && j == n) cout << "YES" << endl;
        else cout << "NO" << endl;

    }
    return 0;
}

 

D. Distinct Characters Queries

樹狀數組。

 

#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <vector>
using namespace std;
int T;
int len;
const int maxn = 1e5+5;
int c[27][maxn];
char s[maxn];
int lowbit(int x) {
    return x&(-x);
}
void update(int i,int j, int k) {
    while(j <= len) {
        c[i][j] += k;
        j+=lowbit(j);
    }
}
int queryy(int i,int j) {
    int ans = 0;
    while(j) {
        ans += c[i][j];
        j-=lowbit(j);
    }
    return ans;
}
int main() {
    cin >> (s+1);
    len = strlen(s+1);
    scanf("%d",&T);
    int q;
    int x,z;
    char y;
    for(int i = 1; i <= len; i++) update(s[i]-'a',i,1);
    for(int i = 1; i <= T; i++) {
        scanf("%d",&q);
        if(q == 1) {
            scanf("%d %c",&x,&y);
            update(s[x] - 'a', x, -1);
            update(y-'a',x,1);
            s[x] = y;
        }
        else {
            cin >> x >> z;
            int sum = 0;
            for(int j = 0; j < 26; j++) {
                if(queryy(j,z) - queryy(j,x-1)>=1) sum++;
            }
            cout << sum << endl;
        }
    }

    return 0;
}

 

Codeforces Beta Round #91 (Div. 2 Only) A. Lucky Division

 

#include <iostream>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
using namespace std;
deque<int> q;
bool f;
int n;
char s;
int main() {
    cin >> n;
    int a[]= {4,7,44,47,74,77,444,447,474,477,744,747,774,777};
    //int cnt = sizeof(a);
    //cout << cnt<<endl;
    for(int i = 0; i < 14; i++) {
        if(n % a[i] == 0) {f = 1;
            break;}

    }
    if(f) cout <<"YES" << endl;
    else cout << "NO" << endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章