Codeforces Round #342 (Div. 2)总结

来源:互联网 发布:淘宝的优化建议 编辑:程序博客网 时间:2024/05/17 02:14

A题:

A题题目链接

题目描述:

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.

题意:

给定n元钱,可以买两种饮料,一种饮料是塑料瓶子装的饮料,单价为a元,而另外一种是玻璃瓶子装的饮料,单价为b元,但是玻璃瓶子拿到商店可以返还c元。问n元钱最多能够喝到多少瓶饮料?

解析:

其实这道题是典型的分类讨论的题目,我们可以这样想,既然要尽可能的买到最多的饮料,那么每瓶饮料的平均单价要最低,也就是说,我们购买饮料的时候,力求成本最低。那么我们可以比较两种饮料,塑料瓶子的饮料成本为a元每瓶,而玻璃瓶子的饮料成本为(b-c)元每瓶。所以呢,在大方面上我们就可以分成两个方向进行讨论。

一种是塑料瓶子成本更低,反之是玻璃瓶子。塑料瓶子成本更低的话,由于瓶子无法换钱,所以能买饮料的数量就等于n/a

反之则是玻璃瓶子,当玻璃瓶子成本更低的时候,我们应该注意一个问题,那就是我们要先买饮料,然后再将玻璃瓶子拿去换钱,所以呢,在买最后一瓶的时候,我们应该单独考虑(因为可能最后一瓶瓶子换的钱加上剩下的钱不再足以购买一瓶玻璃装的饮料,而前面的都是b-c的组合),在玻璃瓶子饮料的成本更低的情况下,我们应该尽可能的先买玻璃瓶子的饮料,然后再买塑料瓶子的饮料。

完整代码实现:

#include<cstdio>#include<algorithm>typedef long long ll;int main(){    ll n,a,b,c;    while(scanf("%I64d%I64d%I64d%I64d",&n,&a,&b,&c)==4&&n&&a&&b&&c)    {        ll ans = 0;        if(n >= b && a >= (b - c))        {            ans += (n - b) / (b - c) + 1;            n -= ans *(b-c);        }        ans += n / a;        printf("%I64d\n",ans);    }    return 0;}
上述代码if循环中语句的解释:

由于是单独分离出一个b进行分类讨论,那么剩下的部分就是(n - b) / (b - c)的余数,必然是小于b-c的,然后加上最后一瓶饮料返还的c元,剩下的部分是小于b元的,是不足以再买一瓶玻璃瓶子的饮料。所以我们再尽可能的利用剩下的钱买塑料瓶子的饮料。

B题:

B题题目链接

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 000 characters. 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".

题意:

给定两个字符串,问第一个字符串至少几个字母被替换成‘#’,才能使得第二个子串不是第一个字符串的子串,若不需要替换,则输出0即可。

解析:

其实这道题目是对字符串操作的考察,要使得第二个子串不是第一个字符串的子串,那么与第二个字符串相同的部分应该全部被“破坏”,例如:

输出样列3

sirisiri

sir

我们只需要将字符串1的两个sir部分的任意字母用‘#’替换掉即可。

因此输出2,这样的话,实际上就转换成了字符串2在字符串1中出现了多少次的问题。

这样的话,我们就可以暴力遍历一遍字符串1便可得出答案。

我们在这里可以用三种方法解决:
1.指针法(处理字符串常用方法,但要注意细节问题以及指针在各操作结束后的指向问题,十分易错)

可以在子函数中声明两个字符指针,分别储存字符串1和字符串2当前遍历到的位置,分两种情况:

(1).如果遍历到的字符串1的部分与字符串2相同,那么计数器加1,并且当前遍历到的位置更新到该部分字符串的后一位

(2).如果遍历到的字符串1的部分与字符串2相同,那么计数器的值不更新,当前遍历到的位置与原来相比退后一位,这样的话才能做到逐一遍历。

完整代码实现:

#include<cstdio>#include<algorithm>const int maxn = 1e5;char str1[maxn+10],str2[100];int match(char *s1,char *s2);int main(){    while(scanf("%s%s",str1,str2)==2)    {        printf("%d\n",match(str1,str2));    }    return 0;}int match(char *s1,char *s2){    int sum = 0;    char *origin1 = s1,*origin2 = s2;    while(*s1 != '\0')    {        while(*s1 == *s2 && *s2 != '\0')        {            s1++;            s2++;        }        if(*s2 == '\0')        {            sum++;            s2 = origin2;            origin1 = s1;        }        else        {            if(*s1=='\0')  break;            else            {                origin1++;                s1 = origin1;                s2 = origin2;            }        }    }    return sum;}

