Educational Codeforces Round 3

A. USB Flash Drives

  • 降序排序后,贪心,甚至不会爆longlong
void solve()
{
int n,m;
cin>>n>>m;
vector<int> a(n);
for(int i=0;i<n;i++)
cin>>a[i];
sort(all(a));reverse(all(a));
int s=0,ans=0;
for(int i=0;i<n;i++)
{
if(s>=m) break;
s+=a[i],ans++;
}
cout<<ans<<endl;
return;
}

B. The Best Gift

  • 大意:有多少种互不相同不同的组合
  • 用map记录这个类型出现的次数
  • 一本书的贡献为 与这本书不同类型的数量
void solve()
{
int n,m;
cin>>n>>m;
LL ans=0,s=0;
vector<int> a(n);
map<int,int> mp;
for(int i=0;i<n;i++)
{
cin>>a[i];
mp[a[i]]++;
s++;
}
for(auto it:mp)
{
ans+=(it.se*(s-it.se));
}
cout<<ans/2ll<<endl; return;
}

C. Load Balancing

  • 大意:求让最大值和最小值的差最小的操作次数

  • 对数组进行排序,使之升序

  • \(s\) 为所有数之和,\(k\) 代表平均数,\(r\) 代表余数

  • $k = \frac{s}{n} $, $ r =s\%n $

  • 当 $ r=0 $ ,最大值和最小值的差为 \(0\),答案即为\(\sum_{i=1}^n \max(a_i-k,0)\)

  • 当 $ r=1 $ ,最大值和最小值的差为 \(1\),答案即为\(\sum_{i=1}^{n-r} \max(a_i-k,0)+\sum_{i=n-r+1}^{n} \max(a_i-k-1,0)\)

LL a[N],b[N];
void solve()
{
LL n;
cin>>n;
LL ans=0,s=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
s+=a[i];
}
sort(a+1,a+1+n);
LL k=s/n,r=s%n;
for(int i=1;i<=n;i++)
b[i]=k;
for(int i=n;i>=n-r+1;i--)
{
b[i]++;
}
for(int i=1;i<=n;i++)
ans+=max(a[i]-b[i],0ll); cout<<ans<<endl;
return;
}

D. Gadgets for dollars and pounds

  • 二分,前缀和,贪心

  • 大意:在 \(n\) 天中买东西,每天有不同的汇率,\(m\) 个商品,只能使用pounds或dollars,要买 \(k\) 个,但只有 \(s\) 元钱,判断能不能买,能输出哪天能买完,和哪一天买第几个商品

  • 在 \(1,...,n\) 天中,若第 \(x\) 天能买完 \(k\) 个,那 第 \(k+1\) 天也能买完,也就是在 \(1,...,n\) 天中找到最小满足条件的 \(x\),且第 \(x-1\) 天不能买完,这是有单调性的,所以我们二分哪天能买完

  • check中,在 \(1,...,x\) 天中找到最小pounds和dollars的汇率分别是\(a1\) 和 \(b1\),设消费为 \(sum\) 。题目并没有限制每天购买商品的数量,我们等到汇率最小那天买完是最划算的贪心

  • 这里就有一个问题,如何去确定换了前去买用pounds,还是用dollars的?

  • 因为买\(k\) \((1 ≤ k ≤ 2·10^5)\) 个商品,可以对pounds,和dollars进行排序,用前缀和处理

  • \(sum=s1[x]·a1+s2[y]·b1\) \((0 ≤ x ≤ \text{pounds商品个数},0 ≤ y ≤ \text{dollars商品个数},x+y=k)\),运用前缀和就可以在\(O(k)\)的时间内处理出第 \(x\) 天结束的最小花销

  • 剩下的输出方案,记录下最小花销下的 \(x\) 、 \(y\) 和 pounds和dollars 汇率最小的日子就很容易解决了

代码有点丑QAQ

const int N = 200010;

