CoderForces Round526 (A~E)题解
A. The Fair Nut and Elevator
1 second
256 megabytes
standard input
standard output
The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.
It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:
- Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
- Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
- Moves from the bb-th floor back to the xx-th.
The elevator never transposes more than one person and always goes back to the floor xx before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the aa-th floor to the bb-th floor requires |a−b||a−b|units of electricity.
Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.
The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of floors.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100) — the number of people on each floor.
In a single line, print the answer to the problem — the minimum number of electricity units.
3
0 2 1
16
2
1 1
In the first example, the answer can be achieved by choosing the second floor as the xx-th floor. Each person from the second floor (there are two of them) would spend 44 units of electricity per day (22 to get down and 22 to get up), and one person from the third would spend 88units of electricity per day (44 to get down and 44 to get up). 4⋅2+8⋅1=164⋅2+8⋅1=16.
In the second example, the answer can be achieved by choosing the first floor as the xx-th floor.
题解:就是一部电梯固定在某一位置,然后给你一些人,他们每天会上下楼,从a层到b层会花费|a-b|的费用,电梯运行为:从固定点x到a接乘客,送到b,然后再回到固定点x;
求最小花费;
枚举即可;
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int a[];
int main()
{
int n,s,Mix=INF;
cin>>n;
for(int i=;i<n;i++) cin>>a[i];
for(int i=;i<n;i++)
{
s=;
for(int j=;j<n;j++) s+=(abs(i-j)*+j*+i*)*a[j];
Mix=min(Mix,s);
}
cout<<Mix<<endl;
return ;
}
B. Kvass and the Fair Nut
1 second
256 megabytes
standard input
standard output
The Fair Nut likes kvass very much. On his birthday parents presented him nn kegs of kvass. There are vivi liters of kvass in the ii-th keg. Each keg has a lever. You can pour your glass by exactly 11 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by ss liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible.
Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by ss liters of kvass.
The first line contains two integers nn and ss (1≤n≤1031≤n≤103, 1≤s≤10121≤s≤1012) — the number of kegs and glass volume.
The second line contains nn integers v1,v2,…,vnv1,v2,…,vn (1≤vi≤1091≤vi≤109) — the volume of ii-th keg.
If the Fair Nut cannot pour his glass by ss liters of kvass, print −1−1. Otherwise, print a single integer — how much kvass in the least keg can be.
3 3
4 3 5
3
3 4
5 3 4
2
3 7
1 2 3
In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg.
In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg.
In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is −1.
题解:
题意:就是给你N个酒桶,每个酒桶有不同的酒,然后给你一个杯子k,问能不能倒满这个杯子,不能输出-1,能的话输出剩余酒桶中最多的为多少。
题解:一开始暴力,结果一直tle,后来发现是二分,时间不够了,实际上二分即可解决,下来又想想,发现一个巧妙思路 很简单就过了。
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,s,sum,Min,a[];
bool Judge(ll x) {return sum-n*x>=s? :;}
int main()
{
scanf("%lld%lld",&n,&s);Min=;
for(int i=;i<=n;++i) { scanf("%lld",a+i);sum+=a[i];Min=min(Min,a[i]);}
ll l=,r=Min,ans=-;
if(sum<s) {puts("-1");return ;}
while(l<=r)
{
ll mid=l+r>>;
if(Judge(mid)) l=mid+,ans=max(ans,mid);
else r=mid-;
}
printf("%lld\n",ans);
return ;
}
C. The Fair Nut and String
1 second
256 megabytes
standard input
standard output
The Fair Nut found a string ss. The string consists of lowercase Latin letters. The Nut is a curious guy, so he wants to find the number of strictly increasing sequences p1,p2,…,pkp1,p2,…,pk, such that:
- For each ii (1≤i≤k1≤i≤k), spi=spi= 'a'.
- For each ii (1≤i<k1≤i<k), there is such jj that pi<j<pi+1pi<j<pi+1 and sj=sj= 'b'.
The Nut is upset because he doesn't know how to find the number. Help him.
This number should be calculated modulo 109+7109+7.
The first line contains the string ss (1≤|s|≤1051≤|s|≤105) consisting of lowercase Latin letters.
In a single line print the answer to the problem — the number of such sequences p1,p2,…,pkp1,p2,…,pk modulo 109+7109+7.
abbaa
5
baaaa
4
agaa
In the second example, there are 44 possible sequences. [2][2], [3][3], [4][4], [5][5].
In the third example, there are 33 possible sequences. [1][1], [3][3], [4][4].
题解:
思路:
对于找到的每一个a…a区间
设ans为这之前已经找到的匹配数pos为该区间a的数量
则对于每一种匹配方案,该a…a区间的每一个a都可以与之匹配形成新的合法字符串
所以答案加上pos*ans+pos(注意单个a也是合法字符串)
每找到一段a区间统计答案即可复杂度O(n)
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
const int INF=0x3f3f3f3f;
const int maxn=1e5+;
ll len,ans,pos,flag,dp[maxn];
char s[maxn],s1[maxn];
int main()
{
scanf("%s",s);
ans=len=pos=;flag=;
for(int i=,n=strlen(s);i<n;++i) if(s[i]=='a' || s[i]=='b') s1[++len]=s[i];
s1[++len]='b';
for(int i=;i<=len;++i)
{
if(s1[i]=='a') pos++,flag=;
else ans=(ans+ans*pos%mod)%mod,ans=(ans+pos)%mod,pos=;
}
if(flag) puts("");
else printf("%lld\n",ans);
return ;
}
D. The Fair Nut and the Best Path
3 seconds
256 megabytes
standard input
standard output
The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city uu and go by a simple path to city vv. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.
A filling station is located in every city. Because of strange law, Nut can buy only wiwi liters of gasoline in the ii-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.
He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.
The first line contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the number of cities.
The second line contains nn integers w1,w2,…,wnw1,w2,…,wn (0≤wi≤1090≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.
Each of the next n−1n−1 lines describes road and contains three integers uu, vv, cc (1≤u,v≤n1≤u,v≤n, 1≤c≤1091≤c≤109, u≠vu≠v), where uu and vv — cities that are connected by this road and cc — its length.
It is guaranteed that graph of road connectivity is a tree.
Print one number — the maximum amount of gasoline that he can have at the end of the path.
3
1 3 3
1 2 2
1 3 2
3
5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1
The optimal way in the first example is 2→1→32→1→3.
The optimal way in the second example is 2→42→4.
题解:
题意:
一棵n个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~n-1的排列),要求找到一条路径,使得路径的mex最大。
对于一个根节点,维护从叶子节点到该根节点的最长路径和次长路径(这里指剩余量最大和次大),然后f[u]记录从某点到该最大路径,
则ans=max(ans,max(f[u],m[u]+mx1+mx2))即可;
参考代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read()
{
int x=,f=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';
return x*f;
}
const int N = ;
struct Edge{
int to, nxt, w;
Edge() {}
Edge(int a,int b,int c) { to=a,nxt=b,w=c;}
}e[N << ];
int head[N], w[N], En; inline void add_edge(int u,int v,int w)
{
++En; e[En] = Edge(v, head[u], w); head[u] = En;
++En; e[En] = Edge(u, head[v], w); head[v] = En;
} LL f[N], Ans;
void dfs(int u,int fa)
{
f[u] = w[u];
LL mx1 = -1e18, mx2 = -1e18;
for(int i=head[u];i;i=e[i].nxt)
{
int v=e[i].to;
if (v==fa) continue;
dfs(v,u);
LL t=f[v]-e[i].w;
if(t>mx1) mx2=mx1,mx1=t;
else if(t>mx2) mx2=t;
if(f[v]>e[i].w) f[u]=max(f[u],f[v]-e[i].w+w[u]);
}
Ans=max(Ans,max(f[u],mx1+mx2+w[u]));
} int main()
{
int n = read();
for (int i = ; i <= n; ++i) w[i] = read();
for (int i = ; i < n; ++i)
{
int u = read(), v = read(), w = read();
add_edge(u, v, w);
}
dfs(, );
cout << Ans<<endl;
return ;
}
E. The Fair Nut and Strings
1 second
256 megabytes
standard input
standard output
Recently, the Fair Nut has written kk strings of length nn, consisting of letters "a" and "b". He calculated cc — the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.
Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string ss and not biggerthan string tt. He is interested: what is the maximum value of cc that he could get.
A string aa is lexicographically smaller than a string bb if and only if one of the following holds:
- aa is a prefix of bb, but a≠ba≠b;
- in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.
The first line contains two integers nn and kk (1≤n≤5⋅1051≤n≤5⋅105, 1≤k≤1091≤k≤109).
The second line contains a string ss (|s|=n|s|=n) — the string consisting of letters "a" and "b.
The third line contains a string tt (|t|=n|t|=n) — the string consisting of letters "a" and "b.
It is guaranteed that string ss is lexicographically not bigger than tt.
Print one number — maximal value of cc.
2 4
aa
bb
6
3 3
aba
bba
8
4 5
abbb
baaa
8
In the first example, Nut could write strings "aa", "ab", "ba", "bb". These 44 strings are prefixes of at least one of the written strings, as well as "a" and "b". Totally, 66 strings.
In the second example, Nut could write strings "aba", "baa", "bba".
In the third example, there are only two different strings that Nut could write. If both of them are written, c=8c=8.
题解:
在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多。
考虑将其分为n层,然后对于每一层,我们在k和该层最多可以添加的数量取最小值;(如果没有s和t的限制,i层可以选2^i个);
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define N 1100000
#define L 1000000
typedef long long ll;
const ll inf=1e14+;
inline ll read()
{
char ch=;
ll x=,flag=;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-;}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*flag;
}
char s[N],t[N];
ll dp[N][][];//第N层是否与s相同,是否与t相同
int main()
{
ll n=read(),m=read();
scanf("%s",s+);scanf("%s",t+);
dp[][][]=;
for(ll i=;i<n;i++)
{
if(s[i+]==t[i+]) dp[i+][][]=dp[i][][];
else dp[i+][][]=dp[i+][][]=dp[i][][];
dp[i+][][]+=dp[i][][];
dp[i+][][]+=dp[i][][];
dp[i+][][]=min(*dp[i][][],inf); if(s[i+]=='a') dp[i+][][]=min(dp[i+][][]+dp[i][][],inf);
if(t[i+]=='b') dp[i+][][]=min(dp[i+][][]+dp[i][][],inf);
}
ll ans=;
for(ll i=;i<=n;i++)
{
ll tot=;
for(ll j=;j<=;j++)
for(ll k=;k<=;k++)
tot=min(tot+dp[i][j][k],m);
ans+=tot;
}
printf("%lld",ans);
return ;
}
CoderForces Round526 (A~E)题解的更多相关文章
- CoderForces Round60-(1117A,1117B,1117C题解)
A. Best Subsegment time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
随机推荐
- redis 数据库主从不一致问题解决方案
在聊数据库与缓存一致性问题之前,先聊聊数据库主库与从库的一致性问题. 问:常见的数据库集群架构如何? 答:一主多从,主从同步,读写分离. 如上图: (1)一个主库提供写服务 (2)多个从库提供读服务 ...
- Java基础:数组的声明,循环,赋值,拷贝。
数组的声明 一般有两种形式的声明: int[] a; int a[]; 这两种声明都没错,但是大多数都使用第一种声明,因为变量名是独立的,不带任何符号. 声明一个数组,并没有将a真正的初始化为一个数组 ...
- spark集群搭建(三台虚拟机)——hadoop集群搭建(2)
!!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...
- Mongodb自动备份数据库并删除指定天数前的备份
1.创建Mongodb数据库备份目录 mkdir -p /home/backup/mongod_bak/mongod_bak_now mkdir -p /home/backup/mongod_bak/ ...
- 【Flume】Flume基础之安装与使用
1.Flume简介 (1) Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集.聚集.移动的服务,Flume只能在Unix环境下运行. (2) Flume基于流式架构,容错性强, ...
- 力扣(LeetCode)平方数之和 个人题解
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...
- C语言1博客作业01
1 你对软件工程专业或者计算机科学与技术专业了解是怎样? 主修大数据技术导论.数据采集与处理实践(Python).Web前/后端开发.统计与数据分析.机器学习.高级数据库系统.数据可视化.云计算技术. ...
- js数组合并以及对象的遍历
这是很基础的知识,but,对于一只未系统学习过js,只略懂搬砖的跨界狗,还是经常犯错: 场景:移动端上拉加载更多. 初始数组合并后来请求的数组. 使用concat方法,不过要主要: 使用concat, ...
- Openlayers 实现轨迹播放/暂停/重新播放/从点击处播放/提速/减速
说明: 我的需求是需要实现轨迹播放/暂停/重新播放/从点击处播放,因此封装了一个类 解决方案: 1.初始化:主要是处理一下图层以及数据,通过插值构造一个全局数组 /** * @description ...
- 《master the game of GO wtth deep neural networks and tree search》研究解读
现在”人工智能“如此火爆的一大直接原因便是deepmind做出的阿尔法狗打败李世石,从那时开始计算机科学/人工智能成为了吹逼的主流.记得当时还是在学校晚新闻的时候看到的李世石输的消息,这个新闻都是我给 ...