@Dmaxiya
2018-08-17T02:19:15.000000Z
字数 4452
阅读 1406
Codeforces
Contests 链接:Codeforces Round #460 (Div. 2)
过题数:3
排名:2001/13616
有 个超市,每个超市里的苹果价格为 元每 千克,现在要买 千克的苹果,问最少需要花费多少元。
第一行为两个整数 ,接下去 行每行两个整数 。
输出要买 千克苹果的最小花费,与标准答案误差在 内都认为程序是正确的。
| 输入 | 输出 | 提示 |
|---|---|---|
| 3 5 1 2 3 4 1 3 |
1.66666667 | 最优的选择是在第 个超市买 千克苹果,总花费为 元。 |
| 2 1 99 100 98 99 |
0.98989899 | 最优的选择是在第 个超市买 千克苹果,总花费为 元。 |
对每一个超市计算一次 千克苹果的总价,取最小值。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longint n, m;double a, b;int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d", &n, &m) != EOF) {double ans = INT_MAX;for(int i = 0; i < n; ++i) {scanf("%lf%lf", &a, &b);ans = min(ans, a / b * m);}printf("%.10f\n", ans);}return 0;}
如果一个数字各个位上的数字和等于 ,那么这个数字就是“完美数”,现在给定数字 ,求第 小的“完美数”。
输入只包含一个整数 。
输出第 小的“完美数”。
| 输入 | 输出 |
|---|---|
| 1 | 19 |
| 2 | 28 |
打表预处理,打表到 可以得到最小的 个答案。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 100000 + 100;int n, cnt;int ans[maxn];bool judge(int x) {int tmp = 0;while(x != 0) {tmp += x % 10;x /= 10;}return tmp == 10;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);cnt = 1;for(int i = 1; i <= 20000000; ++i) {if(judge(i)) {ans[cnt++] = i;}}while(scanf("%d", &n) != EOF) {printf("%d\n", ans[n]);}return 0;}
在一个 的字符矩阵里,问在同一行或者同一列内总共能找到多少个连续的 个
.。
第一行为三个整数 ,接下去 行每行 个字符,每个字符只可能是
.或者*。
输出答案。
| 输入 | 输出 | 提示 |
|---|---|---|
2 3 2**.... |
3 | 可以找到以下三种连续的 个 .: |
1 2 2.. |
1 | |
3 3 4.*.*.*.*. |
0 |
直接按照题意横着、竖着各扫一次,注意特判 的情况。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 2000 + 100;int n, m, k, ans;char str[maxn][maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d%d", &n, &m, &k) != EOF) {ans = 0;for(int i = 0; i < n; ++i) {scanf("%s", str[i]);int tmp = 0;for(int j = 0; j < m; ++j) {if(str[i][j] == '.') {++tmp;} else {tmp = 0;}if(tmp >= k) {++ans;}}}for(int j = 0; j < m; ++j) {int tmp = 0;for(int i = 0; i < n; ++i) {if(str[i][j] == '.') {++tmp;} else {tmp = 0;}if(tmp >= k) {++ans;}}}if(k == 1) {ans /= 2;}printf("%d\n", ans);}return 0;}
在一个 个节点 条边的有向图上,每个节点上都有一个小写字母,定义一条路径的权值为这条路径上所有节点的字符出现次数的最大值,输出图上所有路径的最大权值。
第一行为两个整数 ,第二行为一个字符串 ,第 个小写字母表示第 个节点上的字符,接下去 行每行两个整数 ,表示从节点 到节点 有一条边。给出的图可能有自环、重边。
如果最大权值为无穷大,则输出 ,否则输出最大权值。
| 输入 | 输出 | 提示 |
|---|---|---|
| 5 4 abaca 1 2 1 3 3 4 4 5 |
3 | 最大权值的路径为 ,因为这条路径上出现了 个 'a',所以其权值为 。 |
| 6 6 xzyabc 1 2 3 1 2 3 5 4 4 3 6 4 |
-1 | |
| 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 |
4 |
如果图上存在环则输出 ,用拓扑排序判环,用一个 数组记录到达第 个节点时,字符 出现的最大次数, 的转移可以在拓扑排序的同时进行。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 300000 + 100;int n, m, u, v, ans;char str[maxn];int deg[maxn], dp[maxn][26];vector<int> G[maxn];queue<int> que;int id(char ch) {return ch - 'a';}bool topsort() {for(int i = 1; i <= n; ++i) {if(deg[i] == 0) {que.push(i);int w = id(str[i]);++dp[i][w];}}while(!que.empty()) {int tmp = que.front();que.pop();int len = G[tmp].size();for(int i = 0; i < len; ++i) {int pos = G[tmp][i];--deg[pos];if(deg[pos] == 0) {que.push(pos);}for(int j = 0; j < 26; ++j) {int d = (id(str[pos]) == j);dp[pos][j] = max(dp[pos][j], dp[tmp][j] + d);}}}for(int i = 1; i <= n; ++i) {if(deg[i] != 0) {return true;}}return false;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d", &n, &m) != EOF){ans = 0;for(int i = 1; i <= n; ++i) {G[i].clear();deg[i] = 0;for(int j = 0; j < 26; ++j) {dp[i][j] = 0;}}scanf("%s", str + 1);for(int i = 0; i < m; ++i) {scanf("%d%d", &u, &v);G[u].push_back(v);++deg[v];}if(topsort()) {printf("-1\n");continue;}for(int i = 1; i <= n; ++i) {for(int j = 0; j < 26; ++j) {ans = max(ans, dp[i][j]);}}printf("%d\n", ans);}return 0;}