Codeforces Round #311 (Div. 2)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #311 (Div. 2)相关的知识,希望对你有一定的参考价值。

我仅仅想说还好我没有放弃,还好我坚持下来了。

最终变成蓝名了,或许这对非常多人来说并不算什么。可是对于一个打了这么多场才好不easy加分的人来说,我真的有点激动。

心脏的难受或许有点是由于晚上做题时太激动了,看别人过得那么快自己也紧张了。

后来看到有非常多大神给我的建议是,刚開始学习的人,手速并非唯一重要的关键,掌握算法,一步步系统的完好它才是最重要的。

所以这一回也没有太紧张,也没有做一题看一下排名。

然后居然第一次进了前1000。rating一下子加了220多。

希望这是一个好的开端,我还想继续下去,变得更强大!



题解:(由于C题我不会,所以把它写在了最前面)

C. Arthur and Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

In total the table Arthur bought has n legs, the length of the i-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

Input

The first line of the input contains integer n (1?≤?n?≤?105) — the initial number of legs in the table Arthur bought.

The second line of the input contains a sequence of n integers li (1?≤?li?≤?105), where li is equal to the length of the i-th leg of the table.

The third line of the input contains a sequence of n integers di (1?≤?di?≤?200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

Output

Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

Sample test(s)
input
2
1 5
3 2
output
2
input
3
2 4 4
1 1 1
output
0
input
6
2 2 1 1 3 3
4 3 5 5 2 1
output
8


题意:

xx人他买了一个不是脚的长度长短不一的桌子,它的桌子有n个脚。然后每一个脚的长度分别为l[i]。

每次移动掉第i个脚所花费的力气为d[i]。

要使桌子满足稳定的条件为:

1)它是一仅仅脚的,那么它肯定是稳的

2)它是2仅仅脚的,那么假设两条腿长度都同样的话,那么就说明它是稳的

3)它是k仅仅脚的,那么当脚的长度等于如今桌子中所含的最大长度的数量大于k/2时,则说明它也是稳的

然后要你求出使得桌子变得稳定的它所要付出的最少的力气。

题解:

这道题我是看别人代码,然后理解后才打出来的。不得不说别人的脑回路还真是复杂,都能写出这么复杂的代码。而我如今还仅仅在刷刷水题,(太弱了。

。。

我就大概讲一下核心的思路,其余的都在代码中凝视的比較具体了:

int ans=inf;
    for(itr=mp.begin();itr!=mp.end();itr++){	//枚举所出现过的长度 
        int l=(*itr).first;		//l中存的是长度 
        int num=(*itr).second;	//num中存的是出现过的次数 
        int power=s[l];		//power中存的是这个长度所须要的力量数 
        int all=num;		//all中临时储存了出现过的次数 
        for(int i=200;i>=1;i--){		//对力量进行暴力枚举 
            int nn=lower_bound(G[i].begin(),G[i].end(),l)-G[i].begin();	//这里是为了找到小于等于l的数有几个 
            all+=nn;
            power+=nn*i;
            if(all>=num*2){
                power-=i*(all-num*2+1);
                break;
            }
        }
        ans=min(ans,sum-power);
    }

首先这里的目的是为了求出所须要的最小的力量度。那么我们讲一下大概的思路:

我们的想法是,如果每次l都是最大的长度。然后我们在再满足题目中条件的情况下去求力量数。

这里我们採用了暴力和贪心的想法,我们 对力量进行枚举,我们要尽可能的先贪心的选取较大的力量(也就是说我们选进去的腿是不用被拆掉的)。

然后这里能够用二分求出在当前力量下小于等于l长度的有多少个。

当满足all>=num*2时,说明我们已经选多了,所以我们要减去一些腿,那么减去哪些呢?(和上面一样,也是贪心的减去当前力量下的那些桌子腿,由于它们所消耗的力量比較少。。。

由于我们这里求的是不须要减去的木棍,所以我们要求的是力量的最大值。所以题目要求的力量的最小值就是sum-power了。对吧。。

唔。。。

这里还用了vector,还有迭代器,总之STL用的巧那自然是十分好的。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
#define maxn 111111
#define inf 999999999
vector<int> G[222]; //是以能量作为划分标准的; 
map<int,int> mp;
map<int,int>::iterator itr;
int s[maxn];
struct node{
    int l,d;
}a[maxn];
int main(){
    int n;
    scanf("%d",&n);
    int sum=0;
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].l);
        mp[a[i].l]++;		//mp数组是为了记录出现的次数的 
    }
    for(int i=0;i<n;i++){
        scanf("%d",&a[i].d);
        sum+=a[i].d;  	//sum中保存的是总共的力量数 
        s[a[i].l]+=a[i].d;	//s数组中存的是每一种长度所须要的力量数 
        G[a[i].d].push_back(a[i].l);	//G数组把所需相同多的力量的长度都存在一起 
    }
    for(int i=1;i<=200;i++){
        sort(G[i].begin(),G[i].end());	//对G依照相同力量,长度从小到大開始排序 
    }
    int ans=inf;
    for(itr=mp.begin();itr!=mp.end();itr++){	//枚举所出现过的长度 
        int l=(*itr).first;		//l中存的是长度 
        int num=(*itr).second;	//num中存的是出现过的次数 
        int power=s[l];		//power中存的是这个长度所须要的力量数 
        int all=num;		//all中临时储存了出现过的次数 
        for(int i=200;i>=1;i--){		//对力量进行暴力枚举 
            int nn=lower_bound(G[i].begin(),G[i].end(),l)-G[i].begin();	//这里是为了找到小于等于l的数有几个 
            all+=nn;
            power+=nn*i;
            if(all>=num*2){
                power-=i*(all-num*2+1);
                break;
            }
        }
        ans=min(ans,sum-power);
    }
    printf("%d\n",ans);
}
/*
6
2 2 1 1 3 3
4 3 5 5 2 1
*/



