2022/9/27(cf·div2#823)https://codeforces.com/contest/1730

https://codeforces.com/contest/1730/problem/A
贪心
多余c的肯定用c花费更少,小于c的1块1块的付花钱更少,等于随意

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int b[200005];

signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		map<int,int>mp;
		set<int>st;
		int n,x;
		cin>>n>>x;
		for(int i=0;i<n;i++)
		{
			cin>>b[i];
			mp[b[i]]++;
			st.insert(b[i]);
		}
		
		int sum1=0;
		set<int>::iterator i;
		for(i=st.begin();i!=st.end();i++)
		{
			if(mp[*i]>=x)sum1+=x;
			else sum1+=mp[*i];
		}
		cout<<sum1<<endl;	
	}
}

https://codeforces.com/contest/1730/problem/B
二分答案或三分

#include<bits/stdc++.h>//三分
using namespace std;
#define int long long
#define endl '\n'
int b[200005],a[200005];
int n;
struct node
{
	long double x,c;
}t[200005];

bool cmp(node l,node r)
{
	if(l.x==r.x)
		return l.c<r.c;
	else return l.x<r.x;
}

long double cek(long double mid)
{
	long double res=0;
	for(int i=0;i<n;i++)
	{
		res=max(res,t[i].c+abs(mid-t[i].x));//根据题目推测出来
	}
	return res;//可以理解为最长花费的时间
}

signed main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int tt;
	cin>>tt;
	while(tt--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>t[i].x;
		for(int i=0;i<n;i++)
			cin>>t[i].c;
		//sort(t,t+n,cmp);
		
		long double l=0,r=1e9;
		while(l+1e-6<r)//加法更快
		{
			long double ll=l+(r-l)/3;
			long double rr=r-(r-l)/3;
			if(cek(rr)<cek(ll))l=ll;//三分直接赋值,无需+1e-6;
			else r=rr;
		}
		printf("%.5Lf\n",l);//注意是Lf
	}
}
#include<bits/stdc++.h>//二分
using namespace std;
#define int long long
int a[200005],b[200005];
int n;

double cek(double x)
{
	double res=0;
	for(int i=1;i<=n;i++)
	{
		res=max(res,b[i]+abs(x-a[i]));
	}
	return res;
}
signed main()
{
	int tt;
	cin>>tt;
	while(tt--)
	{
		//	int n;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<=n;i++)
			cin>>b[i];
		
		double l=0,r=1e9;
		while(r-l>1e-6)
		{
			double mid=(l+r)/2;
			if(cek(mid)>cek(mid+1e-6))l=mid+1e-6;
			else r=mid-1e-6;//注意这里是1e-6,不是1
		}
		printf("%.5lf\n",l);
	}
}

https://codeforces.com/contest/1730/problem/C

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int b[200005],a[200005];
int n;
struct node
{
	char c;
	int id;
}t[200005];

bool cmp(node l,node r)
{
	if(l.c==r.c)
		return l.id<r.id;
	else return l.c<r.c;
}

signed main()
{
	//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int tt;
	cin>>tt;
	while(tt--)
	{
		string s;
		cin>>s;
	
		for(int i=0;i<s.size();i++)
		{
			t[i].c=s[i];
			t[i].id=i;
		}
		sort(t,t+s.size(),cmp);//第一次从小到大排序
		int res=0;
		for(int i=0;i<s.size();i++)
		{
			if(t[i].id>=res)res=t[i].id;//这一步很关键,是需要自己想出来的点,大意是把无需变换的位置做好标记,为后面的比较做准备		
			else //说明这个字符原本在比它小的字符前,现在排序在后面,进行了操作
			{
				if(t[i].c<'9')t[i].c++;//小于9,则加1
			}
		}
		sort(t,t+s.size(),cmp);//必须再排一次序,例 33133,第一次为13333,有两个33会变成44,按这个排序会变为14433,所以还需要再排一次
		for(int i=0;i<s.size();i++)
			cout<<t[i].c;
		puts("");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值