Codeforces Round #460 (Div. 2) A-D 题解

A:

A. Supermarket
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 0001 ≤ 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 .

Examples
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>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
	const int MT = 5e7;
	char buf[MT]; int c,sz;
	void begin(){
		c = 0;
		sz = fread(buf, 1, MT, stdin);//一次性输入
	}
	template<class T>
	inline bool read(T &t){
		while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
		if( c>=sz) return false;
		bool flag = 0; if( buf[c]== '-') flag = 1,c++;
		for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
		if(flag) t=-t;
		return true;
	}
}
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}


long double x,y;
int main()
{
    SHUT;
    int n,m;
    cin>>n>>m;
    long  double pxy=1e10;
    for(int i=1;i<=n;i++)
    {
        cin>>x>>y;
        long double pp= (x/y);
      //  cout<<pp<<endl;
        if(pxy>pp)
        {
            pxy=pp;
        }
    }
    //cout<<pxy<<endl;
    long double ans= pxy*m;
    cout<<ans<<endl;
    return 0;
}

B;

B. Perfect Number
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
1
output
19
input
2
output
28
Note

The first perfect integer is 19 and the second one is 28.


[打表预处理]    求第k个数 是多少,  这样的数是 把位数分解后和=10

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
	const int MT = 5e7;
	char buf[MT]; int c,sz;
	void begin(){
		c = 0;
		sz = fread(buf, 1, MT, stdin);//一次性输入
	}
	template<class T>
	inline bool read(T &t){
		while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
		if( c>=sz) return false;
		bool flag = 0; if( buf[c]== '-') flag = 1,c++;
		for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
		if(flag) t=-t;
		return true;
	}
}
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}

int p[maxn];
int k;
void init()
{
    k=0;
    for(int i=10;i<=20001000;i++)
    {
        int t=i;
        int s=0;
        while(t)
        {
            s+=t%10;
            t/=10;
        }

        if(s==10)
            p[++k]=i;
    }
}
int main()
{
    int x;
    init();
  //  cout<<p[10000]<<endl;
    cin>>x;
    cout<<p[x]<<endl;

    return 0;

}

C:

C. Seat Arrangements
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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)

[思路]

 只能横着 或者 只能竖着, n^2 内 DP 一波, DP[i-1][j]+DP[i][j-1]+1

特殊处理 当k=1是 ans/2

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=50005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

namespace IO {
	const int MT = 5e7;
	char buf[MT]; int c,sz;
	void begin(){
		c = 0;
		sz = fread(buf, 1, MT, stdin);//一次性输入
	}
	template<class T>
	inline bool read(T &t){
		while( c < sz && buf[c] != '-' && ( buf[c]<'0' || buf[c] >'9')) c++;
		if( c>=sz) return false;
		bool flag = 0; if( buf[c]== '-') flag = 1,c++;
		for( t=0; c<=sz && '0' <=buf[c] && buf[c] <= '9'; c++ ) t= t*10 + buf[c]-'0';
		if(flag) t=-t;
		return true;
	}
}
inline void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);};}
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
inline ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
inline ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
inline ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
inline ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
inline ll inv2(ll b){return qpow(b,MOD-2);}

int n,m,k;
int mp[2500][2500];
int dp[2500][2500][2];
int main()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        for(int j=1;j<=m;j++)
        {
            if(str[j-1]=='*')
                mp[i][j]=1;
            else
                mp[i][j]=2;
        }
    }

    mem(dp,0);
   // dp[0][0][0]=dp[0][0][1]=1;
    //for(int k=0;k<2;k++)
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mp[i][j]==2)
            {
               // if(mp[i-1][j]==0)
                    dp[i][j][0]=dp[i-1][j][0]+1;
               // if(mp[i][j-1]==0)
                    dp[i][j][1]=dp[i][j-1][1]+1;
                if(dp[i][j][1]>=k)
                    ans++;
                if(dp[i][j][0]>=k)
                    ans++;
            }
            else
                dp[i][j][0]=dp[i][j][1]=0;
        }
    }
    /*for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cout<<"[ "<<dp[i][j][0]<<" "<<dp[i][j][1]<<" ] ";
        }
        cout<<endl;
    }*/
    if(k==1)
        ans/=2;
    cout<<ans<<endl;
    return 0;
}

D:

D. Substring
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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.




[题意]

给n点,m边, str串 , 每个字母代表一个节点,求 长度最长的字母是多少?,

如果存在环, 则 答案为-1;

[思路]

DFS判断环或者BFS

26个字母 全部一遍, 找26 个中 最大的.

对于每一个字母, 去DFS路径最长的长度.

#include <iostream>
#include <bits/stdc++.h>
#include <stdio.h>

#define SHUT ios_base::sync_with_stdio(false); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
using namespace std;
const int MAXN=3*1e5+10;
const int INF=0x3f3f3f3f;
int vis[MAXN];
int dp[MAXN];
vector<int>e[MAXN];
int n,m;
string str;
int flag;
void to_dfs(int u)
{
    flag|=(vis[u]==1);
    if(vis[u])return ;
    vis[u]=1;
    for(int i=0;i<(int)e[u].size();i++)
    {
        to_dfs(e[u][i]);
    }
    vis[u]=2;
}
void dfs(int u,int s)
{
    if(vis[u]) return;
    vis[u]=1;
    int cont=0;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        dfs(v,s);
        cont=max(cont,dp[v]);
    }
    dp[u]=cont+ (str[u-1]-'a'==s);
}
int main()
{
    SHUT;
    memset(vis,0,sizeof(vis));

    cin>>n>>m>>str;
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        e[x].push_back(y);
    }
    for(int i=1;i<=n;i++)
        if(!vis[i])
            to_dfs(i);
    if(flag)
        return 0*printf("-1\n");
    int ans=-INF;
    for(int i=0;i<26;i++)
    {
        for(int j=1;j<=n;j++)
            dp[j]=vis[j]=0;
        for(int j=1;j<=n;j++)
            if(!vis[j])
                dfs(j,i);
        for(int j=1;j<=n;j++)
            ans=max(ans,dp[j]);
    }
    cout<<ans<<endl;
    return 0;
}



123

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值