URAL 2023. Donald is a postman (预处理)

2023. Donald is a postman

Time limit: 1.0 second

Memory limit: 64 MB

Donald Duck works as a postman for the Walt Disney Studios. He delivers children’s letters from all over the world to his friends, which are cartoon characters. The Studios has three cases for the letters,
with nine sections in each case. Every section has the name of the receiver on it. All cases stand in a row as it is shown at the picture below.

Donald Duck have brought n letters today. Initially, he stands near the leftmost case. He has to make one step to go to the neighboring case or to the previous one. How many steps will he make
until he puts all the letters into the respective sections, if he does this in the order they are in his bag?

Input

The first line contains an integer n that is the amount of letters in Donald’s bag (1 ≤ n ≤ 1 000). The following n lines contain receivers of the letters in the order they
are in the bag.

Output

Output the number of steps Donald should make to put all the letters into the cases.

Sample

input output
4
Aurora
Tiana
Ariel
Mulan
5

解析:预处理一下各个名字在哪个case里面,然后再按照输入顺序移动即可。

AC代码:

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

int a[30];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif //sxk

    for(int i=0; i<26; i++){
        if(i == 'A' - 'A' || i == 'P' - 'A' || i == 'O' - 'A' || i == 'R' - 'A')
            a[i] = 1;
        else if(i == 'B' - 'A' || i == 'M' - 'A' || i == 'S' - 'A')
            a[i] = 2;
        else if(i == 'D' - 'A' || i == 'G' - 'A' || i == 'J' - 'A' || i == 'K' - 'A' || i == 'T' - 'A' || i == 'W' - 'A')
            a[i] = 3;
        else a[i] = 0;
    }
    int n;
    string s;
    while(scanf("%d", &n)==1){
        int ans = 0, now = 1;
        for(int i=0; i<n ; i++){
            cin>>s;
            ans += a[ s[0] - 'A' ] > now ? a[ s[0] - 'A' ] - now : now - a[ s[0] - 'A' ];
            now = a[ s[0] - 'A' ];
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-30 14:43:12

URAL 2023. Donald is a postman (预处理)的相关文章

Ural 1635 Mnemonics and Palindromes(DP)

题目地址:Ural 1635 又是输出路径的DP...连着做了好多个了.. 状态转移还是挺简单的.要先预处理出来所有的回文串,tag[i][j]为1表示字符串i--j是一个回文串,否则不是回文串.预处理时要用n^2的方法,即枚举回文串中间,可以分奇数和偶数两种分别求一次. 然后dp转移方程为,若tag[j][i]==1,dp[i]=min(dp[i],dp[j-1]+1); 对于最令人讨厌的路径输出,可以用pre来记录回文串前驱分裂点,然后根据这些分裂点来输出. 代码如下: #include <

URAL 1297 后缀数组:求最长回文子串

思路:这题下午搞了然后一直WA,后面就看了Discuss,里面有个数组:ABCDEFDCBA,这个我输出ABCD,所以错了. 然后才知道自己写的后缀数组对这个回文子串有bug,然后就不知道怎么改了. 然后看题解,里面都是用RMQ先预处理任意两个后缀的最长公共前缀,因为不太知道这个,所以又看了一下午,嘛嘛-- 然后理解RMQ和后缀一起用的时候才发现其实这里不用RMQ也可以,只要特殊处理一下上面这个没过的例子就行了,哈哈--机智-- 不过那个国家集训队论文里面正解是用RMQ做的,自己还得会和RMQ一

ACM 中 矩阵数据的预处理

我们考虑一个$N\times M$的矩阵数据,若要对矩阵中的部分数据进行读取,比如求某个$a\times b$的子矩阵的元素和,通常我们可以想到$O(ab)$的遍历那个子矩阵,对它的各个元素进行求和.然而当$a$或$b$很大的时候,这样的计算显得太慢,如果要求子矩阵的最大元素和(如Ural 1146),更是简直慢到不能忍. 于是就想,能不能在读入数据的同时,我就先进行一些预处理,使得到后面进行计算的时候,可以简化一些步骤呢?答案是可以的. 通常对矩阵有两种预处理: 一种是把矩阵拍扁,即把前一列或

URAL 1707. Hypnotoad&#39;s Secret(树状数组)

URAL 1707. Hypnotoad's Secret 题目链接 题意:这题设置的恶心不能多说,构造点和矩形,大概就是问每个矩形里面是否包含点 思路:树状数组,把点排序,按y轴,在按x轴,在按询问,这样每次遇到一个点就在相应的扫描线上加,遇到查询就询问出左边到这个点位置的,就能预处理出每个点左下角包含的点的个数,然后每个矩形再利用容斥原理去搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm&

Ural 1146 Maximum Sum(DP)

题目地址:Ural 1146 这题是求最大子矩阵和.方法是将二维转化一维. 首先用n*n的方法来确定矩阵的列.需要先进行预处理,只对每行来说,转化成一维的前缀和,这样对列的确定只需要前后两个指针来确定,只需要用前缀和相减即可得到.前后两个指针用n*n的枚举. 确定好了哪几列,那么再确定行的时候就转化成了一维的最大连续子序列的和.再来一次O(n)的枚举就可以. 这样,总复杂就变成了O(n^3),对于n为100来说,已经足够了. 代码如下: #include <iostream> #include

postman进行http接口测试

转载地址: http://blog.csdn.net/five3/article/details/53021084 HTTP的接口测试工具有很多,可以进行http请求的方式也有很多,但是可以直接拿来就用,而且功能还支持的不错的,我使用过的来讲,还是postman比较上手. 优点: 1.支持用例管理 2.支持get.post.文件上传.响应验证.变量管理.环境参数管理等功能 3.支持批量运行 4.支持用例导出.导入 5.支持云端保存用例[付费用户] 可以说POSTMAN满足了HTTP接口测试的大部

Petrozavodsk Winter-2013. Ural FU Contest Problem D. Five Palindromes manacher、一个串切割成5个回文子串、优化

Ural Federal University Contest, SKB Kontur Cup Petrozavodsk Winter Training Camp, Saturday, February 2, 2013 Problem D. Five Palindromes Input file: input.txt Output file: output.txt Time limit: 2 seconds (3 seconds for Java) Memory limit: 256 mebib

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

URAL 1146 Maximum Sum(最大子矩阵的和 DP)

Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最开始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4),就不知道该怎么办了.问了一下,是压缩矩阵,转换成最大字段和的问题. 压缩行或者列都是可以的. 1 int n, m, x, y, T, t; 2 int Map[1010][1010]; 3 4 int main() 5 { 6 while(~scanf("%d", &n)) 7 { 8 memset(Map, 0, siz