【codechef】The Warehouse(灵活题)

来源:互联网 发布:如何进入淘宝vip商场 编辑:程序博客网 时间:2024/05/05 12:39

https://www.codechef.com/problems/WPROB/

做法挺巧妙的。首先枚举所有最后排序可能,对每种情况进行讨论:规定最后的顺序是o1o2o3。如果s[i]==o1,那么s[i]要移到最左边o1块里的最右边,移动的距离肯定是c2+c3,因为o1已经都在最左边,肯定要经过现有o2o3个数。o2类推。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define mod 1000000007using namespace std;string s;long long int ans;void fun(char o1,char o2,char o3){long long int tmp=0,c1=0,c2=0,c3=0;for(int i=0;i<s.length();i++){if(s[i]==o1){c1++;tmp+=c2+c3;}else if(s[i]==o2){c2++;tmp+=c3;}elsec3++;}ans=min(ans,tmp);}int main(){  std:ios_base::sync_with_stdio(false);  int t;  cin>>t;  while(t--)  { ans=100000000000LL;  cin>>s;  fun('b','g','r');  fun('b','r','g');  fun('g','b','r');  fun('g','r','b');  fun('r','b','g');  fun('r','g','b');  cout<<ans<<"\n";  }  return 0;}


0 0
原创粉丝点击