A. Ilya and Diplomas
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.

At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one diploma.

They also decided that there must be given at least min1 and at most max1 diplomas of the first degree, at least min2 and at mostmax2 diplomas of the second degree, and at least min3 and at most max3 diplomas of the third degree.

After some discussion it was decided to choose from all the options of distributing diplomas satisfying these limitations the one that maximizes the number of participants who receive diplomas of the first degree. Of all these options they select the one which maximizes the number of the participants who receive diplomas of the second degree. If there are multiple of these options, they select the option that maximizes the number of diplomas of the third degree.

Choosing the best option of distributing certificates was entrusted to Ilya, one of the best programmers of Berland. However, he found more important things to do, so it is your task now to choose the best option of distributing of diplomas, based on the described limitations.

It is guaranteed that the described limitations are such that there is a way to choose such an option of distributing diplomas that all nparticipants of the Olympiad will receive a diploma of some degree.

Input

The first line of the input contains a single integer n (3?≤?n?≤?3·106) — the number of schoolchildren who will participate in the Olympiad.

The next line of the input contains two integers min1 and max1 (1?≤?min1?≤?max1?≤?106) — the minimum and maximum limits on the number of diplomas of the first degree that can be distributed.

The third line of the input contains two integers min2 and max2 (1?≤?min2?≤?max2?≤?106) — the minimum and maximum limits on the number of diplomas of the second degree that can be distributed.

The next line of the input contains two integers min3 and max3 (1?≤?min3?≤?max3?≤?106) — the minimum and maximum limits on the number of diplomas of the third degree that can be distributed.

It is guaranteed that min1?+?min2?+?min3?≤?n?≤?max1?+?max2?+?max3.

Output

In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.

The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first degree. Of all the suitable options, the best one is the one which maximizes the number of participants who receive diplomas of the second degree. If there are several of these options, the best one is the one that maximizes the number of diplomas of the third degree.

Sample test(s)
input
6
1 5
2 6
3 7
output
1 2 3 
input
10
1 2
1 3
1 5
output
2 3 5 
input
6
1 3
2 2
2 2
output
2 2 2 
题目的大致意思是:

