Codeforces Round #307 (Div. 2) A、B

来源:互联网 发布:js split数组 编辑:程序博客网 时间:2024/05/25 21:36

551A - GukiZ and Contest
直接结构体排序即可,不多说了。

#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <ctype.h>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>#include <assert.h>#include <time.h>#include <sstream>typedef long long LL;const int INF = 500000001;const double EPS = 1e-9;const double PI = acos(-1.0);using namespace std;struct p{    int v;    int id;    int ans;}s[2005];int cmp(const void *a, const void *b){    struct p c = *(struct p *)a;    struct p d = *(struct p *)b;    return d.v - c.v;}int cmp2(const void *a, const void *b){    struct p c = *(struct p *)a;    struct p d = *(struct p *)b;    return c.id - d.id;}int main(){    int n;    while(~scanf("%d", &n))    {        for(int i = 0; i < n; i++)        {            scanf("%d", &s[i].v);            s[i].id = i;        }        qsort(s, n, sizeof(s[0]), cmp);        s[0].ans = 1;        int tmp = s[0].v;        int trank = 1;        for(int i = 1; i < n; i++)        {            if(s[i].v == tmp)            {                s[i].ans = trank;            }            else            {                tmp = s[i].v;                trank = i + 1;                s[i].ans = i + 1;            }        }        qsort(s, n, sizeof(s[0]), cmp2);        for(int i = 0; i < n; i++)        {            printf(i?" %d":"%d", s[i].ans);        }        puts("");    }    return 0;}

551B - ZgukistringZ
比赛时用了一种错误的做法做的。。
错误思路是用数组下标分别求出a, b, c三个串中字符的个数。
然后令a能拼出子串b的最大的个数为max1.
a能拼出子串c的最大的个数为max2.
然后把判断一下max1与max2的大小,并把对应的子串在a字符中减掉。
之后处理剩余a中的字符是否能拼出较小个数的那个子串。处理之后输出剩余的字符。。。。
以上思路是错误的。。

给两组数据:

ffccccaa
acc
fc
fffccccccccaaaa
fc
acc
Answer:
fcfcacca
fcfcfcaccacccaa

最后发现直接枚举b和c的个数即可。
假设字符串a中有b子串为x。枚举此时a字符串减掉x个b串剩余的字符串能拼成几个子串c。

#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <ctype.h>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>#include <assert.h>#include <time.h>#include <sstream>typedef long long LL;const int INF = 500000001;const double EPS = 1e-9;const double PI = acos(-1.0);using namespace std;int f[3][27], tmp[27];int gao(int k, char *s, int len){    for(int i = 0; i < len; i++)    {        f[k][s[i]-'a']++;    }}int work(int k, int v){    for(int i = 0; i < 26; i++)    {        f[0][i] -= f[k][i] * v;    }}int main(){    #ifdef _Test        freopen("test0.in", "r", stdin);        freopen("test0.out", "w", stdout);        srand(time(NULL));    #endif    char a[100005], b[100005], c[100005];    int len1, len2, len3;    while(~scanf("%s %s %s", a, b, c))    {        memset(f, 0, sizeof(f));        len1 = strlen(a);        len2 = strlen(b);        len3 = strlen(c);        gao(0, a, len1);        gao(1, b, len2);        gao(2, c, len3);        memcpy(tmp, f[0], sizeof(tmp));        int lb, lc;        int maxx = -1;        for(int s = 0; ;s++)        {            int res = INF;            for(int i = 0; i < 26; i++)            {                if(f[2][i]) res = min(res, f[0][i] / f[2][i]);            }            if(res + s > maxx)            {                maxx = res + s;                lc = res;                lb = s;            }            bool flag = false;            for(int i = 0; i < 26; i++)            {                if(f[0][i] < f[1][i])                {                    flag = true;                    break;                }                else                {                    f[0][i] -= f[1][i];                }            }            if(flag) break;        }        for(int i = 0; i < lb; i++)        {            printf("%s", b);        }        for(int i = 0; i < lc; i++)        {            printf("%s", c);        }        memcpy(f[0], tmp, sizeof(tmp));        work(1, lb);        work(2, lc);        for(int i = 0; i < 26; i++)        {            for(int j = 0; j < f[0][i]; j++)            {                printf("%c", 'a' + i);            }        }        puts("");    }    return 0;}
0 0
原创粉丝点击