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题解的更多相关文章

  1. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  2. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  3. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  4. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  5. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  6. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  7. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  8. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  9. 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 ...

  10. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

随机推荐

  1. Semaphore.release()方法的底层原理

    一.release() 方法代码解析 当调用 release() 方法时,实际调用的是 AQS 的 releaseShared(1) 方法.以下是其详细工作流程: public final boole ...

  2. 使用open-feign进行远程服务调用

    想要远程调用别的服务 1).引入open-feign包 2).编写一个接口,告诉SpringCloud这个接口是调用哪个远程的服务 a.声明接口的每一个方法都是调用哪个远程服务的那个请求 3).开启远 ...

  3. nginx配置代理指向Redis

    stream { upstream redis { server 127.0.0.1:6379 max_fails=3 fail_timeout=30s; #*redis-addres*替换为真实地址 ...

  4. CSS横向滚动

    Flex版本 .super { display: flex; width: 100%; overflow-x: scroll; white-space: nowrap; } .sub { width: ...

  5. devops组件搭配选型

    名称 作用 备注 sentry 异常捕获系统 gitlab 代码仓库 jenkins 持续集成 open-falcon 监控系统 grafana 监控FE prometheus 监控系统 thanos ...

  6. vue报错:Property or method "xxx" is not defined on the instance but referenced during render.

    vue报错:Property or method "attendanceDetaill" is not defined on the instance but referenced ...

  7. [笔记]通过命令行连接MySQL数据库服务器的几种方式总结如下

    通过命令行连接MySQL数据库服务器的几种方式总结如下: 1.连接本地数据库,用户名为"root",密码"123456"(注意:"-p"和& ...

  8. 《原型设计工具深度解析:Axure到墨刀的实战指南》

    原型设计工具深度解析:从Axure到墨刀的实战应用 项目背景 "Shou学"作为信息学院本科必修课指南平台,需通过高保真原型实现课程导航.知识点拆解.习题模拟等核心功能.本文结合& ...

  9. html_py

    Sock.py import socket def handle_request(client):     buf=client.recv(1024)     client.send(bytes(&q ...

  10. 记一次ASP.NET CORE线上内存溢出问题与dotnet-dump的排查方法

    前言 这周系统更新了一个版本,部署到线上. 客户反馈整个系统全部都卡顿,随即我们上服务器检查 发现整个服务器内存竟然达到了20-30G的占用..如图: 其中有一个订单服务,独自占用13-18G内存, ...