week16--Codeforces杂题整理

其实这周代码源没写,,又是蓝桥杯又是周赛的,于是这篇周报就把之前做的Codeforces的一些杂题整理整理放上来吧。。。

Reorder the Array

题面翻译

题目大意:

给定一个序列,序列里的元素可以以任意顺序重新排好

序列里某位置排序后的数可能会大于原数,问这样的位置最多能有多少个

输入格式:

第一行一个整数 n n n,表示序列长度

第二行 n n n个整数,表示初始序列

输出格式:

一个整数,表示满足条件的位置最多能有多少个

题目描述

You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.

For instance, if we are given an array $ [10, 20, 30, 40] $ , we can permute it so that it becomes $ [20, 40, 10, 30] $ . Then on the first and the second positions the integers became larger ( $ 20>10 $ , $ 40>20 $ ) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $ 2 $ . Read the note for the first example, there is one more demonstrative test case.

Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.

输入格式

The first line contains a single integer $ n $ ( $ 1 \leq n \leq 10^5 $ ) — the length of the array.

The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_i \leq 10^9 $ ) — the elements of the array.

输出格式

Print a single integer — the maximal number of the array’s elements which after a permutation will stand on the position where a smaller element stood in the initial array.

样例 #1

样例输入 #1

7
10 1 1 1 5 5 3

样例输出 #1

4

样例 #2

样例输入 #2

5
1 1 1 1 1

样例输出 #2

0

提示

In the first sample, one of the best permutations is $ [1, 5, 5, 3, 10, 1, 1] $ . On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.

In the second sample, there is no way to increase any element with a permutation, so the answer is 0.

这题可以 O(n) 处理,不用二分
首先很明显肯定是大的数对应原本小的数,将数组从大到小排个序先
我们可以认为操作是这样的:第一大的数对应第二大的,第二大的数对应第三大的,以此类推
但是有些数字是相同的,所以我们不能这么做
用一个 l 来表示当前被用来对应的数字用到了第几个,
O(n) 扫一遍就行了
如果当前可以对应就对应,如果当前不能就不管这个数了
但是很不幸排序是 nlog n 的

#include 
using namespace std;


int n,temp;
int a[100005];

int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++) cin >> a[i];
    
    sort(a, a + n);
    
    for(int i = 0; i < n; i ++)
    {
        if(a[temp] < a[i]) temp ++;
    }
    cout <<temp;
    return 0;
}

Follow Traffic Rules

题面翻译

两个城市之间有一条直达路,路上只放了一个限速牌。限速牌只限制它所在那一点的速度,驶过限速牌可以以任意速度行驶。

市民们的汽车加(减)速度恒为 a   k m / h 2 a \ \mathrm{km/h^2} a km/h2,且最大速度为 v   k m / h v \ \mathrm{km/h} v km/h。路总长为 l   k m l \ \mathrm{km} l km。限速牌限速为 w   k m / h w \ \mathrm{km/h} w km/h,被放在距离起点城市 d   k m d \ \mathrm{km} d km 的位置上。汽车在起点速度为 0 0 0。要求找到市民们在最优情况下,通过该路的最短时间。

汽车可以以任意速度进入终点城市。

