A. Non-zero

Description:

Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \dots, a_n\)]. In one step they can add \(1\) to any element of the array. Formally, in one step they can choose any integer index \(i\) (\(1 \le i \le n\)) and do \(a_i := a_i + 1\).

If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.

What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make \(a_1 + a_2 +\) \(\dots\) \(+ a_n \ne 0\) and \(a_1 \cdot a_2 \cdot\) \(\dots\) \(\cdot a_n \ne 0\).

Input:

Each test contains multiple test cases.

The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.

The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) — the size of the array.

The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-100 \le a_i \le 100\)) — elements of the array .

Output

For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

Sample Input:

4

3

2 -1 -1

4

-1 0 0 1

2

-1 2

3

0 -2 1

Sample Output:

1

2

0

2

思路:

遇0加1,和为零再加1.

AC代码:

  1. #include<bits/stdc++.h>
  2. const int mod = 1e9 + 7;
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. int s[150];
  7. int main() {
  8. int _,n;
  9. scanf("%d",&_);
  10. while(_--){
  11. scanf("%d",&n);
  12. int sum=0;
  13. for(int i=1;i<=n;++i){
  14. scanf("%d",&s[i]);
  15. sum+=s[i];
  16. }
  17. int ans=0;
  18. for(int i=1;i<=n;++i){
  19. if(s[i]==0) ans++,sum++;
  20. }
  21. if(sum==0){
  22. ans++;
  23. }
  24. printf("%d\n",ans);
  25. }
  26. return 0;
  27. }

B. Assigning to Classes

Description:

Reminder: the median of the array \([a_1, a_2, \dots, a_{2k+1}]\) of odd number of elements is defined as follows: let \([b_1, b_2, \dots, b_{2k+1}]\) be the elements of the array in the sorted order. Then median of this array is equal to \(b_{k+1}\).

There are \(2n\) students, the \(i\)-th student has skill level \(a_i\). It's not guaranteed that all skill levels are distinct.

Let's define skill level of a class as the median of skill levels of students of the class.

As a principal of the school, you would like to assign each student to one of the \(2\) classes such that each class has odd number of students (not divisible by \(2\)). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.

What is the minimum possible absolute difference you can achieve?

Input:

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.

The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the number of students halved.

The second line of each test case contains \(2n\) integers \(a_1, a_2, \dots, a_{2 n}\) (\(1 \le a_i \le 10^9\)) — skill levels of students.

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).

Output

For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.

Sample Input:

3

1

1 1

3

6 5 4 1 2 3

5

13 4 20 13 2 5 8 3 17 16

Sample Output:

0

1

5

思路:

中间两个数之差

AC代码:

  1. #include<bits/stdc++.h>
  2. const int mod = 1e9 + 7;
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. const int maxn = 2e5+7;
  7. int s[maxn];
  8. int main() {
  9. int _,n;
  10. scanf("%d",&_);
  11. while(_--) {
  12. scanf("%d", &n);
  13. n*=2;
  14. for (int i = 1; i <= n; ++i) {
  15. scanf("%d", &s[i]);
  16. }
  17. sort(s+1,s+1+n);
  18. printf("%d\n",s[n/2+1]-s[n/2]);
  19. }
  20. return 0;
  21. }

C. Anu Has a Function

Description:

Anu has created her own function \(f\): \(f(x, y) = (x | y) - y\) where \(|\) denotes the bitwise OR operation. For example, \(f(11, 6) = (11|6) - 6 = 15 - 6 = 9\). It can be proved that for any nonnegative numbers \(x\) and \(y\) value of \(f(x, y)\) is also nonnegative.

She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.

A value of an array \([a_1, a_2, \dots, a_n]\) is defined as \(f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)\) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?

Input:

The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).

The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)). Elements of the array are not guaranteed to be different.

Output

Output \(n\) integers, the reordering of the array with maximum value. If there are multiple answers, print any.

Sample Input:

4

4 0 11 6

Sample Output:

11 6 4 0

Sample Input:

1

13

Sample Output:

13

思路:

打一下表,发现只与第一个数有关

观察后发现,一个序列的价值等于:

设\(s[1]=a\),其他所有数的或运算为\(b\)

\(a-(a​\)&\(b)​\) 所以找独一无二的最大的二进制位,因为如果这个二进制位有多个,那就没有价值.

AC代码:

  1. #include<bits/stdc++.h>
  2. const int mod = 1e9 + 7;
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. const int maxn = 2e5+7;
  7. int s[maxn],n;
  8. #define f(x,y) (x|y)-y
  9. int check(){
  10. int ans=f(s[1],s[2]);
  11. for(int i=3;i<=n;++i){
  12. ans=f(ans,s[3]);
  13. }
  14. return ans;
  15. }
  16. int main() {
  17. scanf("%d",&n);
  18. for(int i=1;i<=n;++i){
  19. scanf("%d",&s[i]);
  20. }
  21. int a=-1;
  22. for(int j=30,cnt,b=-1;j>=0;--j){
  23. cnt=0;
  24. for(int i=1;i<=n;++i){
  25. if(s[i] >> j & 1){
  26. cnt++;
  27. b=i;
  28. }
  29. }
  30. if(cnt==1){
  31. a=b;
  32. break;
  33. }
  34. }
  35. if(a!=-1)printf("%d",s[a]);
  36. for(int i=1;i<=n;++i) if(i!=a) printf(" %d",s[i]);
  37. return 0;
  38. }

