【Codeforces 1129A】Toy Train
【链接】 我是链接,点我呀:)
【题意】
火车从1,2,3...n->1的方式绕圈走。(即每次从i走到i+1)
有一些点有货物需要装载,但是每个点只能装上去一个货物。
每个货物都有目标点卸货点(卸货的时候不限量)
问你假设火车起点为s(s=1,2,3...n)时,完成所有点的装货卸货任务需要的最小时间。
【题解】
会发现其实每个点出去的任务都是互相独立的。
某个点在做运载任务的时候,其他人也可以同时进行运载任务(路过了就捎上它的运载物就行,反正装运不需要时间)。
那么现在的问题仅仅是,让每个点完成所有任务的时间最短。
然后取所有点完成任务的时间中最长的那个作为答案就好了。
设第i个点的任务个数为cnt[i]
每个点i完成所有任务的时间为
起点s到i所需的时间+n*(cnt[i]-1)+cnt[i]个任务中所需时间最短的那个任务所用时间
前cnt[i]-1个任务肯定要绕cnt[i]-1圈然后回到原来的位置的.
最后一个任务才是关键,我们只要让最后一个任务的时间最短,那么肯定第i个点的所有任务完成的时间也就最短了。
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5000;
int n,m;
int mi[N+10],cnt[N+10];
int get_dis(int x,int y){
if (y>=x) return y-x;
else return y+n-x;
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >>n >> m;
for (int i = 1;i <= m;i++){
int x,y;
cin >> x >> y;
cnt[x]++;
if (mi[x]==0)
mi[x]=get_dis(x,y);
else
mi[x] = min(mi[x],get_dis(x,y));
}
for (int i = 1;i <= n;i++){
int ans = 0;
for (int j = 1;j<=n;j++){
ans = max(ans,get_dis(i,j)+n*(cnt[j]-1)+mi[j]);
}
cout<<ans<<" ";
}
return 0;
}
【Codeforces 1129A】Toy Train的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【33.33%】【codeforces 586D】Phillip and Trains
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
随机推荐
- is not mapped [from错误
我出现的错误是:org.hibernate.hql.ast.QuerySyntaxException: loginuser is not mapped [from loginuser] 配置文件如下: ...
- 私有CA和证书
证书类型 证书授权机构的证书 服务器 用户证书 获取证书两种方法 使用证书授权机构: 生成签名请求(csr ) 将csr发送给CA 从CA处接收签名 自签名的证书: 自已签发自己的公钥 openSSL ...
- SQL Server触发器创建、删除、修改、查看示例步骤
一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约`束. 二﹕ SQL Server为每个触发 ...
- tp5增加验证的自定义规则
- daily_journal_3 the game of thrones
昨晚追完了最爱的美剧(the game of thrones),哇,看到结局有点崩溃.果然还是美帝淫民开放,各种乱伦,在七夕收到的万点暴击就祝天下有情人就像剧中一样终是血亲. 昨天算是完成了git的复 ...
- Using 10053 Trace Events and get outline
When it comes to performance tuning, we can spend time on one or both ends of the problem. On the &q ...
- ASP.NET 简介(转自Wiki)
ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...
- Spring Cloud学习(一)
SpringCloud是什么? Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案. Spring Cloud对微服务基础框 ...
- Storm编程入门API系列之Storm的可靠性的ACK消息确认机制
概念,见博客 Storm概念学习系列之storm的可靠性 什么业务场景需要storm可靠性的ACK确认机制? 答:想要保住数据不丢,或者保住数据总是被处理.即若没被处理的,得让我们知道. publi ...
- poj1240 Pre-Post-erous!
思路: 根据前序序列和后序序列递归构造m叉树,确定每个节点的子节点数量.再用组合数公式累乘. 实现: #include <iostream> using namespace std; ][ ...