CCPC训练赛题解补题

文章讲述了在Codeforces竞赛中的四个问题,涉及内存优化、通过找规律解决精度问题、字符串操作(包括二进制转化和排序),以及利用几何知识解题。
摘要由CSDN通过智能技术生成

B. Memory

Problem - B - Codeforces(原题出处点击这里)

题意:根据题目中给出的求和公式,将输入的数据进行判断,输出”+“或”-“或”0“;因为范围特别大,精度特别高,所以循环遍历的方法一定是WA的。我们根据找规律可以看出来有mood[i]=mood[i-1]/2+a[i];有了这个公式再对精度处理一下,就能得到正确答案了

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
const int N=1e5+10;
int a[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
	}
	int x=0;
	double flag=0;
	for(int i=1; i<=n; i++)
	{
		x+=a[i];
		if(x>0)
		{
			cout<<"+";
		}
		else if(x<0)
		{
			cout<<"-";
		}
		else
		{
			if(flag==0)
			{
				cout<<"0";
			}
			else if(flag>0)
			{
				cout<<"+";
			}
			else
			{
				cout<<"-";
			}
		}
		if(x%2!=0)
		{
			flag=1.0*x/2.0-(x/2);
		}
		x/=2;
	}
}

A. 赛前须知

Problem - A - Codeforces(原题链接点击这里)

题意:这题很简单,十个选择题的答案竖着输出出来即可

#include<bits/stdc++.h>
#define int long long 
#define endl "\n"
using namespace std;
const int N=1e6+5;
void solve(){
	cout << "A" << endl;
	cout << "C" << endl;
	cout << "A" << endl;
	cout << "C" << endl;
	cout << "A" << endl;
	cout << "C" << endl;
	cout << "A" << endl;
	cout << "C" << endl;
	cout << "A" << endl;
	cout << "C" << endl;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t;
	//cin >> t;
	//while(t--) 
	solve();
	return 0;
}

K. str进制

Problem - K - Codeforces(原题链接点击这里)

题意:这题和我们平时写的二进制转化如出一辙,但是我们这一题不是一直辗转相处一个数字了,这个数字是变化的

#include<bits/stdc++.h>
#define int long long 
#define endl "\n"
using namespace std;
const int N=1e6+5;
int a[N];
void solve(){
	int m,n;
	cin >> m >> n;
	string s;
	cin >> s;
	int d;cin >> d;
	int k=0;
	int len=s.size()-1;
	while(m--){
		a[k++]=d%(s[len]-'0');
		d=d/(s[len]-'0');
		--len;
	}
	for(int i=k-1;i>=0;--i){
		cout << a[i];
	}
	return ;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t;
	//cin >> t;
	//while(t--) 
	solve();
	return 0;
}

M. 小H的糖果

  Problem - M - Codeforces(原题链接点击这里)

题意:输入一串字符串,可以修改一处,该如何替换糖果以及选择起始位置的糖果可以使这个字符串按照字典序排列最大

Examples

InputcopyOutputcopy
10
zzazzzabcd
zzzzzzabcd
InputcopyOutputcopy
8
azzzabcd
zzzzbcd

 这一题我们用到了一个比较笨的方法,我们使用遍历一遍字符串,将每个字符改成‘z’,然后比较大小,最后将最大的字符串给输出出来。我们比较笨,就会用简单暴力的办法

#include<bits/stdc++.h>
#define int long long 
#define endl "\n"
using namespace std;
void solve(){
	int n;
	string s;
	cin >> n >> s;string s1=s;
	for(int i=0;i<s.size();++i)
	{
		string s2=s.substr(i);
		for(int j=0;j<s2.size();++j)
		{
			if (s2[j]=='z')continue;
			char t=s2[j];
			s2[j]='z';
			if(s2>s1) s1=s2;
			s2[j]=t;	
		}
	}					
	cout << s1;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int t;
	//cin >> t;
	//while(t--) 
	solve();
	return 0;
}

J. pass

Problem - J - Codeforces(原题链接点击这里)

题意:这一题看着很难,还给出来几何图形,直接从气势上就吓到我们了,其实赛后补题的时候我们去写能发现其实也不是很难,考察到了对几何图形的掌握,有数学思维在里面

 对此,我们可以先判断一些条件,例如当w<b的时候一定输出-1,当a>w的时候一定输出0.000000000,这是两个特殊情况,比较容易判断。当c的长度,也就是轮子的高度为0的时候,如果我们能将车子弄起来,并且车子的另一端没有碰到河水那么我们就一定能正常行驶,这时轮子的最小值就是0.000000000。当我们轮子的高度为0,但是一端出去了,我们就要判断一下

我自己画了一个图是自己的理解

 

 各位自行理解一下这两幅图的含义吧,这样我们用相似三角形就能求出来c的高,这样求出来的值就是最小值

下面是代码

#include <bits/stdc++.h>
#define int long long 
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define PII pair<int,int> 
using namespace std;
void solve ()
{
	int n;cin>>n;double a,b,h,w;
	for (int i=0;i<n;i++)
	{
		cin>>a>>b>>h>>w;
		double ans;
		if (a>w)
		{
			 ans=0;
			printf("%.10lf\n",0);
		}
		
		else 
		{
			if (w<=b)printf("-1\n");
			else 
			{
				double l=sqrt(w*w-b*b)-a;
				if (l<=0)
				{
					ans=0;
					printf("%.10lf\n",0);
				}
				else 
				{
					 ans=b/w*l;
					printf("%.10lf\n",ans);
				}
				
			}
		}
	}
}

signed main ()
{
//	IOS;
	int T=1;
//	cin>>T;
	while (T--) solve ();
	return 0;
}

G. 小P玩钢4

Problem - G - Codeforces(原题链接点击这里)

InputcopyOutputcopy
5 5
0 0 0 0 0
0 0 0 1 0
0 1 1 1 0
0 0 1 0 0
0 0 0 0 0
9

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int m,n,sum,st[1005][1005],cnt;
int main()
{
	cin>>m>>n;
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cin>>st[i][j];
		}
	}
	for(int k=5;k>=1;k--)
	{
		for(int i=1;i<=m;i++)
	    {
		    for(int j=1;j<=n;j++)
		    {
		    	cnt=0;
		     if(st[i][j]==1)
			 cnt=st[i][j]+st[i-1][j]+st[i][j-1]+st[i][j+1]+st[i+1][j];
			 if(cnt==k)
			 {
			 	sum+=cnt;
			 	st[i][j]=0;
			 }
		    }
	    }
	}
	cout<<sum<<endl;
	return 0;
}

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值