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

A. Best Subsegment

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given array a1,a2,,ana1,a2,…,an. Find the subsegment al,al+1,,aral,al+1,…,ar (1lrn1≤l≤r≤n) with maximum arithmetic mean 1rl+1i=lrai1r−l+1∑i=lrai(in floating-point numbers, i.e. without any rounding).

If there are many such subsegments find the longest one.

Input

The first line contains single integer nn (1n1051≤n≤105) — length of the array aa.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai1090≤ai≤109) — the array aa.

Output

Print the single integer — the length of the longest subsegment with maximum possible arithmetic mean.

Example
input
Copy
5
6 1 6 6 0
output
Copy
2

The subsegment [3,4][3,4] is the longest among all subsegments with maximum arithmetic mean.

 题解:题目意思就是让你找一个序列的子序列平均值最大的(如果均值相同,则找最长的)

这题是水题,其实就是找到最大的那个数Maxnum,然后统计Maxnum连续出现的最多次数;

参考代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1e5+10;
 5 int n,a[maxn];
 6 int main()
 7 {
 8     scanf("%d",&n);
 9     int Max=-1,ans=-1,cnt=0;
10     for(int i=1;i<=n;++i) scanf("%d",a+i),Max=max(Max,a[i]);
11     for(int i=1;i<=n;++i)
12     {
13         if(a[i]==Max) cnt++;
14         else ans=max(ans,cnt),cnt=0;
15     }
16     ans=max(ans,cnt); 
17     cout<<ans<<endl;
18     
19     return 0;
20 }
View Code

