A. Alternative Thinking

这个标的是dp,但是我感觉就只能算思维题,不是特别难,

你仔细想想就知道,你先求出01这样子满足条件的个数,如果要进行改变,最多只可以增加两个,也可以增加一个或者不增加。

如果有连续的两个1或者0那么肯定至少可以增加一个,如果有两个不同的00 或者11  或者是两个11连续 或者两个00不连续 或者三个相同的0或者1都是可以增加两个的。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3ff3f3f
using namespace std;
const int maxn = 1e6 + ;
char a[maxn];
int num[maxn]; int main()
{
int n;
scanf("%d", &n);
scanf("%s", a + );
int len = strlen(a + );
int ans = ;
int f = ;
for(int i=;i<=len;i++) num[i] = a[i] - '';
int flag = num[];
for(int i=;i<=len;i++) if (num[i] == !flag) flag = num[i], ans++;
for (int i = ; i < len; i++) if (num[i] == num[i + ]) f = ;
//printf("%d\n", ans);
int ex = ;
if(f) printf("%d\n", ans);
else
{
for(int i=;i<len;i++)
{
if(num[i]==num[i+])
{
ex++;
int x = i;
while (num[x] == num[x + ]) x++;
i = x;
}
}
for (int i = ; i < len - ; i++) if (num[i] == num[i + ] && num[i] == num[i + ]) ex = ;
if (ex >= ) printf("%d\n", ans + );
else printf("%d\n", ans + );
}
return ; }
/*
56
10101011010101010101010101010101010101011010101010101010
*/

A

A. Pride

这个题目不是很难,思维题,这个题目有几种情况要考虑,

第一个是存在1的情况,那么答案就是n-flag,flag表示1的数量。

第二个就是看他们的所有数gcd是不是等于1 如果不是那么就输出-1

第三个就是gcd==1 所以但是没有1 这个时候我们就可以找到每一个一段进行gcd==1 把所有的段都保存下来,然后求最小的数值让他gcd==1

找到之后就再加上n-1即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 2e3 + ;
queue<int>que;
int a[maxn]; int gcd(int a,int b)
{
return b == ? a : gcd(b, a%b);
} int main()
{
int n;
scanf("%d", &n);
int flag = ;
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if (a[i] == ) flag++;
}
if(flag)
{
printf("%d\n", n - flag);
return ;
}
flag = ;
int ans = a[];
int ax = a[];
for(int i=;i<=n;i++)
{
if (gcd(a[i], a[i - ]) == ) flag = ;
ans = gcd(ans, a[i]);
ax = gcd(ax, a[i]);
if(ans==)
{
que.push(i);
ans = a[i + ];
}
}
if (flag) printf("%d\n", n);
else if (ax != ) printf("-1\n");
else
{
int an = inf;
while(!que.empty())
{
int num = ;
int u = que.front(); que.pop();
int ex = a[u]; u--;
while(ex!=)
{
ex = gcd(ex, a[u]);
u--;
num++;
}
an = min(an, num);
}
printf("%d\n", n - + an);
}
return ;
}

A

dp 20190617的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

随机推荐

  1. Java编程最差实践常见问题详细说明(1)转

    Java编程最差实践常见问题详细说明(1)转     原文地址:http://www.odi.ch/prog/design/newbies.php 每天在写Java程序, 其实里面有一些细节大家可能没 ...

  2. java异常处理:finally中不要return

    java异常处理:finally中不要return 复制代码 public class Ex1 { public static void main(String[] args) { System.ou ...

  3. 墨者学院靶场:uWSGI(CVE-2018-7490)路径遍历漏洞复现

    0x01漏洞简介 uWSGI是一款Web应用程序服务器,它实现了WSGI.uwsgi和http等协议.uWSGI 2.0.17之前版本中存在路径遍历漏洞,该漏洞源于程序没有正确的处理DOCUMENT_ ...

  4. python初学(二)

    1.输入一个整数列表L,判断L中是否存在相同的数字: (1)若存在,输出YES,否则输出NO: (2)若存在,输出YES,同时输出相同的数字:否则输出NO. l=list(input()) print ...

  5. 理解SVG的缩放 偏移的计算公式

    SVG中DOM元素的偏移与缩放都是基于SVG元素的左上角,所以如何理解与计算SVG中元素的真实位置就比较难,下面的例子都以圆(circle)为例. 1.缩放假定缩放的比例为s,执行缩放后,圆的圆心坐标 ...

  6. ASE课程总结 by 张葳

    本期ASE课程分为两个阶段,第一阶段的personal project与第二阶段的team project,其中,第一阶段旨在锻炼我们个人的问题解决能力和编程能力,第二阶段则锻炼主要我们的管理能力,合 ...

  7. codeforces 122C perfect team

    You may have already known that a standard ICPC team consists of exactly three members. The perfect ...

  8. 深度剖析前端JavaScript中的原型(JS的对象原型)

          这张图片有点劝退了,哈哈哈~    通过原型机制,JavaScript 中的对象从其他对象继承功能特性:这种继承机制与经典的面向对象编程语言的继承机制不同.本文将探讨这些差别,解释原型链如 ...

  9. Mac安装aws-cli全过程,通过命令行上传文件到aws s3协议服务器

    第一次使用aws,首先查询了各种资料,我第一步需要做的是安装aws-cli,而安装aws-cli之前需要安装python3,当然你安装python3之前你还需要安装homebrew,当然我正在安装的过 ...

  10. thinkphp--导入导出xls文件

    /** * 数组转xls格式的excel文件 * @param array $data 需要生成excel文件的数组 * @param string $filename 生成的excel文件名 * 示 ...