Codeforces Round #542(Div. 2) CDE 思维场
C
https://codeforces.com/contest/1130/problem/C
题意
给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终点,只允许挖一条穿越海的隧道,假设隧道连接的两个陆地分别为(x1,y1),(x2,y2),则挖隧道的花费为\((y1-x1)*(y1-x1)+(y2-x2)*(y2-x2)\)
题解
- 分别找出和起点,和终点连接的陆地(找联通块),枚举两个点维护最小值
代码
#include<bits/stdc++.h>
#define pii pair<int,int>
#define se second
#define ft first
#define mk make_pair
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int dis(pii a,pii b){
return (a.ft-b.ft)*(a.ft-b.ft)+(a.se-b.se)*(a.se-b.se);
}
vector<pii>st,ed;
int n,sx,sy,ex,ey,i,vi[55][55],ans;
char g[55][55];
void dfs(int x,int y,int fg){
vi[x][y]=1;
if(fg)st.push_back(mk(x,y));
else ed.push_back(mk(x,y));
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(nx<1||ny<1||nx>n||ny>n||g[nx][ny]=='1'||vi[nx][ny])continue;
dfs(nx,ny,fg);
}
return;
}
int main(){
cin>>n;
cin>>sx>>sy>>ex>>ey;
for(i=1;i<=n;i++)scanf("%s",g[i]+1);
dfs(sx,sy,0);
memset(vi,0,sizeof(vi));
dfs(ex,ey,1);
ans=1e9;
for(auto i:st)
for(auto j:ed)
ans=min(ans,dis(i,j));
cout<<ans;
}
D
https://codeforces.com/contest/1130/problem/D2
题意
给你一个有n(<=5000)个点的环,m颗糖果(m<=20000),有一辆车在这个环上顺时针跑,然后在每个点上有糖果,他们需要你用车将他们运到另一个点,限制是你在每一个点只能拿一个糖果,从一个点到另一个点需要一秒,分别输出从每个点开始运送完全部糖果最少需要多少时间
题解
- 每个点只能拿一个糖果
- 对于每个点,先拿距离远的,再拿距离近的(这样保证最后半圈的距离最小)
- 然后拿完每个点需要的最短时间,维护最大时间就是答案
代码
#include<bits/stdc++.h>
using namespace std;
int n,m,i,j,x,y,ans;
vector<int>a[5005];
int dis(int x,int y){
if(x<=y)return y-x;
else return y-x+n;
}
bool cmp(int a,int b){
return dis(i,a)<dis(i,b);
}
int main(){
cin>>n>>m;
for(i=1;i<=m;i++){
cin>>x>>y;
a[x].push_back(y);
}
for(i=1;i<=n;i++)sort(a[i].begin(),a[i].end(),cmp);
for(i=1;i<=n;i++){
ans=0;
for(j=1;j<=n;j++){
if(a[j].size())ans=max((int)(n*(a[j].size()-1)+dis(j,a[j][0])+dis(i,j)),ans);
}
cout<<ans<<" ";
}
}
E
https://codeforces.com/contest/1130/problem/E
题意
一个长度为n的序列(n<=2000),找出a[l,r]*(r-l+1)最大的连续子序列(a[i]<=1e6),下面给你一个别人错误的程序
function find_answer(n, a)
# Assumes n is an integer between 1 and 2000, inclusive
# Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1]
res = 0
cur = 0
k = -1
for i = 0 to i = n-1
cur = cur + a[i]
if cur < 0
cur = 0
k = i
res = max(res, (i-k)*cur)
return res
这时候给你一个x(x<=1e9), 你要构造一个上述要求的序列,使得标准程序和上述程序算出的答案相差x
题解
- 上述程序算法大概是一遇到和<0,就更新答案,并清零当前值,这个算法假如没有(r-l+1)这个加权是对的,这样可能存在a[l,r]比较小的正数,但是(r-l+1)比较大的区间没有考虑到
- 并不用去考虑标准程序是怎么写的,只需要专注于构造出符合答案的数据就行
- 假设第一项为-1,后面的和s保证>=1(直接令第二项为2,就可以保证),那么上述程序算出来的答案一定是\(s*(n-1)\),正确答案是\((s-1)*n\),得到等式\((s-1)*n-s*(n-1)=x\),化简得\(s-n=x\)
- 可以贪心+暴力找到n和a[]
代码
#include<bits/stdc++.h>
using namespace std;
int k,s,x;
vector<int>ans;
int main(){
cin>>k;
ans.push_back(-1);ans.push_back(2);
s=1;
while(1){
x=k-s+ans.size();
if(abs(x)<=1e6){ans.push_back(x);break;}
else {ans.push_back(1e6);s+=1e6;}
}
cout<<ans.size()<<endl;
for(auto x:ans)printf("%d ",x);
}
Codeforces Round #542(Div. 2) CDE 思维场的更多相关文章
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #395 (Div. 2)(A.思维,B,水)
A. Taymyr is calling you time limit per test:1 second memory limit per test:256 megabytes input:stan ...
- Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...
- Codeforces Round #542 (Div. 1) 题解
开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...
- Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS
题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...
- Codeforces Round #539 (Div. 2) D 思维
https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...
- Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化
D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- 测试用户体验相关——UI设计准则及方法
之前跟我们uxc同学聊过一些,记录下来,方便在工作中不断渗透深入和理解,能够逐渐养成比较好的审美和对UI交互问题的敏锐的觉察力. 以问题为导向来吧... 第一个问题:一个menu中的图标一定要风格一致 ...
- 【centos】centos安装g++
gcc在Centos下的安装:使用的是以下语句:yum install gcc 以为安装g++,类似的应该使用:yum install g++ 可是提示:command is not found 查询 ...
- 8.17 纯css画一个着重号图标
今天看到一个同事写的着重号图标,我以为是图片,仔细一看,是span标签!哇!!学习一下哈哈 图标长这样: CSS代码: .hint{ display: inline-block; width: 20p ...
- python脚本linux上后台执行
1.脚本后加& 加了&以后可以使脚本在后台运行,这样的话你就可以继续工作了.但是有一个问题就是你关闭终端连接后,脚本会停止运行 python3 run.py >/dev/null ...
- declare -A color
#!/bin/bash ## 声明变量 declare -A color # 定义颜色 # bc_color : background color color[red]="\e[1;31m& ...
- 字符串加u的特殊需求
#coding:utf-8 L = ['a','b','c'] S = [] for i in L: tmp = str(i).decode('utf-8') S.append(tmp) print ...
- 微信小程序中用setData修改一个对象的属性值
原创文章 1. Page({ data: { items:{ //items为一个对象,is_like和like分别为其两个属性 is_like: 0, like: 0 ...
- ofo退押金脚本
同事钉钉给的 因为押金一直没退,电话很难打进去,咨询客服排队要等好久,一直几千位. 长时间挂机就自动退出客服了,所以自动写了一个脚本,目前已经成功退押金了.所以共享出来 1.关注ofo小黄车订阅号,注 ...
- istio promethus收集不到数据
1.重新apply istio.yaml kubectl apply -f install/kubernetes/istio.yaml kubectl get metrics.config.istio ...
- iOS.Operation-on-ZipFile
Operation on ZipFile Reference 在Mac OS X和iOS中操作.zip文件(例如创建zip文件, 从zip文件中抽取数据): 1. http://stackoverfl ...