AS的刷题纪录

CCF-CSP

题型分析

相对容并且常见的题型需要优先了解和练习。这有助于保证获得基本分。

CCF-CSP常见的考题类型:

简单计算,例如:CCF201709-1 打酱油(100分)。

序列处理,例如:CCF201612-1 中间数(100分)。

文本处理,例如:CCF201509-3 模板生成系统(100分),CCF201604-3 路径解析(100分)。

模拟题,例如:CCF201609-3 炉石传说(100分)。

搜索问题(DFS,BFS,优先搜索),例如:CCF201512-4 送货(100分)。

图论算法(最小生成树等等),例如:CCF201609-4 交通规划(100分)。

动态规划(DP),例如:CCF201612-4 压缩编码(100分)。

数学计算题(函数关系等等),例如:CCF201412-2 Z字形扫描(100分),CSP202012-1 期末预测之安全指数(100分)。

通过CCF-CSP考试(300分)必须掌握的题型:数学计算题、模拟、图论算法和文本处理,近年有数学计算题增加的趋势。从历年实际考题看,出题内容应该是开放的,任何类型都是有可能出现的。

4.日常练习

除了使用官方网站的题库进行练习外,平常应该使用ICPC题库进行练习,以适应目前广泛采用的认证考试与比赛的方式。以下2项练习是十分必要的:

     1.在线评测系统(OJ)做题 
     程序逻辑考虑,每个细节都正确;

2.阅读程序设计题解 以下是推荐的题解:

程序设计入门经典题解(百练篇)

程序设计入门经典题解(百练篇)_海岛Blog的博客-CSDN博客

ICPC-C语言入门
https://blog.csdn.net/tigerisland45/category_7336637.html

TYUT-A专题题解(一)

TYUT-A专题题解(一)_海岛Blog的博客-CSDN博客

TYUT-A专题题解(二)

TYUT-A专题题解(二)_海岛Blog的博客-CSDN博客

HDU各种考试题题解

HDU各种考试题题解_海岛Blog的博客-CSDN博客

2021-9数组推导
2018-3碰撞的小球
2017-3学生排队
2017-3分蛋糕
2021-9非零段划分
202006线性计数器

简单的模拟

202104灰度直方图

map

202112期末考试安全系数

模拟

201803跳一跳

模拟

动态记录上一次的数值

PAT

按照此顺序做题*CCF CSP认证从零带你开始刷题(更新中) - 知乎 (zhihu.com)*

模拟

B1001

B1011

ll 的正常运算,搞复杂了

