zoukankan      html  css  js  c++  java
  • CF988E Solution

    题目链接

    题解

    可以发现结尾只得为(00,25,50,75),因此枚举这些数字将其放至结尾即可。若将(n)由右至左从(1)开始编号,将数字(a_i)移至末尾所需操作数为(i-1)。此外,若数对(00,25,50,75)的顺序是正确的,操作数须(-1);若存在前缀0,需要增加前缀0个数的操作数。设(n)数位数为(d),时间复杂度为(O(d^3))

    AC代码

    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int inf=0x3f3f3f3f3f3f3f3f;
    int a[20];
    bool check(int i,int j)
    {
    	return ((a[i]==7 && a[j]==5) || (a[i]==5 && !a[j]) || (!a[i] && !a[j]) || (a[i]==2 && a[j]==5));
    }
    signed main()
    {
    	int n,cnt=0,ans=inf;
    	scanf("%lld",&n);
    	while(n) {a[++cnt]=n%10; n/=10;}
    	for(int i=1;i<=cnt;i++)
    	{
    		for(int j=i+1;j<=cnt;j++)
    		{
    			if(check(i,j) || check(j,i)) 
    			{
    				int tmp=i-1+j-1,ti=a[i],tj=a[j],pos=cnt,num=0;
    				tmp-=check(j,i);
    				a[i]=0,a[j]=0;//a[i],a[j]无法阻止前缀0的脚步
    				while(!a[pos] && pos)
    				{
    					if(pos!=i && pos!=j) num++;//但它们不在前缀0的队伍中
    					pos--;
    				}
    				tmp+=num; a[i]=ti,a[j]=tj;
    				ans=min(ans,tmp);
    			}
    		} 
    	}
    	if(ans==inf) printf("-1");
    	else printf("%lld",ans);
    	return 0;
    }
    
  • 相关阅读:
    序列化
    执行mysql脚本
    MinGW-notepad++开发c/c++程序
    MySql免安装版配置方法
    Wamp 简单使用方法
    [锋利JQ]-图片提示效果
    [锋利的JQ]-超链接提示效果
    PHPcms 系统简单使用
    NC帮助文档网址
    NC的开发模型
  • 原文地址:https://www.cnblogs.com/violetholmes/p/14607384.html
Copyright © 2011-2022 走看看