D. Aerodynamic

Description:

Guy-Manuel and Thomas are going to build a polygon spaceship.

You're given a strictly convex (i. e. no three points are collinear) polygon \(P​\) which is defined by coordinates of its vertices. Define \(P(x,y)​\) as a polygon obtained by translating \(P​\) by vector \(\overrightarrow {(x,y)}​\). The picture below depicts an example of the translation:

Define \(T\) as a set of points which is the union of all \(P(x,y)\) such that the origin \((0,0)\) lies in \(P(x,y)\) (both strictly inside and on the boundary). There is also an equivalent definition: a point \((x,y)\) lies in \(T\) only if there are two points \(A,B\) in \(P\) such that \(\overrightarrow {AB} = \overrightarrow {(x,y)}\). One can prove \(T\) is a polygon too. For example, if \(P\) is a regular triangle then \(T\) is a regular hexagon. At the picture below \(P\) is drawn in black and some \(P(x,y)\) which contain the origin are drawn in colored:

The spaceship has the best aerodynamic performance if \(P\) and \(T\) are similar. Your task is to check whether the polygons \(P\) and \(T\) are similar.

Input:

The first line of input will contain a single integer \(n\) (\(3 \le n \le 10^5\)) — the number of points.

The \(i\)-th of the next \(n\) lines contains two integers \(x_i, y_i\) (\(|x_i|, |y_i| \le 10^9\)), denoting the coordinates of the \(i​\)-th vertex.

It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.

Output

Output "YES" in a separate line, if \(P\) and \(T\) are similar. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower).

Sample Input:

4

1 0

4 1

3 4

0 3

Sample Output:

YES

Sample Input:

3

100 86

50 0

150 0

Sample Output:

nO

Sample Input:

8

0 0

1 0

2 1

3 3

4 6

3 6

2 5

1 3

Sample Output:

YES

思路:

首先这个题意太劝退了.

这个题就是说一个图形P不断移动,但是\((0,0)\)点必须在图形内部或边界上,最后形成的轨迹图形T.

画一下然后感受一下就是中心对称图形(其实可以猜),\(n\)为偶数时才可能是中心对称图形.

简单证明一下就是对于P上点\((x,y)\),以一个端点\((x_0,y_0)\),T上一定有\((x-x_0,y-y_0)\),也一定有\((x_0-x,y_0-y)\).如果P上没有中心对称相对应的点,那肯定形状不相似....说不清楚,还是感受一下吧.

判断中心对称图形就是相对的点连线看看是否相交于一点.

AC代码:

  1. #include<bits/stdc++.h>
  2. const int mod = 1e9 + 7;
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. const int maxn = 2e5+7;
  7. int a[maxn],b[maxn];
  8. int main() {
  9. int n;
  10. cin>>n;
  11. for(int i=1;i<=n;i++)
  12. cin>>a[i]>>b[i];
  13. if(n%2==1){
  14. cout<<"NO";
  15. return 0;
  16. }
  17. for(int i=2;i<=n/2;i++){
  18. if(a[i]+a[n/2+i]!=a[1]+a[n/2+1]||b[i]+b[n/2+i]!=b[1]+b[n/2+1]){
  19. cout<<"NO";
  20. return 0;
  21. }
  22. }
  23. cout<<"YES";
  24. return 0;
  25. }

E. Water Balance

Description:

There are \(n\) water tanks in a row, \(i\)-th of them contains \(a_i\) liters of water. The tanks are numbered from \(1\) to \(n\) from left to right.

You can perform the following operation: choose some subsegment \([l, r]\) (\(1\le l \le r \le n\)), and redistribute water in tanks \(l, l+1, \dots, r\) evenly. In other words, replace each of \(a_l, a_{l+1}, \dots, a_r\) by \(\frac{a_l + a_{l+1} + \dots + a_r}{r-l+1}\). For example, if for volumes \([1, 3, 6, 7]\) you choose \(l = 2, r = 3\), new volumes of water will be \([1, 4.5, 4.5, 7]\). You can perform this operation any number of times.

What is the lexicographically smallest sequence of volumes of water that you can achieve?

As a reminder:

A sequence \(a\) is lexicographically smaller than a sequence \(b\) of the same length if and only if the following holds: in the first (leftmost) position where \(a\) and \(b\) differ, the sequence \(a\) has a smaller element than the corresponding element in \(b\).

Input:

