Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2
A. Be Positive
链接:http://codeforces.com/contest/1130/problem/A
题意: 给一段序列,这段序列每个数都除一个d(−1e3≤d≤1e3)除完后,如果正数的个数可以大于序列长度的一半输出这个d,否则输出0
思路: 直接找正数和负数的个数,正数个数大于一半输出1,负数个数大于一半输出-1,都不大于一半长度输出0就好了
代码:
#include<bits/stdc++.h>
using namespace std; int main()
{
int n,x,a,b;
a = ;b = ;
cin>>n;
for(int i = ;i <= n;i ++){
cin>>x;
if(x>) a++;
else if(x<) b++;
}
if(n%==) n = n/+;
else n = n/;
if(a >= n) cout<<<<endl;
else if(b >= n) cout<<-<<endl;
else cout<<<<endl;
}
B. Two Cakes
链接:http://codeforces.com/contest/1130/problem/B
思路:
每次只有两种取法,每次取局部最优就好了
实现代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 1e5+;
vector<ll>v[M];
int main()
{
ll n,x;
cin>>n;
for(int i = ;i < n*;i ++){
cin>>x;
v[x].push_back(i);
}
ll ans = v[][]+v[][];
for(int i = ;i < n;i ++)
ans += min(abs(v[i][]-v[i+][])+abs(v[i][]-v[i+][]),abs(v[i][]-v[i+][])+abs(v[i][]-v[i+][]));
cout<<ans<<endl;
}
C. Connect
链接:http://codeforces.com/contest/1130/problem/C
题意:给一副地图,0代表陆地,1代表海洋,你只能在陆地上行动,两片陆地之间可以最多建一个通道,通道有花费,问从起点到终点最小的花费是多少
思路:因为图非常小,我们先判断下能否直接从起点到终点,如果不能那么代表起点和终点在两片陆地上,我们分别将两片陆地的所有点存到两个vector里,两重循环枚举一遍所有点,取最小的花费就好了
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = ;
const int inf = 0x3f3f3f3f;
int flag,n;
struct node{
int x,y;
};
node s,t;
vector<node>v[];
int vis[M][M];
char mp[M][M];
void dfs(int x,int y,int cnt){
if(x > n||x < ||y > n||y < ) return;
if(vis[x][y]||mp[x][y]=='') return ;
if(x==t.x&&y==t.y&&cnt==) flag = ;
vis[x][y] = ;
node now; now.x = x;now.y = y;
v[cnt].push_back(now);
dfs(x+,y,cnt); dfs(x-,y,cnt); dfs(x,y+,cnt); dfs(x,y-,cnt);
return ;
} int main()
{
cin>>n;
cin>>s.x>>s.y;
cin>>t.x>>t.y;
for(int i = ;i <= n;i ++)
for(int j = ;j <= n;j ++)
cin>>mp[i][j];
dfs(s.x,s.y,);
int mn = inf;
if(flag) cout<<<<endl;
else{
dfs(t.x,t.y,);
for(auto &i: v[]){
for(auto &j: v[]){
mn = min(mn,(i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y));
}
}
cout<<mn<<endl;
}
}
D2. Toy Train
链接:http://codeforces.com/contest/1130/problem/D2
题意:有从1到n的环状铁道路线,给出m个糖果,每个糖果有信息a,b,代表该糖果在a点要送到b点,问1到n依次为起点的情况下送完所有糖果要走多远
思路:因为要计算的是送完所有糖果需要多远,最优的送法肯定是将离自己最近糖果留到最后,所以我们只需要维护每个点拥有的糖果中需要送的地方离本身点最近的那个糖果,并且维护下每个点有多少颗糖果,因为对于每个点来说一圈才能送掉一颗糖果,最后依次枚举每个起点,看所有点最晚要多久送完,取最久的就好了。
D1和D2是一道题只不过D1的数据小一点,代码两道题都能过。
实现代码;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int M = 6e3;
const int inf = 0x3f3f3f3f;
int n,m,a,b;
int cnt[M],mn[M];
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
int mx = ,maxx= ;
cin>>n>>m;
memset(mn,inf,sizeof(mn));
for(int i = ;i <= m;i ++){
cin>>a>>b;
if(b < a) b += n;
if(b - a < mn[a]) mn[a] = b-a;
cnt[a]++;
}
for(int i = ;i <= n;i ++){
maxx = ;
for(int j = ;j <= n;j ++){
if(cnt[j]){
if(j < i) maxx = max(maxx,j+n-i+mn[j]+(cnt[j]-)*n);
else maxx = max(maxx,j-i+mn[j]+(cnt[j]-)*n);
}
}
cout<<maxx<<endl;
}
return ;
}
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) A - D2的更多相关文章
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) C(二分+KMP)
http://codeforces.com/contest/1129/problem/C #include<bits/stdc++.h> #define fi first #define ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code
题意:给你n个01字符,每次问你前缀的所有本质不同的子串,由摩斯密码组成的方案数和. 题解:离线处理,把字符建sam,通过topo序来dp计算每个节点表示的子串方案数的和.统计答案时,把n个字符挨个匹 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)
A - Toy Train 很显然,一个站有多少个糖,那么就要从这个点运多少次.设第i个点有\(a_i\)个糖,那么就要转\(a_i-1\)圈,然后再走一段.很显然最后一段越小越好. 然后枚举起点后, ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1) 题解
A. Toy Train 时间限制:2 seconds 内存限制:256 megabytes 题意 有编号111~n(n≤5000)n(n\le 5000)n(n≤5000)的车站顺时针呈环排列,有m ...
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- int和integer;Math.round(11.5)和Math.round(-11.5)
int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...
随机推荐
- vue文档全局api笔记2
1.Vue.filter( id, [definition] ) 在组件内注册 <template> <div id="app"> <div clas ...
- Of Study
Bacon Reading maketh a full man; conference a ready man; and writing an exact man. And therefore, if ...
- mysql-SQL Error: 1205, SQLState: 41000
mysql-SQL Error: 1205, SQLState: 41000——CSDN问答频道https://ask.csdn.net/questions/176492 mysql-SQL Erro ...
- 如何在 Linux 中查找最大的 10 个文件
https://linux.cn/article-9495-1.html
- 简单理解laravel框架中的服务容器,服务提供者以及怎样调用服务
laravel被称为最优雅的框架,最近正在学习中,对于用惯了thinkphp.ci框架的人来说,服务容器.服务提供者,依赖注入这些概念简直是一脸懵逼.我花了些时间梳理了一下,也不敢确定自己说的是对 ...
- JavaScript charAt() 方法
<script> var str="abcdef"; alert(str[0]); //a,高版本浏览器兼容 alert(str.charAt(0)); //a,兼容所 ...
- java.lang.NoClassDefFoundError: org/apache/log4j/Priority的问题解决
在pom 文件中添加 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artif ...
- 搞了一下午时间全浪费在这了,其实是自己拷贝了patch文件,导致tab变成了空格的错
很老实的基于最新的kernel,源文件,修改了代码.通过diff -uNr --show-c-function dir1 dir2 > ipv6.patch制作了patch文件,准备代码上库构建 ...
- jenkins的 git多分支自动构建
一.先做好jenkins和gitlab的webhook自动构建 二.选择哪个分支(我这是test分支) 三.选择build Triggers 四.过滤test分支 五.保存即可
- PyCharm的使用
1.pycharm的下载和安装 首先,去jetbrains官网https://www.jetbrains.com/pycharm/download/#section=windows 中下载最新版的pr ...