Codeforces Round 891(div3)

文章讲述了作者在Codeforces编程竞赛中遇到的问题,涉及排序、数学建模(一元二次方程)以及动态规划的应用,强调了比赛中的实际思考过程和细节处理的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

机房真的是太热了,我tm服了,感觉一点也学不进去啊...

8.6那天打了百度之星,还是一样的烂...

暑假集训快结束了...

自己还是一如既往的菜....

前天的cf又打烂了..我真的服了...

第二题卡了.第三题没出..

但是第4题和第5题思路很快..

第4题10分钟出来了,第5题20分钟出来了

第二题感觉题意都没读太懂...然后就开始做了,但是这都是小问题..

我做出来了..但是tm因为一个小问题,调了快半个小时..

s[i]='8'让s[i]变成‘9’不是s[i]+'1'!!!!

而是s[i]+1或者s[i]+'1'-‘0’

一个盲点在比赛中可能是致命的!!

Problem - C - Codeforces

一个小时做这题...

最后还没出来...

比赛过程中我太注重统计每个数的个数了,然后想从这入手,发现无从下手啊...

我尝试了好多方法:方程组或者看规律,但是最后都没看出什么

看了大佬的代码后...我恍然大悟...

我们将初始数组a从小到大排序

第一个数的贡献为n-1,第二个数的贡献为n-2,第三个数的贡献为n-3,倒数第2个数的贡献为1

最后一个数的贡献为0

而(n-1+n-2+n-3....+1)=(n-1)*n/2

所以我们只需跳即可

int cnt=0;
		
		for(int i=0;i<n-1;i++){
			cout<<a[cnt]<<" ";
			cnt=cnt+n-i-1;
		}

因为最后一个数的贡献为0,我们需要单独加上,注意看a的范围..不要加大了

!!

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
#include <list>
using namespace std;
typedef  long  long ll ;
typedef  unsigned long  long ull ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
int t;

int main(){
	scanf("%d",&t);
	while(t--){
		int  n;
		scanf("%d",&n);
		int m=(n-1)*n/2;
		vector<int>a;
		
		for(int i=0;i<m;i++){	
			int x;
			scanf("%d",&x);
			a.push_back(x);
		}
		
		sort(a.begin(),a.end());
		 
		int cnt=0;
		
		for(int i=0;i<n-1;i++){
			cout<<a[cnt]<<" ";
			cnt=cnt+n-i-1;
		}
		
		printf("1000000000\n");
	}
	
	
	return 0;
}

 

Problem - D - Codeforces

山东省省赛原题吧.

移项即可

Au-Av>=Bu-Bv

左右两边同时加上Av-Bu

得到Au-Bu>= AV-Bv(符号不变!!!,一开始没证明成立)

然后我们统计Au-Bu的差,然后从大到小排序即可

然后从1开始统计相等的个数,如果不相等就break

Problem - E - Codeforces

这题也好简单...

就是题目有点难懂...看了好多遍...

但是思路还是很快的

首先我们先排序

比如s1 s2 s3 s4 s5 s6 s7,

我们发现选择si的贡献为:

设si前面有n个数,后面有m个数

对前面贡献为

n*s[i]-(前n个数的前缀和)+n

对后面的贡献为

(后m个数的后缀和)-m*s[i]+m

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
#include <list>
using namespace std;
typedef  long  long ll ;
typedef  unsigned long  long ull ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
int t;
struct Node{
	ll x;
	int id;
}a[200005];
ll s[200005];
bool cmp(Node s1,Node s2){
	return s1.x<s2.x;
}
ll now[200005];
int main(){
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i].x);
			a[i].id=i;
		}
		sort(a+1,a+1+n,cmp);
		
		
		
		for(int i=1;i<=n;i++){
			s[i]=s[i-1]+a[i].x;
		}
		
		for(ll i=1;i<=n;i++){
			ll ans=0;
			ans++;
			ans=ans+(s[n]-s[i]-a[i].x*(n-i)+n-i);
			ans=ans+(i-1+(i-1)*a[i].x-s[i-1]);		
			now[a[i].id]=ans;
		}
		
		for(int i=1;i<=n;i++){
			printf("%lld ",now[i]);
		}
		printf("\n");
		
	} 


	return 0;
}

 

Problem - F - Codeforces

这题还是比较有意思的

主要考察数学

一看数据范围,知道查询花费为O(1)或者O(logn)

但是logn一般来说就是二分,倍增,双指针啥的..