给定区间 [ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tcpgEuJu-1647866535544)(https://www.zhihu.com/equation?tex=-2%5E%7B31%7D)] , [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XOLLyGEJ-1647866535547)(https://www.zhihu.com/equation?tex=2%5E%7B31%7D)] ] 内的 3 个整数 ABC,请判断 A+B 是否大于 C

输入格式:

输入第 1 行给出正整数 T (≤10),是测试用例的个数。随后给出 T 组测试用例,每组占一行,顺序给出 ABC。整数间以空格分隔。

输出格式:

对每组测试用例,在一行中输出 Case #X: true 如果 A+B>C,否则输出 Case #X: false,其中 X 是测试用例的编号(从 1 开始)。

错误:没有考虑等于的情况

B1016

vector模板

B1032

特殊情况考虑

极端情况:

1/-cnf

得分0/-cnf

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+10;
int main(){
	ll n;cin>>n;
	ll a[N]={0};
	ll tem=0;
	for(ll i=0;i<n;i++){
		ll j,m;cin>>j>>m;
		a[j]+=m;
		tem=max(tem,j);
	}
	ll temp=0,nu=0;
	for(int i=0;i<=tem;i++){
		if(temp<=a[i])temp=a[i],nu=i;
        /*开始想,那不是最大就是最小,
		如果只有一个学校参加比赛,而且还只拿了0分,这种情况代码是否可以运行。
		果然,原来的代码没有考虑这种情况,
		于是加了>=的判断条件,AC满分。*/
	}
	cout<<nu<<" "<<temp;
	return 0;
}


b1016

板子用得太单调了,

hack点

10000012 0 123 5

before :00000

//高精加法
#include<bits/stdc++.h>
using namespace std;
vector<int>A,B,C;
string a,b;
char n,m;
void add(){
    int t=0;//存进位
    for(int i=0;i<A.size() || i<B.size() ;i++){
        if(i<A.size()) t+=A[i];
        if(i<B.size()) t+=B[i]; 
        C.push_back(t%10);//余数
        t=t/10;
    }
    if(t) C.push_back(1);//最高位进位
    while(C.size() > 1 && C.back() == 0) C.pop_back();//去除前导零
    //hack:10000012 0 123 5
}
int main(){
    cin>>a>>n>>b>>m;bool f1=0,f2=0;
    for(int i=a.size()-1;i>=0;i--) if(a[i]==n)f1=1,A.push_back(a[i]-'0');//逆序存放
    for(int i=b.size()-1;i>=0;i--) if(b[i]==m)f2=1,B.push_back(b[i]-'0');
    if(f1==0){
    	A.push_back(0);
	}
	if(f2==0){
		B.push_back(0);
	}
    add();
    for(int i=C.size()-1;i>=0;i--) cout<<C[i];
    return 0;
}

洛谷

字符串

cmp排序+sort

1012.拼数

cmp:reutrn a+b<b+a;

!单个数比较太麻烦,反正是Ai组合,返回比较模拟情况;

二分

1577.切绳子

1)二分的前提在于它寻找的范围很大,也就是指,找到某个特定要求的数值时,人为确定范围l=0,r=~,

2)二分终止条件:l>r

​ 当他们跳出时,l=r=所求;

​ 一般地,if mid<k,r=mid-1;

​ else l=mid+1;

