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 思维场的更多相关文章

  1. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  2. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  3. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

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

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

  6. Codeforces Round #542 (Div. 1) 题解

    开学了住校了打不了深夜场 好难受啊QwQ A 显然对于每个起点,我们只需要贪心记录这个起点出发出去的糖果数量以及离自己最近的糖果 因为这个起点最后一次装载糖果一定是装载终点离自己最近的那个糖果 $ O ...

  7. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  8. Codeforces Round #539 (Div. 2) D 思维

    https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...

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

随机推荐

  1. Ubuntu虚拟机全屏问题

    方法一:修改分辨率 xrandr查看能用的 xrandr -s 1920x1200改变. 方法二:安装新vmtools apt-get install open-vm-tools apt-get in ...

  2. 小程序 picker 多列选择器 数据动态获取

    需求是将各校区对应各班级的数据 以两列选择器的方式展示出来,并且可以在选择完成之后记录选结果参数. 校区数据 和 班级数据 分别是两个接口,以 校区 teach_area_id 字段关联 其各班级数据 ...

  3. [Java]Spring Ioc讲解,不怕你不懂

    原文地址 引述:IoC(控制反转:Inverse of Control)是Spring容器的内核,AOP.声明式事务等功能在此基础上开花结果.但是IoC这个重要的概念却比较晦涩隐讳,不容易让人望文生义 ...

  4. three.js raycaster射线碰撞的坑 (当canvas大小 不是屏幕大小是解决拾取物体的办法)

    这里只是记录一下坑,方便查阅,内容主要援引自:three.js Raycaster 射线拾取 canvas不占满整屏时射线拾取存在偏差 1. 世界坐标系: 世界坐标系位于屏幕的中心(0,0,0),往右 ...

  5. Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍

    Pycharm使⽤用秘笈v0.3PyCharm使⽤用秘籍 1. PyCharm的基本使⽤用 在PyCharm下为你的Python项⽬目配置Python解释器器 1. Project:当前项⽬目名> ...

  6. php 多进程

    php 在使用场景中一般是处理web应用,所以多进程使用不适合在web中使用,且php-fpm中pcntl_fork不能使用,所以使用场景是在cgi模式下 一个进程调用pcntl_fork函数后,系统 ...

  7. 2018 How to register and install LAUNCH ICARSCAN software ?

    2018 New Version ICARSCAN is available now! Here’s the instruction on how to install ICARSCAN softwa ...

  8. hihoCoder1159 扑克牌

    一道记忆化搜索 原题链接 和着色方案很像,这里就不详细阐述,可以去我博客里的着色方案里看. 但要注意本题不一样的是同种面值的牌花色不同,所以在转移时还需要乘上同种面值的牌的个数. #include&l ...

  9. PhpStorm 为 Laravel 搭建 PhpUnit 单元测试环境

    1.PhpStorm 中打开项目的路径为 Laravel 安装的根目录 2.点击右下角 EventLog 提示按钮, 初始化 Composer 的设置 3.打开单元单测试示例类,按提示点击 Fix . ...

  10. [SoapUI] 从上一个测试步骤获取ID list,通过Groovy脚本动态生成 Data Source 供后面的步骤使用

    https://support.smartbear.com/readyapi/docs/testing/data-driven/types/groovy.html 从官网拷贝code到SoapUI里面 ...