但都不是啊

然后我就对原式下手了

ai+aj=x;

ai*aj=y

得到ai^2+aj^2=x^2-2y

但是统计ai^2+aj^2需要花O(n^n),mp可能会爆

然后我又想了想:

ai=x-aj

代入下面式子

得到(x-aj)*aj=y

等等这不是一元二次方程吗???

当时想的是如果有2个解x1,x2

x1的个数为n,x2的个数为m

对答案贡献为n*(n-1)/2+m*n+m*(m-1)/2;

但样例没过...

然后我有加上了条件特判...

过了

但是看题解后明白了

x1+x2=x

x1*x2=y

.....原来如此...

垃圾版代码:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
#include <list>
using namespace std;
typedef  long  long ll ;
typedef  unsigned long  long ull ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
int t;
int main(){
	
	scanf("%d",&t);
	
	while(t--){
		int n;
		scanf("%d",&n);
		map<ll,ll>mp;
		
		for(int i=1;i<=n;i++){
			ll x;
			scanf("%lld",&x);
			mp[x]++;
		}	
		
		
		int q;
		scanf("%d",&q);
		
		vector<ll>a;
		
		while(q--){	
		
			ll ans=0;
			ll x,y;
			scanf("%lld%lld",&x,&y);
			if(x*x-4*y<0||(ll)sqrt(x*x-4*y)*(ll)sqrt(x*x-4*y)!=x*x-4*y){
				a.push_back(0);
				continue;
			}
			
			ll p=sqrt(x*x-4*y);
			int flag=0;
			
			if((x+p)%2==0){
				if((x+p)/2+(x+p)/2==x&&(x+p)/2*(x+p)/2==y){
					ans=ans+mp[(x+p)/2]*(mp[(x+p)/2]-1)/2;
				}
				flag=1;
			}
			
			if((x-p)%2==0&&p!=0){
				if((x-p)/2+(x-p)/2==x&&(x-p)/2*(x-p)/2==y){
					ans=ans+mp[(x-p)/2]*(mp[(x-p)/2]-1)/2;
				}
				
				if(flag){	
					if((x+p)/2+(x-p)/2==x&&(x+p)/2*(x-p)/2==y){
					ans=ans+mp[(x+p)/2]*mp[(x-p)/2];
					}
				}
			}
			
			a.push_back(ans); 
			
		}		
		
		for(int i=0;i<a.size();i++){
			printf("%lld ",a[i]);
		} 
		printf("\n");
			
	}
	

	return 0;
}

 

 

 正确代码

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include <utility>
#include <list>
using namespace std;
typedef  long  long ll ;
typedef  unsigned long  long ull ;
#define pii pair<int,int>
const int inf = 0x3f3f3f3f;//106110956
inline int read(){
    int x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9'){
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9'){
        x = (x<<1) + (x<<3) + (ch^48);
        ch = getchar();
    }
    return x * f;
}
void print(__int128 num) {
	if(num) {
		print(num/10);
		putchar(num%10+'0');
	}
}
int t;
int main(){
	
	scanf("%d",&t);
	
	while(t--){
		
		int n;
		scanf("%d",&n);
		map<ll,ll>mp;
		
		for(int i=1;i<=n;i++){
			ll x;
			scanf("%lld",&x);
			mp[x]++;
		}	
		
		
		int q;
		
		scanf("%d",&q);
		
		
		vector<ll>a;
		
		
		while(q--){	
		
			ll ans=0;
			ll x,y;
			scanf("%lld%lld",&x,&y);
			if(x*x-4*y<0||(ll)sqrt(x*x-4*y)*(ll)sqrt(x*x-4*y)!=x*x-4*y){
				a.push_back(0);
				continue;
			}
			
			ll p=sqrt(x*x-4*y);
			
			ll x1,x2;
			
			
			if((x+p)%2==0)x1=(x+p)/2;
			else {
				a.push_back(0);
				continue; 
			} 
			
			if((x-p)%2==0)x2=(x-p)/2;
			else {
				a.push_back(0);
				continue;
			}
			
			
			if(x1!=x2){
				ans=ans+mp[x1]*mp[x2];
			}else{
				ans=ans+mp[x1]*(mp[x1]-1)/2; 
			}
		
			a.push_back(ans); 
			
		}		
		
		for(int i=0;i<a.size();i++){
			printf("%lld ",a[i]);
		} 
		printf("\n");
			
	}
	
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值