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

  1. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

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

  3. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)

    A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...

  4. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)C. Morse Code

    题意:给你n个01字符,每次问你前缀的所有本质不同的子串,由摩斯密码组成的方案数和. 题解:离线处理,把字符建sam,通过topo序来dp计算每个节点表示的子串方案数的和.统计答案时,把n个字符挨个匹 ...

  5. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 1)

    A - Toy Train 很显然,一个站有多少个糖,那么就要从这个点运多少次.设第i个点有\(a_i\)个糖,那么就要转\(a_i-1\)圈,然后再走一段.很显然最后一段越小越好. 然后枚举起点后, ...

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

  7. Codeforces Round 542 (Div. 2)

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

  8. Codeforces Round #542 题解

    Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...

  9. int和integer;Math.round(11.5)和Math.round(-11.5)

    int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0,而Integer的默认值为null,即Integer可 ...

随机推荐

  1. Django 中间件版登录验证

    中间件版的登录验证需要依靠session,所以数据库中要有django_session表. urls.py # urls.py from django.conf.urls import url fro ...

  2. python-BeautifulSoup库详解

    快速使用 通过下面的一个例子,对bs4有个简单的了解,以及看一下它的强大之处: from bs4 import BeautifulSoup html = ''' <html><hea ...

  3. Pytorch 初识

    文章目录 一个简单的回归网络的例子 再来一个例子 官方教程上图片识别的例子 import torch import torch.nn as nn import torch.nn.functional ...

  4. R语言绘制直方图,

    直方图: 核密度函数: 练习题目1: 绘制出15位同学体重的直方图和核密度估计图,并与正态分布的概率密度函数作对比 代码如下: > w <- c(75.0, 64.0, 47.4, 66. ...

  5. 通过设置线程池的最小线程数来提高task的效率,SetMinThreads。

    http://www.cnblogs.com/Charltsing/p/taskpoolthread.html task默认对线程的调度是逐步增加的,连续多次运行并发线程,会提高占用的线程数,而等若干 ...

  6. Elasticsearch IK+pinyin

    如何在Elasticsearch中安装中文分词器(IK+pinyin)   如果直接使用Elasticsearch的朋友在处理中文内容的搜索时,肯定会遇到很尴尬的问题——中文词语被分成了一个一个的汉字 ...

  7. Java 常见编码格式——URL、Base64

    数据编码 我们对数据进行编码是因为在某些情况下,不能直接传输中文字符或者其他字符,比如在设置http协议的头部信息或者cookie时,如果value有中文字符,那么就需要将中文字符使用某种编码方式进行 ...

  8. vue动态class——实现tag的选中状态

    vue动态class——实现tag的选中状态 <template> <div class="common-nav"> <div class=" ...

  9. Azure系列2.1.4 —— BlobInputStream

    (小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...

  10. [转帖]dd命令详解

    dd命令详解 https://czmmiao.iteye.com/blog/1748748 之前一直对linux的命令很恐惧 现在发现 其实不是那么复杂 要仔细学习就可以了 比如 dd = disk ...