Codeforces Round #460 (Div. 2) A-D 题解
A:
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.
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.
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 .
3 5 1 2 3 4 1 3
1.66666667
2 1 99 100 98 99
0.98989899
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;
#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:
只能横着 或者 只能竖着, 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:
[题意]
给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