A. Omkar and Password

题意:每次可以将相邻不相等的两项替换成他们的和(原来的两个数变成一个数),问最短能变成多短。

思路:其实会发现,如果这个序列里但凡存在一对a[i]!=a[i+1]的,不管i+1后面有多少相同的,我们都可以产生连锁反应把整个序列变成只有一项。如 3 7 7 7.... ,不管后面7有多少,我一个3就可以把它们全部打乱。所以只需要判断序列是否全相等即可。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[maxn]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
if(n==1)
{
cout<<1<<endl;
}
else
{
int flag = 1;
rep(i,2,n) if(a[i]!=a[i-1]) flag = 0;
if(flag) cout<<n<<endl;
else cout<<1<<endl;
}
}
return 0;
}

B. Omkar and Infinity Clock

题意: 有k个操作,每次把所有a[i]变成最大值 - a[i],问最后序列。

思路:随便写几个例子会发现有规律存在,其实就只在两个序列里面来回变动。所以只需要考虑奇偶性就行。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[maxn]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(); ll k = read();
ll ma = -inf, mi = inf;
rep(i,1,n) a[i] = read(), ma = max(ma,a[i]), mi = min(mi, a[i]);
if(k&1)
{
rep(i,1,n) cout<<ma - a[i]<<' '; cout<<'\n';
}
else
{
rep(i,1,n) cout<<(ma - mi) - (ma - a[i])<<' '; cout<<'\n';
}
}
return 0;
}

C. Omkar and Waterslide

题意:每次可以将序列里面一段非降子串全部+1,问最少多少步可以使得最后整体变为一个非降序列。

思路:贪心+差分。我们先拿一个例子来说,如图

我们发现从下标2->4的元素都是递减的,这一部分是肯定至少要加到a[1]那么大的。我们看看怎么加最少,首先最小的a[4]先自增,等增到a[3]那么大的时候就和a[3]一起增,同理他们俩和a[2]一样大的时候再拉上a[2]一起增,然后一路到a[1]。这样我们发现只需要考虑第一位和第四位的差分就行了。换句话说,我们这个要增加的区间里面只需要考虑最大值和最小值。因为其中间的元素我们都可以搭顺风车一起增。那到a[5]的时候发现比a[4]大了,如果它是直接比a[1]都大,我们就先把前面四个增到a[1]就行了。但是如果像图里这样刚好处于前面的最大最小值之间的话,我们就贪心一下,反正a[5]也要增加到那么大,我们先看看他后面有多少能先增加到它那么大的然后一起搭顺风车。然后发现a[6]就是,所以当a[6]增加到a[5]的时候,就可以和前面几个的一起到a[1]了。em。。。如果还不明白的话就看代码吧,非常简便。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[maxn]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
ll pre = a[1];
ll ans = 0;
ll cur = 0;
rep(i,1,n)
{
if(a[i] < a[i-1]) cur += a[i-1] - a[i];
else
{
ans += cur, cur = 0, pre = a[i];
}
}
ans += cur;
cout<<ans<<endl;
}
return 0;
}

D. Omkar and Bed Wars

题意:n个人环形坐下,每个人可以给相邻的人扇耳光,如果只有一个人扇了你一耳光,你就扇回去。反之可以扇左右随便一个。现在给你一个可能不按照规则来的,问你最小改变多少个人可以所有人遵循上述规则。

思路:这题看了dalao思路才明白。

1.我们定义一个联通块为一个连续有相同方向的序列。如RRRLRL,前三个R就是一个联通块。然后发现我们操作一个联通块最小只需要len/3次就够了。即隔两位改变一个,如上述RRR变成RLR就满足题意。

2.所以就统计一下这个序列有多少个联通块,每个联通块的贡献就是len/3 。 最后注意如果尾部的联通块和开头的朝向相同,他们其实是一个来的,所以合并一下。

