A. The Fair Nut and Elevator

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

In a single line, print the answer to the problem — the minimum number of electricity units.

Examples
input
3
0 2 1
output
16
input
2
1 1
output

4
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

 
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input

Copy
3 3
4 3 5
output

Copy
3
input

Copy
3 4
5 3 4
output

Copy
2
input
3 7
1 2 3
output-1

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

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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:

  1. For each ii (1≤i≤k1≤i≤k), spi=spi= 'a'.
  2. 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.

Input

The first line contains the string ss (1≤|s|≤1051≤|s|≤105) consisting of lowercase Latin letters.

Output

In a single line print the answer to the problem — the number of such sequences p1,p2,…,pkp1,p2,…,pk modulo 109+7109+7.

Examples
input
abbaa
output

Copy
5
input

Copy
baaaa
output

Copy
4
input
agaa
output

 3
In the first example, there are 55 possible sequences. [1][1], [4][4], [5][5], [1,4][1,4], [1,5][1,5].

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

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print one number — the maximum amount of gasoline that he can have at the end of the path.

Examples
input

Copy
3
1 3 3
1 2 2
1 3 2
output

Copy
3
input

Copy
5
6 3 2 5 0
1 2 10
2 3 3
2 4 1
1 5 1
output 7

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

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

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.

Output

Print one number — maximal value of cc.

Examples
input

Copy
2 4
aa
bb
output

Copy
6
input

Copy
3 3
aba
bba
output

Copy
8
input

Copy
4 5
abbb
baaa
output

Copy
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)题解的更多相关文章

  1. CoderForces Round60-(1117A,1117B,1117C题解)

    A. Best Subsegment time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  3. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  4. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  5. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  6. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  7. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  8. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  9. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

随机推荐

  1. 012.Kubernetes二进制部署worker节点Flannel

    一 部署flannel 1.1 安装flannel kubernetes 要求集群内各节点(包括 master 节点)能通过 Pod 网段互联互通.flannel 使用 vxlan 技术为各节点创建一 ...

  2. 本地yum配置

    yum yum(Yellow dog Updater, Modified)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件包管理器.基于 RPM 包管理,能够从 ...

  3. 网站搭建-IIS Windows系统搭建网站 (不小心看到自己的密码 - 怎么找回网站记住的密码)

    上一期说到IIS可以用自己喜欢的网站来直接玩,然后得得瑟瑟将自己的博客园账号首页拿过去玩(今天第一天水博客园). 然后自己访问啊,访问啊,然后就一直点啊点的,当然,其实后面的链接都是跳转到博客园里面去 ...

  4. 微信小程序引入全局或公共样式

    在开发的过程中,总会遇到很多可复用性的样式,为了代码更加的简洁和减少微不住道的文件体积,我抽取了一部分的公共样式,并全局引入,不知是否妥当,如有更好的想法,欢迎一起探讨 在app.wxss中引入 然后 ...

  5. Flutter 构建的 Mac 桌面应用上无法发出网络?

    在上一篇文章中我们分享了,如何开发桌面应用.在本章文章中,来解决一下为何在 Mac 中无法发出网络情况的原因. 起因 事情​起因是这样的:我总觉得写一个 Demo 不足以体现我们开发同学的能力.直到最 ...

  6. drf序列化组件之视图家族

    一.视图家族的分类 1.导入分类 from rest_framewok import views, generics, mixins, viewsets views:视图类 ​ 两大视图类:APIVi ...

  7. [Windows篇] 在windows 10上源码编译gtest 并编写CMakeLists.txt

    本文首发于个人博客https://kezunlin.me/post/aca50ff8/,欢迎阅读! compile gtest on windows 10 Guide compile gtest on ...

  8. Python和Java的区别

    这里是我的一些总结,有些是参考别人的(在这里谢谢!!!) 区别: 1.Python比Java简单,学习成本低,开发效率高2.Java运行效率高于Python,尤其是纯Python开发的程序,效率极低3 ...

  9. 音频工具kaldi部署及模型制作调研学习

    语音识别简介 语音识别(speech recognition)技术,也被称为自动语音识别(英语:Automatic Speech Recognition, ASR).计算机语音识别(英语:Comput ...

  10. 从UI设计转向前端的艰辛过程,从背单词开始。。。

    很纠结到底是继续做UI设计还是转行前端呢?从刚开始的害怕代码到接触代码又喜欢代码的过程,我在想我是不是太飘了,我感觉我做事就是三分钟热度.我感觉学前端对我最大的阻碍就是英语单词了,10个单词里面最起码 ...