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. struct和union,enum分析

    空结构体占用的内存多大? struct d { }; int main() { struct d d1; struct d d2; printf("%d,%0x\n",sizeof ...

  2. Python开发【第四篇】:模块

    双层装饰器示例 __author__ = 'Golden' #!/usr/bin/env python # -*- coding:utf-8 -*-     USER_INFO = {}     de ...

  3. 1.尽量以const ,enum,inline替换define

    1.#define为预处理阶段命令 原因:有可能记号名称没有进入记号表,而出现编译错误,即编译器并没看到过该定义. class专属常量const 一般定义为static,保证该常量至多有一份实体. 枚 ...

  4. 集成bug统计链接

    http://crab.baidu.com/http://bugly.qq.com/ http://bughd.com/ http://www.umeng.com/analyticshttp://tr ...

  5. Java开发MIS系统需要的技术及其作用

    1.后台框架部分,常用spring.struts2(Struts2框架,提供了一种基于MVC体系结构的工程序的开发方法,具有组件模块化.灵活性和重用性等优点,使基于MVC模式的程序结构更加清晰,同时也 ...

  6. 5E - A + B Again

    There must be many A + B problems in our HDOJ , now a new one is coming. Give you two hexadecimal in ...

  7. 探索未知种族之osg类生物---器官初始化二

    那我们回到ViewerBase::frame函数中来,继续看看为什么osg生命刚刚出生的时候会大哭,除了初始化了eventQuene和cameraManipulator之外还对那些器官进行了初始化.在 ...

  8. POJ 2449Remmarguts' Date 第K短路

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 29625   Accepted: 8034 ...

  9. UI设计初学者必备的工具以及学习路线(附思维导图)

    今天千锋UI设计小编着重为大家介绍5个学习ui设计必须要会的工具和软件以及UI设计学习路线,希望能对大家所帮助. UI设计必要的工具和软件 1.PS 图像处理合成软件 ui设计核心软件,强大的图像处理 ...

  10. 服务程序 -st

    Windows 服务由三部分组成:1.一个服务可执行文件:2.一个服务控制程序(SCP):3.服务控制管理器(SCM),负责在 HKLM\SYSTEM\CurrentControlSet\Servic ...