跳到正文

jyx130320

1457323

宣:https://www.luogu.com.cn/team/104520 || 做题 [现代] jyx130320 洛谷题库里,做了几道题,快来快来数一数,24678。WA、TLE还有MIE。就是没有AC,没有AC。

在洛谷打开
资料、奖项与版本

资料

编号
1457323
抓取

偷看主页的银!


最长上升子序列

C++
#include<bits/stdc++.h>
using namespace std;
int s[100005],tot=0;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int k;
        cin>>k;
        if(i==1)s[++tot]=k;
        else if(s[tot]<k){
            s[++tot]=k;
        }else{
            int tmp=lower_bound(s+1,s+tot+1,k)-s;
            s[tmp]=k;
        }
    }
    cout<<tot;
    return 0;
}

C++
fa[i]->i的父节点
dep[i]->i的深度(从1开始)

C++
ceil(x);//求x向上取整,返回值类型为小数
floor(x);//求x向下取整,返回值类型为小数
log(x);//表示以e为底x的对数,返回值类型为小数 
log(a)/log(b);//表示以b为底a的对数 
log2(a);//表示以2为底a的对数 

C++
#include <bits/stdc++.h>
using namespace std;
/*
STL里面的栈 
*/
int main(){
	//stack<数据类型> 栈名;
	stack<int> st;//定义一个栈
	//栈名.函数名
	st.push(1);//入栈
	st.push(3);
	st.push(5);
	cout<<"栈顶:"<<st.top()<<endl;//取栈顶  5
	cout<<"栈大小:"<<st.size()<<endl;//取大小 3
	st.pop();//出栈 把5出栈
	//遍历栈
	while(!st.empty()){
		cout<<st.top()<<" ";
		st.pop();
	}
	return 0;
}


C++
binary_search有4个参数
//1.查找的数组下标开始(是地址)
//2.查找的数组下标结束(是地址)
//3.要查找的对象(要和数组一个类型)
//4.comp;即自定义的比较方式。初始都是默认升序,同sort,不知道怎么写的自行在网上借鉴sort的写法,我在这里不过多阐述
//如果查找到了,返回bool值true,没找到返回bool值false
//二分查找无论何时都要用顺序,STL也一样,顺序方式就在comp里写!!!

乐乐爱迷你. (最后有一个英文句号)


C++
sqrt(abs(x1-x))+sqrt(abs(y1-y))//[x,y]到[x1,,y1]的距离

1100011101100abcd8z3edesertEosssaaaasdfgdesertE|ooooo


C++
//辗转相除法求最大公约数
int gcd(int a,int b){
    while(b!=0){
        int temp=a%b;
        a=b;
        b=temp;
    }
    return a;
}

//最小公倍数=两数乘积/最大公约数
int lcm(int a,int b){
    return(a*b)/gcd(a,b);
}

C++
#include<bits/stdc++.h>//在所有的数字m后面插入q
using namespace std;
struct node{
	int x;
	node *next;
};
int main(){
	int n;
	cin>>n;
	node *head,*tail;
	head=new node;
	head->next=NULL;
	tail=head;
	node *p;
	for(int i=1;i<=n;i++){
        int x;
        cin>>x;
		p=new node;
		p->x=x;
		p->next=NULL;
		tail->next=p;
		tail=p;
	}
	p=head->next;
	int m,q;
    cin>>m>>q;
    while(p!=NULL){
        if(p->x==m){
            node *p1;
            p1=new node;
            p1->x=q;
            p1->next=NULL;
            p1->next=p->next;
            p->next=p1;
            p=p->next->next;
        }else{
            p=p->next;
        }
    }
    p=head;
    while(p->next!=NULL){
		cout<<p->next->x<<" ";
		p=p->next;
	}
	return 0;
}

C++
#include<bits/stdc++.h>//输入n个整数,用给定的 n个整数创建一个单链表
using namespace std;
struct node{
    int x;
    node *next;
};
int main(){
    int n;
    cin>>n;
    node *head,*tail;
    head=new node;
	head->next=NULL;
	tail=head;
    node *p;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        p=new node;
        p->x=x;
        p->next=NULL;
        tail->next=p;
		tail=p;
    }
    p=head;
	while(p->next!=NULL){
		cout<<p->next->x<<" ";
		p=p->next;
	}
    return 0;
}

