Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类
题意:给定一个数,可以对其做交换相邻两个数字的操作。问最少要操作几步,使得可以被25整除。
思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75;
自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况);自己是在数据中,该对了自己的代码,
看了队长和%王宣凯的代码,觉得那才是现场能ac的思路。--暴力交换;
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <list> #include <iterator> #include <cmath> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue #define Pll pair<ll,ll> #define Pii pair<int,int> #define fi first #define se second #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) typedef long long ll; typedef unsigned long long ull; const int inf = 0x3f3f3f3f; /*-----------------show time----------------*/ ll n; string str; int a[5][3]; int main(){ cin>>str; int len = str.length(); reverse(str.begin(),str.end()); int flag0=0,flag5=0; memset(a,inf,sizeof(a)); for(int i=0;i<len; i++) //先记录所有可能的四种情况。 { if(str[i]=='2') { if(a[1][2]==inf)a[1][2] = i-1; } else if(str[i]=='0') { if(a[2][2]!=inf&&a[2][1]==inf) { a[2][1] = i + 1; } else if(a[2][1]==inf) { a[2][1] = i; } if(a[4][1]==inf) { a[4][1] = i; } else if(a[4][2]==inf)a[4][2] = i-1; } else if(str[i]=='5') { if(a[1][2]!=inf&&a[1][1]==inf) { a[1][1] = i + 1; } else if(a[1][1]==inf) { a[1][1] = i; } if(a[2][2]==inf)a[2][2] = i-1; if(a[3][2]!=inf&&a[3][1]==inf) { a[3][1] = i + 1; } else if(a[3][1]==inf) { a[3][1] = i; } } else if(str[i]=='7') { if(a[3][2]==inf)a[3][2] = i-1; } } int ans = -1; for(int i=1; i<=4;i ++) //这里要确定会不会在交换中有0的情况。 { if(a[i][1]!=inf&&a[i][2]!=inf&&(i==1||i==3)) { char q,w; int id = i; if(id==1) q = '2',w = '5'; // if(id==2) q = '5',w = '0'; if(id==3) q = '7',w = '5'; // if(id==4) q = '0',w = '0'; int tot = 0; // debug(id); int flag1 = 0,flag2 = 0; for(int i=0;i<len; i++) { if(q==str[i]&&i!=len-1)flag1 = 1; if(w==str[i]&&i!=len-1)flag2 = 1; } if(flag1==0||flag2==0) for(int i=len-1; i>=0; i--) { if(str[i] != q && str[i]!= w &&str[i]!='0')break; if(str[i]=='0')tot++; } int tmp = a[i][1] + a[i][2] + tot; if(ans==-1)ans =tmp; else ans = min(ans,tmp); } else if(a[i][1]!=inf&&a[i][2]!=inf) { char q,w; q = '5',w = '0'; int tot = 0; // debug(id); int flag1 = 0,flag2 = 0; int tt = 0; //记录0的个数 for(int i=0;i<len; i++) { if(q==str[i]&&i!=len-1)flag1 = 1; if(w==str[i]&&i!=len-1)tt++; } if(i==4||tt==1)flag1=1,flag2=1; if(flag1==0||flag2==0) { for(int i=len-1; i>=0; i--) { if(str[i] != q &&str[i]!='0')break; if(str[i]=='0')tot++; } tot--; } int tmp = a[i][1] + a[i][2] + tot; if(ans==-1)ans =tmp; else ans = min(ans,tmp); } } cout<<ans<<endl; return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <list> #include <iterator> #include <cmath> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue #define Pll pair<ll,ll> #define Pii pair<int,int> #define fi first #define se second #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) typedef long long ll; typedef unsigned long long ull; const int inf = 0x3f3f3f3f; /*-----------------show time----------------*/ string str,tmp; int cnt = 0; int sw(char a,int len) { for(int i=len;i>=0;i--) { int t = 0; if(tmp[i]==a) { for(int j=i;j<len;j++) swap(tmp[j],tmp[j+1]),t++; return t; } } return inf; } int main(){ cin>>str; int len = str.length(); int ans = inf; //00 int x; tmp = str; x = sw('0',len-1); x += sw('0',len-2); int i; for(cnt = 0, i=0; i<len&&tmp[i]=='0' ;i++)cnt++; ans = min(ans,x + cnt); //一开始写成了for(int i=0,cnt = 0; i<len&&tmp[i]=='0' ;i++)cnt++; //使得cnt的计数出了循环就没了效果。 //25 tmp = str; x = sw('5',len-1); x += sw('2',len-2); for(cnt = 0, i=0; i<len&&tmp[i]=='0' ;i++)cnt++; ans = min(ans,x + cnt); //75 tmp = str; x = sw('5',len-1); x += sw('7',len-2); for(cnt = 0, i=0; i<len&&tmp[i]=='0' ;i++)cnt++; ans = min(ans,x + cnt); //50 tmp = str; x = sw('0',len-1); x += sw('5',len-2); for(cnt = 0, i=0; i<len&&tmp[i]=='0' ;i++)cnt++; ans = min(ans,x + cnt); if(ans>=inf)puts("-1"); else cout<<ans<<endl; return 0; }
skr