zoukankan      html  css  js  c++  java
  • Codeforces Round #654 (Div. 2) 题解

    A. Magical Sticks

    网址:https://codeforces.com/contest/1371/problem/A

    A penguin Rocher has (n) sticks. He has exactly one stick with length (i) for all (1≤i≤n).

    He can connect some sticks. If he connects two sticks that have lengths (a) and (b), he gets one stick with length (a+b). Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.

    He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?

    Input

    The input consists of multiple test cases. The first line contains a single integer (t) ((1≤t≤1000)) — the number of test cases. Next (t) lines contain descriptions of test cases.

    For each test case, the only line contains a single integer (n) ((1≤n≤10^9)).

    Output

    For each test case, print a single integer — the answer to the problem.

    Example
    input
    4
    1
    2
    3
    4
    
    output
    1
    1
    2
    2
    
    Note

    In the third case, he can connect two sticks with lengths (1) and (2) and he will get one stick with length (3). So, he will have two sticks with lengths (3).

    In the fourth case, he can connect two sticks with lengths (1) and (3) and he will get one stick with length (4). After that, he will have three sticks with lengths ({2,4,4}), so two sticks have the same length, and one stick has the other length.

    解析

    这道题我当时第一遍的时候还觉得挺难,其实是一道很有趣的简单题。
    首先考虑首尾配对,若(n)为偶数,则一共有(n/2)个对;

    如果是奇数个呢?那么,就把最后一个数固定住,剩下的首尾配对即可(因为剩下的配对后的长度一定为最后一根棍的长度)。

    代码
    #include<iostream>
    #include<cstdio>
    using namespace std;
    int n;
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T --)
    	{
    		scanf("%d", &n);
    		printf("%d
    ", (n - 1) / 2 + 1);
    	}
    	return 0;
    }
    
    B. Magical Calendar

    网址:https://codeforces.com/contest/1371/problem/B

    A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily (7) days!

    In detail, she can choose any integer (k) which satisfies (1≤k≤r), and set (k) days as the number of days in a week.

    Alice is going to paint some (n) consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.

    She wants to make all of the painted cells to be connected by side. It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side.

    Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar's sides.

    For example, in the picture, a week has (4) days and Alice paints (5) consecutive days. ([1]) and ([2]) are different shapes, but ([1]) and ([3]) are equal shapes.

    image

    Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive n days and paints them in calendar started in one of the days of the week. As was said before, she considers only shapes, there all cells are connected by side.

    Input

    The input consists of multiple test cases. The first line contains a single integer (t) ((1≤t≤1000)) — the number of test cases. Next (t) lines contain descriptions of test cases.

    For each test case, the only line contains two integers (n, r) ((1≤n≤10^9,1≤r≤10^9)).

    Output

    For each test case, print a single integer — the answer to the problem.

    Please note, that the answer for some test cases won't fit into (32)-bit integer type, so you should use at least (64)-bit integer type in your programming language.

    Example
    input
    5
    3 4
    3 2
    3 1
    13 7
    1010000 9999999
    
    output
    4
    3
    1
    28
    510049495001
    
    Note

    In the first test case, Alice can set (1,2,3) or (4) days as the number of days in a week.

    There are (6) possible paintings shown in the picture, but there are only (4) different shapes. So, the answer is (4). Notice that the last example in the picture is an invalid painting because all cells are not connected by sides.

    image

    In the last test case, be careful with the overflow issue, described in the output format.

    解析

    考虑:当(k<n)时,一定有k种情况;当(k=n)的情况下有n种情况。当(k>n)的情况下,没有。

    代码
    #include<iostream>
    #include<cstdio>
    using namespace std;
    long long int n, r;
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T --)
    	{
    		scanf("%d %lld", &n, &r);
    		if(r < n) printf("%lld
    ", r * (r + 1ll) / 2);
    		else printf("%lld
    ", n * (n - 1ll) / 2 + 1);
    	}
    	return 0;
    }
    

    网址:https://codeforces.com/contest/1371/problem/C

    Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has (a) vanilla cookies and (b) chocolate cookies for the party.

    She invited (n) guests of the first type and (m) guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:

    If there are (v) vanilla cookies and (c) chocolate cookies at the moment, when the guest comes, then

    • if the guest of the first type: if (v>c) the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie.

    • if the guest of the second type: if (v>c) the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie.
      After that:

    • If there is at least one cookie of the selected type, the guest eats one.

    • Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home.
      Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question.

    Input

    The input consists of multiple test cases. The first line contains a single integer (t) ((1≤t≤1000)) — the number of test cases. Next (t) lines contain descriptions of test cases.

    For each test case, the only line contains four integers (a, b, n, m) ((0≤a,b,n,m≤10^18,n+m≠0)).

    Output

    For each test case, print the answer in one line. If there exists at least one valid order, print "Yes". Otherwise, print "No".

    You can print each letter in any case (upper or lower).

    Example
    input
    6
    2 2 1 2
    0 100 0 1
    12 13 25 1
    27 83 14 25
    0 0 1 0
    1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000
    
    output
    Yes
    No
    No
    Yes
    No
    Yes
    
    Note

    In the first test case, let's consider the order ({1,2,2}) of types of guests. Then:

    • The first guest eats a chocolate cookie. After that, there are (2) vanilla cookies and (1) chocolate cookie.
    • The second guest eats a chocolate cookie. After that, there are (2) vanilla cookies and (0) chocolate cookies.
    • The last guest selects a chocolate cookie, but there are no chocolate cookies. So, the guest gets angry.
      So, this order can't be chosen by Anna.

    Let's consider the order ({2,2,1}) of types of guests. Then:

    • The first guest eats a vanilla cookie. After that, there is (1) vanilla cookie and (2) chocolate cookies.
    • The second guest eats a vanilla cookie. After that, there are (0) vanilla cookies and (2) chocolate cookies.
    • The last guest eats a chocolate cookie. After that, there are (0) vanilla cookies and (1) chocolate cookie.
      So, the answer to this test case is "Yes".

    In the fifth test case, it is illustrated, that the number of cookies ((a+b)) can be equal to zero, but the number of guests ((n+m)) can't be equal to zero.

    In the sixth test case, be careful about the overflow of (32)-bit integer type.

    解析

    (n)个第一类人就是哪种饼干多选哪个,(m)个第二种就是哪种饼干少选哪个。
    如果一样的话,对于两种客人,选哪种都一样(因为顺序并没有要求,假如说按照规则第一类选了第二种饼干,这等价于该人选第一种饼干两种饼干数再打颠倒);

    • 首先,如果总饼干数(a+b)都不够(n+m)个,则无论怎样都并不符题意;
    • 如若(min(a,b))竟小于(m),那么无论怎样依旧不符合题意(证明:第一类客人先到一定不比第二类客人先到优,则(min(a,b))必须大于等于(m));除此之外,其他均可以。
    代码
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    long long int a, b, n, m;
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T --)
    	{
    		scanf("%lld %lld %lld %lld", &a, &b, &n, &m);
    		if(a + b >= m + n && min(a, b) >= m) puts("Yes");
    		else puts("No");
    	}
    	return 0;
    }
    

    D. Grid-00100

    网址:https://codeforces.com/contest/1371/problem/D
    A mad scientist Dr.Jubal has made a competitive programming task. Try to solve it!

    You are given integers (n,k). Construct a grid (A) with size (n×n) consisting of integers (0) and (1). The very important condition should be satisfied: the sum of all elements in the grid is exactly (k). In other words, the number of (1) in the grid is equal to (k).

    Let's define:

    (A_{i,j}) as the integer in the (i)-th row and the (j)-th column.
    (R_i=A_{i,1}+A_{i,2}+...+A_{i,n}) (for all (1≤i≤n)).
    (C_j=A_{1,j}+A_{2,j}+...+A_{n,j}) (for all (1≤j≤n)).
    In other words, (R_i) are row sums and (C_j) are column sums of the grid (A).
    For the grid (A) let's define the value (f(A)=(max(R)−min(R))^2+(max(C)−min(C))^2) (here for an integer sequence (X) we define (max(X)) as the maximum value in (X) and (min(X)) as the minimum value in (X)).
    Find any grid (A), which satisfies the following condition. Among such grids find any, for which the value (f(A)) is the minimum possible. Among such tables, you can find any.

    Input

    The input consists of multiple test cases. The first line contains a single integer (t) ((1≤t≤100)) — the number of test cases. Next (t) lines contain descriptions of test cases.

    For each test case the only line contains two integers (n, k) ((1≤n≤300,0≤k≤n^2)).

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

    Output

    For each test case, firstly print the minimum possible value of (f(A)) among all tables, for which the condition is satisfied.

    After that, print (n) lines contain (n) characters each. The (j)-th character in the (i)-th line should be equal to (A_{i,j}).

    If there are multiple answers you can print any.

    Example
    input
    4
    2 2
    3 8
    1 0
    4 16
    
    output
    0
    10
    01
    2
    111
    111
    101
    0
    0
    0
    1111
    1111
    1111
    1111
    
    Note

    In the first test case, the sum of all elements in the grid is equal to (2), so the condition is satisfied. (R_1=1,R_2=1) and (C_1=1,C_2=1). Then, (f(A)=(1−1)^2+(1−1)^2=0), which is the minimum possible value of (f(A)).

    In the second test case, the sum of all elements in the grid is equal to (8), so the condition is satisfied. (R_1=3,R_2=3,R_3=2) and (C_1=3,C_2=2,C_3=3). Then, (f(A)=(3−2)^2+(3−2)^2=2). It can be proven, that it is the minimum possible value of (f(A)).

    解析

    这道题首先有一个结论:横纵互不干涉。

    基于这条性质,我们可以只考虑一维的情况。(n)个格子(k)(1),让你分配每个格子的个数。

    则,我们把这些数越平均越好(显然)。

    由此可知,横纵每列的数便都清楚。那么接下来怎么办?毕竟还要构造出矩形。

    等等,不是正方形吗!?一个极为好用的性质横空浮现————对称性。

    也就是说,我们横纵分配的数是可以对称的!

    于是,我们可以构造:那些分配余数了的列在该列中的与对角线的交点填数字。

    其次,我们再从对角线前开始填数(环形啊)即可。

    代码
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int SIZE = 300 + 10;
    int n, k, a[SIZE][SIZE];
    int main()
    {
    	int t;
    	scanf("%d", &t);
    	while(t --)
    	{
    		memset(a, 0, sizeof(a));
    		scanf("%d %d", &n, &k);
    		if(!k)
    		{
    			cout << "0" << "
    ";
    			for(int i = 0; i < n; ++ i)
    			{
    				for(int j = 0; j < n; ++ j)
    					printf("0");
    				puts("");
    			}
    		}
    		else
    		{
    			printf("%d
    ", (k % n != false) << 1);
    			int p = k / n, mod = k % n;
    			for(int i = n - mod; i < n; ++ i) a[i][i] = 1;
    
    			for(int i = n - 1; i >= 0; -- i)
    			{
    				for(int j = i - 1; j > i - p - 1; -- j)
    				{
    					a[i][(j + n) % n] = 1;
    				}
    			}
    			for(int i = 0; i < n; ++ i)
    			{
    				for(int j = 0; j < n; ++ j)
    				{
    					printf("%d", a[i][j]);
    				}
    				puts("");
    			}
    		}
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    Java中RuntimeException和Exception
    RuntimeException和Exception的区别
    Spring事务异常回滚
    iOS 卖票中多线程分析;
    凝视转换(部分)
    HDU 5386 Cover(模拟)
    iOS开发之软键盘使用小技巧
    【每日算法】高速幂
    CKEditor高级编辑器
    iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
  • 原文地址:https://www.cnblogs.com/zach20040914/p/13345326.html
Copyright © 2011-2022 走看看