Educational Codeforces Round 3 个人总结A-D
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的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- 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 ...
- 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 ...
随机推荐
- [转]Windows 批处理命令教程
第一章 批处理基础 第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令统称批处理命令.小知识:可 ...
- 提取可执行文件中的调试信息 objcopy --only-keep-debug app app.debug
https://blog.csdn.net/CaspianSea/article/details/17269977 set-debug-directory show-debug-directory i ...
- eclipse设置UTF-8(字符编码集)所有方式
一.全部方式 1.修改workspace编码 windows->preferences->gengral->workspace 选择other->UTF-8. 2.修改Con ...
- ansible笔记第二章(ansible-varable变量)
(1)变量类型 1.1在playbook文件中的play使用变量 [root@m01 project1]# cat vars_1.yml - hosts: oldboy vars: - web_p ...
- SQLSERVER自动备份数据库
1. 通过操作系统的定时任务执行 创建两个文件,auto.bat和auto.sql,使用bat调用sql文件中的代码段 auto.bat内容 sqlcmd -S localhost,2433 -U s ...
- Python-celery介绍与快速上手
1.celery介绍: celery是一个基于Python开发的模块,可以帮助我们在开发过程中,对任务进行分发和处理. 详细介绍取自:Python之celery的简介与 ...
- ESP_IDF中使用TFT_eSPI库驱动ST7789V
前言: 想学习创建好看的菜单界面很久了,寒假在家正好有时间,手中恰好有一块ST7789的tft屏幕,正好拿来练练手. Step 1: 在github中找到TFT_eSPI库(网址:docs · mas ...
- OpenMP fortran 学习
参考自TAMU的PPThttps://people.math.umass.edu/~johnston/PHI_WG_2014/OpenMPSlides_tamu_sc.pdf 什么是OpenMP 在C ...
- kmp失效函数(测试博客的编辑器)
1 int *find_next(char *p) 2 { 3 int k = -1; //right shift i-k 4 int i = 0; 5 int m = strlen(p); 6 in ...
- C# DateTime转换为字符串
12小时制:DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") 24小时制:DateTime.Now.ToString("yyyy- ...