zoukankan      html  css  js  c++  java
  • CodeForces div3 第一场

    A Wrong Subtraction

    题意: 对于一个数操作n次,操作如下: 如果末尾是0就将这个数除以10, 如果末尾不是0就将这个数-1, 直接做就好了。

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 1e5+10;
    17 int main(){
    18     ///Fopen;
    19     int n, k;
    20     scanf("%d%d", &n, &k);
    21     while(k--){
    22         int t = n%10;
    23         if(t) n--;
    24         else n/=10;
    25     }
    26     printf("%d", n);
    27     return 0;
    28 }
    View Code

    B Two-gram

    题意:求2个连续字符出现次数最多的那2个字符。

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 1e3+10;
    17 int cnt[N][N];
    18 char str[N];
    19 int main(){
    20     ///Fopen;
    21     int n;
    22     scanf("%d", &n);
    23     scanf("%s", str);
    24     int ans = -1, s1, s2;
    25     for(int i = 0; i < n-1; i++){
    26         int x = str[i]-'A';
    27         int y = str[i+1]-'A';
    28         cnt[x][y]++;
    29         if(cnt[x][y] > ans){
    30             ans = cnt[x][y];
    31             s1 = x;
    32             s2 = y;
    33         }
    34     }
    35     printf("%c%c",s1+'A',s2+'A');
    36     return 0;
    37 }
    View Code

    C Less or Equal

    题意:给你一个数组, 要求找到一个数在[1,1e9]之间,使得恰好有k个数小于等于他,如果没有输出-1, 有就输出任意一个合法答案。

    题解:sort一下数组, 如果第k个数等于第k+1个数就找不到一个数满足题意了。 还有0个的情况。

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 2e5+10;
    17 int n, m;
    18 int A[N];
    19 int main(){
    20     ///Fopen;
    21     scanf("%d%d", &n, &m);
    22     for(int i = 1; i <= n; i++)
    23         scanf("%d", &A[i]);
    24     sort(A+1, A+1+n);
    25     if(m == 0){
    26         if(A[1] == 1) printf("-1");
    27         else printf("1");
    28     }
    29     else if(m == n){
    30         printf("%d", (int)1e9);
    31     }
    32     else{
    33         if(A[m] == A[m+1]) printf("-1");
    34         else printf("%d", A[m]);
    35     }
    36     return 0;
    37 }
    View Code

    D Divide by three, multiply by two

    题意:将题目给的数重新排列,使得前一个数是后一个数的3倍,或者后一个数是前一个数2倍的情况最多。

    题解:这个特殊的数列是不可能形成一个环的。DFS找一下就可以了,从长的开始输出。(也有更简单的方法, 但是当时写了DFS就DFS了。。。)

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 1e2+10;
    17 int n, m;
    18 LL A[N];
    19 int cnt[N];
    20 LL ans[N];
    21 map<LL,int> mp;
    22 void dfs(LL u){
    23     cnt[mp[u]] = 1;
    24     if(u%3 == 0 && mp.count(u/3)){
    25         if(cnt[mp[u/3]] == -1)
    26             dfs(u/3);
    27         cnt[mp[u]] = max(cnt[mp[u]],1+cnt[mp[u/3]]);
    28     }
    29     if(mp.count(u*2)){
    30         if(cnt[mp[u*2]] == -1)
    31             dfs(u*2);
    32         cnt[mp[u]] = max(cnt[mp[u]],1+cnt[mp[u*2]]);
    33     }
    34 }
    35 void Show(LL u, int h){
    36     printf("%I64d ", u);
    37     A[mp[u]] = 0;
    38     h--;
    39     if(h == 0) return ;
    40     if(u%3 == 0 && mp.count(u/3) && cnt[mp[u/3]] == h && A[mp[u/3]] != 0)
    41         Show(u/3,h);
    42 
    43     else if(mp.count(u*2)
    44             && A[mp[u*2]] != 0
    45              && cnt[mp[u*2]] == h){
    46         Show(u*2,h);
    47     }
    48 }
    49 int main(){
    50     scanf("%d", &n);
    51     memset(cnt, -1, sizeof(cnt));
    52     for(int i = 1; i <= n; i++){
    53         scanf("%I64d", &A[i]);
    54         mp[A[i]] = i;
    55     }
    56     for(int i = 1; i <= n; i++){
    57         if(cnt[i] == -1){
    58             dfs(A[i]);
    59         }
    60 
    61     }
    62     while(1){
    63         int Max = 0, save;
    64         for(int i = 1; i <= n; i++){
    65             if(cnt[i] >= Max && A[i] != 0)
    66                 Max = cnt[i], save = i;
    67         }
    68         if(Max == 0) break;
    69         Show(A[save], Max);
    70     }
    71     return 0;
    72 }
    View Code

    E Cyclic Components

    题意:求环的个数, 这个环需要这个环上的点都只有2条边。

    题解:DFS一下, 从某个点出发, 如果按照一个方向走能回到该点, 并且路上的点都只有2条边, 就cnt++;

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 1e5+10;
    17 vector<int> son[2*N];
    18 int vis[N*2];
    19 int cnt = 0;
    20 void dfs(int u, int l, int v){
    21     if(u == v){cnt++; return ;}
    22     if(vis[v]) return ;
    23     vis[v] = 1;
    24     if(son[v].size() == 2){
    25         if(l == son[v][0]) dfs(u,v, son[v][1]);
    26         else dfs(u,v,son[v][0]);
    27     }
    28 }
    29 int main(){
    30     ///Fopen;
    31     int n, m;
    32     scanf("%d%d", &n, &m);
    33     for(int i = 1; i <= m; i++){
    34         int u, v;
    35         scanf("%d%d", &u, &v);
    36         son[u].pb(v);
    37         son[v].pb(u);
    38     }
    39     for(int i = 1; i <= n; i++){
    40         if(!vis[i]) {
    41             vis[i] = 1;
    42             if(son[i].size() == 2){
    43                 dfs(i, i, son[i][0]);
    44             }
    45         }
    46     }
    47     printf("%d", cnt);
    48     return 0;
    49 }
    View Code

    F. Consecutive Subsequence

    题意:求最长的递增子序列。 这个序列的前一项与后一项相差为1。

    题解:开一个map, 映射一下某个值上一次访问的位置,然后每次处理一个新的点, 如果比这个点小1的值的mp不为0, 就将这个点的位置指向上一个比他小1的点,记录长度, 如果没有比他小1的点, 就指向-1, 然后长度记为1, 然后每次更新最长的值 和 相应的位置就好了。

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
     4 #define LL long long
     5 #define ULL unsigned LL
     6 #define fi first
     7 #define se second
     8 #define pb push_back
     9 #define lson l,m,rt<<1
    10 #define rson m+1,r,rt<<1|1
    11 #define max3(a,b,c) max(a,max(b,c))
    12 #define min3(a,b,c) min(a,min(b,c))
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const LL mod = 1e9+7;
    16 const int N = 2e5+10;
    17 map<int,int> mp;
    18 int cnt[N];
    19 int pre[N];
    20 int A[N];
    21 int Max = 0, B = 0;
    22 void Show(int u){
    23     if(u == -1) return;
    24     Show(pre[u]);
    25     printf("%d ", u);
    26 }
    27 int main(){
    28     ///Fopen;
    29     int n;
    30     scanf("%d", &n);
    31     for(int i = 1; i <= n; i++)
    32         scanf("%d", &A[i]);
    33     for(int i = 1; i <= n; i++){
    34         int t = A[i]-1;
    35         if(mp.count(t)){
    36             pre[i] = mp[t];
    37             cnt[i] = cnt[pre[i]]+1;
    38         }
    39         else pre[i] = -1, cnt[i] = 1;
    40         if(Max < cnt[i]){
    41             Max = cnt[i];
    42             B = i;
    43         }
    44         mp[A[i]] = i;
    45     }
    46     printf("%d
    ", cnt[B]);
    47     Show(B);
    48     return 0;
    49 }
    View Code
  • 相关阅读:
    Maria 与Ann的故事
    引语
    Preface
    Chapter 1 Foundation
    Roman to Integer
    Integer to Roman
    Container with most water
    palindrome number
    String to Integer (atoi)
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/MingSD/p/9036282.html
Copyright © 2011-2022 走看看