【02】熟悉语法
熟悉语法
作者:abnb250/山中鹤鸣
博客:https://www.cnblogs.com/abnb250
交流群:1062464078
回到主目录点这里
前言
这是拿来作为踏入算竞训练的第一步,先将最简单的语法掌握。
做到一定程度的有思路,然后能够使用代码进行实现。
同时也是在为算竞的思考方式做准备。
rating 800~1000
1A theatre square
题目
题目链接在这里题目
思路
矩形,要满足铺的砖覆盖广场,只需要考虑矩形的长宽被覆盖即可。
AC代码
#include <iostream>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n, m, a;
cin >> n >> m >> a;
ll l, w;
if(n % a == 0) l = n/a ;
else l = n/a + 1;
m % a == 0 ? w = m/a : w = m/a + 1;
cout << l * w;
return 0;
}
4A Watermelon
题目
题目链接在这里题目
思路
西瓜可以分成两个大于0的偶数重量。那么本身重量必须是偶数,其次,分成两份,1份可以是2,另一份大于0即可,因为必然是偶数。
AC代码
#include <iostream>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll flag = 0;
if(n % 2 == 0 && n - 2 > 0){
flag = 1;
}
if(flag) cout << "YES" << '\n';
else cout << "NO" << '\n';
return 0;
}
71A Way Too Long Words
题目
题目链接在这里题目
思路
没啥好说的
AC代码
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
while(n--){
string s;
cin >> s;
if(s.length() > 10){
char a, b;
a = s[0];
b = s[s.length()-1];
string num = to_string(s.length()-2);
s = "";
s.push_back(a);
s += num;
s.push_back(b);
}
cout << s << '\n';
}
return 0;
}
158A Next Round
题目
题目链接在这里题目
思路
根据样例,我们又两种情况,一种是第k名分数大于0,一种是等于0。
大于0的,统计第k名后与第k名分数相同的人数cnt, 结果是k+cnt
等于0的,统计第k名前大于0的数量res,结果是res
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 55;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n, k;
cin >> n >> k;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
ll cnt = 0;
for(ll i = k; i < n; i++){
if(a[i] == a[k-1]) cnt++;
}
if(a[k-1] > 0)
cout << k + cnt << '\n';
else{
ll res = 0;
for(ll i = 0; i < n; i++){
if(a[i] > 0 ) res++;
}
cout << res << '\n';
}
return 0;
}
263A Beautiful Matrix
题目
题目链接在这里题目
思路
交换相邻行和列,本质上和字符1上下左右移动无异,所以步骤其实就是距离。
AC代码
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;
const ll N = 10;
ll g[N][N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll ix, iy;
for(ll i = 1; i <= 5; i++){
for(ll j = 1; j <= 5; j++){
cin >> g[i][j];
if(g[i][j] == 1){
ix = i; iy = j;
}
}
}
cout << abs(ix - 3) + abs(iy - 3) << '\n';
return 0;
}
339A Helpful Maths
题目
题目链接在这里题目
思路
有加号和没加号两种情况,无加号的,直接输出;有加号的,存到数组内,排序,再填入字符串中。
有加号的,需要考虑运算的数不止有一位数,可能有多位数。
AC代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 110;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string s;
cin >> s;
ll cnt = 0;
ll len = s.length();
for(ll i = 0, j = 0; i < len; i++){ //i在前,j在后
if(s[i] == '+'){
string t;
while(j <= i){
t.push_back(s[j]);
j++;
}
a[cnt++] = stoi(t);
}
}
for(ll i = len-1; i >= 0; i--){
if(s[i] == '+'){
string t;
while(i+1 <= len-1){
t.push_back(s[i+1]);
i++;
}
a[cnt++] = stoi(t);
break;
}
}
ll flag = 0;
for(ll i = 0; i < len; i++){
if(s[i] == '+') flag = 1;
}
if(flag == 0){
cout << stoi(s);
return 0;
}
sort(a, a+cnt);
string res;
for(ll i = 0; i < cnt; i++){
string t;
t = to_string(a[i]);
res += t;
if(i < cnt - 1) res += "+";
}
cout << res;
return 0;
}
467A George and Accommodation
题目
题目链接在这里题目
思路
没啥可说的
AC代码
#include <iostream>
using namespace std;
using ll = long long;
int main(){
ll n;
cin >> n;
ll res = 0;
while(n--){
ll pi, qi;
cin >> pi >> qi;
if(qi - pi >= 2) res++;
}
cout << res << '\n';
return 0;
}
546A Soldier and Bananas
题目
题目链接在这里题目
思路
求和就是1到i一直累加。n(n+1)/2
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll k, n, w;
cin >> k >> n >> w;
ll res = 0;
res = max(0LL, w*(w+1)/2 * k - n);
cout << res;
return 0;
}
617A Elephant
题目
题目链接在这里题目
思路
贪心,先走最大步。
AC代码
#include <iostream>
#include <cmath>
using namespace std;
using ll = long long;
int main(){
ll dis;
cin >> dis;
ll res = 0;
res += dis/5;
dis %= 5;
res += dis/4;
dis %= 4;
res += dis/3;
dis %= 3;
res += dis/2;
dis %= 2;
res += dis;
cout << res;
return 0;
}
791A Bear and Prime 100
题目
题目链接在这里题目
思路
没啥好说的
AC代码
#include <iostream>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll a, b;
cin >> a >> b;
ll cnt = 0;
while(a <= b){
a *= 3;
b *= 2;
//cout << a << ' ' << b << '\n';
cnt++;
}
cout << cnt;
return 0;
}
116A Tram
题目
题目链接在这里题目
思路
没啥
AC代码
#include <iostream>
#include <cstring>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll cnt = 0;
ll res = 0;
while(n--){
ll up, down;
cin >> down >> up;
cnt += (-down + up);
res = max(res, cnt);
}
cout << res;
return 0;
}
231A Team
题目
题目链接在这里题目
思路
求和
AC代码
#include <iostream>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll res = 0;
while(n--){
ll a, b, c;
cin >> a >> b >> c;
if(a + b + c >= 2) res++;
}
cout << res;
return 0;
}
313A Ilya and Bank Account
题目
题目链接在这里题目
思路
题目注意看,有坑
AC代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll x;
cin >> x;
ll res = 0;
if(x >= 0) res = x;
else{
string s = to_string(x);
string t = s;
t.pop_back();
t.pop_back();
t.push_back(s[s.length()-1]);
s.pop_back();
res = max(stoi(t), stoi(s));
}
cout << res;
return 0;
}
492B Vanya and Lanterns
题目
题目链接在这里题目
思路
主要是需要考虑头和尾
AC代码
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
using ll = long long;
const ll N = 1010;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n, l;
cin >> n >> l;
for(ll i = 0 ; i < n; i++){
cin >> a[i];
}
sort(a,a+n);
cout << '\n';
ll maxd = 0;
for(ll i = 1; i < n; i++){
maxd = max(a[i] - a[i-1], maxd);
}
double res = maxd*1.0 / 2;
res = max(res, (a[0] - 0)*1.0);
res = max(res, (l - a[n-1])*1.0);
cout << fixed << setprecision(10) << res << '\n';
return 0;
}
580A Kefa and First Steps
题目
题目链接在这里题目
思路
一个遍历直接遍历即可,注意n为1的情况。
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 1e5+5;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
ll cnt = 1;
ll res = 1;
for(ll i = 1; i < n; i++){
if(a[i] >= a[i-1]) cnt++;
else cnt = 1;
res = max(res, cnt);
}
cout << res;
return 0;
}
96A Football
题目
题目链接在这里题目
思路
没啥好说的
AC代码
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll cnt = 1;
ll flag = 1;
string s;
cin >> s;
ll len = s.length();
for(ll i = 1; i < len; i++){
if(s[i] == s[i-1]){
cnt++;
}else{
cnt = 1;
}
if(cnt == 7){
flag = 0;
break;
}
}
if(flag){
cout << "NO" << '\n';
}
else{
cout << "YES" << '\n';
}
return 0;
}
702A Maximum Increase
题目
题目链接在这里题目
思路
判断res的语句不要放在某个状态下,会导致漏状态。
AC代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 1e5+5;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll cnt = 1;
ll res = 1;
for(ll i = 0 ; i < n; i++){
cin >> a[i];
if(i >= 1){
if(a[i] > a[i-1]) cnt++;
else{
cnt = 1;
}
res = max(res, cnt);
}
}
cout << res << '\n';
return 0;
}
768A Oath of the Night's Watch
题目
题目链接在这里题目
思路
关键在于考虑到会有等于最大值的数或有等于最小值的数。
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 1e5+5;
ll a[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a+n);
ll res = 0;
ll minnum = a[0];
ll maxnum = a[n-1];
for(ll i = 0; i < n; i++){
if(a[i] > minnum && a[i] < maxnum){
res++;
}
}
cout << res << '\n';
return 0;
}
843A The Useless Toy
题目
题目链接在这里题目
思路
关键在两个方向都要考虑
AC代码
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
char a[12] = {118, 60, 94, 62, 118, 60, 94, 62, 118, 60, 94, 62};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
char b, e;
cin >> b >> e;
ll t;
cin >> t;
t = t % 4;
ll cw = 0;
ll ccw = 0;
ll bi;
for(ll i = 3; i < 7; i++){
if(a[i] == b) bi = i;
}
if(a[bi - t] == e) ccw = 1;
if(a[bi + t] == e) cw = 1;
if(ccw == 1 && cw == 1) cout << "undefined" << '\n';
else if(ccw == 0 && cw == 1) cout << "cw" << '\n';
else if(ccw == 1 && cw == 0) cout << "ccw" << '\n';
return 0;
}
1013A Piles With Stones
题目
题目链接在这里题目
思路
本质是后面数组的和小于等于前面数组的和
AC代码
#include <iostream>
#include <cmath>
using namespace std;
using ll = long long;
const ll N = 100;
ll a[N], b[N];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll suma = 0, sumb = 0;
for(ll i = 0; i < n; i++){
cin >> a[i];
suma += a[i];
}
for(ll i = 0; i < n; i++){
cin >> b[i];
sumb += b[i];
}
if(sumb <= suma){
cout << "YES" << '\n';
}
else{
cout << "NO" << '\n';
}
return 0;
}
1107A Digits Sequence Dividing
题目
题目链接在这里题目
思路
n最大为300,得使用字符串表示。
AC代码
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll q;
cin >> q;
while(q--){
ll n;
cin >> n;
string s;
cin >> s;
if(n == 2){
if(s[0] >= s[1]) cout << "NO" << '\n';
else{
cout << "YES" << '\n' << 2 << '\n' << s[0] << ' ' << s[1] << '\n';
}
}
else{
string s2;
for(ll i = 1; i < n; i++){
s2.push_back(s[i]);
}
cout << "YES" << '\n' << 2 << '\n' << s[0] << ' ' << s2 << '\n';
}
}
return 0;
}
1703A YES or YES?
题目
题目链接在这里题目
思路
纪念最后一题
AC代码
#include <iostream>
#include <string>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll q;
cin >> q;
while(q--){
ll flag = 0;
string s;
cin >> s;
if(s[0] == 'y' || s[0] == 'Y'){
if(s[1] == 'e' || s[1] == 'E'){
if(s[2] == 's' || s[2] == 'S'){
flag = 1;
}
}
}
if(flag) cout << "YES" << '\n';
else cout << "NO" << '\n';
}
return 0;
}
康复训练
这是我休息两个月后的康复训练做的,
如果是没有基础的话,可以先跳过,
甚至可以直接跳过。
466A Cheap Travel(1200)
题目
题目链接在这里题目
AC代码
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
const ll INF = 0x3f3f3f3f3f3f3f3f;
void solve(){
ll n, m, a, b;
cin >> n >> m >> a >> b;
ll res = INF;
for(ll k = 0; k <= n; k++){
ll sum = 0;
sum += k*b;
sum += max(0LL, n-k*m)*a;
// cout << sum << ' ' << k << '\n';
res = min(res, sum);
}
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
327A Flipping Game(1200)
题目
题目链接在这里题目
AC代码
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 110;
ll a[N];
void solve(){
ll n;
cin >> n;
ll res = 0, sum = 0;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
for(ll i = 0; i < n; i++){
for(ll j = i; j < n; j++){
sum = 0;
for(ll k = 0; k < n; k++){
if(i <= k && k <= j){
sum += (1 - a[k]);
}
else sum += a[k];
}
// cout << sum << ' ' << i << ' ' << j << '\n';
res = max(res, sum);
}
}
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
903C Boxes Packing(1200)
题目
题目链接在这里题目
思路
一个小盒子放入一个大盒子,再放入一个更大盒子。
两个小盒子,一个放入大盒子,大盒子放入更大盒子,留下小盒子和更大盒子。
也就是说,所有小盒子都可以放入数量比自己多的更大盒子,而数量更多的小盒子会一直剩下来。
那么本质上最少的可见盒子其实就是出现次数最多的种类出现次数。
AC代码
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
const ll N = 1e5+10;
ll a[N], num[N];
void solve(){
ll n;
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
sort(a,a+n);
ll res = 0;
ll sum = 0;
ll index = 0;
for(ll i = 0; i < n; i++){
if(i == 0){
sum++;
continue;
}
if(a[i] == a[i-1]){
sum++;
}
else if(a[i] != a[i-1]){
num[index++] = sum;
sum = 1;
}
if(i == n - 1){
num[index++] = sum;
}
}
// for(ll i = 0; i < index; i++) cout << num[i] << ' ';
// cout << '\n';
for(ll i = 0; i < index - 1; i++){
num[i+1] = max(num[i], num[i+1]);
}
res = num[index-1];
if(index == 0) res = 1;
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
999C Alphabetic Removals(1200)
题目
题目链接在这里题目
思路
字符串长度为4x1e5,很大,根据字典序删除前k个,最多二重循环。不能k x n,会超时,所以不能直接删除
一重循环的方法的话,可以给每一个字符打标记,记录是第几个被删的字符。那么26 x n,不超过1e8,可行。
AC代码
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 4*1e5+10;
ll a[N];
void solve(){
ll n, k;
cin >> n >> k;
string s;
cin >> s;
ll c = 0;
for(ll i = 0; i < 26; i++){
char ch = 'a' + i;
for(ll j = 0; j < n; j++){
if(s[j] == ch){
a[j] = ++c;
}
}
}
// cout << s << '\n';
// for(ll i = 0; i < n; i++){
// cout << a[i] << ' ';
// }cout << '\n';
for(ll i = 0; i < n; i++){
if(a[i] <= k) a[i] = 0;
}
string res;
for(ll i = 0; i < n; i++){
if(a[i] != 0){
res.push_back(s[i]);
}
}
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
1029B Creating the Contest(1200)
题目
题目链接在这里题目
思路
一组问题有没有效,在于除了最后一个问题外的每一个问题,有比它大又不超过2倍难度的问题。
难度递增且都不相同,所以除了最后一个问题的下一个问题就是比该问题难的问题,那只要判断难度有没有超过两倍。
如果超过了两倍,说明该问题 i 和下一个问题 j 不能同时抽,由于问题 i 前面的问题难度更小,所以前面的问题也不能和问题 j 同时抽。
那么问题 i 和 j 之间相当于有一堵墙,所以抽问题其实只能在两堵墙内抽,要数量最多的数量,就是两堵墙内所有问题的数量。
遍历数组,统计每一堵墙之间的题目数量,在同两堵墙内,数量++,不在的话,数量重置为1,找数量的最大值即可。
AC代码
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
const ll N = 2*1e5+10;
ll a[N];
void solve(){
ll n;
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
ll res = 0;
ll sum = 1;
for(ll i = 0; i < n-1; i++){
if(a[i+1] <= a[i]*2){
sum++;
// cout << sum << '\n';
continue;
}
else{
// cout << sum << '\n';
res = max(sum, res);
sum = 1;
}
}
res = max(sum, res);
cout << res << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
2182C Production of Snowmen(1200)
题目
题目链接在这里题目
思路
t和n都是5000,使用O(n4)肯定超时,我们可以怎么优化到O(n2)及以下
结果不能使用+1的统计数量,只能够通过计算得到。要的是所有可能,想到好几种同时成立的可能相乘或相加。
a和b,b和c,都有b,所以我们可以使用b来找到a成立的数量和c成立的数量,然后乘以b的数量。
a成立的数量就是对于每一个b和每一个偏移组合起来的种类中,a都小于b的数量
c成立同理。
AC代码
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
const ll N = 5010;
ll a[N], b[N], c[N];
void solve(){
ll t;
cin >> t;
while(t--){
ll n;
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i];
}
for(ll i = 0; i < n; i++){
cin >> b[i];
}
for(ll i = 0; i < n; i++){
cin >> c[i];
}
ll resi = 0, resk = 0;
for(ll k = 0; k < n; k++){ //k种偏移量
// i
ll ok = 1;
for(ll i = 0; i < n; i++){
if(b[i] <= a[(i + k)%n]){
ok = 0;
break;
}
}
resi += ok;
ok = 1;
for(ll i = 0; i < n; i++){
if(c[i] <= b[(i+k)%n]){
ok = 0;
break;
}
}
resk += ok;
}
ll res = resi * resk * n;
cout << res << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
1859C Another Permutation Problem(1200)
题目
题目链接在这里题目
思路
n为2,最后两个数倒转。
n为3,同理,n为4,同理。
n为10,倒数4个数倒转。
我们可以充分猜测,每一个倒转的情况之间,必有解。
暴力枚举所有倒转情况,n种,遍历数组求代价,n个元素。
时间复杂度O(n^2),不会超时。
AC代码
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
const ll N = 510;
ll a[N];
void solve(){
ll t;
cin >> t;
while(t--){
ll n;
cin >> n;
ll res = 0;
for(ll i = 0; i < n; i++){
//从第i位开始倒数
for(ll j = 0; j < i; j++){
a[j] = j + 1;
}
for(ll j = i; j < n; j++){
a[j] = n - (j - i);
}
//验证
// for(ll j = 0; j < n; j++){
// cout << a[j] << ' ';
// }cout << '\n';
ll sum = 0;
ll maxj = 0;
for(ll j = 0; j < n; j++){
sum += a[j] * (j+1);
maxj = max(a[j] * (j+1), maxj);
}
res = max(res, sum - maxj);
}
cout << res << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}

浙公网安备 33010602011771号