C++
//插入1-n的链表元素
#include<bits/stdc++.h>
using namespace std;
struct node{
	int x;
	node *next;
};
int main(){
	int n;
	cin>>n;
	node *head,*tail;
	head=new node;
	head->next=NULL;
	tail=head;
	node *p;
	for(int i=1;i<=n;i++){
		p=new node;
		p->x=i;
		p->next=NULL;
		tail->next=p;
		tail=p;
	}
	p=head;
	while(p->next!=NULL){
		cout<<p->next->x<<" ";
		p=p->next;
	}
	return 0;
}

C++
round(double x);//将x四舍五入成整数。

C++
//欧几里得算法(最大公因数)
/*int gcd(int x,int y){
    return y?gcd(y,x%y):x;
}*///递归
int gcd(int x,int y){
    while(y){
        int temp=x%y;
        x=y;
        y=temp;
    }
    return x;
}//循环

C++
//高精度除以低精度
#include <bits/stdc++.h>
using namespace std;
const int N=1e3+5;//请根据需要改大小
int a[N],c[N];
long long b;//除数
int main()
{
	string s1;//被除数
	cin>>s1>>b;
	int lena=s1.size();
	for(int i=0;i<lena;i++)
	{
		a[i]=s1[i]-'0';
	}
	int lenc=lena;
	long long r=0;
	for(int i=0;i<lenc;i++)
	{
		r=r*10+a[i];
		if(r>=b)
		{
			c[i]=r/b;
			r=r%b;
		}
	}
	int idx=0;
	while(idx<lenc-1&&c[idx]==0)
	{
		idx++;
	}
	for(int i=idx;i<lenc;i++)
	{
		cout<<c[i];//商
	}
	cout<<endl<<r;//余数
	return 0;
}

C++
insert(l,r,c,b); //数组b区间[l,r]加c(需前缀和处理)(需自定义函数)
void insert(int l,int r,int c,int b[]) {
    b[l]+=c;
    b[r+1]-=c;
}

insert(l1,r1,l2,r2,c,b); //数组b矩阵[l1,r1]到[l2,r2]加c(需前缀和处理)(需自定义函数)
void insert(int l1,int r1,int l2,int r2,int c,int b[]){
    b[l1][r1]+=c;
    b[l1][r2+1]-=c;
    b[l2+1][r1]-=c;
    b[l2+1][r2+1]+=c;
}

vector<int>vec={1,2,4};
vec.insert(vec.begin()+2,3);//在索引2前插入3
// 结果:{1, 2, 3, 4}

string s ="123456";
s.insert(3,"abc");//在位置3插入"abc"
// 结果:"123abc456"

二分答案(最后一个>=x)

C++
int f(int l,int r,int x){
    int ans=-1;
    while(l<=r){
        int mid=l+(r-l)/2;
        if(a[mid]==x){
            ans=mid;
            l=mid+1;
        }else if(a[mid]<x){
            l=mid+1;
        }else r=mid-1;
    }
    return ans;
}

二分答案(第一个>=x)

C++
int f(int l,int r,int x){
    int ans=-1;
    while(l<=r){
        int mid=l+(r-l)/2;
        if(a[mid]==x){
            ans=mid;
            r=mid-1;
        }else if(a[mid]<x){
            l=mid+1;
        }else r=mid-1;
    }
    return ans;
}

C++
cbrt(n)//n的立方

二分(闭区间)

C++
int f(int x){
	int l=1,r=n;
	while(l<=r){
		int m=(l+r)/2;
		if(a[m]<x){
			l=m+1;
		}else{
			r=m-1;
		}
	}
	return a[l]==x;
}

C++
 //快速幂函数(a^b)%p
int ksm(int a, int b, int p){
    int sum=1; //初始化结果为1
    a = a % p; //先对 a 取模,防止溢出
    while(b>0){
         //如果当前二进制位为1,将结果乘以a
        if (b&1){
            sum=(sum*a)%p;
        }
         //将a平方,继续处理下一位
        a=(a*a)%p;
        b=b>>1;//右移一位,相当于 b/=2
    }
    return sum;
}

