zoukankan      html  css  js  c++  java
  • Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).

    Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.

    The example of encrypting of a single row of japanese crossword.

    Help Adaltik find the numbers encrypting the row he drew.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' — to white square in the row that Adaltik drew).

    Output

    The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.

    The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.

    Examples
    Input
    3
    BBW
    Output
    1
    2
    Input
    5
    BWBWB
    Output
    3
    1 1 1
    Input
    4
    WWWW
    Output
    0
    Input
    4
    BBBB
    Output
    1
    4
    Input
    13
    WBBBBWWBWBBBW
    Output
    3
    4 1 3
    Note

    The last sample case correspond to the picture in the statement.
    题意:给你一个长度为n的串 统计黑色块的个数以及长度 输出

    题解:水

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<map>
     9 #include<set>
    10 #include<cmath>
    11 #include<queue>
    12 #include<bitset>
    13 #include<math.h>
    14 #include<vector>
    15 #include<string>
    16 #include<stdio.h>
    17 #include<cstring>
    18 #include<iostream>
    19 #include<algorithm>
    20 #pragma comment(linker, "/STACK:102400000,102400000")
    21 using namespace std;
    22 #define  A first
    23 #define B second
    24 const int N=50010;
    25 const int mod=1000000007;
    26 const int MOD1=1000000007;
    27 const int MOD2=1000000009;
    28 const double EPS=0.00000001;
    29 //typedef long long ll;
    30 typedef __int64 ll;
    31 const ll MOD=1000000007;
    32 const int INF=1000000010;
    33 const ll MAX=1ll<<55;
    34 const double eps=1e-5;
    35 const double inf=~0u>>1;
    36 const double pi=acos(-1.0);
    37 typedef double db;
    38 typedef unsigned int uint;
    39 typedef unsigned long long ull;
    40 int n;
    41 char  a[105];
    42 int ans[105];
    43 int main()
    44 {
    45     scanf("%d",&n);
    46     scanf("%s",a+1);
    47     int exm=0;
    48     for(int i=1;i<=n;i++)
    49     {
    50         int j=i;
    51         if(a[i]=='B')
    52         {
    53             int jishu=0;
    54             for(j=i;;j++)
    55                 if(a[j]=='B')
    56                 jishu++;
    57                else
    58                 break;
    59             ans[exm++]=jishu;
    60         }
    61         i=j;
    62     }
    63      printf("%d
    ",exm);
    64      if(exm)
    65     {
    66     printf("%d",ans[0]);
    67     for(int i=1;i<exm;i++)
    68         printf(" %d",ans[i]);
    69         cout<<endl;
    70     }
    71     return 0;
    72 }
    B. Passwords
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.

    Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.

    Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.

    Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).

    Input

    The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.

    The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.

    The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.

    Output

    Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.

    Examples
    Input
    5 2
    cba
    abc
    bb1
    abC
    ABC
    abc
    Output
    1 15
    Input
    4 100
    11
    22
    1
    2
    22
    Output
    3 4
    、Note

    Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.

    Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.

    题意:给你n个不同的密码 以及一个正确的密码(来自这n个密码)  尝试密码的规则

    1.由密码短的到长的

    2.每尝试一次耗时1s

    3.每尝试失败k次等待5s

    求密码输入正确的 最短时间和最长时间

    题解: 存储n个密码 按照密码的长度排序 找到(正确密码的长度)的最小序号st和最大序号ed

    输出最短时间 (st-1)/k*5+st 最长时间 (ed-1)/k*5+ed

     1 /******************************
     2 code by drizzle
     3 blog: www.cnblogs.com/hsd-/
     4 ^ ^    ^ ^
     5  O      O
     6 ******************************/
     7 #include<bits/stdc++.h>
     8 #include<map>
     9 #include<set>
    10 #include<cmath>
    11 #include<queue>
    12 #include<bitset>
    13 #include<math.h>
    14 #include<vector>
    15 #include<string>
    16 #include<stdio.h>
    17 #include<cstring>
    18 #include<iostream>
    19 #include<algorithm>
    20 #pragma comment(linker, "/STACK:102400000,102400000")
    21 using namespace std;
    22 #define  A first
    23 #define B second
    24 const int mod=1000000007;
    25 const int MOD1=1000000007;
    26 const int MOD2=1000000009;
    27 const double EPS=0.00000001;
    28 //typedef long long ll;
    29 typedef __int64 ll;
    30 const ll MOD=1000000007;
    31 const int INF=1000000010;
    32 const ll MAX=1ll<<55;
    33 const double eps=1e-5;
    34 const double inf=~0u>>1;
    35 const double pi=acos(-1.0);
    36 typedef double db;
    37 typedef unsigned int uint;
    38 typedef unsigned long long ull;
    39 struct node
    40 {
    41     int  len;
    42     char a[105];
    43 }N[105],M;
    44 bool cmp(struct node aa,struct node bb)
    45 {
    46     return aa.len<bb.len;
    47 }
    48 int n,k;
    49 char b[105];
    50 int main()
    51 {
    52     scanf("%d %d",&n,&k);
    53     for(int i=1;i<=n;i++)
    54     {
    55         scanf("%s",N[i].a);
    56         N[i].len=strlen(N[i].a);
    57     }
    58     scanf("%s",b);
    59     int l=strlen(b);
    60     sort(N+1,N+1+n,cmp);
    61     int st,ed;
    62     int flag=0;
    63     for(int i=1;i<=n;i++)
    64     {
    65         if(N[i].len==l&&flag==0)
    66         {
    67             st=i;
    68             ed=i;
    69             flag=1;
    70         }
    71          if(N[i].len==l&&flag==1)
    72         {
    73             ed=i;
    74         }
    75     }
    76     cout<<(st-1)/k*5+st<<" "<<(ed-1)/k*5+ed<<endl;
    77     return 0;
    78 }
    C. Journey
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

    Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.

    Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.

    Input

    The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

    The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

    It is guaranteed, that there is at most one road between each pair of showplaces.

    Output

    Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

    Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

    If there are multiple answers, print any of them.

    Examples
    Input
    4 3 13
    1 2 5
    2 3 7
    2 4 8
    Output
    3
    1 2 4
    Input
    6 6 7
    1 2 2
    1 3 3
    3 6 3
    2 4 2
    4 6 2
    6 5 1
    Output
    4
    1 2 4 6
    Input
    5 5 6
    1 3 3
    3 5 3
    1 2 2
    2 4 3
    4 5 2
    Output
    3
    1 3 5

    题意:n个点 m条单向边 每条边有一个权值 起点为1终点为n 输出使得权值和小于等于t的路径并且要求路径含有的节点尽量多
    题解:前向星存图 dp[i][j]表示以节点i结尾的经过j个节点的权值和 dfs出dp[i][j]的最小值
    pd[i][j] 记录状态(i,j)的父亲节点 记录路径。

      1 /******************************
      2 code by drizzle
      3 blog: www.cnblogs.com/hsd-/
      4 ^ ^    ^ ^
      5  O      O
      6 ******************************/
      7 #include<bits/stdc++.h>
      8 #include<map>
      9 #include<set>
     10 #include<cmath>
     11 #include<queue>
     12 #include<bitset>
     13 #include<math.h>
     14 #include<vector>
     15 #include<string>
     16 #include<stdio.h>
     17 #include<cstring>
     18 #include<iostream>
     19 #include<algorithm>
     20 #pragma comment(linker, "/STACK:102400000,102400000")
     21 using namespace std;
     22 #define  A first
     23 #define B second
     24 const int mod=1000000007;
     25 const int MOD1=1000000007;
     26 const int MOD2=1000000009;
     27 const double EPS=0.00000001;
     28 //typedef long long ll;
     29 typedef __int64 ll;
     30 const ll MOD=1000000007;
     31 const int INF=1000000010;
     32 const ll MAX=1ll<<55;
     33 const double eps=1e-5;
     34 const double inf=~0u>>1;
     35 const double pi=acos(-1.0);
     36 typedef double db;
     37 typedef unsigned int uint;
     38 typedef unsigned long long ull;
     39 struct node
     40 {
     41     int pre;
     42     int to;
     43     int w;
     44 }N[5005];
     45 int nedge=0;
     46 int pre[5005];
     47 int dp[5005][5005];
     48 int pd[5005][5005];
     49 vector<int >ans;
     50 void add(int a,int b,int c)
     51 {
     52     nedge++;
     53     N[nedge].to=b;
     54     N[nedge].w=c;
     55     N[nedge].pre=pre[a];
     56     pre[a]=nedge;
     57 }
     58 int n,m,k;
     59 int aa,bb,cc;
     60 void dfs1(int pos,int num,int we,int father)
     61 {
     62     if(dp[pos][num]<=we) return ;
     63     dp[pos][num]=we;pd[pos][num]=father;
     64     for(int i=pre[pos];i;i=N[i].pre)
     65     {
     66         if(we+N[i].w<=k)
     67             dfs1(N[i].to,num+1,we+N[i].w,pos);
     68     }
     69 }
     70 void dfs2(int pos,int num)
     71 {
     72     ans.push_back(pos);
     73     if(pd[pos][num]==-1)
     74     {
     75         printf("%d
    ",ans.size());
     76         for(int i=ans.size()-1;i>=0;i--)
     77             printf("%d ",ans[i]);
     78         printf("
    ");
     79         return ;
     80     }
     81     else
     82     dfs2(pd[pos][num],num-1);
     83 }
     84 int main()
     85 {
     86     scanf("%d %d %d",&n,&m,&k);
     87     for(int i=1;i<=n;i++)
     88         for(int j=1;j<=n;j++)
     89          dp[i][j]=1e9+7;
     90     for(int i=1;i<=m;i++)
     91     {
     92       scanf("%d %d %d",&aa,&bb,&cc);
     93       add(aa,bb,cc);
     94     }
     95     dfs1(1,1,0,-1);
     96     for(int i=n;i>=2;i--)
     97     {
     98         if(dp[n][i]<=k)
     99         {
    100             dfs2(n,i);
    101             break;
    102         }
    103     }
    104     return 0;
    105 }
    D. Maxim and Array
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.

    Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.

    Input

    The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.

    The second line contains n integers a1, a2, ..., an () — the elements of the array found by Maxim.

    Output

    Print n integers b1, b2, ..., bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.

    If there are multiple answers, print any of them.

    Examples
    Input
    5 3 1
    5 4 3 5 2
    Output
    5 4 3 5 -1 
    Input
    5 3 1
    5 4 3 5 5
    Output
    5 4 0 5 5 
    Input
    5 3 1
    5 4 4 5 5
    Output
    5 1 4 5 5 
    Input
    3 2 7
    5 4 2
    Output
    5 11 -5

    题意:n个数 k次操作 每次操作为 对某一个数增加x或减少x 现在要求n个数连乘的值尽可能的小
    输出k次操作之后的n个数
    题解:首先需要判断当前的n个数的连乘结果ans的正负,若ans为正数 则需要最小的操作数使得ans变为负数
    对于每一个值都记录了 pos位置 w绝对值 what正负 优先队列存储 按照绝对值升序排列。如果将ans变为负数(能够变为的最大的负数)
    则每次取出优先队列中绝对值最小的一个数 每次都将绝对值增加x 其他属性不变  加入到队列中 并且k--。直到k==0
    若ans无法变为负数 则将所有的操作都贡献到绝对值最小的值上。注意细节的处理!
     
      1 /******************************
      2 code by drizzle
      3 blog: www.cnblogs.com/hsd-/
      4 ^ ^    ^ ^
      5  O      O
      6 ******************************/
      7 #include<bits/stdc++.h>
      8 #include<map>
      9 #include<set>
     10 #include<cmath>
     11 #include<queue>
     12 #include<bitset>
     13 #include<math.h>
     14 #include<vector>
     15 #include<string>
     16 #include<stdio.h>
     17 #include<cstring>
     18 #include<iostream>
     19 #include<algorithm>
     20 #pragma comment(linker, "/STACK:102400000,102400000")
     21 using namespace std;
     22 #define  A first
     23 #define B second
     24 const int mod=1000000007;
     25 const int MOD1=1000000007;
     26 const int MOD2=1000000009;
     27 const double EPS=0.00000001;
     28 //typedef long long ll;
     29 typedef __int64 ll;
     30 const ll MOD=1000000007;
     31 const int INF=1000000010;
     32 const ll MAX=1ll<<55;
     33 const double eps=1e-5;
     34 const double inf=~0u>>1;
     35 const double pi=acos(-1.0);
     36 typedef double db;
     37 typedef unsigned int uint;
     38 typedef unsigned long long ull;
     39 struct node
     40 {
     41     ll w;
     42     int pos;
     43     int what;
     44     friend bool operator < (node a, node b)
     45     {
     46         return  a.w > b.w;
     47     }
     48 } N[200005];
     49 priority_queue<node> pq;
     50 ll n,k,x;
     51 node s1,s2,s3,s4;
     52 ll ans[200005];
     53 int main()
     54 {
     55     scanf("%I64d %I64d %I64d",&n,&k,&x);
     56     ll exm;
     57     int gg=1;
     58     for(int i=1; i<=n; i++)
     59     {
     60         scanf("%I64d",&exm);
     61         if(exm<0)
     62         {
     63             N[i].w=0-exm;
     64             N[i].pos=i;
     65             N[i].what=0;
     66             gg=gg*(-1);
     67         }
     68         else
     69         {
     70             N[i].w=exm;
     71             N[i].what=1;
     72             N[i].pos=i;
     73         }
     74         pq.push(N[i]);
     75     }
     76     if(gg==1)//正数
     77     {
     78         s1=pq.top();
     79         pq.pop();
     80         if(s1.w>=x*k)//无法变负
     81         {
     82             ans[s1.pos]=s1.w-x*k;
     83             k=0;//操作数变为0
     84         }
     85         else
     86         {
     87             ll ggg=(s1.w+x)/x;//变为最大的负数需要的操作数
     88             k-=ggg;
     89             s2.w=0-(s1.w-ggg*x);
     90             s2.pos=s1.pos;
     91             if(s1.what)//注意当前这个数的符号的变化
     92                 s2.what=0;
     93             else
     94                 s2.what=1;
     95             pq.push(s2);//重新入队
     96         }
     97     }
     98     while(!pq.empty())
     99     {
    100         if(k==0)
    101             break;
    102         s1=pq.top();
    103         pq.pop();
    104         s2.w=s1.w+x;
    105         s2.pos=s1.pos;
    106         s2.what=s1.what;
    107         pq.push(s2);
    108         k--;//操作数减少
    109     }
    110     while(!pq.empty())
    111     {
    112         s1=pq.top();
    113         pq.pop();
    114         if(s1.what)
    115             ans[s1.pos]=s1.w;//通过位置找到答案
    116         else
    117             ans[s1.pos]=0-s1.w;
    118     }
    119     printf("%I64d",ans[1]);
    120     for(int i=2; i<=n; i++)
    121     {
    122         printf(" %I64d",ans[i]);
    123     }
    124     printf("
    ");
    125     return 0;
    126 }
  • 相关阅读:
    mybatis-Generator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap
    Linux 常用的命令
    Linux 查找文件
    eclipse tasks
    Myeclipse 文件注释和解注释
    proxool连接池 异常
    EL 表达式 函数 操作 字符串
    Myeclipse 选中高亮
    Spring 定时作业
    linux配置java环境变量(详细)
  • 原文地址:https://www.cnblogs.com/hsd-/p/5926321.html
Copyright © 2011-2022 走看看