2.直接用双重for循环遍历两个字符串,思想与上述指针法相同,完整代码实现:

#include<cstdio>#include<algorithm>const int maxn = 1e5;char str1[maxn+10],str2[100];int main(){    while(scanf("%s%s",str1,str2)==2)    {        int sum = 0;        for(int i = 0;str1[i] != '\0';i++)        {           int k = i,j = 0;           for(;str2[j] != '\0';k++,j++)           {               if(str1[k] != str2[j])                    break;           }           if(str2[j]=='\0')           {               sum++;               i = k - 1;           }        }        printf("%d\n",sum);    }    return 0;}
3.第三种方法是借鉴别人的,直接运用字符串处理函数strstr

strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL

百度百科描述:

包含文件:string.h
函数名: strstr
函数原型:
1
extern char *strstr(char *str1, const char *str2);
语法:
1
strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
例子:
1
2
3
char str[]="1234xyz";
char *str1=strstr(str,"34");
cout << str1 << endl;
显示的是: 34xyz
完整代码实现:

#include<cstdio>#include<algorithm>#include<cstring>const int maxn = 1e5;char s1[maxn+10],s2[100];int main(){    while(scanf("%s%s",s1,s2)==2)    {        int sum = 0,i = 0;        char *pc;        while(s1[i])        {            if ((pc=strstr(&s1[i],s2) )!=NULL)            {                ++sum;                i=pc-s1+strlen(s2);   //注意在这里s1是数组首元素的地址,那么pc-s1则表示pc指针后移的位数,然后再跳过匹配到的子串的长度                                      //然后再继续调用strstr函数判断是否为子串即可                continue;            }            break;        }        printf("%d\n",sum);    }    return 0;}

C题:

C题题目链接

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和k,然后输出第k列的和,以及一个n*n的矩阵。

这个矩阵需要满足的条件:

1.矩阵中所有的元素都是1~n^2中的元素之一,并且每个数只能出现一次

2.每一行的数字都是升序排列的

3.第k列数字的和要尽可能的大。

解析:

由于是英文题目,这道题题目意思可能比较难懂,但是通过输出样例也可以看出题目要表达的意思。首先我们分析题目要求,既然我们要第k列的元素尽可能大,而且又要满足每一行的数字都是升序排列的,所以,在第k列的左边的数,均小于第k列对应元素,同样的右边的则大于。由于输出矩阵可以有多个,输出其中任意一个即可,输出的值却是一定的,那么我们可以观察一下第二个输出样例,其实它是从一个矩阵变换而来的,我们可以把这个矩阵变换成原来的样子:

1 2 11 12 13

3 4 14 15 16

5 6 17 18 19

7 8 20 21 22

9 10 23 24 25

这样的话我们其实可以看出,把比第k列小的数都放在第k列的左边,然后从第k列开始,从第一行到第n行,依次升序填数,这样的话,使得每一行的第k列元素都尽可能最大,而这样才能使第k列的和最大。而如果是第一列,由于每一行都是升序的,那么只需要从左到右从上到下依次填入1~n^2即可。

完整代码实现:

#include<cstdio>#include<algorithm>using namespace std;typedef long long ll;const int maxn = 500;int tmp[maxn+20][maxn+20];int main(){    int n,k;    while(scanf("%d %d",&n,&k) == 2 && n && k)    {        ll sum = 0,flag = 1;        if(k == 1)        {            for(int i = 1; i <= n; i++)            {                sum += flag;                flag += n;            }            ll flag = 1;            for(int i = 1; i <= n; i++)            {                for(int j = 1; j <= n; j++)                {                    tmp[i][j] = flag;                    ++flag;                }            }        }        else        {            for(int i = 1; i <= n; i++)                for(int j = 1; j < k; j++)                {                    tmp[i][j] = flag;                    ++flag;                }            for(int i = 1; i <= n; i++)                for(int j = k; j <= n; j++)                {                    tmp[i][j] = flag;                    ++flag;                }            for(int i = 1; i <= n; i++)                sum += tmp[i][k];        }        printf("%I64d\n",sum);        for(int i = 1; i <= n; i++)        {            for(int j = 1; j <= n; j++)            {                if(j == 1)   printf("%d",tmp[i][j]);                else         printf(" %d",tmp[i][j]);            }            printf("\n");        }    }    return 0;}

如有错误,还请指正,O(∩_∩)O谢谢




0 0
原创粉丝点击