C++
#include<bits/stdc++.h>
using namespace std;
/*归并排序*/
const int N=2e5+5;//常量
int a[N];//原数组
int b[N];//辅助数组
void merge(int left,int mid,int right)
{
   int i=left,j=mid+1,k=left;//i左区间,j右区间下标,k合并之后下标
   while(i<=mid && j<=right){
   	if(a[i]<=a[j]) b[k++]=a[i++];//选择左区间
   	else b[k++]=a[j++];//选择右区间
   }
   while(i<=mid){//当左边有剩余
   	b[k++]=a[i++];
   }
   while(j<=right){//当右边有剩余
   	b[k++]=a[j++];
   }
   for(int x=left;x<=right;x++) a[x]=b[x];//从辅助数组存回原数组 [left,right] 		
}
void merge_sort(int left,int right)
{
   //分解问题,求解问题
   if(left==right) return;//区间内只有一个数,无需分解
   int mid=(left+right)/2;//[left,mid]  [mid+1,right]
   merge_sort(left,mid);//左区间
   merge_sort(mid+1,right);//右区间
   //合并答案
   merge(left,mid,right);
}
int main(){
   int n;
   cin>>n;
   for(int i=1;i<=n;i++) scanf("%d",&a[i]);//cin>>a[i];
   merge_sort(1,n);
   for(int i=1;i<=n;i++) printf("%d ",a[i]);//cout<<a[i]<<" ";
   return 0;
}

C++
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int arr[N];
//快速排序
//从小到大排序 
void qsort(int left, int right){
	if(left>right) return;//区间不存在,直接返回
	int mid = (left+right)/2;//中间位置
	swap(arr[mid], arr[right]);//交换枢纽元和最右边元素
	int i = left, j=right - 1;//设置i,j变量
	while(i <= j){//当i,j没有交错时,一直执行
		while(arr[i] < arr[right])//当i位置上的值小于枢纽元时,一直向右走。
			i++;
		while(arr[j] > arr[right])//当j位置上的值大于枢纽元时,一直向左走。
			j--;
		if(i<=j){//如果i,j都停止时,i,j没有交错。则交换i,j的值
			swap(arr[i], arr[j]);
			i++;
			j--;
		}
	}
	swap(arr[i], arr[right]);//交换枢纽元和i位置的值,i位置的元素代表已经排好的,所以左部分的右边界为i-1,右部分的左边界为i+1 
	qsort(left, i-1);//对左边快速排序
	qsort(i+1, right);//对右边快速排序
}

int main(){
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) cin >> arr[i];
	qsort(1, n);
	for(int i=1; i<=n; i++) cout << arr[i] << " ";
	return 0;
}

C++
//广度优先搜索
#include <bits/stdc++.h>
using namespace std;
int mp[105][105];
bool vis[105][105];
int n;
int sx,sy,fx,fy;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
struct node{
	int x,y;
};
bool bfs()
{
	queue<node> q;
	q.push({sx,sy});//起点入队
	vis[sx][sy]=1;
	while(!q.empty()){//q.size()
		node t=q.front();//取队首 tx=t.x  ty=t.y
		int tx=t.x;
		int ty=t.y;
		q.pop();//队首出队
		if(tx==fx && ty==fy) return 1;//到达终点
		//拓展下一步
		for(int i=0;i<4;i++){
			int nx=tx+dx[i];
			int ny=ty+dy[i];
			if(nx<1||nx>n||ny<1||ny>n) continue;
			if(vis[nx][ny]) continue;
			if(mp[nx][ny]==1) continue;
			q.push({nx,ny});
			vis[nx][ny]=1;
		}
	}
	return 0;
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++) cin>>mp[i][j];
	}
	cin>>sx>>sy>>fx>>fy;
	//bool f=bfs();
	if(bfs()) cout<<"YES";
	else cout<<"NO";
	return 0;
}


C++
//图的表示—邻接表
#include<bits/stdc++.h>
using namespace std;
struct node{
    int v,w;
};
vector<node>g[10000005];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v,w;
        cin>>u>>v>>w;
        g[u].push_back({v,w});
    }
    for(int i=1;i<=n;i++){
        cout<<i<<":";
        for(int j=g[i].size()-1;j>=0;j--){
            int v=g[i][j].v;
            int w=g[i][j].w;
            printf("(%d,%d)->",v,w);
  	    //起点的编号:(终点编号,权值)
        }
        cout<<endl;
    }
    return 0;
}

C++
freopen("    .in","r",stdin);
freopen("    .out","w",stdout);

C++
scanf("%",&); printf("%",);
共 2 篇
  1. 休闲·娱乐

    宣https://www.luogu.com.cn/team/104520

    宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104520 宣104…

  2. 题解

    题解:P12529 [XJTUPC 2025] 对称隔离:黑白之战

    题目:给一个n*m的字符矩阵,把一些W改为B,使左右或上下对称。 因为有两种情况(使左右或上下对称),可以用两个矩阵表示两种情况,又单独一个矩阵输入。 输入后在遍历一遍,像这样: 最后判断是否任意两个黑色地毯所在的格子不共享公共边就可以了。 AC代码: 注:输出是Yes和No。 …