| 题目 | 分数 |
|---|---|
| T1 | 90 |
| T2 | 100 |
| T3 | 0 |
| T4 | 8 |
| T5 | 0 |
| T6 | 50 |
| T7 | 未完成 |
T1 [GESP202312 一级] 小杨的考试
有思路
题意:
今天是第天,为星期几,要求出过了天之后,是星期几
思路:
先输入,由于所以他加上的数可能大于7,不为一周所以我们要输出取模7的结果,但可能取模7为0,这个时候会输出0,所以我们还得加一个特判,去判断取模7为不为0,为0输出7
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b;
cin >> a >> b;
if(a + b > 7){
if((a+b) % 7 == 0){
cout << a - a-1;
}else{
cout << (a+b) % 7;
}
}else{
cout << a+b;
}
return 0;
}正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a,b;
cin >> a >> b;
if((a+b) % 7 == 0){
cout << 7;
}else{
cout << (a+b) % 7;
}
return 0;
}错因:
特判的概念弄错误了,没有去自己实践
T3 [NOIP 2011 提高组] 铺地毯
无思路
题意:
有一个代表有多少个地毯,每个地毯都会给出左下角的坐标,还会给出这块地毯往轴的长度以及往轴的长度
思路:
根据题意,因为前面的地毯会被后面的地毯覆盖,所以可以从后往前遍历地毯,找出最后一个(从后往前遍历的第一个)覆盖所求位置的地毯并输出,若未找到,按题目要求输出
讲的详细一点就是这样(地毯所有点):
左上角是点,右下角是点,若在地毯内,则且。
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e4 + 5;
int a[N],b[N],g[N],k[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
cin >> a[i] >> b[i] >> g[i] >> k[i];
}
int x,y;
cin >> x >> y;
//枚举每个地毯的点
for(int i = n ; i >= 1 ; i--){
if(x >= a[i] && x <= a[i]+g[i] && y >= b[i] && y <= b[i] + k[i]){//判断所求位置是否被地毯覆盖
cout << i;
return 0;
}
}
cout << -1;
return 0;
}错因:
没有去枚举每个点的情况,只想到了怎么去把这些枚举,但判断的条件没搞清吧
T4 【CSGRound2】光骓者的荣耀
题意:
有个数字,代表从 —> 的距离,还有一个代表他的传送器可以传送的距离,传送器只能用一次
思路:
由于小K的不稳定传送器只能传送一次,可以想到用总时间-传送节约的跑的时间
思路:
求出传送节约时间的最大值即可求出最短需要的时间,如果一个一个枚举的相隔k的城市之间的时间的话,会超时,这个时候要想到用一个求区间和的前缀和,用前缀和求从i地跑到i+1地所需的时间,最后用s[n] - 最大值就是总距离
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int a[N];
int s[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin >> n >> k;
for(int i = 1 ; i <= n - 1; i++){
cin >> a[i];
s[i] = s[i-1] + a[i];
}
int maxx = 0;
for(int i = 1 ; i <= n - k - 1 ; i++){
maxx = max(maxx,s[i]);
}
cout << maxx;
return 0;
}正确代码:
枚举起点做法:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int a[N];
int s[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin >> n >> k;
n--;//因为n是城市数量,所以减一
for(int i = 1 ; i <= n; i++){
cin >> a[i];
s[i] = s[i-1] + a[i];
}
int maxx = 0;
for(int l = 1 ;l <= n-k+1; l++){//枚举的是起点
int r=l+k-1;//求终点
maxx = max(maxx,s[r] - s[l-1]);
}
cout << s[n] - maxx;
return 0;
}枚举终点做法:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int a[N];
int s[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin >> n >> k;
n--;//因为n是城市数量,所以减一
for(int i = 1 ; i <= n; i++){
cin >> a[i];
s[i] = s[i-1] + a[i];
}
int maxx = 0;
for(int r = k ;r <= n; r++){//枚举的是终点
maxx = max(maxx,s[r] - s[r-k]);
}
cout << s[n] - maxx;
return 0;
}错因:
这个本蒟蒻要讲讲了:
一开始枚举的是
for(int i = 1 ; i <= n - k - 1 ; i++){
maxx = max(maxx,s[i]);
}但这么做明显错误,为什么呢?因为这个枚举的是起点,而后面直接跟s[i]比较,这有点…
第二次改动
嗯对,我记得是这样
for(int i = 1 ; i <= n - k - 1 ; i++){
maxx = max(maxx,s[i] - s[i - n])
}然后也错了,为啥错也一目了然,这前缀和求错了,明明枚举的起点
最后一次改动
for(int l = 1 ;l <= n-k+1; l++){
int r=l+k-1;
//cout << r << " " << l <<" "<<s[r]<<" "<<s[l-1]<< endl;
maxx = max(maxx,s[r] - s[l-1]);
}注释不用管,打断点的,这会枚举对了终点,再求出前缀和
P.S.:
循环要加一,!?强强?!
T5 T5
题意:
给定一个数组,由 1 个 1,2 个 2,3 个 3,4 个 4,5 个 5,6 个 6,7 个 7,8 个 8,9 个 9,10 个 10…以此类推组成,说实话,这个挺阴的,可精了,移步测试点信息:
思路:
这谁绷得住,第一次做没看到测试点信息,不会了直接
首先肯定要把这一大串数字存起来,再用前缀和预处理一下,最后做区间和即可
那么问题来了,循环到什么情况下结束呢?存进去的时候下标是什么呢?
一个一个回答:
1.数据写了,最多,只要到达即可…吗?(移步问2)
2.下标当然有办法,用一个变量存即可,存进去一个,变量++,只要变量大于次方即可,正好解决问1
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int a[N];
int s[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int idx = 1;
for(int i = 1 ;; i++){
for(int j = 1 ; j <= i ; j++){
a[idx] = i;
idx++;
if(idx > 1e6){//这里不写数据会炸
break;
}
}
if(idx > 1e6){
break;
}
}
for(int i = 1 ; i <= 1e6 ; i++){
s[i] = s[i-1] + a[i];//预处理区间和
}
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++){
int l,r;
cin >> l >> r;
cout << s[r] - s[l-1] << "\n";
}
return 0;
}错因:
没有认真审题,我感觉我但凡读懂题目,就会做了
T6 T6
有思路
题意:
这个题目很经典,典型的
质数+前缀和
光知道这个没用,首先这个题目是要我们看,一个区间里有多少个满足条件的数(条件不说了)
思路:
直接求区间和++ = 超时
所以我们得用前缀和,但前缀和怎么用也是个问题
举个例子吧:
1
3 7
数组 3 4 5 6 7
满足条件 0 1 0 1 0(乱打的,不代表实际答案)
前缀和 0 1 1 2 2
所以只要寻找s[7] - s[3-1]之间的前缀和就可以了OvO
错误代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int f(int a){
if (a < 2){
return 0;
}
for (int i = 2; i * i <= a; i++){
if (a % i == 0){
return 0;
}
}
return 1;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T--){
int l,r,cnt = 0;
cin >> l >> r;
for(int i = l ; i <= r ; i++){
int k = (i + 1) / 2;
if(f(i) && f(k)){
cnt++;
}
}
cout << cnt << "\n";
}
return 0;
}正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int f(int a){//判断质数
if (a < 2){
return 0;
}
for (int i = 2; i * i <= a; i++){
if (a % i == 0){
return 0;
}
}
return 1;
}
const int N = 1e5 + 5;
int s[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//与举例没区别
for(int i = 1 ; i <= 1e5 ; i++){
s[i] = s[i-1];
if( f(i)==1 && f((i+1)/2)==1 ){
s[i]++;
}
}
int t;
cin >> t;
while(t--)
{
int l,r;
cin >> l >> r;
cout << s[r] - s[l-1] << endl;
}
return 0;
}错因:
直接用的二层,没有去想前缀和
T7 [COCI 2017/2018 #3] Programiranje
无思路
题意:
一共有次询问,要去判断在一个字符串的a 到 b 之间的字母 与 c 到 d 之间的字母分开拼起来是否相等
思路:
感觉有点水
这道题当然也是用前缀和,但这个是要存一个字母,聪明如我(dog)用二维,一个存字符串的位置,一个存字母
s[字符串的位置][字母]
就相当于s[1][‘a’]以此类推
还有一个问题,就是怎么去判断相等?
1.前缀和不是存了每个字母有多少个吗,直接看个数相不相等不得了
错误代码:
无
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 5e4 + 5;
int s[N][155];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string t;
cin >> t;
t = '!' + t;
for(int i = 1 ; i <= t.size() ; i++){
//继承过来
for(int j = 'a' ; j <= 'z' ; j++){
s[i][j] = s[i-1][j];
}
s[i][t[i]]++;
}
int T;
cin >> T;
while(T--){
int a,b,c,d;
cin >> a >> b >> c >> d;
int f = 1;//标记
for(int i = 'a' ; i <= 'z' ; i++){
if(s[b][i] - s[a-1][i] != s[d][i] - s[c-1][i]){//不相等即为0
f = 0;
break;
}
}
if(f == 1){
cout << "DA" << "\n";
}else{
cout << "NE" << "\n";
}
}
return 0;
}错因:
水一次,不知道说啥
没有用二维,前缀和
感谢观看!累死我了!晚安😴!