算法基础系列
前言
这类题型没有特定的算法,一般通过暴力枚举,模拟一下问题,即可解决
重要两点
- 顺序,怎样做到不重不漏枚举完
- 优化,如何优化,减少时间复杂度
整数取数模板
int t = x % 10;
x /= 10;
字符数取数模板
int x = 0;
for(int i = 0; i < str.size(); i++)
x = x * 10 + str[i] - '0';
练习题
连号区间数
思路
- 当区间
(max - min) == (R - L)
时,表示区间是连续的,无空缺的
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, INF = 1e8;
int n, a[N];
int main()
{
cin >> n;
int res = 0;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
{
int maxv = -INF, minv = INF;
for (int j = i; j < n; j++)
{
maxv = max(maxv, a[j]);
minv = min(minv, a[j]);
if (maxv - minv == j - i)
res++;
}
}
cout << res << endl;
return 0;
}
递增三元组
思路:
前缀和
1、cnt[i]
表示在A中i这个值出现多少次,表示在C中i这个值出现多少次
2、关键的一步,通过s[]
记录当前数组从1~i
中,所有数出现的次数的前缀和
3、sa[]
记录在A中由多少个数小于B[i]
,sa[i] = s[b[i] - 1]
4、sc[]
记录在C中由多少个数大于B[i]
, sc[i] = s[N - 1] - s[b[i]]
5、由于a[]
和c[]
互斥,通过乘法原理可得res += as[i] * cs[i]
二分也能做
sort一遍,再二分
前缀和代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef long long LL;
int n;
int a[N], b[N], c[N]; // a b c 三个数组
int sa[N], sc[N]; // a 和 c 的前缀和数组
// 在a 和 c 中 有多少个a[i] 小于 b[i]
int cnt[N], s[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]), a[i]++;
for (int i = 0; i < n; i++)
scanf("%d", &b[i]), b[i]++;
for (int i = 0; i < n; i++)
scanf("%d", &c[i]), c[i]++;
//求 as[]
// 在 A 中,i出现的次数
for (int i = 0; i < n; i++)
cnt[a[i]]++;
for (int i = 1; i < N; i++)
s[i] = s[i - 1] + cnt[i];
for (int i = 0; i < n; i++)
sa[i] = s[b[i] - 1]; //记录在A中由多少个数小于B[i]
memset(cnt, 0, sizeof cnt);
memset(s, 0, sizeof s);
//求 cs[]
for (int i = 0; i < n; i++)// 在 C 中,i出现的次数
cnt[c[i]]++;
for (int i = 1; i < N; i++)
s[i] = s[i - 1] + cnt[i];
for (int i = 0; i < n; i++)//记录在C中由多少个数大于B[i]
sc[i] = s[N - 1] - s[b[i]];
LL res = 0;
for (int i = 0; i < n; i++)
res += (LL)sa[i] * sc[i];
printf("%lld", res);
return 0;
}
二分代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long lld;
const int N = 100005;
int a[N], b[N], c[N];
int n;
lld sum;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &c[i]);
//由于二分的前提是单调序列 所以预先对a b c排序 直接sort
sort(a + 1, a + 1 + n);
sort(b + 1, b + 1 + n);
sort(c + 1, c + 1 + n);
for (int i = 1; i <= n; i++)
{
//直接用STL中的两个二分函数解决
lld x = (lower_bound(a + 1, a + 1 + n, b[i]) - a) - 1; //在数组a中找比b[i]小的数
lld y = n - (upper_bound(c + 1, c + 1 + n, b[i]) - c) + 1; //在数组c中找比b[i]大的数
sum += x * y;
}
printf("%lld", sum);
return 0;
}
特别数的和
特别数的和(易)
枚举即可,常用的取个位数值的模板题
#include <iostream>
using namespace std;
int n, res;
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
int x = i;
while(x)
{
int t = x % 10;
x /= 10;
if(t == 0 || t == 2 || t == 1 || t == 9)
{
res += i;
break;
}
}
}
cout << res << endl;
return 0;
}
错误票据
这题的难点在于数据的读入
读入的处理有两种方法
一是忽略掉行数n,直接读取数据直到没有数据
二是用getline()
方法读取一张行,可参考我之前的博客,点这里
写法有三种,推荐用第三种
第一种,用bool
数组,开到最大 N
,在输入的同时判断重号。找断号时,找到整组数据的最小值,如果该位置是false
既是断号
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N =100010;
bool st[N];//表示每个数是否出现过
int n,n1;
int main()
{
int a[N];
string line;
cin>>n1;
int i=0;
int res1=0,res2=0;
getline(cin,line);
while(n1--)
{
getline(cin,line);
stringstream ssin(line);//以空格为界限将字符串分开
while(ssin>>a[n]) n++;
}
for(int i=0;i<n;i++)
{
if(st[a[i]]) res1=a[i];//重号id
st[a[i]]=true;
}
int minv=9999;
for(int i=0;i<n;i++)
if(minv>=a[i]) minv=a[i];
//cout<<"minv="<<minv<<endl;
for(int i=minv;i<minv+n;i++)
{
if(!st[i])//没出现过
res2=i;
}
cout<<res2<<" "<<res1<<endl;
}
第二种,排序做法,sort
一遍
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
int n, a[N];
int main()
{
int cnt;
cin >> cnt;
string line;
//把回车读取掉 两种写法都可以
//getchar();
getline(cin, line);
while (cnt --)
{
getline(cin, line);
stringstream ssin(line);
while (ssin >> a[n])
n++;
}
sort(a, a + n);
int res1, res2;// 第一个是断号 第二个是重号
for (int i = 0; i < n; i++)
{
if(a[i] - a[i-1] >= 2)
res1 = a[i] - 1;
else
if(a[i] == a[i-1])
res2 = a[i];
}
cout << res1 << ' ' << res2 << endl;
return 0;
}
第三种做法,结合了第一种和第二种做法的优化版
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5+5, INF = 0x3f3f3f3f;
int ha[N];
int main()
{
int n;
cin >> n;//这个读入没什么用
int minv = INF, maxv = -INF;
int tp;
while(cin >> tp)//直接读到文件尾部停止
{
if(tp < minv) minv = tp;
if(tp > maxv) maxv = tp;
ha[tp] ++;
}
int ans1 = 0, ans2 = 0;
for(int i = minv; i <= maxv; ++ i)
{
if(ha[i] == 0) ans1 = i;
if(ha[i] == 2) ans2 = i;
}
cout << ans1 << " " << ans2 << endl;
}
回文日期(经典)
经典日期题,会一道会一类日期题
两种做法
思路
- 枚举回文数而不是枚举日期
- 判断回文数是否在范围内
- 判断日期是否合法
关键点:从年份入手,开始枚举
#include <bits/stdc++.h>
using namespace std;
int day1, day2, res;
int days[13] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int date)
{
int year = date / 10000;//取出年份
int month = date % 10000 / 100; //取出月份
int day = date % 100;//取天数
if(month ==0 || month > 12)//先不考虑闰年
return false;
if(day == 0 || month != 2 && day > days[month])//判断天数
return false;
if(month == 2)//判断闰年
{
int leap = year % 100 && year % 4 == 0 || year % 400 == 0;//是为1 否为0
if(day > 28 + leap)
return false;
}
return true;
}
int main()
{
cin >> day1 >> day2;
for (int i = 1000; i < 10000; i++)
{
int date = i, x = i;
for (int j = 0; j < 4; j ++)//转换为日期
date = date * 10 + x % 10, x /= 10;//省略写法 一句写完
if(day1<=date&&date<=day2&&check(date))
res++;
}
cout << res << endl;
return 0;
}
另一种做法是,枚举月和日,用日月算出年和日期
时间复杂度是
O
(
366
)
O(366)
O(366)
非常的妙
#include<bits/stdc++.h>
using namespace std;
int a, b;
long long cnt = 0;
int days[] = {-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
scanf("%d %d", &a, &b);
for(int month = 1; month <= 12; month ++){
for(int day = 1; day <= days[month]; day ++){
int year = 1000 * (day % 10) + 100 * (day / 10) + 10 * (month % 10) + month / 10;
int date = 10000 * year + 100 * month + day;
if(date >= a && date <= b) cnt ++;
}
}
printf("%d\n", cnt);
return 0;
}
日期问题
日期问题
常规题,参考前一题,回文日期
#include <bits/stdc++.h>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check_valid(int year, int month, int day)
{
if (month == 0 || month > 12)
return false;
if (day == 0)
return false;
if (month != 2)
{
if (day > days[month])
return false;
}
else
{
int leap = year % 100 && year % 4 == 0 || year % 400 == 0;
if (day > 28 + leap)
return false;
}
return true;
}
int main()
{
int a, b, c;
scanf("%d/%d/%d", &a, &b, &c);
for (int data = 19600101; data < 20591231; data++)
{
int year = data / 10000, month = data % 10000 / 100, day = data % 100;
if (check_valid(year, month, day))
{
if (year % 100 == a && month == b && day == c || // 年/月/日
month == a && day == b && year % 100 == c || // 月/日/年
day == a && month == b && year % 100 == c)
printf("%d-%02d-%02d\n", year, month, day);
}
}
return 0;
}
航班时间
航班时间
思路:
这题的难点在输入的数据的处理上
做法是全部转换为秒
#include <bits/stdc++.h>
using namespace std;
int get_second(int h,int m,int s)
{
return h * 3600 + m * 60 + s;
}
int get_time()
{
string line;
getline(cin, line);
if (line.back() != ')')
line +=" (+0)";
int h1, m1, s1, h2, m2, s2, d;
sscanf(line.c_str(), "%d:%d:%d %d:%d:%d (+%d)", &h1, &m1, &s1, &h2, &m2, &s2, &d);
return get_second(h2, m2, s2) - get_second(h1, m1, s1) + d * 24 * 3600;
}
int main()
{
int n;
scanf("%d\n", &n); //把回车读掉
while (n--)
{
int time = (get_time() + get_time()) / 2;
int hour = time / 3600, minute = time % 3600 / 60, second = time % 60;
printf("%02d:%02d:%02d\n", hour, minute, second);
}
return 0;
}