codeforce 375_2_b_c

codeforce 375_2

标签: 水题


好久没有打代码,竟然一场比赛两次卡在边界条件上。。。。跪
b.题意很简单。。。纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 300;
char a[N];
int main()
{
    int n;
    int l;
    int sum;
    int tm;
    int Max;
    while(~scanf("%d",&n))
    {
        getchar();
        gets(a);
        a[n] = '_';
        l = tm = sum = Max = 0;
        for(int i = 0; i <= n; i++)
        {
            if(a[i]!='('&&a[i]!=')'&&a[i]!='_'){
                tm++;
            }
            else if(a[i]=='(') {
                if(l==0&&tm>0) Max = max(Max,tm);
                if(l>0&&tm>0) sum++;
                l++;
                tm = 0;
            }
            else if(a[i]==')'){
                l--;
                if(tm>0) sum++;
                tm = 0;
            }
            else if(a[i]=='_'){
                if(l==0){
                    if(tm>0) Max = max(Max,tm);
                }
                else if(l>0){
                    if(tm>0) sum++;
                }
                tm = 0;
            }
        }
        printf("%d %d
",Max,sum);
    }
    return 0;
}

c.题意:一个巧妙的写法,已知最后每个数字最后应该出现的位置个数是([n/m])那么我们可以从前到后扫描一遍,遇到非法的位置,将这个位置换成一个需要被替换的值(要先处理好每个数已经有了几个,换成个数不够的数字就好了)但是。。。我又脑残的忘记了top要限制<m不然有可能越界。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2010;
struct Node{
    int num;
    int all;
    bool operator <(const Node n) const
    {
        return all<n.all;
    }
}cnt[N];
int mp[N];
int vis[N];
int jd[N];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int tm = n/m;
        int c = 0;
        for(int i = 1; i <= m; i++) cnt[i].num = i,cnt[i].all = 0;
        for(int i = 0; i < n; i++){
            scanf("%d",&mp[i]);
            if(mp[i]<=m){
                cnt[mp[i]].all++;
            }
        }
        int sum = 0;
        for(int i = 1; i <= m; i++){
            if(cnt[i].all<tm) sum+=(tm-cnt[i].all);
        }
        sort(cnt+1,cnt+m+1);
        int top = 1;
        memset(vis,0,sizeof(vis));
        for(int i = 0; i < n; i++){
            if(mp[i]<=m&&vis[mp[i]]<tm) {
                vis[mp[i]]++;
            }
            else {
                if(cnt[top].all >= tm) {
                    top++;
                    if(cnt[top].all>=tm) break;
                }
                mp[i] = cnt[top].num;
                cnt[top].all++;
                vis[mp[i]]++;
            }
        }
        printf("%d %d
",tm,sum);
        for(int i = 0; i < n-1; i++){
            printf("%d ",mp[i]);
        }
        printf("%d
",mp[n-1]);
    }
}

原文地址:https://www.cnblogs.com/shanyr/p/5968696.html