Codeforces Round #355 (Div. 2) A B C D vector + map spfa



链接:戳这里


A. Vanya and Fence
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the i-th person is equal to ai.

Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?

Input
The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively.

The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.

Output
Print a single integer — the minimum possible valid width of the road.

Examples
input
3 7
4 5 14
output
4
input
6 1
1 1 1 1 1 1
output
6
input
6 5
7 6 8 9 10 5
output
11
Note
In the first sample, only person number 3 must bend down, so the required width is equal to 1 + 1 + 2 = 4.

In the second sample, all friends are short enough and no one has to bend, so the width 1 + 1 + 1 + 1 + 1 + 1 = 6 is enough.

In the third sample, all the persons have to bend, except the last one. The required minimum width of the road is equal to 2 + 2 + 2 + 2 + 2 + 1 = 11.


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,h;
int main(){
    scanf("%d%d",&n,&h);
    int ans=0;
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        if(x>h) ans+=2;
        else ans++;
    }
    cout<<ans<<endl;
    return 0;
}


B. Vanya and Food Processor
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed h and the processor smashes k centimeters of potato each second. If there are less than k centimeters remaining, than during this second processor smashes all the remaining potato.

Vanya has n pieces of potato, the height of the i-th piece is equal to ai. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number n. Formally, each second the following happens:

If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece.
Processor smashes k centimeters of potato (or just everything that is inside).
Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed.

Input
The first line of the input contains integers n, h and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ h ≤ 109) — the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively.

The second line contains n integers ai (1 ≤ ai ≤ h) — the heights of the pieces.

Output
Print a single integer — the number of seconds required to smash all the potatoes following the process described in the problem statement.

Examples
input
5 6 3
5 4 3 2 1
output
5
input
5 6 3
5 5 5 5 5
output
10
input
5 6 3
1 2 1 1 1
output
2
Note
Consider the first sample.

First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside.
Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining.
Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second.
Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3.
During this second processor finally smashes all the remaining potato and the process finishes.
In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5 = 10 seconds.

In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds.


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n,h,k;
ll a[100100];
int main(){
    cin>>n>>h>>k;
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    ll ans=0,sum=0;
    int t=1;
    while(t<=n){
        while(t<=n && sum+a[t]<=h){
            sum+=a[t];
            t++;
        }
        ans+=sum/k;
        sum%=k;
        if(sum+a[t]>h) sum=0,ans++;
    }
    if(sum) ans++;
    cout<<ans<<endl;
    return 0;
}


C. Vanya and Label
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string s and wants to know the number of pairs of words of length |s| (length of s), such that their bitwise AND is equal to s. As this number can be large, output it modulo 109 + 7.

To represent the string as a number in numeral system with base 64 Vanya uses the following rules:

digits from '0' to '9' correspond to integers from 0 to 9;
letters from 'A' to 'Z' correspond to integers from 10 to 35;
letters from 'a' to 'z' correspond to integers from 36 to 61;
letter '-' correspond to integer 62;
letter '_' correspond to integer 63.
Input
The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.

Output
Print a single integer — the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 109 + 7.

Examples
input
z
output
3
input
V_V
output
9
input
Codeforces
output
130653412
Note
For a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.

In the first sample, there are 3 possible solutions:

z&_ = 61&63 = 61 = z
_&z = 63&61 = 61 = z
z&z = 61&61 = 61 = z


思路:

任意两个字符去&  直接暴力求出每两个字符&的结果  统计出现个数  然后连乘就可以了


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
#define mod 1000000007
map<char,int> mp;
int vis[10010];
string s;
int main(){
    char c;
    for(int i=0;i<=9;i++){
        c=i+'0';
        mp[c]=i;
    }
    int num=0;
    for(int i=10;i<=35;i++){
        c=num+'A';
        mp[c]=i;
        num++;
    }
    num=0;
    for(int i=36;i<=61;i++){
        c=num+'a';
        mp[c]=i;
        num++;
    }
    c='-';mp[c]=62;
    c='_';mp[c]=63;
    map<char,int>::iterator it;
    map<char,int>::iterator it1;
    for(it=mp.begin();it!=mp.end();it++){
       // cout<<it->first<<" ";
        int x=it->second;
        for(it1=mp.begin();it1!=mp.end();it1++){
            int y=it1->second;
            vis[x&y]++;
        }
    }
    ll ans=1;
    cin>>s;
    for(int i=0;i<s.size();i++){
        char c=s[i];
       // cout<<mp[c]<<" "<<vis[mp[c]]<<endl;
        ans=ans*vis[mp[c]]%mod;
    }
    printf("%I64d\n",ans);
    return 0;
}


