Codeforces Round #342 (Div. 2) 题解

来源:互联网 发布:java算法大全 pdf 编辑:程序博客网 时间:2024/06/05 15:34
A. Guest From the Past
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.

Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs a rubles, or in glass liter bottle, that costs b rubles. Also, you may return empty glass bottle and get c (c < b) rubles back, but you cannot return plastic bottles.

Kolya has n rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn't know how to act optimally and asks for your help.

Input

First line of the input contains a single integer n (1 ≤ n ≤ 1018) — the number of rubles Kolya has at the beginning.

Then follow three lines containing integers ab and c (1 ≤ a ≤ 10181 ≤ c < b ≤ 1018) — the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively.

Output

Print the only integer — maximum number of liters of kefir, that Kolya can drink.

Sample test(s)
input
101198
output
2
input
10561
output
2
Note

In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.

In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir.


题意:有两种瓶子,塑料瓶单价a,玻璃瓶单价b,但每瓶可以得到c的收入。问总费用n最多能买多少?


买玻璃瓶:

n-k*b+k*c<b -> k>(n-b)/(n-c)

->  k=(n-b)/(n-c)+1; (n>=b)

可能n-k*b+k*c>=a
所以 k=k+(n-k*b+k*c)/a;

塑料瓶:

ans=n/a
m=n-n/a*a;
ans+=(m-b)/(m-c)+1;(m>=b)

比较:ans与k即可

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;ll a,b,c;int main(){ll n;ll l,r,mid;cin>>n>>a>>b>>c;ll ans=0;ans=n/a;ll cc=0;if(n>=b)cc=(n-b)/(b-c)+1;ll m=n-cc*b+cc*c;cc+=m/a;m=n-ans*a;if(m>=b)ans+=(m-b)/(b-c)+1;ans=max(ans,cc);cout<<ans<<endl;}


B. War of the Corporations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.

This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence.

Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn't contain the name of the phone as a substring.

Substring is a continuous subsequence of a string.

Input

The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100 000characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters.

Output

Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring.

Sample test(s)
input
intellecttell
output
1
input
googleapple
output
0
input
sirisirisir
output
2
Note

In the first sample AI's name may be replaced with "int#llect".

In the second sample Gogol can just keep things as they are.

In the third sample one of the new possible names of AI may be "s#ris#ri".


题意:给出两个字符串,后一个为子串求前面字符串中拥有的最少子串数,如此

可以直接暴力匹配即可,只是匹配时要注意已匹配的字符串不可重复匹配。


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;string s1,s2;int main(){cin>>s1>>s2;int n=s1.length();int m=s2.length();int i,j;i=0;int cnt=0;int k;while(i<n){    j=0;if(s1[i]==s2[j]){k=i;   while(j<m&&k<n&&s1[k]==s2[j])   {   k++;   j++;   }   if(j>=m){ i=k;cnt++;}   else i++;}else i++;}cout<<cnt<<endl;}


C. K-special Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.

Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is called k-special if the following three conditions are satisfied:

  • every integer from 1 to n2 appears in the table exactly once;
  • in each row numbers are situated in increasing order;
  • the sum of numbers in the k-th column is maximum possible.

Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.

Output

First print the sum of the integers in the k-th column of the required table.

Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.

If there are multiple suitable table, you are allowed to print any.

Sample test(s)
input
4 1
output
281 2 3 45 6 7 89 10 11 1213 14 15 16
input
5 3
output
855 6 17 18 199 10 23 24 257 8 20 21 223 4 14 15 161 2 11 12 13

 题意:求n*n的矩阵,k列为所有列值最大,每行为升序。

直接构造矩阵即可(数字是1~n^2)


1~k-1列数字必定小于n*(k-1)则为方便及保证从大到小,则从1~n*(k-1)按行保存前k-1列,再后续按行存下后n-k+1列.


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <string>#include <map>using namespace std;const int N=505;int a[N][N];int main(){int n,k;cin>>n>>k;int t=1;for(int i=1;i<=n;i++)for(int j=1;j<k;j++){    a[i][j]=t++;}for(int i=1;i<=n;i++){    for(int j=k;j<=n;j++){   a[i][j]=t++;}}int sum=0;for(int i=1;i<=n;i++)sum+=a[i][k];cout<<sum<<endl;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)cout<<a[i][j]<<" ";cout<<endl;}}















0 0
原创粉丝点击