Educational Codeforces Round 3

---恢复内容开始---

A. USB Flash Drives

水题,排序即可

int a[1111];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    int ans=0; sort(a,a+n);
    for(int i=n-1;m>0;i--)
        ans++,m-=a[i];
    cout<<ans<<endl;
}

B. The Best Gift

本来以为要缩点,结果m<10,具体看代码

int x,n,m,b[11];
ll sum;
int main()
{
    memset(b,0,sizeof(b)); sum=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&x),b[x]++,sum++;
    ll ans=0;
    for(int i=1;i<=m;i++) {
        sum-=b[i];
        ans+=sum*b[i];
    }
    cout<<ans<<endl;
}

C. Load Balancing

依旧是水题。。

typedef long long ll;
int a[111111];
int main()
{
    ll ans=0,sum=0;
    int n; scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]),sum+=a[i];
    sort(a,a+n);
    ll sa=sum/n+1,sb=sum/n;
    int na=sum%n,nb=n-sum%n;
    //cout<<na<<nb<<sa<<sb<<endl;
    for(int i=0;i<n;i++) {
        if(nb&&a[i]<=sb)
            nb--,ans+=sb-a[i];
        else if(na&&a[i]<=sa)
            na--,ans+=sa-a[i];
    }
    cout<<ans<<endl;
}

D. Gadgets for dollars and pounds

题目读起来很麻烦,而且刚开始还理解错题意了。。以为每件物品既可以用英镑支付又可以用美金支付,

写完才发现样例过不了。

题目一看就是二分,第一反应以为要使用RMQ,后来发现不用也行。

首先把用美金和英镑的分开并排序,然后对于天数二分,在天数区间内遍历

得到最低美金兑换的日子,和英镑的日子,然后比较后选择话费最少burles的gadget购买

typedef long long ll;
typedef pair<int,int> pii ;

#define N 200089
#define M 400009
#define f0(i,n) for(int i=0;i<(n);i++)
#define ff(i,n) for(int i=1;i<=(n);i++)
#define p_b push_back
#define m_p make_pair
#define Abs(a) (a)<0?-(a):(a)
#define Clr(a) memset(a,0,sizeof(a))
#define MID(a,b) (a+((b-a)>>1))
#define Lbit(a) (x)&(-(x))
#define X first
#define Y second
const ll INF=1LL<<60-1;
const int lim=1<<25+1; 

int n,m,k,s;
int a[N],b[N];
vector<pii> d,p;
vector<pii> ans,tans;

int judge(int day) {
    tans.clear();
    int d0=1,d1=1;
    for(int i=1;i<=day;i++)     {
        if(a[i]<a[d0]) d0=i;
        if(b[i]<b[d1]) d1=i;
    }
    int cnt=k,pd=0,pp=0;
    ll sb=s;
    while(cnt) {
        ll vd=INF,vp=INF;
        if(pd<d.size())
            vd=1LL*a[d0]*d[pd].X;
        if(pp<p.size())
            vp=1LL*b[d1]*p[pp].X;

        if(vd<vp) {
            //ans[d[pd].Y]=d0;
            tans.p_b(m_p(d[pd].Y,d0));
            cnt--; sb-=vd; pd++;
        }
        else {
            //ans[p[pp].Y]=d1;
            tans.p_b(m_p(p[pp].Y,d1));
            cnt--; sb-=vp; pp++;
        }
    }
//    cout<<day<<" "<<sb<<" "<<cnt<<endl;
    if(sb>=0) {
        ans=tans;  return 1;
    }
    return 0;
}
bool cmp(pii c1,pii c2) {  return c1.X<c2.X; }

