您的位置:首页 > 其它

codeforces 919非官方题解(不完整版)

2018-02-02 21:42 337 查看

CodeForces 919 非官方题解

官方题解:http://codeforces.com/blog/entry/57462

919A

思路

在每家店计算所需要的花费,从而求出最小花费。纯暴力。

代码

#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while(ch > '9'||ch < '0') {
if(ch == '-') {
f = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + (int)(ch - 48);
ch = getchar();
}
x = x * f;
}

int main() {
int n, m;
double res = 100000000;
read(n);
read(m);
int a, b;
for(int i = 0; i < n; i++) {
read(a);
read(b);
double temp = 1.0 * a * m / b;
if(temp < res) {
res = temp;
}
}
printf("%.8f", res);
return 0;

}


919B

思路

我是先在本地跑了一下,发现不需要到1s就能生成第1-10000的满足条件的数据,于是直接暴力。

代码

#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

int cal(int x) {
int temp = 0;
while(x > 0) {
temp += (x % 10);
x /= 10;
}
return temp;
}
void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while(ch > '9'||ch < '0') {
if(ch == '-') {
f = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + (int)(ch - 48);
ch = getchar();
}
x = x * f;
}

int main() {
int k;
read(k);
int cnt = 0;
long long i = 1;
while(cnt < k) {
if(cal(i) == 10) {
cnt++;
}
i++;
}
i--;
cout << i << endl;
return 0;
}


919C

思路

这题是说,只能横着或者竖着找足够多的空位。并且对于不同方案的定义我们可以看出,只与占了哪些位置有关系,而不关心你和朋友是怎么坐的。所以就是横着找,看有多少个符合的情况,加上竖着找,看有什么符合的情况。当然需要特判一下k=1k=1的情况,因为会有重复。本人的代码可能不是很好理解,并且也不是写的很好,可以自己找一个比较好理解的代码。当然官方题解的做法和我的做法是一样的。

代码

#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

void read(int &x) {
x = 0;
int f = 1;
char ch = getchar();
while(ch > '9'||ch < '0') {
if(ch == '-') {
f = -1;
}
ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + (int)(ch - 48);
ch = getchar();
}
x = x * f;
}

const int maxn = 2010;
char seats[maxn][maxn];
int main() {
int n, m, k;
read(n);
read(m);
read(k);
for(int i = 0; i < n; i++) {
for(int j = 0; j <= m; j++) {
scanf("%c", &seats[i][j]);
}
}
long long temp = 0;
if(k == 1) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(seats[i][j] == '.') {
temp++;
}
}
}
cout << temp << endl;
return 0;
}
long long res = 0;
for(int i = 0; i < n; i++) {
int cnt = 0;
for(int j = 0; j < m; j++) {
if(seats[i][j] == '.') {
cnt++;
} else {
if(cnt - k + 1 > 0) {
res += cnt - k + 1;
}
cnt = 0;
}
}
if(cnt - k + 1 > 0) {
res += cnt - k + 1;
}
}
for(int i = 0; i < m; i++) {
int cnt = 0;
for(int j = 0; j < n; j++) {
if(seats[j][i] == '.') {
cnt++;
} else {
if(cnt - k + 1 > 0) {
res += cnt - k + 1;
}
cnt = 0;
}
}
if(cnt - k + 1 > 0) {
res += cnt - k + 1;
}
}
cout << res << endl;
return 0;
}


919D

思路

直接看标准答案的思路的,拓扑排序加dp,我把标准答案的注释都加了起来,应该还是能看懂的

代码

#include <bits/stdc++.h>

#define N 300010
using namespace std;
/**
* from: 边的起点
* to:边的终点
* next:上一个以from为起点的边
*/
struct Edge {
int from, to, next;
} edge
;
/**
* head[i]表示以i为边的起点的最后一条边的编号
*/
int head
, tot;

inline void addedge(int u, int v) {
edge[++tot] = (Edge) {u, v, head[u]}, head[u] = tot;
}

/**
*  f[i][j] to respect when you are at the point i, then how many letters j you can get
*/
int f
[26], n, m, d
;
/**
* s存储节点的字母,从1开始,s[1]是第一个节点的字母
*/
char s
;
queue<int> Q;

int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y);
d[y]++;
}

/******************************************************************************************************/
// 拓扑排序
for (int i = 1; i <= n; i++) {
if (!d[i]) {
Q.push(i);
f[i][s[i] - 'a'] = 1;
}
}

int rem = n;
while (!Q.empty()) {
int now = Q.front();
Q.pop();
rem--;
for (int i = head[now]; i; i = edge[i].next) {
Edge e = edge[i];
for (int j = 0; j < 26; j++) {
//                printf("now == e.from: %d\n", now == e.from);
f[e.to][j] = max(f[e.to][j], f[now][j] + (s[e.to] - 'a' == j));
}
d[e.to]--;
if (!d[e.to]) {
Q.push(e.to);
}
}
}
// 利用拓扑排序结果来判环
if (rem) {
puts("-1");
return 0;
}
/*********************************************************************************************************/
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 26; j++) {
ans = max(ans, f[i][j]);
}
}
printf("%d\n", ans);
return 0;
}


919E

数论一点都不会,这题补不动。。。

919F

博弈论也是一点都没接触过啊,也不会。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: