第九届“图灵杯”(12 / 13)
Posted Ja_King_ZH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第九届“图灵杯”(12 / 13)相关的知识,希望对你有一定的参考价值。
A.大学期末现状
签到题
#include<iostream>
using namespace std;
int main()
int x;
cin >> x;
if(x >= 60) cout << "jige,haoye!" << endl;
else cout << "laoshi,caicai,laolao" << endl;
return 0;
B.G1024
求解最早可以多久离开
#include<iostream>
using namespace std;
int main()
int n, k;
cin >> n >> k;
int f = 0;
for(int i = 1; i <= k; i ++ )
int a, b;
cin >> a >>b;
if(b - a >= n)
cout << i <<endl;
f= 1;
break;
if(!f) cout << "G!" << endl;
return 0;
C.NEUQ
题意:将一字符串变为NEUG组成,最少需要删除几个字母
题解:从头向后匹配,不相同,ans++,最后特判一下是不是G结尾
#include<iostream>
using namespace std;
const int N = 1e6 + 10;
char a[N];
int main()
int n;
cin >> n;
cin >> a;
char c[5] = "NEUQ";
int j = 0;
int ans = 0;
for(int i = 0; i < n; i ++ )
if(a[i] == c[j])
j++;
j %= 4;
else
ans++;
if(j != 0) ans += j;
cout << ans << endl;
return 0;
D.小G的任务
题意:定义ai为该数每一位上的和,给定n,求解1到n的ai和。
题解:暴力,从前加到后
#include<iostream>
using namespace std;
typedef long long ll;
ll ans;
int main()
int n;
cin >> n;
for(int i = 1; i <= n; i ++ )
int j = i;
while(j != 0)
ans += j % 10;
j = j / 10;
cout << ans << endl;
return 0;
E.nn与游戏
题意:有n对玩家,每队玩家需要抓到自己对应的人,不能超出区域边界,别人占领的地方也不能走,问最小步数,不能到达输出-1.
题解:裸bfs
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n, m;
const int N = 1e3 + 10;
int a[N][N];
typedef pair<int, int>PII;
PII s[15], e[15];
int dist[N][N];
int dx[4] = 1, 0, -1, 0 , dy[4] = 0, 1, 0, -1 ;
void bfs(PII x, PII y)
memset(dist, -1, sizeof dist);
dist[x.first][x.second] = 0;
queue<PII>q;
q.push(x);
while (!q.empty())
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++)
int aa = t.first + dx[i], bb = t.second + dy[i];
if (aa < 0 || aa >= n || bb < 0 || bb >= n) continue;
if (dist[aa][bb] != -1) continue;
if (a[aa][bb] == -1 || a[aa][bb] == 1 || (a[aa][bb] == 2 && aa != y.first && bb != y.second)) continue;
dist[aa][bb] = dist[t.first][t.second] + 1;
q.push( aa, bb );
cout << dist[y.first][y.second] << endl;
int main()
cin >> n >> m;
for (int i = 0; i < m; i++)
int x, y;
cin >> x >> y;
a[x][y] = -1;
int t;
cin >> t;
for (int i = 1; i <= t; i++)
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
s[i] = x1, y1 ;
e[i] = x2, y2 ;
a[x1][y1] = 1;
a[x2][y2] = 2;
for (int i = 1; i <= t; i++)
bfs(s[i], e[i]);
return 0;
F.第二大数
题意:求解l到r区间的第二大数…
题解:暴力
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e4 + 10;
long long a[N];
int main()
int n;
cin >> n;
for(int i = 1; i <= n; i ++ )
cin >> a[i];
long long ans = 0, res = 1e18, maxi = 0;
for(int i = 1; i < n; i ++ )
for(int j = i + 1; j <= n; j ++ )
if(j - i == 1)
res = min(res, a[i]);
res = min(res, a[j]);
maxi = max(a[i], a[j]);
else
res = max(res, a[j]);
if(res > maxi)
res = maxi;
maxi = max(maxi, a[j]);
ans += res;
cout << ans << endl;
return 0;
G.Num
题意:给定一个整数n,判断其是否可以分解为ab + a + b的形式。
题解:左右各加1得n+1 = ab + a + b + 1,化简得n+1 = (a + 1)*(b + 1),即n+1为合数,即不为质数。
#include<iostream>
using namespace std;
bool isprime(int x)
for(int i = 2; i <= x / i; i ++ )
if(x % i == 0) return false;
return true;
int main()
int n;
cin >> n;
if(n < 3)
cout << "No" << endl;
return 0;
if(isprime(n + 1)) cout << "No" << endl;
else cout << "Yes" << endl;
return 0;
H.特征值
题意:求解一个数从该数+该数/10 + 该数/100+…直至为0
题解:由于此题爆longlong,所以用前缀和+高精度求解
#include<iostream>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 5e5 + 10;
int ss[N];
int n = 1;
int main()
char a[N];
vector<int>c;
cin >> a + 1;
int sz = strlen(a + 1);
for(int i = 1; i <= sz; i ++ )
ss[i] = ss[i - 1] + a[i] - '0';
int t = 0;
for(int i = sz; i >= 1; i-- )
c.push_back((ss[i] + t) % 10 );
t = (ss[i] + t) / 10;
if(t) c.push_back(t);
for(int i = c.size() - 1; i >= 0; i -- )
cout << c[i];
return 0;
I.最大公约数
题意:定义x为a数组的所有数的最大公因数,你可以选择区间[l, r],使得a[l]–,a[r]++,为最多能产生的最大公约数的个数。
题解:将所有数相加,求其因子个数即可。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
int n;
cin >> n;
long long res = 0;
for(int i = 0; i < n; i ++ )
int a;
cin >> a;
res += a;
int ans = 0;
for(int i = 1; i <= res / i; i ++ )
if(res % i == 0) ans += 2;
if(res % i == 0 && i == res / i) ans--;
cout << ans;
return 0;
J.玄神的字符串
贪心!
#include<iostream>
using namespace std;
int f[2];
int main()
string s;
cin >> s;
for(int i = 0; i < s.size(); i ++ )
if(s[i] == '0') f[0]++;
else f[1]++;
int a, b, c;
cin >> a >> b >> c;
int ans = 0;
if(a > b)
if(f[0] % 2 == 0) ans = (f[0] + f[1]) / 2 * b;
else
ans = (f[0] + f[1] - 2) / 2 * b;
if(b + c > a) ans += a;
else ans = ans + b + c;
else
ans = min(f[0], f[1]) * a;
if(a + c > b) ans = ans + b * (abs(f[0] - f[1]) / 2);
else ans = ans + (a + c) * (abs(f[0] - f[1]) / 2);
cout << ans << endl;
return 0;
K.金牌厨师
二分+差分
#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
const int N = 3e5 + 10;
struct node
int l, r;
Node[N];
int n, m;
map<int, int>mp;
int c[N];
bool inline check(int x)
memset(c, 0, sizeof c);
for (int i = 1; i <= m; i++)
int R = Node[i].l + x - 1;
if (R <= Node[i].r) c[R]++, c[Node[i].r + 1]--;
for (int i = 1; i <= n; i++)
c[i] += c[i - 1];
if (c[i] >= x) return 1;
return 0;
int main()
cin >> n >> m;
for(int i = 1; i <= m; i ++ )
int l, r;
cin >> l >>以上是关于第九届“图灵杯”(12 / 13)的主要内容,如果未能解决你的问题,请参考以下文章
第九届“图灵杯”NEUQ-ACM程序设计竞赛个人赛 签到题11题