int main()
{

    int x,y,z;
    cin>>n>>m>>k>>s;
    d.clear();  p.clear();
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    for(int i=1;i<=m;i++) {
        scanf("%d%d",&x,&y);
        if(x==1) d.p_b(m_p(y,i));
        else p.p_b(m_p(y,i));
    }
    sort(d.begin(),d.end(),cmp);
    sort(p.begin(),p.end(),cmp);
    int l=1,r=n,mid,res,flag=0;
    while(l<=r) {
        mid=(l+r)>>1;
        if(judge(mid)) {
            flag=1; res=mid; r=mid-1;
        }
        else l=mid+1;
        //cout<<l<<" "<<r<<endl;
    }
    if(flag) {
        printf("%d\n",res);
        for(int i=0;i<ans.size();i++)
            printf("%d %d\n",ans[i].X,ans[i].Y);
    }
    else puts("-1");
    return 0;
}

---恢复内容结束---

时间: 2024-08-24 03:47:06

Educational Codeforces Round 3的相关文章

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(

Educational Codeforces Round 26 D dp,思维

Educational Codeforces Round 26 D. Round Subset 题意:有 n 个数,从中选出 k 个数,要使这 k 个数的乘积末尾的 0 的数量最多. tags:dp好题 dp[i][j][l] 表示前 i 个数,选取了其中 j 个数,分解因子后有 l 个 5时,最多有多少个 2 .i 这一维明显可以省略. 这题一开始有个地方写挫了..选取 j 个数时,应该反着来,即 for( j, k, 1) ,不是 for( j, 1, k) ,不然会多算. #include

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Educational Codeforces Round 23 D. Imbalanced Array(单调栈)

题目链接:Educational Codeforces Round 23 D. Imbalanced Array 题意: 给你n个数,定义一个区间的不平衡因子为该区间最大值-最小值. 然后问你这n个数所有的区间的不平衡因子和 题解: 对每一个数算贡献,a[i]的贡献为 当a[i]为最大值时的 a[i]*(i-l+1)*(r-i+1) - 当a[i]为最小值时的a[i]*(i-l+1)*(r-i+1). 计算a[i]的l和r时,用单调栈维护.具体看代码,模拟一下就知道了. 然后把所有的贡献加起来.

Educational Codeforces Round 25 F. String Compression(kmp+dp)

题目链接:Educational Codeforces Round 25 F. String Compression 题意: 给你一个字符串,让你压缩,问压缩后最小的长度是多少. 压缩的形式为x(...)x(...)  x表示(...)这个出现的次数. 题解: 考虑dp[i]表示前i个字符压缩后的最小长度. 转移方程解释看代码,这里要用到kmp来找最小的循环节. 当然还有一种找循环节的方式就是预处理lcp,然后通过枚举循环节的方式. 这里我用的kmp找的循环节.复杂度严格n2. 1 #inclu

Educational Codeforces Round 23 E. Choosing The Commander (trie)

题目链接: Educational Codeforces Round 23 E. Choosing The Commander 题意: 一共有n个操作. 1.  插入一个数p 2.  删除一个数p 3.  询问有多少个数 使得 x^p<l 题解: 对于前两种操作用01trie就能解决. 对于对三个操作,我们考虑在trie上搜索. 1.  当l的bit位是1时,那边bit位是p的字数全部的数都会小于l,(因为p^p=0) 2.  当l的bit为是0时,那边只能向bit位是p的子树中搜. 这样算下来

CodeForces - 837E - Vasya&#39;s Function | Educational Codeforces Round 26

/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b-gcd(a, b)); 求 f(a, b) , a,b <= 1e12 分析: b 每次减 gcd(a, b) 等价于 b/gcd(a,b) 每次减 1 减到什么时候呢,就是 b/gcd(a,b)-k 后 不与 a 互质 可先将 a 质因数分解,b能除就除,不能

Educational Codeforces Round 22 E. Army Creation(主席树)

题目链接:Educational Codeforces Round 22 E. Army Creation 题意: 给你n个数和一个数k,然后有q个询问. 每个询问 有一个区间[l,r],问你这个区间内在满足每一种数不超过k的情况下,最大能选多少个数出来. 强制在线. 题解: 一看就要用到主席树,和主席数求区间内有多少不同的数的个数处理方法相同. 依次将每个数插入,当这个数出现的个数等于k了,就把最前面的那个数删掉. 然后询问就访问root[r]就行了. 第一次写完数据结构没有调试一遍过样例,一