如今有三种层次的奖状,然后要满足每一种奖状都必须含有它的min值(同一时候也要<=max值),也就是说每一个奖状都必须有人获奖。

想法:

暴力分类就好

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define maxn 3333333
int main(){
    int n;
    int min[4],max[4];
    scanf("%d",&n);
    for(int i=1;i<=3;i++){
        scanf("%d%d",&min[i],&max[i]);
    }
    int sum=0;
    sum=n-(min[2]+min[3]);
    if(sum>max[1]){
        printf("%d ",max[1]);
        int sum2=n-max[1]-min[3];
        if(sum2>max[2]) {
            printf("%d ",max[2]);
            int sum3=n-max[2]-max[1];
            printf("%d\n",sum3);
        }
        else printf("%d %d\n",sum2,min[3]);
    }
    else printf("%d %d %d\n",sum,min[2],min[3]);
}

B. Pasha and Tea
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha‘s friends. The i-th cup can hold at most ai milliliters of water.

It turned out that among Pasha‘s friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:

  • Pasha can boil the teapot exactly once by pouring there at most w milliliters of water;
  • Pasha pours the same amount of water to each girl;
  • Pasha pours the same amount of water to each boy;
  • if each girl gets x milliliters of water, then each boy gets 2x milliliters of water.

In the other words, each boy should get two times more water than each girl does.

Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha‘s friends.

Input

The first line of the input contains two integers, n and w (1?≤?n?≤?1051?≤?w?≤?109) — the number of Pasha‘s friends that are boys (equal to the number of Pasha‘s friends that are girls) and the capacity of Pasha‘s teapot in milliliters.

The second line of the input contains the sequence of integers ai (1?≤?ai?≤?1091?≤?i?≤?2n) — the capacities of Pasha‘s tea cups in milliliters.

Output

Print a single real number — the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn‘t exceed 10?-?6.

Sample test(s)
input
2 4
1 1 1 1
output
3
input
3 18
4 4 4 2 2 2
output
18
input
1 5
2 3
output
4.5
题意:

xx有一个容量为w升的水壶(他最多仅仅有w升水。由于仅仅能煮一次水,题中说的),然后总共同拥有2n个人来參加他的party。

xx倒水有例如以下规则:

1)女孩子中所倒的水量是同样的

2)男孩子中所到的水量也是同样的

3)男孩子的获得水量是女孩子的2倍

然后输出一个实数,问你在满足上面的条件下,这2n个人他们所能获得的最多的水量是多少

题解:

事实上就是列简单的方程,然后求出约束条件就好(水题一枚~

哦。对了,最后好多人说都卡了精度。那么最后输出.7lf就好

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
#define maxn 111111
#define inf 999999999
__int64 a[maxn*2];
int main(){
    __int64 n,w;
    scanf("%I64d%I64d",&n,&w);
    for(int i=0;i<2*n;i++){
        scanf("%I64d",&a[i]);
    }
    sort(a,a+2*n);
    int min1=inf,min2=inf;
    for(int i=0;i<n;i++){
        if(a[i]<min1) min1=a[i];
    }
    for(int i=n;i<2*n;i++){
        if(a[i]<min2) min2=a[i];
    }
    double x;   //代表的是女生的喝水量。
    double max=0;   //zongde
    double t1,t2,t3;
    t1=(w*1.0)/(3.0*n);
    t2=min2*1.0/2.0;
    t3=min1*1.0;
    double tot=0;
    tot=min(t1,min(t2,t3));
    x=tot*n*1.0*3;
    printf("%.7lf\n",x); 
}

如今最终成为了expert。。。我一定还会再上去的。加油,hades!


以上是关于Codeforces Round #311 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #311 (Div. 2) D. Vitaly and Cycle(二分图染色,奇环)

Codeforces Round #436 E. Fire(背包dp+输出路径)

[ACM]Codeforces Round #534 (Div. 2)

Codeforces Round #726 (Div. 2) B. Bad Boy(贪心)

CodeForces - 311B:Cats Transport (DP+斜率优化)

Codeforces311 B. Cats Transport(斜率优化dp)