CoderForces Round526 (A~E)题解

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 |ab||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 (1n1001≤n≤100) — the number of floors.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1000≤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). 42+81=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;

求最小花费;

枚举即可;

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int INF=0x3f3f3f3f;
 5 int a[105];
 6 int main()
 7 {
 8     int n,s,Mix=INF;
 9     cin>>n;
10     for(int i=0;i<n;i++) cin>>a[i];
11     for(int i=0;i<n;i++)
12     {
13         s=0;
14         for(int j=1;j<n;j++) s+=(abs(i-j)*2+j*2+i*2)*a[j];
15         Mix=min(Mix,s);
16     }
17     cout<<Mix<<endl;
18     return 0;
19 }
View Code

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 (1n1031≤n≤103, 1s10121≤s≤1012) — the number of kegs and glass volume.

The second line contains nn integers v1,v2,,vnv1,v2,…,vn (1vi1091≤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,后来发现是二分,时间不够了,实际上二分即可解决,下来又想想,发现一个巧妙思路 很简单就过了。

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll n,s,sum,Min,a[1010];
 5 bool Judge(ll x) {return sum-n*x>=s? 1:0;}
 6 int main()
 7 {
 8     scanf("%lld%lld",&n,&s);Min=1000000000010;
 9     for(int i=1;i<=n;++i) { scanf("%lld",a+i);sum+=a[i];Min=min(Min,a[i]);}
10     ll l=0,r=Min,ans=-1;
11     if(sum<s) {puts("-1");return 0;}
12     while(l<=r)
13     {
14         ll mid=l+r>>1;
15         if(Judge(mid)) l=mid+1,ans=max(ans,mid);
16         else r=mid-1;
17     }
18     printf("%lld
",ans);
19     return 0;
20 }
View Code

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

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define mod 1000000007
 5 const int INF=0x3f3f3f3f;
 6 const int maxn=1e5+10;
 7 ll len,ans,pos,flag,dp[maxn];
 8 char s[maxn],s1[maxn];
 9 int main()
10 {
11     scanf("%s",s);
12     ans=len=pos=0;flag=1;
13     for(int i=0,n=strlen(s);i<n;++i) if(s[i]=='a' || s[i]=='b') s1[++len]=s[i];
14     s1[++len]='b';
15     for(int i=1;i<=len;++i)
16     {
17         if(s1[i]=='a') pos++,flag=0;
18         else ans=(ans+ans*pos%mod)%mod,ans=(ans+pos)%mod,pos=0;
19     }
20     if(flag) puts("0");
21     else printf("%lld
",ans);
22     return 0;
23 }
View Code

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 (1n31051≤n≤3⋅105) — the number of cities.

The second line contains nn integers w1,w2,,wnw1,w2,…,wn (0wi1090≤wi≤109) — the maximum amounts of liters of gasoline that Nut can buy in cities.

Each of the next n1n−1 lines describes road and contains three integers uu, vv, cc (1u,vn1≤u,v≤n, 1c1091≤c≤109, uvu≠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 2132→1→3.

The optimal way in the second example is 242→4.

 题解:

题意:

  一棵n个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~n-1的排列),要求找到一条路径,使得路径的mex最大。

对于一个根节点,维护从叶子节点到该根节点的最长路径和次长路径(这里指剩余量最大和次大),然后f[u]记录从某点到该最大路径,

则ans=max(ans,max(f[u],m[u]+mx1+mx2))即可;

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 inline int read() 
 5 {
 6     int x=0,f=1;char ch=getchar();
 7     for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 8     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
 9     return x*f;
10 }
11 const int N = 300005;
12 struct Edge{
13     int to, nxt, w;
14     Edge() {}
15     Edge(int a,int b,int c) { to=a,nxt=b,w=c;}
16 }e[N << 1];
17 int head[N], w[N], En;
18 
19 inline void add_edge(int u,int v,int w) 
20 {
21     ++En; e[En] = Edge(v, head[u], w); head[u] = En;
22     ++En; e[En] = Edge(u, head[v], w); head[v] = En;
23 }
24 
25 LL f[N], Ans;
26 void dfs(int u,int fa) 
27 {
28     f[u] = w[u];
29     LL mx1 = -1e18, mx2 = -1e18;
30     for(int i=head[u];i;i=e[i].nxt) 
31     {
32         int v=e[i].to;
33         if (v==fa) continue;
34         dfs(v,u);
35         LL t=f[v]-e[i].w;
36         if(t>mx1) mx2=mx1,mx1=t;
37         else if(t>mx2) mx2=t;
38         if(f[v]>e[i].w) f[u]=max(f[u],f[v]-e[i].w+w[u]);
39     }
40     Ans=max(Ans,max(f[u],mx1+mx2+w[u]));
41 }
42 
43 int main() 
44 {
45     int n = read();
46     for (int i = 1; i <= n; ++i) w[i] = read();
47     for (int i = 1; i < n; ++i) 
48     {
49         int u = read(), v = read(), w = read();
50         add_edge(u, v, w);
51     }
52     dfs(1, 0);
53     cout << Ans<<endl;
54     return 0;
55 }
View Code

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 aba≠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 (1n51051≤n≤5⋅105, 1k1091≤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个);

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 1100000
 4 #define L 1000000
 5 typedef long long ll;
 6 const ll inf=1e14+7;
 7 inline ll read()
 8 {
 9     char ch=0;
10     ll x=0,flag=1;
11     while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
12     while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
13     return x*flag;
14 }
15 char s[N],t[N];
16 ll dp[N][2][2];//第N层是否与s相同,是否与t相同 
17 int main()
18 {
19     ll n=read(),m=read();
20     scanf("%s",s+1);scanf("%s",t+1);
21     dp[0][1][1]=1;
22     for(ll i=0;i<n;i++)
23     {
24         if(s[i+1]==t[i+1]) dp[i+1][1][1]=dp[i][1][1];
25         else dp[i+1][1][0]=dp[i+1][0][1]=dp[i][1][1];
26         dp[i+1][1][0]+=dp[i][1][0];
27         dp[i+1][0][1]+=dp[i][0][1]; 
28         dp[i+1][0][0]=min(2*dp[i][0][0],inf);
29         
30         if(s[i+1]=='a') dp[i+1][0][0]=min(dp[i+1][0][0]+dp[i][1][0],inf);
31         if(t[i+1]=='b') dp[i+1][0][0]=min(dp[i+1][0][0]+dp[i][0][1],inf);
32     }
33     ll ans=0;
34     for(ll i=1;i<=n;i++)
35     {
36         ll tot=0;
37         for(ll j=0;j<=1;j++)
38             for(ll k=0;k<=1;k++)
39                 tot=min(tot+dp[i][j][k],m);
40         ans+=tot;
41     }
42     printf("%lld",ans);
43     return 0;
44 }
View Code
原文地址:https://www.cnblogs.com/csushl/p/10176828.html