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[][];
int main(){ cin>>str;
int len = str.length();
reverse(str.begin(),str.end());
int flag0=,flag5=;
memset(a,inf,sizeof(a));
for(int i=;i<len; i++) //先记录所有可能的四种情况。
{
if(str[i]=='')
{
if(a[][]==inf)a[][] = i-;
}
else if(str[i]=='')
{
if(a[][]!=inf&&a[][]==inf)
{
a[][] = i + ;
}
else if(a[][]==inf)
{
a[][] = i;
}
if(a[][]==inf)
{
a[][] = i;
}
else if(a[][]==inf)a[][] = i-;
}
else if(str[i]=='')
{
if(a[][]!=inf&&a[][]==inf)
{
a[][] = i + ;
}
else if(a[][]==inf)
{
a[][] = i;
} if(a[][]==inf)a[][] = i-; if(a[][]!=inf&&a[][]==inf)
{
a[][] = i + ;
}
else if(a[][]==inf)
{
a[][] = i;
}
}
else if(str[i]=='')
{
if(a[][]==inf)a[][] = i-;
}
}
int ans = -;
for(int i=; i<=;i ++) //这里要确定会不会在交换中有0的情况。
{
if(a[i][]!=inf&&a[i][]!=inf&&(i==||i==))
{
char q,w;
int id = i;
if(id==) q = '',w = '';
// if(id==2) q = '5',w = '0';
if(id==) q = '',w = '';
// if(id==4) q = '0',w = '0';
int tot = ;
// debug(id);
int flag1 = ,flag2 = ;
for(int i=;i<len; i++)
{
if(q==str[i]&&i!=len-)flag1 = ;
if(w==str[i]&&i!=len-)flag2 = ;
}
if(flag1==||flag2==)
for(int i=len-; i>=; i--)
{
if(str[i] != q && str[i]!= w &&str[i]!='')break;
if(str[i]=='')tot++;
}
int tmp = a[i][] + a[i][] + tot;
if(ans==-)ans =tmp;
else ans = min(ans,tmp);
}
else if(a[i][]!=inf&&a[i][]!=inf)
{
char q,w;
q = '',w = '';
int tot = ;
// debug(id);
int flag1 = ,flag2 = ;
int tt = ; //记录0的个数
for(int i=;i<len; i++)
{
if(q==str[i]&&i!=len-)flag1 = ;
if(w==str[i]&&i!=len-)tt++;
}
if(i==||tt==)flag1=,flag2=;
if(flag1==||flag2==)
{
for(int i=len-; i>=; i--)
{
if(str[i] != q &&str[i]!='')break;
if(str[i]=='')tot++;
}
tot--;
}
int tmp = a[i][] + a[i][] + tot;
if(ans==-)ans =tmp;
else ans = min(ans,tmp);
}
}
cout<<ans<<endl;
return ;
}
自己写的分类讨论
#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 = ;
int sw(char a,int len)
{
for(int i=len;i>=;i--)
{
int t = ;
if(tmp[i]==a)
{
for(int j=i;j<len;j++)
swap(tmp[j],tmp[j+]),t++;
return t;
}
}
return inf;
}
int main(){
cin>>str;
int len = str.length();
int ans = inf;
//
int x;
tmp = str;
x = sw('',len-);
x += sw('',len-);
int i;
for(cnt = , i=; i<len&&tmp[i]=='' ;i++)cnt++;
ans = min(ans,x + cnt);
//一开始写成了for(int i=0,cnt = 0; i<len&&tmp[i]=='0' ;i++)cnt++;
//使得cnt的计数出了循环就没了效果。
//
tmp = str;
x = sw('',len-);
x += sw('',len-); for(cnt = , i=; i<len&&tmp[i]=='' ;i++)cnt++;
ans = min(ans,x + cnt);
//
tmp = str;
x = sw('',len-);
x += sw('',len-); for(cnt = , i=; i<len&&tmp[i]=='' ;i++)cnt++;
ans = min(ans,x + cnt); //
tmp = str;
x = sw('',len-);
x += sw('',len-); for(cnt = , i=; i<len&&tmp[i]=='' ;i++)cnt++;
ans = min(ans,x + cnt);
if(ans>=inf)puts("-1");
else
cout<<ans<<endl;
return ;
}
%mxk
Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类的更多相关文章
- Codeforces Round #486 (Div. 3) E. Divisibility by 25
Codeforces Round #486 (Div. 3) E. Divisibility by 25 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #486 (Div. 3) D. Points and Powers of Two
Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...
- Codeforces Round #486 (Div. 3) A. Diverse Team
Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Des ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #486 (Div. 3)-B. Substrings Sort
B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #486 (Div. 3)988D. Points and Powers of Two
传送门:http://codeforces.com/contest/988/problem/D 题意: 在一堆数字中,找出尽量多的数字,使得这些数字的差都是2的指数次. 思路: 可以知道最多有三个,差 ...
- Codeforces Round #486 (Div. 3)-C. Equal Sums
C. Equal Sums time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- 【iOS】更新 CocoaPods 后 Podfile 报错
更新了 CocoaPods 后,再执行 "pod install" 时报了如下错误: [!] The dependency `AFOnoResponseSerializer` is ...
- 归并排序(从上到下、从下到上)——C语言
归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divide)成一些小的问题然后递归 ...
- 前端笔记之React(六)ES6的Set和Map&immutable和Ramda和lodash&redux-thunk
一.ES6的Set.Map数据结构 Map.Set都是ES6新的数据结构,都是新的内置构造函数,也就是说typeof的结果,多了两个: Set 是不能重复的数组 Map 是可以任何东西当做键的对象 E ...
- Flink 从0到1学习 —— Flink 中如何管理配置?
前言 如果你了解 Apache Flink 的话,那么你应该熟悉该如何像 Flink 发送数据或者如何从 Flink 获取数据.但是在某些情况下,我们需要将配置数据发送到 Flink 集群并从中接收一 ...
- Java课堂 动手动脑6
一.下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d;d=m;d=(Dog)m;d=c;c=(Cat)m; 先进行自我判断, 1.代码: class Mammal{} c ...
- ThreadPoolExecutor线程池的一个面试题
问题:现有一个线程池,参数corePoolSize = 5,maximumPoolSize = 10,BlockingQueue阻塞队列长度为5,此时有4个任务同时进来,问:线程池会创建几条线程? 如 ...
- ns3 802.11b PHY model
I use the ubuntu and do not install the chinse input. The Code: c file requires gnu gsl library, it ...
- CSS3:pointer-events | a标签禁用
用纯css就能实现取消事件响应的方法,pointer-events,使用起来更加简单,它可以: pointer-events: auto | none | visiblePainted | visib ...
- 爬虫之爬取电影天堂(request)
#需要通过代码打开https://www.dytt8.net/网站,拿到网站内容 from urllib.request import urlopen #拿到urlopen import re con ...
- PKI机制总结
PKI,全称是Public Key Infrastructure,可译为公钥基础设施.它是因特网中节点通信的安全保障机制,HTTPS中的‘S’就来源于PKI. 要去学习一个技术,首先要从它的源头考虑— ...