Educational Codeforces Round 42 (Rated for Div. 2)

11 篇文章 0 订阅
/*
C
题意:给你一个数,问最少删除几个数字使得剩下的数
是一个正整数的平方数(所以0是不符合题意的,当时被
这个坑了);

思路:直接dfs爆搜枚举了,找到最大的那个平方数,判断
位数就可以了;
*/

#include<bits/stdc++.h>
using namespace std;
struct node
{
    long long x;
    int b[15];
}g[15];
bool vis[15];
int a[15];
int x[15];
#define LL long long
LL ma = 0;
int cnt = 0;
bool ch(LL N)
{
    LL i;
    for(i=1; N>0; i+=2)  //变化步长为2, 初值为1,一直减到N大于0
    {
        N-=i;
    }
    if(N==0)
        return true;  //如果N减到最后,恰好等于0,就是平方数
    else
        return false;  //否则,就不是平方数
}
void dfs(int st, LL v)
{
    if(ch(v))
        ma = max(ma,v);
    for(int i = st+1; i < cnt; i++)
        dfs(i,v*10+g[st].b[i]);
}
int main()
{
    LL n;
    cin>>n;
    if(ch(n))
    {
        cout<<'0'<<endl;
        return 0;
    }
    while(n > 0)
    {
        x[cnt++] = n%10;
        n /= 10;
    }
    for(int i = cnt-1; i >= 0; i--)
    {
        a[cnt-1-i] = x[i];
        //cout<<a[cnt-1-i];
    }
    for(int i = 0; i < cnt; i++)
    {
        g[i].x = a[i];
        for(int j = i+1; j < cnt; j++)
            g[i].b[j] = a[j];
    }
    for(int i = 0; i < cnt; i++)
        dfs(i,g[i].x);
    //cout<<cnt<<endl;
    //cout<<ma<<endl;
    if(ma == 0)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int ans = 0;
    while(ma > 0)
    {
        ma /= 10;
        ans++;
    }
    cout<<cnt-ans<<endl;
    return 0;
}

/*
D
题意:给你一串序列,每每两个相同的数字可以相加
成一个数字(相同的数字中前面的一个删除加到后面
一个上,求最后剩下的序列)

思路:用map存数字在数组中的位置,从前向后遍历数
组,如果map中存在相同的数字,那么在原数组中删除
这个数,如果没有,则将这个数加入map中;
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL a[150010];
map<LL,int> b;
int main()
{
    int n;
    cin>>n;
    int cnt = 0;
    for(int i = 0; i < n; i++)
        cin>>a[i];
    for(int i = 0; i < n; i++)
    {
        if(b.end() == b.find(a[i]))
            b[a[i]] = i;
        else
        {
            a[b[a[i]]] = -1;
            //cout<<b[a[i]]<<endl;
            cnt++;
            b.erase(a[i]);
            a[i] *= 2;
            i--;
        }
    }
    int i;
    cout<<n-cnt<<endl;
    for(i = 0; i < n; i++)
    {
        if(a[i] != -1)
        {
            cout<<a[i];
            break;
        }
    }
    for(int j = i+1; j < n; j++)
    {
        if(a[j] != -1)
            cout<<" "<<a[j];
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值