蓝桥杯C++大学B组一个月冲刺记录2024/3/11
规则:每日三题
1. 有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。
模拟
如果直接按照八位数穷举日期会tle。按照年份递增累加,然后处理月份和天数的的总天数。
#include<iostream>
using namespace std;
int res[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int check_run(int year){
if((year%4 == 0 && year%100!=0) || year % 400 == 0) return 1;
else return false;
}
int get_tot(int year,int month,int day){
int tot = 0;
for(int i = 1;i < month; ++i){
tot += res[i];
}
if(month > 2) tot += check_run(year);
return tot + day;
}
int main(){
int a,b;
while(cin >> a >> b){
if(a > b) swap(a,b);
int ans = 0;
int ay = a/10000,am = a / 100 % 100,ad = a % 100;
int by = b/10000,bm = b / 100 % 100,bd = b % 100;
for(int i = ay;i < by;++i){
ans += 365 + check_run(i);
}
ans += get_tot(by,bm,bd) - get_tot(ay,am,ad) + 1;
cout << ans << endl;
}
return 0;
}
2.回文日期(二)
2020年春节期间,有一个特殊的日期引起了大家的注意:2020年 2月 2日。
因为如果将这个日期按 “yyyymmdd” 的格式写成一个 8 位数是 20200202,恰好是一个回文数。
我们称这样的日期是回文日期。
有人表示 20200202 是“千年一遇” 的特殊日子。
对此小明很不认同,因为不到 2 年之后就是下一个回文日期:20211202 即 2021年 12 月 2 日。
也有人表示 20200202 并不仅仅是一个回文日期,还是一个 ABABBABA 型的回文日期。
对此小明也不认同,因为大约 100
年后就能遇到下一个 ABABBABA 型的回文日期:21211212 即 2121 年 12月 12日。
算不上“千年一遇”,顶多算“千年两遇”。
给定一个 8 位数的日期,请你计算该日期之后下一个回文日期和下一个 ABABBABA 型的回文日期各是哪一天。
模拟
和昨天的那个题思路类似,通过列举年份。将年份处理成回文,以及ABABABAB式的日期,在判断这个日期是否合理。
#include<iostream>
using namespace std;
int res[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int check_run(int year){
if( (year%4 == 0&&year%100 != 0 ) || year%400 == 0) return 1;
else return 0;
}
bool check(int date){
int year = date / 10000;
int month = date / 100 % 100;
int day = date % 100;
if(month <= 0||month >12||day <= 0) return false;
if(month != 2){
if(day > res[month]) return false;
}
else{
if(day > res[month] + check_run(year)) return false;
}
return true;
}
int handle1(int x){
int tot = 0;
while(x != 0){
tot = tot * 10 + x % 10;
x = x / 10;
}
return tot;
}
int handle2(int x){
int t = x/100;
return t + t*100;
}
int main(){
int a;
cin >> a;
int m = 0,n = 0;
for(int i = a/10000;i;++i){
if(m != 0&&n != 0) break;
int date1 = i * 10000 + handle1(i);
if(date1 > a && m == 0 && check(date1)) m = date1;
int temp = handle2(i);
int date2 = temp * 10000 + handle1(temp);
if(date2 > a && n == 0 && check(date2)){
if(date2 % 10 != date2 / 10 % 10) n = date2;
}
}
cout << m << '\n' << n << endl;
return 0;
}
3.日期计算
给定一个年份 y和一个整数 d,问这一年的第 d 天是几月几日?
注意闰年的 2月有 29天。
满足下面条件之一的是闰年:
年份是 4 的整数倍,而且不是 100的整数倍;年份是 400的整数倍
模拟
#include<iostream>
using namespace std;
int res[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int check_run(int year){
if( (year%4 == 0&&year%100 != 0 ) || year%400 == 0) return 1;
else return 0;
}
int main(){
int year,days;
cin >> year >> days;
int month = 1;
res[2] += check_run(year);
for(int i = 1;i <= 12; ++i){
if(days > res[i]){
days = days - res[i];
month++;
}
else break;
}
cout << month << '\n' << days << endl;
return 0;
}
4.小朋友排队
n个小朋友站成一排。
现在要把他们按身高从低到高的顺序排列,但是每次只能交换位置相邻的两个小朋友。
每个小朋友都有一个不高兴的程度。
开始的时候,所有小朋友的不高兴程度都是 0。
如果某个小朋友第一次被要求交换,则他的不高兴程度增加 1
,如果第二次要求他交换,则他的不高兴程度增加 2
(即不高兴程度为 3),依次类推。当要求某个小朋友第 k次交换时,他的不高兴程度增加 k。
请问,要让所有小朋友按从低到高排队,他们的不高兴程度之和最小是多少。
如果有两个小朋友身高一样,则他们谁站在谁前面是没有关系的。
这个题总算是想清楚了
(1)由题意可以知道,这种操作类似于冒泡排序。在宏观层面,从贪心的角度考虑,要求总交换的次数最少则交换次数就是序列中逆序对的数量(冒泡排序没交换一次逆序对的数量减少一)。
(2)由于只能交换相邻位置的两个数,从微观个体 i 而言,要想把 i 前面的所有比 i 大的数放置到 i 的后面,把 i 后面比 i 小的数放置在 i 的前面。则 i 需要的交换的次数就是包含 i 的逆序对个数k。(i 前面比它大的数 + i 后面比他小的数)
(3)所以每一个数的不高兴度就是 k * (k + 1) /2(等差数列求和)
#include<iostream>
#include<vector>
using namespace std;
const int M = 1e5 + 10;
typedef long long LL;
typedef pair<int,int> PII;
PII p[M];
int n,res[M];
void merge(int l,int r){
if(l >= r) return;
int mid = (l + r) / 2;
merge(l,mid),merge(mid + 1,r);
int i = l,j = mid + 1;
vector<PII>q;
while(i <= mid && j <= r){
if(p[j].first >= p[i].first){
res[p[i].second] += j - mid - 1;
q.push_back(p[i]);
i++;
}
else{
res[p[j].second] += mid - i + 1;
q.push_back(p[j]);
j++;
}
}
while(i<=mid){
res[p[i].second] += j - mid - 1;
q.push_back(p[i]);
i++;
}
while(j<=r){
q.push_back(p[j]);
j++;
}
for(int i = 0;i < q.size(); ++i) p[i + l] = q[i];
return;
}
int main(){
cin >> n;
for(int i = 1;i <= n; ++i){
cin >> p[i].first;
p[i].second = i;
}
merge(1 , n);
LL ans = 0;
for(int i = 1;i <= n;++i) ans += (LL)res[i] * (res[i] + 1) / 2;
cout << ans << endl;
return 0;
}