1571.眼红(按顺序输出编号,使得不能简单地排序对比

二分寻找;

(结构体应该不行。

1873.砍树

Mirko 设置一个高度参数 HH(米),伐木机升起一个巨大的锯片到高度 HH,并锯掉所有树比 HH 高的部分(当然,树木不高于 HH 米的部分保持不变)。Mirko 就得到树木被锯下的部分。例如,如果一排树的高度分别为 20,15,1020,15,10 和 1717,Mirko 把锯片升到 1515 米的高度,切割后树木剩下的高度将是 15,15,1015,15,10 和 1515,而 Mirko 将从第 11 棵树得到 55 米,从第 44 棵树得到 22 米,共得到 77 米木材。

Mirko 非常关注生态保护,所以他不会砍掉过多的木材。这也是他尽可能高地设定伐木机锯片的原因。请帮助 Mirko 找到伐木机锯片的最大的整数高度 HH,使得他能得到的木材至少为 MM 米。换句话说,如果再升高 11 米,他将得不到 MM 米木材。

2249.查找

vis 数组记忆化

贪心

1080.国王游戏

数据点

1 1.3

1 1.2

2 1

未处理完毕,在于cmp条件给写的是ls*rs<;但是2 1 这样的数据点就会让2被忽略

3817

3817.小 A 每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中糖的个数之和都不大于 xx,至少得吃掉几颗糖。

相邻两数中减小的为后者就好了;

1208.牛奶==背包问题

暴力

取模方法:
取模运算n%p的结果与p的符号无关,由n决定。

(a+b)%p=(a%p+b%p)%p

(a-b)%p=(a%p-b%p)%p

(ab)%p=(a%pb%p)%p

(ab)%p=((a%p)b)%p

给你一个数a,让你求其b次连乘后的结果
当b很小时,一般的循环算法可以解决这个问题(O(B)),但是当b较大时呢
要知道1e18以上,就会long long int 也可能会溢出
而在数论方面这些数又该如何表示?怎样存储?
所以在此我们定义一个模数mod来代替输出
即a^ b=k1mod+t即t=a^b%mod
性质1:(a+b)%m=(a%m+b%m)%m
性质2:(ab)%m=(a%m*b%m)%m

当幂很大时,我们采取分治的思想处理问题
a^ b=((a^ (b/n)) ^ n)*a ^ (b%n);

ll quickmod(ll a,ll b,ll c)
{
    if(b==1)
        return a;
    if(!(b&1))
    {
        ll t=quickmod(a,b/2,c);
        return t*t%c;
    }
    else
    {
        ll t=quickmod(a,b/2,c);
        return t*t%c*a%c;
    }

1618.暴力!!

 if(x<=999&&z<=999&&x<z){
 	int q=p/100%10,w=p/10%10,e=p%10,r=x/100%10,t=x/10%10,y=x%10,u=z/100%10,i=z/10%10,o=z/1%10 ;	
	if(q!=w&&q!=e&&q!=r&&q!=t&&q!=y&&q!=u&&q!=i&&q!=o&&
	w!=e&&w!=r&&w!=t&&w!=y&&w!=u&&w!=i&&w!=o&&
	e!=r&&e!=t&&e!=y&&e!=u&&e!=i&&e!=o&&
	r!=t&&r!=y&&r!=u&&r!=i&&r!=o&&
	t!=y&&t!=u&&t!=i&&t!=o&&
	y!=u&&y!=i&&y!=o&&
	u!=i&&u!=o&&
	i!=o ){if(q*w*e*r*t*y*u*i*o==1*2*3*4*5*6*7*8*9){

	cout<<p<<" "<<x<<" "<<z<<endl;
	f=0;}	} 
 }
for(int a=1;a<4;a++ ){
		for(int b=1;b<4;b++ ){
			for(int c=1;c<4;c++ ){
				for(int d=1;d<4;d++ ){
					for(int e=1;e<4;e++ ){
						for(int f=1;f<4;f++ ){
							for(int g=1;g<4;g++ ){
								for(int h=1;h<4;h++ ){
									for(int i=1;i<4;i++ ){
										for(int j=1;j<4;j++ ){
										if(a+b+c+d+e+f+g+h+i+j==n){
											av[count][0]=a;av[count][1]=b;av[count][2]=c;av[count][3]=d;av[count][4]=e;av[count][5]=f;av[count][6]=g;av[count][7]=h;av[count][8]=i;av[count][9]=j;
											count++;
									}}}}}}}}}}}
	cout<<count<<endl;
	for(int l=0;l<count;l++){
		for(int k=0;k<10;k++){
			cout<<av[l][k]<<" ";

高精度

板子!

DP

1002.过河卒

数组标记每一个可能的范围组,对比模拟结束战斗

递推

1255.数楼梯

递推公式

F=F(n-1)+F(n-2)

(1) ll 与int 终会让你流泪

R69945148.数的计算(构造数

(1 记忆化数组可以防止栈溢出

搜索

1219.八皇后

DFS

1.模拟标记

2.在此基础上递推

3.递推回来时,保证所有搜索到,恢复标记

模拟

DFS

为什么不是贪心?给你所有模拟的结果,比较即可。

and 贪心我们认为其贪心策略是每一次选择的最优,本题是所有最后结果的最优,不能拆成小问题最贪。

2684.堆线段,覆盖,离线

FJ准备分配它的N只奶牛(1 <= N <= 25,000) 做清洁工作,他把一天分成T(1 <= T <= 1,000,000)个时间段,他希望每一个时间段都有奶牛在清洁,但搞清洁的奶牛数越少越好。

结束工作最早的比较就好,动态改变

sort(a,a+n,cmp);
	ll ans=0;
	a[n].b=t+1;
	ll at=1,temp=0;
	for(int i=0;i<=n;i++){
		if(a[i].b>at){
			at=temp+1;
		if(a[i].b>at){
            ans=-1;break;
        }
		temp=a[i].e;//动态改变
		ans++;
		}
		else if(a[i].e>temp)
				temp=a[i].e;//
	}

2191.旋转矩阵

for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(val[i][j]==1)cout<<a[i][j];
		}
	}//1
	for(int i=0;i<n;i++){
		for(int j=n-1;j>-1;j--){
			if(val[j][i]==1)cout<<a[i][n-j-1];
		}
	}//2
	for(int i=n-1;i>=0;i--){
		for(int j=n-1;j>=0;j--){
			if(val[i][j]==1)cout<<a[n-i-1][n-j-1];
		}
	}//3
	for(int i=n-1;i>=0;i--){
		for(int j=0;j<n;j++){
			if(val[j][i]==1)cout<<a[n-i-1][j];
		}
	}//4

1098.字符展开

对条件的考虑缺失会导致更多的失误

3392.涂国旗

特点:

三种组合,总体最小,模拟DFS;

暴力 首先预处理,算出每一行如果分别换成R/W/B需要的成本

然后n^2的枚举前两种颜色的行数,第三种颜色就是剩下的,然后统计比较。注意每行不能小于1。

不用什么优化,如果你高兴的话还能使用前缀和优化。

位运算

1100.高低位交换

cout<<(n>>16)+(n<<16);

数据结构

1160.队列

CodeForces

1612A

A. Distance

time limit per test

3 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Let’s denote the Manhattan distance between two points p1p1 (with coordinates (x1,y1)(x1,y1)) and p2p2 (with coordinates (x2,y2)(x2,y2)) as d(p1,p2)=|x1−x2|+|y1−y2|d(p1,p2)=|x1−x2|+|y1−y2|. For example, the distance between two points with coordinates (1,3)(1,3) and (4,2)(4,2) is |1−4|+|3−2|=4|1−4|+|3−2|=4.

You are given two points, AA and BB. The point AA has coordinates (0,0)(0,0), the point BB has coordinates (x,y)(x,y).

Your goal is to find a point CC such that:

  • both coordinates of CC are non-negative integers;
  • d(A,C)=d(A,B)2d(A,C)=d(A,B)2 (without any rounding);
  • d(B,C)=d(A,B)2d(B,C)=d(A,B)2 (without any rounding).

Find any point CC that meets these constraints, or report that no such point exists.

Input

The first line contains one integer tt (1≤t≤30001≤t≤3000) — the number of test cases.

Each test case consists of one line containing two integers xx and yy (0≤x,y≤500≤x,y≤50) — the coordinates of the point BB.

Output

For each test case, print the answer on a separate line as follows:

  • if it is impossible to find a point CC meeting the constraints, print “-1 -1” (without quotes);
  • otherwise, print two non-negative integers not exceeding 106106 — the coordinates of point CC meeting the constraints. If there are multiple answers, print any of them. It can be shown that if any such point exists, it’s possible to find a point with coordinates not exceeding 106106 that meets the constraints.

Example

input

Copy

10
49 3
2 50
13 0
0 41
42 0
0 36
13 37
42 16
42 13
0 0

output

Copy

23 3
1 25
-1 -1
-1 -1
21 0
0 18
13 12
25 4
-1 -1
0 0

Note

Explanations for some of the test cases from the example:

  • In the first test case, the point BB has coordinates (49,3)(49,3). If the point CC has coordinates (23,3)(23,3), then the distance from AA to BB is |49−0|+|3−0|=52|49−0|+|3−0|=52, the distance from AA to CC is |23−0|+|3−0|=26|23−0|+|3−0|=26, and the distance from BB to CC is |23−49|+|3−3|=26|23−49|+|3−3|=26.
  • In the second test case, the point BB has coordinates (2,50)(2,50). If the point CC has coordinates (1,25)(1,25), then the distance from AA to BB is |2−0|+|50−0|=52|2−0|+|50−0|=52, the distance from AA to CC is |1−0|+|25−0|=26|1−0|+|25−0|=26, and the distance from BB to CC is |1−2|+|25−50|=26|1−2|+|25−50|=26.
  • In the third and the fourth test cases, it can be shown that no point with integer coordinates meets the constraints.
  • In the fifth test case, the point BB has coordinates (42,0)(42,0). If the point CC has coordinates (21,0)(21,0), then the distance from AA to BB is |42−0|+|0−0|=42|42−0|+|0−0|=42, the distance from AA to CC is |21−0|+|0−0|=21|21−0|+|0−0|=21, and the distance from BB to CC is |21−42|+|0−0|=21|21−42|+|0−0|=21.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e3+10;
int main(){
	int t;cin>>t;
	while(t--){
		ll x,y,n=-1,m=-1;cin>>x>>y;
		while(1){
			if((x+y)%2==1)break;
			if(y%2==0&&x%2==0){
				n=x/2;
				m=y/2;
				if(n+m!=(x+y)/2)n=-1,m=-1;
				break;
			}
			else if((x-y)>0&&(x-y)%2==0){
				n=(x-y)/2;
				m=(x+y)/2-n;
				if(n+m!=(x+y)/2)n=-1,m=-1;
				break;
			}
			else if((y-x)>0&&(y-x)%2==0){
				m=(y-x)/2;
				n=(x+y)/2-m;
				break;	
			}
			break;
		}
		cout<<n<<" "<<m<<endl;
	}
	return 0;
}WAWA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e3+10;
int main(){
	int t;cin>>t;
	while(t--){
		ll x,y,n=-1,m=-1;cin>>x>>y;
		if ((x + y) % 2 != 0)
		{
			cout << "-1 -1" << endl;
		}
		else
		{
			int flag = (x + y) / 2;
			if (y >= flag)
			{
				cout << x << " " << y - flag << endl;
			}
			else
			{
				cout << x - flag << " " << y << endl;;
			}
		}
	}
	return 0;
}


acac
    
775a

A. Era

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Shohag has an integer sequence a1,a2,…,ana1,a2,…,an. He can perform the following operation any number of times (possibly, zero):

  • Select any positive integer kk (it can be different in different operations).
  • Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert kk into the sequence at this position.
  • This way, the sequence aa changes, and the next operation is performed on this changed sequence.

For example, if a=[3,3,4]a=[3,3,4] and he selects k=2k=2, then after the operation he can obtain one of the sequences [2–,3,3,4][2_,3,3,4], [3,2–,3,4][3,2_,3,4], [3,3,2–,4][3,3,2_,4], or [3,3,4,2–][3,3,4,2_].

Shohag wants this sequence to satisfy the following condition: for each 1≤i≤|a|1≤i≤|a|, ai≤iai≤i. Here, |a||a| denotes the size of aa.

Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it’s always possible to achieve this goal in a finite number of operations.

Input

The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1001≤n≤100) — the initial length of the sequence.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the sequence.

Output

For each test case, print a single integer — the minimum number of operations needed to perform to achieve the goal mentioned in the statement.

Example

input

Copy

4
3
1 3 4
5
1 2 5 7 4
1
1
3
69 6969 696969

output

Copy

1
3
0
696966

Note

In the first test case, we have to perform at least one operation, as a2=3>2a2=3>2. We can perform the operation [1,3,4]→[1,2–,3,4][1,3,4]→[1,2_,3,4] (the newly inserted element is underlined), now the condition is satisfied.

In the second test case, Shohag can perform the following operations:

[1,2,5,7,4]→[1,2,3–,5,7,4]→[1,2,3,4–,5,7,4]→[1,2,3,4,5,3–,7,4][1,2,5,7,4]→[1,2,3_,5,7,4]→[1,2,3,4_,5,7,4]→[1,2,3,4,5,3_,7,4].

In the third test case, the sequence already satisfies the condition.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e2+10;
int main(){
	int t;cin>>t;
	while(t--){
		ll n;cin>>n;
		ll a[N];
		ll sum=0;ll temp=0;
		for(int i=1;i<=n;i++){
		cin>>a[i];	temp=max(temp,a[i]);
		}
		sum+=temp-n;
		for(int i=1;i<=n;i++){
			if(a[i]>i+sum)sum+=a[i]-i-sum;///???/
            /*我:最开始是++,没有考虑真实需要几个
		}
		cout<<sum<<endl;
	} 
	return 0;
}acac
756b

B. Team Composition: Programmers and Mathematicians

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The All-Berland Team Programming Contest will take place very soon. This year, teams of four are allowed to participate.

There are aa programmers and bb mathematicians at Berland State University. How many maximum teams can be made if:

  • each team must consist of exactly 44 students,
  • teams of 44 mathematicians or 44 programmers are unlikely to perform well, so the decision was made not to compose such teams.

Thus, each team must have at least one programmer and at least one mathematician.

Print the required maximum number of teams. Each person can be a member of no more than one team.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.

This is followed by descriptions of tt sets, one per line. Each set is given by two integers aa and bb (0≤a,b≤1090≤a,b≤109).

Output

Print tt lines. Each line must contain the answer to the corresponding set of input data — the required maximum number of teams.

Example

input

Copy

6
5 5
10 1
2 3
0 0
17 2
1000000000 1000000000

output

Copy

2
1
1
0
2
500000000

Note

In the first test case of the example, two teams can be composed. One way to compose two teams is to compose two teams of 22 programmers and 22 mathematicians.

In the second test case of the example, only one team can be composed: 33 programmers and 11 mathematician in the team.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e+10;
int main(){	
	int t;cin>>t;
	while(t--){
		ll a,b;cin>>a>>b;
		ll tem=(a+b)/4;
		ll te=min(a,b);
		if(tem>te)cout<<te<<endl;
		else cout<<tem<<endl;
	}
	return 0;
}

其他

1.ios::sync_with_stdio(false);这个方法还是要解释一下的

在某些题目中,我们使用普通的cin和cout会超时,所以我们每次只能打scanf和printf,然后一堆的占位符巨麻烦),为什么cin和cout比scanf和printf用的时间多? 这是因为C++中,cin和cout要与stdio同步,中间会有一个缓冲,所以导致cin,cout语句输入输出缓慢,这时就可以用这个语句,取消cin,cout与stdio的同步,说白了就是提速,效率基本与scanf和printf一致。

取模方法:
取模运算n%p的结果与p的符号无关,由n决定。

(a+b)%p=(a%p+b%p)%p

(a-b)%p=(a%p-b%p)%p

(ab)%p=(a%pb%p)%p

(ab)%p=((a%p)b)%p

给你一个数a,让你求其b次连乘后的结果
当b很小时,一般的循环算法可以解决这个问题(O(B)),但是当b较大时呢
要知道1e18以上,就会long long int 也可能会溢出
而在数论方面这些数又该如何表示?怎样存储?
所以在此我们定义一个模数mod来代替输出
即a^ b=k1mod+t即t=a^b%mod
性质1:(a+b)%m=(a%m+b%m)%m
性质2:(ab)%m=(a%m*b%m)%m

当幂很大时,我们采取分治的思想处理问题
a^ b=((a^ (b/n)) ^ n)*a ^ (b%n);

ll quickmod(ll a,ll b,ll c)
{
    if(b==1)
        return a;
    if(!(b&1))
    {
        ll t=quickmod(a,b/2,c);
        return t*t%c;
    }
    else
    {
        ll t=quickmod(a,b/2,c);
        return t*t%c*a%c;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值