D. Vanya and Treasure
time limit per test1.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, an the room located in the i-th row and j-th columns contains the chest of type aij. Each chest of type x ≤ p - 1 contains a key that can open any chest of type x + 1, and all chests of type 1 are not locked. There is exactly one chest of type p and it contains a treasure.

Vanya starts in cell (1, 1) (top left corner). What is the minimum total distance Vanya has to walk in order to get the treasure? Consider the distance between cell (r1, c1) (the cell in the row r1 and column c1) and (r2, c2) is equal to |r1 - r2| + |c1 - c2|.

Input
The first line of the input contains three integers n, m and p (1 ≤ n, m ≤ 300, 1 ≤ p ≤ n·m) — the number of rows and columns in the table representing the palace and the number of different types of the chests, respectively.

Each of the following n lines contains m integers aij (1 ≤ aij ≤ p) — the types of the chests in corresponding rooms. It's guaranteed that for each x from 1 to p there is at least one chest of this type (that is, there exists a pair of r and c, such that arc = x). Also, it's guaranteed that there is exactly one chest of type p.

Output
Print one integer — the minimum possible total distance Vanya has to walk in order to get the treasure from the chest of type p.

Examples
input
3 4 3
2 1 1 1
1 1 1 1
2 1 1 3
output
5
input
3 3 9
1 3 5
8 9 7
4 6 2
output
22
input
3 4 12
1 2 3 4
8 7 6 5
9 10 11 12
output
11


题意:给出n*m的矩阵  aij表示该格点的箱子的类型为aij 

给出p表示箱子的总类型,要求从1走到p最少需要走多少步

当前种类为x的箱子只能去x+1的箱子拿钥匙才能去x+2

开始的位置在(1,1)


思路:

n^3 如果当前种类x的集合的数量小于sqrt(nm)的话,暴力dp更新,否则全图进行一次bfs类似最短路的思想


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct node{
    int x,y;
    node(int x=0,int y=0):x(x),y(y){}
};
vector<node> V[90010];

int n,m,p;
int a[310][310],dp[310][310];
int dis[310][310],vis[310][310];
int xx[4]={-1,0,1,0};
int yy[4]={0,1,0,-1};
bool pd(node t){
    if(t.x<1 || t.x>n || t.y<1 || t.y>m) return false;
    return true;
}
int main(){
    scanf("%d%d%d",&n,&m,&p);
    mst(dp,127);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            if(a[i][j]==1) dp[i][j]=abs(i-1)+abs(j-1);
            V[a[i][j]].push_back(node(i,j));
        }
    }
    int cnt=0;
    for(int i=1;i<p;i++){
        if(V[i].size()<=(int)sqrt(n*m*1.0)){
            for(auto v:V[i+1]){
                for(auto u:V[i]){
                    dp[v.x][v.y]=min(dp[v.x][v.y],dp[u.x][u.y]+abs(u.x-v.x)+abs(u.y-v.y));
                }
            }
        } else{
            mst(dis,127);
            cnt++;
            queue<node> qu;
            for(auto u:V[i]) qu.push(u);
            for(auto u:V[i]) {
                dis[u.x][u.y]=dp[u.x][u.y];
            }
            while(!qu.empty()){
                node now=qu.front(),next;
                qu.pop();
                vis[now.x][now.y]=0;
                for(int j=0;j<4;j++){
                    next.x=now.x+xx[j];
                    next.y=now.y+yy[j];
                    if(pd(next)){
                        if(dis[next.x][next.y]>dis[now.x][now.y]+1){
                            dis[next.x][next.y]=dis[now.x][now.y]+1;
                            if(vis[next.x][next.y]<cnt){
                                vis[next.x][next.y]=cnt;
                                qu.push(next);
                            }
                        }
                    }
                }
            }
            for(auto u:V[i+1]) {
                dp[u.x][u.y]=dis[u.x][u.y];
            }
        }
    }
    printf("%d\n",dp[V[p][0].x][V[p][0].y]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值