A 因数之和为n的最小正数
B 最大子段和
C 最简单的计算机
D 两个整数的除数
E 记忆力逐渐下降
F 悼念512汶川大地震遇难同胞——老人是真饿了
G 涂色!涂色!
比赛链接:https://vjudge.net/contest/345708#overview
密码:1128
A 因数之和为n的最小正数
题目链接:https://vjudge.net/contest/345708#problem/A
由于数据范围较小,直接暴力枚举
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;
int ok(int x)
{
int ans = 0;
for(int i = 1; i <= x; ++i)
if(x%i == 0) ans += i;
return ans;
}
int main() {
int n;
cin >> n;
for(int i = 1; i <= n; ++i)
if(ok(i) == n) {
printf("%d\n", i);
return 0;
}
puts("-1");
return 0;
}
B 最大子段和
题目链接:https://vjudge.net/contest/345708#problem/B
题解:动态规划,状态转移方程,dp[i] = max(dp[i-1] + a[i], 0),ans = max(dp[i])。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll a[N];
int main() {
int n;
ll ans = 0;
cin >> n;
for (int i = 1; i <= n; ++ i) {
cin >> a[i];
a[i] += max(a[i-1], (ll)0);
ans = max(ans, a[i]);
}
cout << ans << "\n";
return 0;
}
C 最简单的计算机
题目链接:https://vjudge.net/contest/345708#problem/C
水题,模拟,照着题意敲代码就行。
int也可以过。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a, b, c;
ll x, y;
while(~scanf("%lld%lld", &x, &y)) {
scanf("%s", s);
a = b = c = 0;
for(int i = 0; s[i]; ++ i) {
if(s[i] == 'A') a = x;
else if(s[i] == 'B') b = y;
else if(s[i] == 'C') x = c;
else if(s[i] == 'D') y = c;
else if(s[i] == 'E') c = a + b;
else if(s[i] == 'F') c = a - b;
}
printf("%d,%d\n", x, y);
}
return 0;
}
D 两个整数的除数
题目链接:https://vjudge.net/contest/345708#problem/D
题解:最大值一定为x,y之一,找出当前确定的最大值的因子,那么剩下最大的数就是另一个。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200];
bool vis[N];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; ++i) cin >> a[i];
sort(a, a+n);
int x = a[n-1], y = 1; //最大值一定为x,y之一,找出当前确定的最大值的因子,那么剩下最大的数就是另一个
for(int i = 0; i < n; ++i) {
if(!vis[a[i]]&&x%a[i]==0) {
vis[a[i]] = 1;
}
else y = max(y, a[i]);
}
cout <<x<<" "<<y<<"\n";
return 0;
}
E 记忆力逐渐下降
题目链接:https://vjudge.net/contest/345708#problem/E
题意:
a + 1 = 1。a、b都为正整数。只知道a和b的第一位(从左往右数的第一位)x、y。问题:构造出合理的a和b,a、b合理即可,答案不唯一。
题解:
1、x + 1 = y时,直接输出x,y。因x、y不能等10,故x = 9时特判,x = 9,y = 1,此时输出9, 10。
2、x = y时,输出x0,y1。
3、其余情况,均无法构造成功。
a、b合理即可,答案不唯一。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int x, y; scanf("%d%d", &x, &y);
if(x == y) printf("%d0 %d1\n", x, y);
else if(x == 9 && y == 1) puts("9 10");
else if(x + 1 == y) printf("%d %d\n", x, y);
else puts("-1");
return 0;
}
F 悼念512汶川大地震遇难同胞——老人是真饿了
题目链接:https://vjudge.net/contest/345708#problem/F
题意:大米种类有m种,援助经费为n。尽可能多的买米。
题解:贪就完事了。先买性比价高,买完了,换下一个最高的。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef pair<double, double> pd;
const int N = 1010;
const double eps = 1e-6;
pd ph[N];
int dcmp(double x, double y) {
if(fabs(x - y) <= eps) return 0;
else if(x > y) return 1;
else return -1;
}
bool cmp(pd x, pd y) {
return x.first < y.first;
}
int main() {
int t; scanf("%d", &t);
while(t--) {
int n, m; scanf("%d%d", &n, &m);
for(int i = 0; i < m; ++ i) scanf("%lf%lf", &ph[i].first, &ph[i].second);
sort(ph, ph + m, cmp);
double ans = 0.0, now = n;
for(int i = 0; i < m; ++ i) {
if(dcmp(now, ph[i].first * ph[i].second) == 0) {
ans += ph[i].second;
now = 0.0;
break;
} else if(dcmp(now, ph[i].first * ph[i].second) == 1) {
ans += ph[i].second;
now -= ph[i].first * ph[i].second;
} else {
ans += now / ph[i].first;
now = 0.0;
break;
}
}
printf("%.2f\n", ans);
}
return 0;
}
G 涂色!涂色!
题目链接:https://vjudge.net/contest/345708#problem/G
题意:染色问题,相邻两个方格不能是同一个颜色,首尾也不能是同一个颜色。颜色有3种
题解:动态规划。当前块i,当前的染色方案数ans[i],则ans[i] = ans[i - 1] + ans[i - 2] * 2。及当前方案数 =
上一个的方案数 (上一个首尾不同,那第i块只有一种颜色可染) + 上一个不合理的方案数* 2 (上一个首尾相同,及第i块有两种颜色可染,第i-1块不合理 = 第i-2块合理直接在尾部(i - 1)加了一个和首部颜色相同的块,故其为ans[i - 2])。ans[1]、ans[2]、ans[3]直接数出来,然后根据递推式打表。int会爆,要开ll。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll ans[55];
ans[1] = 3;
ans[2] = 6;
ans[3] = 6;
for(int i=4; i<=50; ++i) ans[i] = ans[i - 1] + ans[i - 2] * 2;
int n;
while(scanf("%d", &n)==1) printf("%lld\n", ans[n]);
return 0;
}