LL n,m,k,s,a[N],b[N];
int id,d1,d2;
LL cnt1=0,cnt2=0,s1[N],s2[N]; PII c1[N],c2[N]; bool check(LL x)
{
LL a1=1e18,b1=1e18;
for(int i=1;i<=x;i++)
{
if(a[i]<a1)
d1=i,a1=a[i];
if(b[i]<b1)
d2=i,b1=b[i];
} LL sum=1e18;
for(int i=0;i<=k;i++)
{
if( !(i<=cnt1&&k-i<=cnt2)) continue;
LL t1=s1[i],t2=s2[k-i];
LL r1=t1*a1,r2=t2*b1;
if(r1+r2<sum)
sum=r1+r2,id=i;
}
if(sum<=s) return true;
else return false;
}
void solve()
{
cin>>n>>m>>k>>s;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=m;i++)
{
LL t,cc; cin>>t>>cc;
if(t==1) c1[++cnt1].fi=cc,c1[cnt1].se=i;
else
c2[++cnt2].fi=cc,c2[cnt2].se=i;
}
sort(c1+1,c1+1+cnt1);
sort(c2+1,c2+1+cnt2);
for(int i=1;i<=cnt1;i++)
s1[i]=s1[i-1]+c1[i].fi;
for(int i=1;i<=cnt2;i++)
s2[i]=s2[i-1]+c2[i].fi;
if(check(n)==false)
{
cout<<-1<<endl; return;
} int l=1,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
if(check(l))
{
cout<<l<<endl;
vector<PII> ans;
int j=0;
for(int i=1;i<=id;i++)
ans.pb({d1,c1[++j].se});
j=0;
for(int i=id+1;i<=k;i++)
ans.pb({d2,c2[++j].se});
for(auto &it:ans)
cout<<it.se<<" "<<it.fi<<endl;
}
return;
}

Educational Codeforces Round 3 个人总结A-D的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  10. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

随机推荐

  1. read the docs

    1. 在 GitHub 新建一个repo 2. git clone git@github.com:readthedocs/tutorial-template.git 3. 把刚刚 clone 的 re ...

  2. P1219 [USACO1.5]八皇后 Checker Challenge

    好长时间没登博客园了,今天想起了账号密码,遂发一篇题解 最近因为复赛正在复健搜索,所以做了这道题 这道题说难并不是很难,但是在于这个题需要找到两个规律 以下是原题 [USACO1.5]八皇后 Chec ...

  3. 日志服务化&可视化&统计化

    概述: ELK是Elasticsearch(简称es).Logstash.Kibana的简称,这三者是核心套件,但并非全部. Filebeat 日志采集工具  Logstash数据处理引擎   ela ...

  4. Win11右键默认显示更多选项的设置

    怎么让Win11右键默认显示更多选项?有很多朋友不喜欢win11系统的右键菜单显示,经常需要多点一次"显示更多选项"才能看到想要的内容,大家想知道如何让win11右键菜单默认显示更 ...

  5. JS字符串拼接的方法及性能比较

    一.+和+=str += "one" + "two";这段代码在运行过程中,会经历四个步骤:1.在内存中创建一个临时字符串2.将连接后的字符串"one ...

  6. CocosCreator基于jenkins自动构建

    1.新建Item,输入名称后选择Freestyle project后点击确定 2.配置项目,自定义工作目录 3.配置源码管理和要摘取的分支 4.构建触发器选择github触发 5.构建选择执行wind ...

  7. bottle库上传文件

    安装bottle库 pip install bottle 上传代码 import bottle @bottle.get('/upload') def upload_get(): return bott ...

  8. kotlin channel使用注意点

    kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed at kotlinx.coroutines.cha ...

  9. webpack从零开始打造react项目(更新中...)

    创建项目 创建文件夹 webpack-test  使用编辑器打开文件夹,我们初始化管理包 npm init -y 生成一个默认的 pageage.json 文件 要想创建react项目,思考我们之前使 ...

  10. css布局、动画要点

    background属性 属性解释background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设 ...