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("");
}
}