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 ...
随机推荐
- hdoj 4715 Difference Between Primes 素数筛选+二分查找
#include <string.h> #include <stdio.h> const int maxn = 1000006; bool vis[1000006]; int ...
- ue4使用SceneCapture2D创建小地图示例 蓝图
做C++项目的时候遇到了一个小地图的问题,从网上找了个蓝图的思路,转载一下. 原文:https://www.engineworld.cn/thread-3835-1-1.html 本文使用ue4提供的 ...
- go 学习笔记之工作空间
搭建好 Go 的基本环境后,现在可以正式开始 Go 语言的学习之旅,初学时建议在默认的 GOPATH 工作空间规范编写代码,基本目录结构大概是这个样子. . |-- bin | `-- hello.e ...
- logback使用配置
一:logback.xml配置内容如下 <?xml version="1.0" encoding="UTF-8"?> <!-- Copyrig ...
- Docker入门学习笔记
Docker 什么是Docker 虚拟化技术 在计算机中,虚拟化是一种资源管理技术,将计算机中的各种实体资源如:CPU.硬盘.内存等予以抽象.转换后呈现出来打破实体结构间的不可切割的障碍,使用户可以比 ...
- 【故障公告】发布 .NET Core 版博客站点引起大量 500 错误
非常抱歉,今天上午的博客站点故障给大家带来了很大的麻烦,请大家谅解.这次故障是我们发布 .NET Core 版博客站点引起的,虽然我们进行了充分的准备,但还是低估了高并发下的复杂问题. 以下是故障背景 ...
- 利用MAVEN打包可运行jar包,包括依赖的第三方包
转载自:http://bglmmz.iteye.com/blog/2058914 背景: 另一篇文章说了如何利用IDEA来打包,现在来说说如何利用MAVEN打包 目标:应用本身打成一个jar包,依赖的 ...
- SDS模块
早上花了一点时间读了下sds的相关源码,其实sds就是构造了两个字段用来记录len和free的状态,然后还有一个char[]用来记录字符串的值. 然后sds模块的函数都是在模拟str的操作. 比较,追 ...
- 消息中间件-activemq入门(二)
上一节我们了解了JMS规范并且知道了JMS规范的良好实现者-activemq.今天我们就去了解一下activemq的使用.另外我们应该抱着目的去学习,别忘了我们为什么要使用消息中间件:解耦系统之间的联 ...
- 二、java实现多线程的方式?
一.同步?异步? 下面两幅图解释了同步异步. 二.实现多线程的方式 1.继承Thread package threaddemo; class CreateThreadDemo extends Thre ...