牛客手速月赛-41 F

离小白月赛ak最近的一次,压哨交了一发F得了97分,感觉写的很冗余,就回去改了。后来才发现比赛改版以后题目数量变少了,时间也变短了。改完回来比赛都结束了,不过还是WA了。输出没想清楚。
题目
题意: 给定一个长度30w的数,可以将其数位上的数任意调换顺序,问能否经过调换变成375的倍数,输出任意解,无解输出-1.
思路: 首先判断是不是3的倍数,然后再判断125的倍数,因为125的倍数后三个数结尾是限定的那几个组合。最后输出。难点在于:
输出不能有前导零。
我刚开始想的把125交换到结尾,后来发现有可能125就在结尾,原来的指针指的5可能会被换走。
然后转变思路,当出现125的时候跳过,其他的直接输出,把125最后输出。但是这样会导致有前导零,最后没调出来。
参考别人的做法是,用桶记录每个数出现的次数,125先每个数–。判断一下除了0以外还有没有其他数,有的话一定可以规避前导零,不能规避就看其他的组合是否满足题意。感觉很妙啊哈哈
时间复杂度: O(n)
代码:

// Problem: 小红的375
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/11218/F
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int cnt[10];
int a[N<<1];
bool check() //能否没有前导零
{
    if(n == 3) return true;
	for(int i=1;i<=9;++i)
	{
		if(cnt[i]) return true;
	}
	return false;
}
inline void out(int x,int y,int z)
{
	for(int i=9;i>=0;--i)
	{
		for(int j=0;j<cnt[i];++j)
		{
			write(i);
		}
	}
	write(x),write(y),write(z);
}
int sum = 0;
void solve()
{
   while(scanf("%1d",&a[++n])!=EOF)
   {
   	  cnt[a[n]]++;
      sum = (sum + a[n]) % 3;
   }
   n--;
   if(n < 3 || sum)
   {
       write(-1); return ;
   }
   if(cnt[0] >= 3)
   {
   	 cnt[0] -= 3;
   	 if(check())
   	 {
   	 out(0,0,0);
   	 return ;
   	 }
   	 cnt[0] += 3;
   }
   if(cnt[1] && cnt[2] && cnt[5])
   {
   	  cnt[1] -- , cnt[2] -- ,cnt[5] -- ;
   	  if(check())
   	  {
   	  	out(1,2,5); return ;
   	  }
   	  cnt[1] ++ , cnt[2] ++ ,cnt[5] ++ ;
   }
   if(cnt[2] && cnt[5] && cnt[0])
   {
   	  cnt[2] -- , cnt[5] -- ,cnt[0] -- ;
   	  if(check())
   	  {
   	  	out(2,5,0); return ;
   	  }
   	  cnt[2] ++ , cnt[5] ++ ,cnt[0] ++ ;
   }
   if(cnt[3] && cnt[7] && cnt[5])
   {
   	  cnt[3] -- , cnt[7] -- ,cnt[5] -- ;
   	  if(check())
   	  {
   	  	out(3,7,5); return ;
   	  }
   	  cnt[3] ++ , cnt[7] ++ ,cnt[5] ++ ;
   }
   if(cnt[5] && cnt[0] >= 2)
   {
   	  cnt[5] -- , cnt[0] -- ,cnt[0] -- ;
   	  if(check())
   	  {
   	  	out(5,0,0); return ;
   	  }
   	  cnt[5] ++ , cnt[0] ++ ,cnt[0] ++ ;
   }
   if(cnt[6] && cnt[2] && cnt[5])
   {
   	  cnt[6] -- , cnt[2] -- ,cnt[5] -- ;
   	  if(check())
   	  {
   	  	out(6,2,5); return ;
   	  }
   	  cnt[6] ++ , cnt[2] ++ ,cnt[5] ++ ;
   }
   if(cnt[7] && cnt[5] && cnt[0])
   {
   	  cnt[7] -- , cnt[5] -- ,cnt[0] -- ;
   	  if(check())
   	  {
   	  	out(7,5,0); return ;
   	  }
   	  cnt[7] ++ , cnt[5] ++ ,cnt[0] ++ ;
   }
   if(cnt[8] && cnt[7] && cnt[5])
   {
   	  cnt[8] -- , cnt[7] -- ,cnt[5] -- ;
   	  if(check())
   	  {
   	  	out(8,7,5); return ;
   	  }
   	  cnt[8] ++ , cnt[7] ++ ,cnt[5] ++ ;
   }
   write(-1);
}
signed main(void)
{  
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值