SSPU-ACM2020预选第一次模拟赛(A,D,F,H)
A.左右手
知识点:博弈
开始右手先手
如果当前堆有1个石子,就只能取1个,下一堆的石子的先手就变成了左手
如果当前堆有x个石子(x>1),那么可以取走x-1个,左手只能取走剩下的一个,下一堆右手仍为先手
也就是说哪只手先取到第一个非1堆,哪只手就能掌控后面的局势,这只手就能必胜
所以判断一下第一个非1堆的位置的奇偶性就可
如果全为1,则判断n奇偶性
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {int n;cin >> n;int cnt = 0, flag = 1;for (int i = 1; i <= n; i++) {int x;cin >> x;if (x == 1 && flag)cnt++;elseflag = 0;}if (cnt & 1)printf("NO\n");elseprintf("YES\n");return 0;
}
D.东东的头发
知识点:数据类型的范围
数据达到了 1 0 12 10^{12} 1012,超出了int的范围,使用long long类型进行除法运算即可
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {long long n,x;scanf("%lld %lld", &n,&x);printf("%lld\n",n/x);return 0;
}
F.七夕项链
知识点:贪心
数据范围 1 0 6 10^6 106 ,只有 O ( n l o g n ) O(nlogn) O(nlogn)和 O ( n ) O(n) O(n)时间复杂度的算法才能过
从1到n累加,若当前和为负数,继续累加必然不能使结果更优,则归零继续向后累加
时间复杂度 O ( n ) O(n) O(n)
O ( n l o g n ) O(nlogn) O(nlogn)的算法为递归分治,此处不详解
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {int n;cin >> n;int ans = -1e9, res = 0;for (int i = 1; i <= n; i++) {int x;cin >> x;res += x;if (res > ans)ans = res;if (res < 0)res = 0;}printf("%d\n", ans);return 0;
}
H.人类的本质是复读机
知识点:字符串
当作字符串读入输出即可
Code:
#include<bits/stdc++.h>
using namespace std;int main(void) {string ss;cin>>ss;cout<<ss<<endl;return 0;
}
SSPU-ACM2020预选第一次模拟赛(A,D,F,H)
A.左右手
知识点:博弈
开始右手先手
如果当前堆有1个石子,就只能取1个,下一堆的石子的先手就变成了左手
如果当前堆有x个石子(x>1),那么可以取走x-1个,左手只能取走剩下的一个,下一堆右手仍为先手
也就是说哪只手先取到第一个非1堆,哪只手就能掌控后面的局势,这只手就能必胜
所以判断一下第一个非1堆的位置的奇偶性就可
如果全为1,则判断n奇偶性
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {int n;cin >> n;int cnt = 0, flag = 1;for (int i = 1; i <= n; i++) {int x;cin >> x;if (x == 1 && flag)cnt++;elseflag = 0;}if (cnt & 1)printf("NO\n");elseprintf("YES\n");return 0;
}
D.东东的头发
知识点:数据类型的范围
数据达到了 1 0 12 10^{12} 1012,超出了int的范围,使用long long类型进行除法运算即可
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {long long n,x;scanf("%lld %lld", &n,&x);printf("%lld\n",n/x);return 0;
}
F.七夕项链
知识点:贪心
数据范围 1 0 6 10^6 106 ,只有 O ( n l o g n ) O(nlogn) O(nlogn)和 O ( n ) O(n) O(n)时间复杂度的算法才能过
从1到n累加,若当前和为负数,继续累加必然不能使结果更优,则归零继续向后累加
时间复杂度 O ( n ) O(n) O(n)
O ( n l o g n ) O(nlogn) O(nlogn)的算法为递归分治,此处不详解
Code:
#include <bits/stdc++.h>
using namespace std;int main(void) {int n;cin >> n;int ans = -1e9, res = 0;for (int i = 1; i <= n; i++) {int x;cin >> x;res += x;if (res > ans)ans = res;if (res < 0)res = 0;}printf("%d\n", ans);return 0;
}
H.人类的本质是复读机
知识点:字符串
当作字符串读入输出即可
Code:
#include<bits/stdc++.h>
using namespace std;int main(void) {string ss;cin>>ss;cout<<ss<<endl;return 0;
}