CS Academy Round #49 A, B, C

来源:互联网 发布:linux下输入ftp命令 编辑:程序博客网 时间:2024/05/21 12:42

A.Odd Sum

题目链接

https://csacademy.com/contest/round-49/task/odd-sum/statement/

题解

给组数让求其中两两组合可以形成奇数的个数
直接统计奇数偶数个数相乘

AC代码

#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int MAXM = 1e3+10;const int MAXN = 1e5+10;const int mod = 1e9+7;int arr[MAXN];int main(){    int n;    scanf("%d",&n);    int x;    int k2 = 0, k1 = 0;    for(int i = 0;i < n; i++) {        scanf("%d",&x);        if(x&1) k1++;        else k2++;    }    printf("%d\n",k1*k2);return 0;}

B.Money Machine

题目链接

https://csacademy.com/contest/round-49/task/money-machine/

题解

很裸的贪心

AC代码

#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int MAXM = 1e3+10;const int MAXN = 1e5+10;const int mod = 1e9+7;int arr[MAXN];int main(){    int n, c, x1, u, x2;    scanf("%d%d%d%d%d",&n,&c,&x1,&u,&x2);    int ans = 0;    for(int i = 1; i <= n; i++) {        if(c>=u && u < (n-i+1)*x2) {            c -= u; ans += x2;            //printf("%d %d %d\n",c,u,ans);        }        c = c + x1 + ans;    }    printf("%d\n",c);return 0;}

C.Max Substring

题目链接

https://csacademy.com/contest/round-49/task/max-substring/

题解

英语是硬伤啊,硬是看成了暴力KMP,字符串问题

时间复杂度O(n26)
判断
str[pos[i][1]+L]=str[pos[i][2]+L]=str[pos[i][3]+L]=....=str[pos[i][N]+L]在字符串中的位置其中L 为合适的字串大小
pos[i][1] 为i字符在主串中的位置1,2,3。。。

AC代码

//标称emmmmmmm...#include <bits/stdc++.h>using namespace std;#define LL long long#define CLR(a,b) memset(a,(b),sizeof(a))const int MAXM = 1e3+10;const int MAXN = 1e5*4;const int mod = 1e9+7;string str;vector<int> pos[26];int main(){    cin>>str;    int len = str.size();    // get the positions for all charactes    for(int i = 0; i < len; i++) {        pos[str[i]-'a'].push_back(i);    }    // get the max numgber of ocueences of a character    int maxx = 0;    for(int i = 0; i < 26; i++) {        maxx = max(maxx,(int)pos[i].size());    }    string mx_answer = "";    for(int i = 0; i < 26; i++) {        int sz = 1;        if((int)pos[i].size() != maxx) continue;        while(pos[i].back()+sz < len) {            char ch = str[pos[i][0]+sz];            bool flag = true;            //for(auto it = pos[i].begin(); it != pos[i].end(); it++)            for(auto it : pos[i]) {                if(str[it+sz] != ch) flag = false;            }            // the end characters are not equal            if(!flag) break;            // all good ,continue trying to increase size            sz++;        }        // actually compute a string of length 'size' to compare it with the max string        string c_str = "";        for(int j = 0; j < sz; j++) {            c_str += str[pos[i][0]+j];        }        // if the size is bigger or the string is smaller lexicographically        // update the answer        if(sz>(int)mx_answer.size() || sz>c_str.size())            mx_answer = c_str;    }    cout<<mx_answer<<'\n';return 0;}
原创粉丝点击