Educational 25 ABCD

ABC都是靠细节的。

A

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
char str[100];
int out[20];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",str);
    int onum = 0,temp = 0;
    for(int i = 0; i < n; i++)
    {
        if(str[i] == '0')
        {
            out[onum++] = temp;
            temp = 0;
        }
        else   temp ++;
    }
    if(temp != 0) out[onum++] = temp;
    if(str[n-1] == '0') out[onum++] = 0;
    for(int i = 0; i < onum; i++)
        cout << out[i];
    cout << endl;
    return 0;
}

B 觉得写dfs麻烦,写完直接判断发现还不如来一个简单粗暴的dfs

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

char str[50][50];
char moni[50][50];
int game[50][50];
bool check(int x,int y)
{
    int mark = 1,flag = 1,abc = 1 ,def = 1;
    int thx = x,thy = y,num = 1;
    while(++thx < 10 && game[thx][thy] == 1) num++;
    thx = x;
    while(--thx >= 0 && game[thx][thy] == 1) num++;
    if(num >= 5) return true;
    thx = x,num = 1;
    while(++thy < 10 && game[thx][thy] == 1) num++;
    thx = x;thy = y;
    while(--thy >= 0 && game[thx][thy] == 1) num++;
    if(num >= 5) return true;

    thx = x; thy = y,num = 1;
    while(++thx < 10 && ++thy < 10 && game[thx][thy] == 1) num++;
    thx = x, thy = y;
    while(--thx >= 0 && --thy >= 0 && game[thx][thy] == 1) num++;
    if(num >= 5) return true;

    thx = x;thy = y,num = 1;
    while(++thx < 10 && --thy >= 0 && game[thx][thy] == 1) num++;
    thx = x, thy = y;
    while(--thx >= 0 && ++thy < 10 && game[thx][thy] == 1) num++;
    //cout <<"i j " << x<<" " <<y<<" " <<num << endl;
    if(num >= 5) return true;
    return false;
}


int main()
{
    for(int i = 0; i < 10; i++)
    {
        scanf("%s",str[i]);
        for(int j = 0; j < 10; j++)
        {
            if(str[i][j]=='X') game[i][j] = 1;
            else if (str[i][j] == 'O') game[i][j] = 2;
        }

    }
    int flag = 0;
    for(int i = 0; i < 10; i++)
    for(int j = 0; j < 10; j++)
    {
        if(game[i][j] == 0)
        {
            game[i][j] = 1;
            if(check(i,j)) {flag = 1; break;}
            game[i][j] = 0;
        }
        else if(game[i][j] == 1)
        {
            if(check(i,j)) {flag = 1; break;}
        }
    }
    flag ? printf("YES\n") : printf("NO\n");
    return 0;
}

C 简单的贪心,昨晚当场写的时候有个细节出了问题,

出问题的应该是这样一种情况

5 10

1 2 4 30 50  我之前的代码应该会输出2,但是这种只需要1

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
#define ll long long
ll d[1050];
int main()
{
    int n;
    ll k;
    cin >> n;
    cin >> k;
    for(int i = 0; i < n; i++)
        cin >> d[i];
    sort(d,d+n);
    ll temp = k*2;
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        if(d[i] > temp)
        {
            while(temp < d[i])
            {
                ans ++;
                temp =  temp*2;
            }
        }
        if(d[i] * 2 > temp) temp = d[i] * 2;
    }
    cout << ans << endl;
    return 0;
}

D:二分+贪心,中间变量会爆long long:) WA在第14组数据无法自拔

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = 1e6 + 100;
#define ll long long

char str[maxn];
char abc[maxn];
int bq[30];
struct node
{
    int snum,tnum;
    char s;
}data[30];
bool check(ll mid,ll op)
{
    ll temp = 0;
    for(int i = 0; i < 26; i++)
    {
        if(data[i].tnum != 0)
        {
            ll thi = (ll)mid * data[i].tnum - data[i].snum;
            if(thi <= 0) continue;
            temp += thi;
        }
    }
    return temp <= op;
}
int main()
{
    cin >> str >> abc;
    int slen = strlen(str), alen = strlen(abc);
    int op = 0;
    for(int i = 0; i < 26; i++)
        data[i].s = i + 'a';
    for(int i = 0; i < slen; i++)
    {
        if(str[i] == '?') op++;
        else data[str[i]-'a'].snum++;
    }
    for(int i = 0; i < alen; i++) data[abc[i]-'a'].tnum ++ ;
    ll l = 0, r = 5e6,ans = 0;
    while(l <= r)
    {
        ll mid = l+(r-l)/2;
        if(check(mid,op)){l = mid + 1; ans = mid;}
        else r = mid - 1;
    }
    for(int i = 0; i < 26; i++)
    {
        if(data[i].tnum != 0)
        {
            int thi = ans * data[i].tnum - data[i].snum;
            if(thi <= 0) continue;
            bq[i] += thi;
        }
        else continue;
    }
    for(int i = 0; i < slen; i++)
    {
        if(str[i] == '?')
        {
            for(int j = 0; j < 26; j++)
            if(bq[j])  {str[i]=j + 'a'; bq[j]--;break;}
        }
    }
    for(int i = 0; i < slen; i++)
        if(str[i] == '?') str[i] = abc[0];
    cout << str << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值