输入
第一行为两个整数 a a a v v v。( 1 ⩽ a , b ⩽ 10 4 1\leqslant a,b\leqslant {10}^4 1a,b104
第二行为三个整数 l l l d d d w w w。( 2 ⩽ l ⩽ 10 4 2\leqslant l\leqslant {10}^4 2l104 1 ⩽ d < l 1\leqslant d1d<l 1 ⩽ w ⩽ 10 4 1\leqslant w\leqslant {10}^4 1w104

输出
共一行一个数,即答案,至少精确到小数点后 5 5 5 位。

题目描述

Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road’s traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.

It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of $ a $ km/h $ ^{2} $ , and has maximum speed of $ v $ km/h. The road has the length of $ l $ km, and the speed sign, limiting the speed to $ w $ km/h, is placed $ d $ km ( $ 1<=d

The car can enter Bercouver at any speed.

输入格式

The first line of the input file contains two integer numbers $ a $ and $ v $ ( $ 1<=a,v<=10000 $ ). The second line contains three integer numbers $ l $ , $ d $ and $ w $ ( $ 2<=l<=10000 $ ; $ 1<=d

输出格式

Print the answer with at least five digits after the decimal point.

样例 #1

样例输入 #1

1 1
2 1 3

样例输出 #1

2.500000000000

样例 #2

样例输入 #2

5 70
200 170 40

样例输出 #2

8.965874696353

纯折磨人的模拟题,,放上来留个纪念也行

#include 
using namespace std;


int main()
{
    double a,v,l,d,w;
    cin>>a>>v>>l>>d>>w;
    double x=v*v/2/a;
    if(v<=w)
    {
        if(l<=x)
            cout<<fixed<<setprecision(6)<<sqrt(2*l/a)<<endl;
        else
            cout<<fixed<<setprecision(6)<<v/a+(l-x)/v<<endl;
    }
    else
    {
        if(d<=x+(v*v-w*w)/2/a)
        {
            if(sqrt(2*a*d)<=w)
            {
                if(l<=x)
                    cout<<fixed<<setprecision(6)<<sqrt(2*l/a)<<endl;
                else
                    cout<<fixed<<setprecision(6)<<v/a+(l-x)/v<<endl;
            }
            else
            {
                double t1=2*sqrt(w*w/2/a/a+d/a)-w/a;
                x=(v*v-w*w)/a/2;
                if(l-d<=x)
                    cout<<fixed<<setprecision(6)<<t1+(-w+sqrt(w*w+2*a*(l-d)))/a<<endl;
                else
                    cout<<fixed<<setprecision(6)<<t1+(v-w)/a+(l-d-x)/v<<endl;                
            }
        }
        else
        {
            double t1=v/a+(d-x-(v*v-w*w)/2/a)/v+(v-w)/a;
            x=(v*v-w*w)/a/2;
            if(l-d<=x)
                cout<<fixed<<setprecision(6)<<t1+(sqrt(w*w+2*a*(l-d))-w)/a<<endl;
            else
                cout<<fixed<<setprecision(6)<<t1+(v-w)/a+(l-d-x)/v<<endl;
        }
    }
    return 0;
}

pSort

题面翻译

题目描述

给定一个长度为 n n n 的数列 { a n } \{a_n\} {an},初始时 { a n } = { 1 , 2 , … , n } \{a_n\} = \{1, 2, \dots, n\} {an}={1,2,,n}。位置 i i i 上的数可以和位置 i ± d i i \pm d_i i±di 上的数交换。给定一个 1 ∼ n 1 \sim n 1n 的全排列,问初始的数列可否交换成给定的形式。

输入格式

第一行一个整数 n n n

第二行 n n n 个互不相同的整数表示目标数列。

第三行 n n n 个整数表示 d 1 , d 2 , … , d n d_1, d_2, \dots, d_n d1,d2,,dn

输出格式

如果能交换到给定样式,输出 YES,否则输出 NO

数据范围

1 ≤ n ≤ 100 1 \le n \le 100 1n100

题目描述

One day $ n $ cells of some array decided to play the following game. Initially each cell contains a number which is equal to it’s ordinal number (starting from $ 1 $ ). Also each cell determined it’s favourite number. On it’s move $ i $ -th cell can exchange it’s value with the value of some other $ j $ -th cell, if $ |i-j|=d_{i} $ , where $ d_{i} $ is a favourite number of $ i $ -th cell. Cells make moves in any order, the number of moves is unlimited.

The favourite number of each cell will be given to you. You will also be given a permutation of numbers from $ 1 $ to $ n $ . You are to determine whether the game could move to this state.

输入格式

The first line contains positive integer $ n $ ( $ 1<=n<=100 $ ) — the number of cells in the array. The second line contains $ n $ distinct integers from $ 1 $ to $ n $ — permutation. The last line contains $ n $ integers from $ 1 $ to $ n $ — favourite numbers of the cells.

输出格式

If the given state is reachable in the described game, output YES, otherwise NO.

样例 #1

样例输入 #1

5
5 4 3 2 1
1 1 1 1 1

样例输出 #1

YES

样例 #2

样例输入 #2

7
4 3 5 1 2 7 6
4 6 6 1 6 6 1

样例输出 #2

NO

样例 #3

样例输入 #3

7
4 2 5 1 3 7 6
4 6 6 1 6 6 1

样例输出 #3

YES
#include 
using namespace std;

int n;
int p[105];
int ans[105];
bool flag;

int find(int x)
{
    if(x != p[x]) return find(p[x]);
    return p[x];
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++) p[i] = i;
    for(int i = 1; i <= n; i ++) cin >> ans[i];
    
    for(int i = 1; i <= n; i ++)
    {
        int temp;
        cin >> temp;
        if(i - temp >= 1) p[find(i - temp)] = p[find(i)];
        if(i + temp <= n) p[find(i + temp)] = p[find(i)];
    }
    
    for(int i = 1; i <= n; i ++)
    {
        if(find(i) != find(ans[i]))
        {
            flag = 1;
            break;
        }
    }
    if(flag) cout << "NO";
    else cout << "YES";
    return 0;
}

Jeopardy!

题面翻译

题意简述

“Jeopardy!”的决赛将有n问题,每个问题都有对应的得分ai,其中有m个问题可以选择不得分,而将现有总得分翻倍。你可以安排关卡的通过顺序和策略,求最大得分。

输入输出格式

输入格式:

第一行包含两个整数。n和m(1<=n,m<=100;m<=min(n,30))分别代表问题总数和可翻倍问题总数。第二行包含n个整数a1,a2,…,an(1<=ai<=1e7)代表每个问题的价值;第三行包含m个整数b1,b2,…,bm(1<=bi<=n)代表可翻倍问题的编号。问题编号是从1到n。

输出格式:

一行一个数字,表示通过所有关卡最大得分。保证该答案在64位带符号整型范围内。

感谢@Peter_Matthew 提供的翻译

题目描述

‘Jeopardy!’ is an intellectual game where players answer questions and earn points. Company Q conducts a simplified ‘Jeopardy!’ tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2.

The finals will have $ n $ questions, $ m $ of them are auction questions and $ n-m $ of them are regular questions. Each question has a price. The price of the $ i $ -th question is $ a_{i} $ points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player’s points by the value of the question price.

The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again.

All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve.

输入格式

The first line contains two space-separated integers $ n $ and $ m $ $ (1<=n,m<=100; m<=min(n,30)) $ — the total number of questions and the number of auction questions, correspondingly. The second line contains $ n $ space-separated integers $ a_{1},a_{2},…,a_{n} $ $ (1<=a_{i}<=10^{7}) $ — the prices of the questions. The third line contains $ m $ distinct integers $ b_{i} $ $ (1<=b_{i}<=n) $ — the numbers of auction questions. Assume that the questions are numbered from $ 1 $ to $ n $ .

输出格式

In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type.

样例 #1

样例输入 #1

4 1
1 3 7 5
3

样例输出 #1

18

样例 #2

样例输入 #2

3 2
10 3 8
2 3

样例输出 #2

40

样例 #3

样例输入 #3

2 2
100 200
1 2

样例输出 #3

400

一眼贪心,,,

#include 
using namespace std;


int n,m,temp;
long long sum;
int w[105];
bool judge[105];
vector<int> v;

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i ++)
    {
        scanf("%d", &w[i]);
    }
    
    for(int i = 1; i <= m; i ++)
    {
        scanf("%d", &temp);
        judge[temp] = 1;
        v.push_back(w[temp]);
    }
    
    for(int i = 1; i <= n; i ++)
    {
        if(!judge[i]) sum += w[i];
    }
    
    sort(v.begin(),v.end());
    
    for(int i = v.size() - 1; i >= 0; i --)
    {
        sum = max(sum + v[i], sum * 2);
    }
    
    cout << sum;
    return 0;
}

确实太水了,,我自己都看不下去了,,下个星期我一定好好做题。。。

你可能感兴趣的:(c++,算法,蓝桥杯,c++)