B. Emotes

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn emotes in very popular digital collectible card game (the game is pretty famous so we won't say its name). The ii-th emote increases the opponent's happiness by aiai units (we all know that emotes in this game are used to make opponents happy).

You have time to use some emotes only mm times. You are allowed to use any emotion once, more than once, or not use it at all. The only restriction is that you cannot use the same emote more than kk times in a row (otherwise the opponent will think that you're trolling him).

Note that two emotes ii and jj (iji≠j) such that ai=ajai=aj are considered different.

You have to make your opponent as happy as possible. Find the maximum possible opponent's happiness.

Input

The first line of the input contains three integers n,mn,m and kk (2n21052≤n≤2⋅105, 1km21091≤k≤m≤2⋅109) — the number of emotes, the number of times you can use emotes and the maximum number of times you may use the same emote in a row.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109), where aiai is value of the happiness of the ii-th emote.

Output

Print one integer — the maximum opponent's happiness if you use emotes in a way satisfying the problem statement.

Examples
input
Copy
6 9 2
1 3 3 7 4 2
output
Copy
54
input
Copy
3 1000000000 1
1000000000 987654321 1000000000
output
Copy
1000000000000000000

In the first example you may use emotes in the following sequence: 4,4,5,4,4,5,4,4,54,4,5,4,4,5,4,4,5.

 题解:

意思就是给你n个数,然后现在要从这n个数里面挑选m个,每个数可以多次挑选,但不能连续挑选超过k次,(不同位置的数大小相同也是不同的数)

这题就是找到最大和次大的那个数和最大的数出现的次数,然后判断  k==1? 等于一的话,就是最大的数*m, 不等同于1的话,如果最大的数出现的次数

大于1的话,答案也是最大的数*m,如果等于1的话,答案就是k个最大的数,1个次大的数,交替,就是ans=(m-m/(k+1))*Max1+m/(k+1)*Max2;

参考代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll n,m,k,val,ans;
 5 int main()
 6 {
 7     cin>>n>>m>>k;
 8     ll Max1=0,Max2=0,cnt=1;
 9     for(int i=1;i<=n;++i)
10     {
11         cin>>val;
12         if(val>Max1) Max2=Max1,Max1=val,cnt=1;
13         else if(val>Max2) Max2=val;
14         else if(val==Max1) cnt++;
15     }
16     if(k==1)
17     {
18         if(cnt==1) ans=(m-m/2)*Max1+(m/2)*Max2;
19         else if(cnt>1) ans=m*Max1; 
20     } 
21     else
22     {
23         if(cnt>1) ans=m*Max1;
24         else if(cnt==1) ans=m/(k+1)*Max2+(m-m/(k+1))*Max1;
25     }
26     cout<<ans<<endl;
27     ///////////////////////////////////
28     return 0;
29 }
View Code

C. Magic Ship

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You a captain of a ship. Initially you are standing in a point (x1,y1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x2,y2).

You know the weather forecast — the string ss of length nn, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s1, the second day — s2s2, the nn-th day — snsn and (n+1)(n+1)-th day — s1s1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (x,y)(x,y) to (x,y+1)(x,y+1);
  • if wind blows the direction D, then the ship moves from (x,y)(x,y) to (x,y1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (x,y)(x,y) to (x1,y)(x−1,y);
  • if wind blows the direction R, then the ship moves from (x,y)(x,y) to (x+1,y)(x+1,y).

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction Uand the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x1,y+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2)(x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

Input

The first line contains two integers x1,y1x1,y1 (0x1,y11090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2x2,y2 (0x2,y21090≤x2,y2≤109) — the coordinates of the destination point.

It is guaranteed that the initial coordinates and destination point coordinates are different.

The third line contains a single integer nn (1n1051≤n≤105) — the length of the string ss.

The fourth line contains the string ss itself, consisting only of letters U, D, L and R.

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Examples
input
Copy
0 0
4 6
3
UUU
output
Copy
5
input
Copy
0 3
0 0
3
UDD
output
Copy
3
input
Copy
0 0
0 1
1
L
output
Copy
-1
题解:
  题目意思就是一艘船要从起点(x1,y1)到达终点(x2,y2),每天的风向是不同的,给出了n天的风向信息(每n天具有周期性),且每天小船可以往一个方向移动一个单位或者不移动;

让你求到达终点的最短时间,如果不能到达就返回-1;

这题由于答案具有单调性,因此可以二分答案m;对于风的速度和船的速度可以分开计算;

我们先计算出在m天风可以使小船移动到的位置,然后其和终点(x2,y2)的曼哈顿距离如果大于天数,则不能,小于等于天数则满足。一直二分答案,求出最小的天数;

 

参考代码:
 1 #include<bits/stdc++.h>
 2 #define maxl 100010
 3 using namespace std;
 4 typedef long long ll;
 5 #define clr(a,val) memset(a,val,sizeof(a))
 6 #define fi first
 7 #define se second
 8 #define pb push_back
 9 const int INF=0x3f3f3f3f;
10 inline ll read()
11 {
12     ll x=0,f=1;char ch=getchar();
13     while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
14     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
15     return x*f;
16 }
17 int n;
18 int a[maxl];
19 ll sx,sy,ex,ey,cx=0,cy=0;
20 ll tx[maxl],ty[maxl],sumx[maxl],sumy[maxl];
21 char ch[maxl];
22 ll ans;
23 bool judge(ll mid)
24 {
25     ll fx=mid/n*sumx[n]+sumx[mid%n];
26     ll fy=mid/n*sumy[n]+sumy[mid%n];
27     if(abs(ex-(sx+fx))+abs(ey-(sy+fy))<=mid) return 1;
28     else return 0;
29 }
30 void solve()
31 {
32     ll resx=ex-sx,resy=ey-sy,d=0,res=abs(resx)+abs(resy);
33     for(int i=1;i<=n;i++)
34     {
35         if(tx[i]*resx>0) res--;
36         else res++;
37         if(ty[i]*resy>0) res--;
38         else res++;
39         if(res<=i){ans=i;return;}         
40     }
41     if(resx*cx<=0 && cx!=0) d+=cx;
42     if(resy*cy<=0 && cy!=0) d+=cy;
43     if(d>=n){ans=-1;return;}
44     ll l=1,r=1ll<<62,mid;
45     while(l+1<r)
46     {
47         mid=(l+r)>>1;
48         if(judge(mid)) r=mid;
49         else l=mid;
50     }
51     if(judge(l)) ans=l;
52     else if(judge(r)) ans=r;
53     else ans=-1;
54 }
55 int main()
56 {
57     sx=read();sy=read();ex=read();ey=read();
58     scanf("%d",&n);
59     scanf("%s",ch+1);
60     for(int i=1;i<=n;i++)
61     {
62         if(ch[i]=='U') tx[i]=0,ty[i]=1;
63         if(ch[i]=='D') tx[i]=0,ty[i]=-1;
64         if(ch[i]=='L') tx[i]=-1,ty[i]=0;
65         if(ch[i]=='R') tx[i]=1,ty[i]=0;
66         sumx[i]=sumx[i-1]+tx[i];
67         sumy[i]=sumy[i-1]+ty[i];
68         cx+=tx[i];cy+=ty[i];
69     }
70     solve();
71     cout<<ans<<endl;
72     return 0;
73 }
View Code

 

 

posted @ 2019-02-20 12:54  StarHai  阅读(615)  评论(0编辑  收藏  举报