传智杯初赛A~D题解

目录

  • A-莲子的软件工程学
  • B-莲子的机械动力学
  • D-莲子的物理热力学
  • E-梅莉的市场经济学

A-莲子的软件工程学

按要求if else 即可

/*
    悲观看待成功,乐观看待失败。 
    author:leimingze 
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此 
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n;
void solve()
{
	int a,b;
	cin>>a>>b;
	if(b>0)cout<<abs(a)<<endl;
	else cout<<-abs(a)<<endl;
}
signed main() 
{
	io;
	int _;_=1;
	//cin>>_;
	while(_--)solve();
	return 0;
}

B-莲子的机械动力学

高精度加法,只不过对于第i位的是i+1进制

/*
    悲观看待成功,乐观看待失败。 
    author:leimingze 
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此 
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n,m;
const int N=2e5+10;
int a[N],b[N];
void solve()
{
	cin>>n>>m;
	rep(i,1,n)cin>>a[i];
	rep(i,1,m)cin>>b[i];
	vector<int>A,B,C;
	dwn(i,n,1)A.pb(a[i]);
	dwn(i,m,1)B.pb(b[i]);
	int t=0;
	for(int i=0;i<B.size()||i<A.size();i++)
	{
		if(i<A.size())t+=A[i];
		if(i<B.size())t+=B[i];
		C.pb(t%(i+2)),t/=(i+2);
	}
	int maxv=max(A.size(),B.size());
	while(t)C.pb(t%(maxv+1)),t/=(maxv+1);
	while(C.size()>1&&C.back()==0)C.pop_back();
	reverse(all(C));
	for(auto x:C)cout<<x<<' ';
	cout<<endl;
}
signed main() 
{
	io;
	int _;_=1;
	//cin>>_;
	while(_--)solve();
	return 0;
}

D-莲子的物理热力学

这一题相对较难。tag:双指针+贪心
假设最优区间值域范围是[l,r],设v是小于l的个数,u是大于r的个数
那么有两种情况:

  1. 把小于l的的元素进行一次操作,操作次数+v;把大于r的元素进行一次操作,操作次数+v+u
  2. 把大于r的元素进行一次操作,操作次数+u,把小于l的的元素进行一次操作,操作次数+v+u

两种情况取min(u,v)+u+v
最后双指针更新一下res

/*
    悲观看待成功,乐观看待失败。 
    author:leimingze 
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此 
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n,k;
const int N=1e5+10;
int a[N];
void solve()
{
	cin>>n>>k;
	rep(i,1,n)cin>>a[i];
	sort(a+1,a+1+n);
	int j=1;
	int res=ll_INF;
	rep(i,1,min(n,k+1))
	{
		j=max(i,j);
		while((i-1)+(n-j)+min(i-1,n-j)>k)j++;
		res=min(res,a[j]-a[i]);
	}
	cout<<res<<endl;
}
signed main() 
{
	io;
	int _;_=1;
	//cin>>_;
	while(_--)solve();                                                                                                                                       
	return 0;
}

E-梅莉的市场经济学

模拟题,二分找一下在哪个区间,每个区间分成四段模拟一下
屎山代码

/*
    悲观看待成功,乐观看待失败。 
    author:leimingze 
*/
#include
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
const int base=131;
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'//交互题删掉此 
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
int lcm(int a,int b){return a*b/__gcd(a,b);}
int n;
void solve()
{
	cin>>n;
	while(n--)
	{
		int x;
		cin>>x;
		int l=1,r=2e9;
		while(l<r)
		{
			int mid=l+r>>1;
			if(mid*(2*mid-1)>=x)r=mid;
			else l=mid+1;
		}
		int sum=r*(2*r-1)-(r-1)*(2*(r-1)-1);//该周期个数 
		int now=x-(r-1)*(2*(r-1)-1);//该周期的第几个 
		int mid1=1+r*(2*r-1)-(r-1)*(2*(r-1)-1)>>1;
		int mid2=1+mid1>>1;
		int mid3=r*(2*r-1)-(r-1)*(2*(r-1)-1)+mid1>>1;
		int h=mid2-1;
		if(now<=mid2)cout<<now-1<<endl;
		else if(now<=mid1)
		{
			now-=mid2;
			cout<<h-now<<endl;
		}
		else if(now<=mid3)
		{
			now-=mid1;
			cout<<-now<<endl;
		}
		else if(now<=r*(2*r-1)-(r-1)*(2*(r-1)-1))
		{
			now-=mid3;
			cout<<now-h<<endl;
		}
	}
}
signed main() 
{
	io;
	int _;_=1;
	//cin>>_;
	while(_--)solve();
	return 0;
}

你可能感兴趣的:(算法,C++,算法,c++,数据结构)