Codeforces Round #688 (Div. 2) ABC题解
A. Cancel the Trains
签到题,看两边有无相同相对位置出发的,加入计数即可。
view code
#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 endl '\n'
#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 = 1e5+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(), m = read();
map<ll,ll> vis;
rep(i,1,n)
{
ll x = read();
vis[x] = 1;
}
ll cnt = 0;
rep(i,1,m)
{
ll x = read();
if(vis[x]) cnt++;
}
cout<<cnt<<endl;
}
return 0;
}
B. Suffix Operations
题意:每次可以对任意后缀+1或者-1,在操作前你可以将任意一个数改变成任何数。问你最小操作次数使得数列全部相等。
思路:
首先观察通过后缀来改变数组,会有什么性质。
首先,若先不考虑把一个数换掉,要想把所有数变得相等,因为最优肯定是变成最大最小值之间的一个数(不然还要多花步数),所以要花的次数肯定是数组的差分和。
现在我们改变一个数的效果是什么?我们看一下三个例子
7 5 3 (从后往前递增)
把5改成【3,7】之间的都是最优的
3 5 7 (从后往前递减)
同上
7 3 5 (中间“断崖”)
显然5会经历先变成3再一起变成7的过程,这里就浪费了掉到3的步数,这个时候把3换成【5,7】之间的数就同上了。
所以换数产生更优方法的地方就在于消除“断崖”。 而这个过程又相当于“去掉了”这个数。
所以我们再处理完差分和的时候,遍历一遍去掉哪个数最优即可~
view code
#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 endl '\n'
#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];
ll res[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read();
ll cnt = 0;
ll pre = a[n];
a[n+1] = a[n-1];
a[0] = a[2];
per(i,n,1)
{
res[i] = abs(a[i]-pre);
cnt += res[i];
pre = a[i];
}
ll mi = cnt;
res[0] = 0;
per(i,n,1)
{
mi = min(mi,cnt-res[i]-res[i-1]+abs(a[i-1]-a[i+1]));
}
cout<<mi<<endl;
}
return 0;
}
C. Triangles
思路:这个题竟然n方的算法会T,吐了。这里讲一下优化后的方法。
先把0~9各自出现的坐标记录下来。然后从0到9遍历,对于每个数我们贪心的策略肯定是
选出最远的两个x,他们彼此之间的距离当做高,然后选其中一个的y坐标和边界的距离当底。
或者选出最远的两个y,彼此距离当搞,选其中一个x和边界距离当底。
所以就对每个数的所有坐标先进行x排序,再进行y排序。分别固定最远的两个点遍历一遍另一个点,执行上述操作即可。(个人感觉有点麻烦)
view code
#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 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 endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn = 5000+200;
const int maxn1 = 5e6;
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 int read(){ int f = 1; int 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} };
char s[maxn][maxn];
vector<vector<PII> > Map(15);
typedef struct Pos
{
int x;
int y;
}P;
P row[maxn1];
P col[maxn1];
bool cmp1(P a, P b)
{
return a.x < b.x;
}
bool cmp2(P a, P b)
{
return a.y < b.y;
}
int main()
{
int kase;
kase = read();
while(kase--)
{
int n = read();
rep(i,0,10) Map[i].clear();
rep(i,1,n)
{
gets(s[i]+1);
}
rep(i,1,n) rep(j,1,n) Map[s[i][j]-'0'].pb(mp(i,j));
rep(cur,0,9)
{
int ma = 0;
int p1 = 0;
int p2 = 0;
for(int i=0; i<Map[cur].size(); ++i)
{
row[++p1].x = Map[cur][i].fi;
row[p1].y = Map[cur][i].se;
col[++p2].x = Map[cur][i].fi;
col[p2].y = Map[cur][i].se;
}
sort(row+1,row+1+p1, cmp1);
sort(col+1,col+1+p2, cmp2);
if(p1>=2)
{
rep(i,2,p1)
{
ma = max(ma, max( (row[i].x-row[1].x)*(row[i].y-1),(row[i].x-row[1].x)*(n-row[i].y) ) );
ma = max(ma, max( (row[i].x-row[1].x)*(row[1].y-1),(row[i].x-row[1].x)*(n-row[1].y) ) );
}
per(i,p1-1,1)
{
ma = max(ma, max((row[p1].x-row[i].x)*(row[i].y-1),(row[p1].x-row[i].x)*(n-row[i].y) ));
ma = max(ma, max((row[p1].x-row[i].x)*(row[p1].y-1),(row[p1].x-row[i].x)*(n-row[p1].y) ));
}
}
if(p2>=2)
{
rep(i,2,p2)
{
ma = max(ma, max((col[i].y-col[1].y)*(col[i].x-1), (col[i].y - col[1].y)*(n-col[i].x) ));
ma = max(ma, max((col[i].y-col[1].y)*(col[1].x-1), (col[i].y - col[1].y)*(n-col[1].x) ));
}
per(i,p2-1,1)
{
ma = max( ma, max((col[p2].y-col[i].y)*(col[i].x-1), (col[p2].y - col[i].y)*(n-col[i].x) ) );
ma = max( ma, max((col[p2].y-col[i].y)*(col[p2].x-1), (col[p2].y - col[i].y)*(n-col[p2].x) ) );
}
}
printf("%d ",ma);
}
printf("\n");
}
return 0;
}
Codeforces Round #688 (Div. 2) ABC题解的更多相关文章
- Codeforces Round #312 (Div. 2) ABC题解
[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #545 (Div. 1) 简要题解
这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...
随机推荐
- Ruby+Selenium+testunit web自动化demo
1.安装对应库 使用RubyMine新建项目打开终端安装对应库 gem install selenium-webdriver gem install test-unit 如果安装不成功,请切换到国内源 ...
- rabbitmq学习与总结
一.rabbitmq的使用场景 1.高并发的流量削峰 举个例子,假设某订单系统每秒最多能处理一万次订单,也就是最多承受的10000qps,这个处理能力应付正常时段的下单时绰绰有余,正常时段我们下单一秒 ...
- 康谋分享 | AD/ADAS的性能概览:在AD/ADAS的开发与验证中“大海捞针”!
如果您希望从数百万小时的驾驶数据中查找特定的相关驾驶事件和未遂事故,以确保您的所需功能正确运行,最好的方法就是创建一个系统性能的概览分析,实现在数据日志中快速检索关注点.为此,康谋在本文将为您详细介绍 ...
- 4G模块——大夏龙雀DX-CT511-A使用记录
4G模块--大夏龙雀DX-CT511-A使用记录 加回车换行 115200波特率 重启: AT+RESET 6.关闭HTTP服务: AT$HTTPCLOSE 关闭网路 AT+NETCLOSE 1.TC ...
- 【MOOC】华中科技大学操作系统慕课答案-第1~3章单元测试
单选 1 下列说法错误的是 . A. 手工操作阶段,资源利用率低的原因是因为程序的准备和撤销都需要手工完成. B. 单道批处理系统中CPU和外设交替工作和空闲. √C. 单道批处理系统效率之所以比手工 ...
- 一文掌握 Ubuntu 全场景扩容操作
此文章为搬运,原作者链接 一文掌握 Ubuntu 全场景扩容操作 - 南北东西万里程的文章 - 知乎https://zhuanlan.zhihu.com/p/707918020 为 ubuntu扩容, ...
- git管理Unity项目
git管理Unity项目的正确打开方式 在创建仓库的时候进行初始化仓库,选择.gitignore模版的时候选择Unity,就能自动过滤不需要的文件 原文链接:https://blog.csdn.net ...
- Seata源码—3.全局事务注解扫描器的初始化
大纲 1.全局事务注解扫描器继承的父类与实现的接口 2.全局事务注解扫描器的核心变量 3.Spring容器初始化后初始化Seata客户端的源码 4.TM全局事务管理器客户端初始化的源码 5.TM组件的 ...
- C#判断当前时间是否在规定时间段范围内(二维数组超简版)
直接上C#代码 TimeSpan nowTime = DateTime.Now.TimeOfDay; string[,] arr = { { "7:50", "8:10& ...
- 代码随想录第11天 | 二叉树part01
理论基础 需要了解 二叉树的种类,存储方式,遍历方式 以及二叉树的定义 文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7 ...