3.如果全局就一个联通块,如RRRRR,就需要(n+2)/3,即向上取整的意思。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
string s;
cin>>s;
if(n<=2)
{
cout<<0<<endl;
continue;
}
int len = 1;
int ans = 0;
int First = 0;
rep(i,1,s.size()-1)
{
if(s[i] == s[i-1]) len ++;
else
{
if(!First) First = len;
else ans += len/3;
len = 1;
}
}
if(!First) cout<<(n+2)/3<<endl;
else
{
if(s[0] == s[n-1]) cout<<ans + (len + First)/3<<endl;
else cout<<ans + len/3 + First/3 <<endl;
}
}
return 0;
}

Codeforces Global Round 10 ABCD题解的更多相关文章

  1. Codeforces Global Round 1 (A-E题解)

    Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...

  2. Codeforces Global Round 11 个人题解(B题)

    Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...

  3. Codeforces Global Round 2部分题解

    传送门 好难受啊掉\(rating\)了-- \(A\ Ilya\ and\ a\ Colorful\ Walk\) 找到最后一个与第一个颜色不同的,比一下距离,然后再找到最左边和最右边与第一个颜色不 ...

  4. Codeforces Global Round 2 部分题解

    F.Niyaz and Small Degrees 挺sb的一题,为什么比赛时只过了4个呢 考虑当\(x\)固定的时候怎么做.显然可以树形DP:设\(f_{u,i=0/1}\)表示只考虑\(u\)子树 ...

  5. Codeforces Global Round 2 题解

    Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...

  6. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  7. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  8. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  9. Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)

    Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...

  10. 【手抖康复训练1 】Codeforces Global Round 6

    [手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...

随机推荐

  1. Java+Appium+Junit实现app自动化demo

    1.新建maven工程和引入库 步骤参考https://www.cnblogs.com/wanyuan/p/16408758.html 2.编写代码 代码如下: import org.junit.Af ...

  2. ArrayBlockingQueue的take()底层原理

    一.ArrayBlockingQueue 的 take() 方法的底层源码的详细介绍 ArrayBlockingQueue 是 Java 并发包 (java.util.concurrent) 中的一个 ...

  3. 解决github页面打不开 页面加载慢,注册不了显示Unable to verify your captcha response...

    解决国内打开Github页面.注册等问题 下列方法可以解决: github网站页面打不开: github页面打开慢,偶尔打不开,部分页面链接也打不开: 注册不了github账号,找回密码失败等,显示U ...

  4. 在 MySQL 中存储金额数据,应该使用什么数据类型?

    在MySQL中存储金额数据时,最推荐使用 DECIMAL 类型(有时也叫做 NUMERIC).DECIMAL 类型是一种精确的数字类型,适合存储具有小数位的金额数据,因为它不会像浮点数类型那样受到精度 ...

  5. Spring 中@Autowired,@Resource,@Inject 注解实现原理

    使用案例 前置条件: 现在有一个 Vehicle 接口,它有两个实现类 Bus 和 Car ,现在还有一个类 VehicleService 需要注入一个 Vehicle 类型的 Bean: publi ...

  6. 海康摄像头SDK在Linux、windows下的兼容问题

    零.前言 最近一直在做人脸识别相关的应用. 主要就是使用海康的摄像头抓拍.录制视频,使用虹软的sdk进行人脸识别,使用jna调用这些sdk. 海康的sdk在使用时遭遇了很多问题,主要问题就是windo ...

  7. 使用sealos快速搭建kubernetes集群!!!

    什么是sealos? Sealos 是一款基于 Kubernetes 的轻量级操作系统,专为云原生环境设计,主要用于快速部署和管理 Kubernetes 集群.它采用"容器化内核" ...

  8. TensorFlow 基础 (03)

    项目再忙碌, 还是要抽出时间来学习的. 最近到整一些数据清洗小工具, 数据导入数据库工具等... 有种感觉是, 之前我做分析师的时候, 啥工具都没有, 全部我自己造, 数据表整理, 业务整理, 建库建 ...

  9. 浅析NodeJS中的事件循环和异步API

    @charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...

  10. Markdown 使用十分钟入门

    Markdown 十分钟入门 标题 标题前#+空格 支持6级标题 输入样式#+空格:##+空格:以此类推, *注意标题输入的#前面不能有空格 字体 斜体 # 这里不要空格 _斜体_ *加一个* 字体加 ...