The first line contains an integer \(n\) (\(1 \le n \le 10^6\)) — the number of water tanks.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) — initial volumes of water in the water tanks, in liters.

Because of large input, reading input as doubles is not recommended.

Output

Print the lexicographically smallest sequence you can get. In the \(i\)-th line print the final volume of water in the \(i\)-th tank.

Your answer is considered correct if the absolute or relative error of each \(a_i\) does not exceed \(10^{-9}\).

Formally, let your answer be \(a_1, a_2, \dots, a_n\), and the jury's answer be \(b_1, b_2, \dots, b_n\). Your answer is accepted if and only if \(\frac{|a_i - b_i|}{\max{(1, |b_i|)}} \le 10^{-9}\) for each \(i\).

Sample Input:

4

7 5 5 7

Sample Output:

5.666666667

5.666666667

5.666666667

7.000000000

Sample Input:

5

7 8 8 10 12

Sample Output:

7.000000000

8.000000000

8.000000000

10.000000000

12.000000000

Sample Input:

10

3 9 5 5 1 7 5 3 8 7

Sample Output:

3.000000000

5.000000000

5.000000000

5.000000000

5.000000000

5.000000000

5.000000000

5.000000000

7.500000000

7.500000000

思路:

贪心.相邻两项考虑.

假设分成两块\(a\),\(b\),这时有\(y\).如果\(y\leq b\),\(y\)与\(b\)必须合并,因为合并后变小.

如果这时新合并的\(by\leq a\),那么也必须合并,就这样while循环下去.

复杂度分析:感性分析能过...

AC代码:

  1. #include<bits/stdc++.h>
  2. const int mod = 1e9 + 7;
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. const int maxn = 1e6+7;
  7. int s[maxn];
  8. pair<ll,int>que[maxn];
  9. int main() {
  10. int n,pos=0;
  11. scanf("%d",&n);
  12. for(int i=1;i<=n;++i) {
  13. scanf("%d",&s[i]);
  14. }
  15. for(int i=1;i<=n;++i){
  16. que[++pos]=make_pair(s[i],1);
  17. while(pos>1&&que[pos-1].first*que[pos].second>=que[pos].first*que[pos-1].second){
  18. que[pos-1].second+=que[pos].second;
  19. que[pos-1].first+=que[pos].first;
  20. pos--;
  21. }
  22. }
  23. for(int i=1;i<=pos;++i){
  24. double ans=(double)que[i].first/(que[i].second*1.0);
  25. for(int j=0;j<que[i].second;++j){
  26. printf("%.12f\n",ans);
  27. }
  28. }
  29. return 0;
  30. }

[CF百场计划]#2 Codeforces Round #618 (Div. 2)的更多相关文章

  1. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  2. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  3. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  4. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

  5. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik

    http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...

  6. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  7. [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)

    A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...

  8. [CF百场计划]Codeforces Round #617 (Div. 3)

    A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...

  9. CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly

    http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...

随机推荐

  1. python爬虫笔记01

    1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout ...

  2. 用Git从本地上传文件到GitHub

    这几天忙于抢救崩掉的博客,没空更新GitHub上PAT的代码,手动一个个传太慢了,所以我去偷学了一下给Git传文件到GitHub,非教学教程没有图文,有几个前提 你得有github账号,没有就去注册吧 ...

  3. [LeetCode] 932. Beautiful Array 漂亮数组

    For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...

  4. Hibernate--起步

    1.配置对象 配置对象是你在任何 Hibernate 应用程序中创造的第一个 Hibernate 对象,并且经常只在应用程序初始化期间创造.它代表了 Hibernate 所需一个配置或属性文件.配置对 ...

  5. 024-PHP常用字符串函数(一)

    <?php $first = "abc"; $second = "aBc"; )//字串比较 { print("字符串相等:".&qu ...

  6. Idea 打印GC

    设置 Run ⇒ Edit Configurations ⇒ VM options 添加 -XX:+PrintGCDetails 运行程序后会在末尾打印GC信息 2019-11-02 13:07:47 ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-print

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  8. 对 TD tree 的使用体验

    经过这几天对学长们的作品的应用,感触颇多,忍不住写写随笔. 先谈一下,最初的感受吧,那天下午观看,体验学长学姐们的作品时,感觉他们太厉害了,只比我们多学一年,就已经可以做出手机 app ,和 网页了. ...

  9. WindowsForm ComboBoxList 下拉框带复选框 可以动态添加

    先来张效果图: 1.这里需要对控件进行重写,详细内容如下,对此不感兴趣的可以直接跳过这步,下载本人生成的dll,直接看第二小结,下载链接https://pan.baidu.com/s/1gfzrK5t ...

  10. MySQL笔记 01

    STRUCTURE QUERY LANGUAGE 数据库CRUD操作 DDL: 数据库定义语言,定义数据库数据表结构 CREATE(创建): 创建数据库 CREATE DATABASE 数据库名字; ...