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可 ...
随机推荐
- 美团2016秋招笔试B
1.下述解决死锁的方法中,属于死锁预防策略的是? 资源有序分配法 银行家算法:避免死锁 资源有序分配法:预防死锁 资源分配图化简法:检测死锁 撤销进程法:解决死锁 2. 什么是死锁? 如果一个进 ...
- c++ 入门之对象指针
我们想 像使用基本数据类型一样使用类,自然,类自然也有指针,我们通过下面的代码来领教一下对象指针存在的意义: # include "iostream" # include &quo ...
- c++入门之内置数组和array比较
array是C++11中新提出来的容器类型,与内置数组相比,array是一种更容易使用,更加安全的数组类型,可以用来替代内置数组.作为数组的升级版,继承了数组最基本的特性,也融入了很多容器操作,下面介 ...
- [2019BUAA软工助教]Alpha阶段无人转出申请审核结果
[2019BUAA软工助教]Alpha阶段无人转出申请审核结果 一.队伍信息 队伍名 项目 人数 红太阳 社团 8(6+2) pureman 博客园 6 水哥牛逼 招募 6 葫芦娃 拖拽Pytorch ...
- elasticsearch聚合操作——本质就是针对搜索后的结果使用桶bucket(允许嵌套)进行group by,统计下分组结果,包括min/max/avg
分析 Elasticsearch有一个功能叫做聚合(aggregations),它允许你在数据上生成复杂的分析统计.它很像SQL中的GROUP BY但是功能更强大. 举个例子,让我们找到所有职员中最大 ...
- ES使用C#添加和更新文档
ElasticSearch 使用C#添加和更新文档 这是ElasticSearch 2.4 版本系列的第四篇: 第一篇:ES1:Windows下安装ElasticSearch 第二篇:ES2:Elas ...
- Jmeter之发送请求入参必须使用编码格式、Jmeter之发送Delete请求可能入参需要使用编码格式
这里的其中一个属性值必须要先编码再传参才可以,具体可以通过抓包分析观察:
- MySQL的SQL语句优化-group by语句的优化
原文:http://bbs.landingbj.com/t-0-243202-1.html 默认情况下,MySQL排序所有GROUP BY col1, col2, ....,查询的方法如同在查询中指定 ...
- Azure系列2.1.7 —— BlobRequestOptions
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- [官网]How to use distributed transactions with SQL Server on Docker
How to use distributed transactions with SQL Server on Docker https://docs.microsoft.com/en-us/sql/l ...