【CodeForces - 919】 ABCD

A - Supermarket
We often go to supermarkets to buy some fruits or vegetables, and on the tag there prints the price for a kilo. But in some supermarkets, when asked how much the items are, the clerk will say that a yuan for b kilos (You don’t need to care about what “yuan” is), the same as a / b yuan for a kilo.

Now imagine you’d like to buy m kilos of apples. You’ve asked n supermarkets and got the prices. Find the minimum cost for those apples.

You can assume that there are enough apples in all supermarkets.

Input
The first line contains two positive integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 100), denoting that there are n supermarkets and you want to buy m kilos of apples.

The following n lines describe the information of the supermarkets. Each line contains two positive integers a, b (1 ≤ a, b ≤ 100), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.

Output
The only line, denoting the minimum cost for m kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won’t exceed 10 - 6.

Formally, let your answer be x, and the jury’s answer be y. Your answer is considered correct if .

Example
Input
3 5
1 2
3 4
1 3
Output
1.66666667
Input
2 1
99 100
98 99
Output
0.98989899
Note
In the first sample, you are supposed to buy 5 kilos of apples in supermarket 3. The cost is 5 / 3 yuan.

In the second sample, you are supposed to buy 1 kilo of apples in supermarket 2. The cost is 98 / 99 yuan.
水题

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 2e5+11 ;
const int M = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;



int main(){
    double n,m;cin>>n>>m;
    double mn=inf;
    for(int i=1;i<=n;i++){
        double a,b;
        cin>>a>>b;
        mn=min(mn,a/b);
    }
    printf("%.7lf\n",mn*m);
return 0;
}

B - Perfect Number
We consider a positive integer perfect, if and only if the sum of its digits is exactly 10. Given a positive integer k, your task is to find the k-th smallest perfect positive integer.

Input
A single line with a positive integer k (1 ≤ k ≤ 10 000).

Output
A single number, denoting the k-th smallest perfect integer.

Example
Input
1
Output
19
Input
2
Output
28
Note
The first perfect integer is 19 and the second one is 28.

水题
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 2e7+11 ;
const int M = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

vector<int>ve;
bool judge(int i){
    int sum=0;
    while(i){
        sum+=i%10;
        if(sum>10) return false;
        i/=10;
    }
    return sum==10;
}

void init(){
    for(int i=1;i<N;i++){
        if(judge(i)) ve.push_back(i);
        if(ve.size()>10000+1)break;
    }
}

int main(){
   init();
   int n; cin>>n;
  printf("%d\n",ve[n-1]);
return 0;
}

C - Seat Arrangements
Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.

The classroom contains n rows of seats and there are m seats in each row. Then the classroom can be represented as an n × m matrix. The character ‘.’ represents an empty seat, while ‘*’ means that the seat is occupied. You need to find k consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs.

Input
The first line contains three positive integers n, m, k (1 ≤ n, m, k ≤ 2 000), where n, m represent the sizes of the classroom and k is the number of consecutive seats you need to find.

Each of the next n lines contains m characters ‘.’ or ‘‘. They form a matrix representing the classroom, ‘.’ denotes an empty seat, and ‘’ denotes an occupied seat.

Output
A single number, denoting the number of ways to find k empty seats in the same row or column.

Example
Input
2 3 2
**.

Output
3
Input
1 2 2
..
Output
1
Input
3 3 4
.*.
.
.*.
Output
0
Note
In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement.

(1, 3), (2, 3)
(2, 2), (2, 3)
(2, 1), (2, 2)

分析:要么横着连续,要么竖着连续,dp来标记横着到这最大连续多少,标记竖着到这最大连续多少 就ok啦
注意 注意 k==1的时候,坑死我
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 2000+11 ;
const int M = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

bool mp[N][N];
int dp[N];  // 因为横着 不用上一行的状态所以就直接开的一维
int dpcol[N][N]; 
int main(){
    int n,m,k;cin>>n>>m>>k;
    for(int i=1;i<=n;i++){
        string s; cin>>s;
        for(int j=0;s[j];j++){
            if(s[j]=='.') mp[i][j+1]=1;
            else mp[i][j+1]=0;
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        memset(dp,0,sizeof(dp));
        for(int j=1;j<=m;j++){
            if(mp[i][j]) {
                dp[j]=dp[j-1]+1;
                dpcol[i][j]=dpcol[i-1][j]+1;
                if(k==1) {
                    ans++; continue;
                }
            }else {
                dp[j]=dpcol[i][j]=0;
            }
            if(dp[j]>=k) ans++;
            if(dpcol[i][j]>=k) ans++;
        }
    }
    cout<<ans;
return 0;
}

D - Substring
You are given a graph with n nodes and m directed edges. One lowercase letter is assigned to each node. We define a path’s value as the number of the most frequently occurring letter. For example, if letters on a path are “abaca”, then the value of that path is 3. Your task is find a path whose value is the largest.

Input
The first line contains two positive integers n, m (1 ≤ n, m ≤ 300 000), denoting that the graph has n nodes and m directed edges.

The second line contains a string s with only lowercase English letters. The i-th character is the letter assigned to the i-th node.

Then m lines follow. Each line contains two integers x, y (1 ≤ x, y ≤ n), describing a directed edge from x to y. Note that x can be equal to y and there can be multiple edges between x and y. Also the graph can be not connected.

Output
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.

Example
Input
5 4
abaca
1 2
1 3
3 4
4 5
Output
3
Input
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
Output
-1
Input
10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7
Output
4
Note
In the first sample, the path with largest value is 1 → 3 → 4 → 5. The value is 3 because the letter ‘a’ appears 3 times.

分析:和DAG图中求最长路一个思路,我们用topo序来维护 遍历的顺序 来dp,这样就可以减少很多的复杂度 。

遇到DAG图的时候可以考虑以下topo和dp ,用topo序来维护dp很好用 。

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 3e5+11;
vector <int> ve[N];int in[N];
int f[N][30]; char s[N];
void solve(int n ){
    queue<int>Q;
    for(int i=1;i<=n;i++){
        if(!in[i]) {
            Q.push(i);
            for(int j=0;j<26;j++)
            if(s[i]-'a'==j) { f[i][j]=1;  break;}
        }
    }
    int cnt=0;int ans=0;
    while(!Q.empty()){
        int u=Q.front();Q.pop();cnt++;
        for(int i=0;i<ve[u].size();i++){
            int v=ve[u][i];
            if(--in[v]==0) Q.push(v);
            for(int j=0;j<26;j++){
                f[v][j]=max(f[v][j],f[u][j]+int((s[v]-'a')==j));
                ans=max(ans,f[v][j]);
            }
        }
    }
    if(cnt<n) puts("-1"); // 环
    else  printf("%d\n",ans);
}
int main(){
    int n,m;scanf("%d%d",&n,&m);
    scanf("%s",s+1);
    while(m--){
        int a,b;scanf("%d%d",&a,&b);
        ve[a].push_back(b);
        in[b]++;
    }
    solve(n);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值