c语言 函数 内存溢出,Mynavi Programming Contest 2021(AtCoder Beginner C

A - Tiny Arithmetic Sequence

水题,判断3个数是否能构成等差数列

void solve() {

int a, b, c;

cin >> a >> b >> c;

if (a + b == 2 * c || a + c == 2 * b

|| b + c == 2 * a)cout << "Yes\n";

else cout << "No\n";

}

B - Do you know the second highest mountain?

排序,先按山脉高度排序,高度一样则按名字排序

struct node {

string s; int t;

};

vectorv;

bool cmp(node a, node b) {

if (a.t == b.t)return a.s > b.s;

return a.t > b.t;

}

void solve() {

int n;

cin >> n;

for (int i = 0; i < n; ++i) {

string s; int t;

cin >> s >> t;

v.push_back({s, t});

}

sort(v.begin(), v.end(), cmp);

cout << v[1].s << '\n';

}

赛后发现自己写复杂化了

paira[1010];

void solve() {

int n;

cin >> n;

for (int i = 1; i <= n; ++i)cin >> a[i].second >> a[i].first;

sort(a + 1, a + 1 + n);

cout << a[n - 1].second << endl;

}

C - Secret Number

这道题,是检测 0000 ~ 9999 的每一个值,但我们可以通过高桥的字符串进行简化

void solve() {

string s; cin >> s;

int ans = 0;

for (int i = 0; i <= 9999; ++i) {

vectorf(10);

int x = i;

for (int j = 0; j < 4; ++j) {

f[x % 10] = true;

x /= 10;

}

bool f2 = true;

for (int j = 0; j < 10; ++j) {

if (s[j] == 'o' and !f[j]) f2 = false;

if (s[j] == 'x' and f[j]) f2 = false;

}

ans += f2;

}

cout << ans << '\n';

}

S = input()

ans = 0

for i in range(10000):

flag = [False]*10

now = i

for j in range(4):

flag[now%10] = True

now //= 10

flag2 = True

for j in range(10):

if S[j] == 'o' and not flag[j]:

flag2 = False

if S[j] == 'x' and flag[j]:

flag2 = False

ans += flag2

print(ans)

D - Game in Momotetsu World

虽然正向搜索会很麻烦,但反过来从终点搜索起点使用DP记录即可

using ll = long long;

ll n, m, dp[2010][2010];

char s[2011][2011];

void solve() {

scanf("%lld%lld", &n, &m);

for (int i = 1; i <= n; i++)

scanf("%s", s[i] + 1);

memset(dp, 63, sizeof(dp));

dp[n][m] = 0;

for (int i = n; i > 0; i--)

for (int j = m; j > 0; j--) {

if (i == n && j == m)

continue;

dp[i][j] = max((s[i + 1][j] == '+' ? 1 : -1) - dp[i + 1][j],

(s[i][j + 1] == '+' ? 1 : -1) - dp[i][j + 1]);

}

if (dp[1][1] > 0)cout << "Takahashi\n";

else if (dp[1][1] == 0)cout << "Draw\n";

else cout << "Aoki\n";

}

E - Xor Distances

#include#define int long long

#define N 200005

#define MOD 1000000007

using namespace std;

int n, d[N], ans;

vectorto[N], w[N];

void add(int u, int v, int wt) {to[u].push_back(v), w[u].push_back(wt);}

void dfs(int u, int fa) {

for (int i = 0, v; i < to[u].size(); i++)

if ((v = to[u][i]) != fa)d[v] = d[u] ^ w[u][i], dfs(v, u);

}

signed main() {

cin >> n;

for (int i = 1, u, v, wt; i < n;

i++)scanf("%lld%lld%lld", &u, &v, &wt), add(u, v, wt), add(v, u, wt);

dfs(1, 0);

for (int k = 0; k < 60; k++) {

int a = 0;

for (int i = 1; i <= n; i++)a += ((d[i] >> k) & 1);

ans = (ans + a * (n - a) % MOD * ((1ll << k) % MOD) % MOD) % MOD;

}

cout << ans << endl;

return 0;

}

F - Insertion Sort

待补

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值