B. MysticalMosaic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
There is arectangular grid of n rows of m initially-whitecells each.
Arkady performeda certain number (possibly zero) of operations on it. In the i-th operation, anon-empty subset of rows Ri and a non-empty subset ofcolumns Ci are chosen. For each row r in Ri and eachcolumn c in Ci, the intersection of row r andcolumn c is coloured black.
There's anotherconstraint: a row or a column can only be chosen at most once among alloperations. In other words, it means that no pair of (i, j) (i < j) exists suchthat or , where denotes intersection of sets,and denotes the empty set.
You are todetermine whether a valid sequence of operations exists that produces a givenfinal grid.
Input
The first linecontains two space-separated integers n and m (1 ≤ n, m ≤ 50) — thenumber of rows and columns of the grid, respectively.
Each of thefollowing n lines contains a string of m characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representingthe desired setup.
Output
If the givengrid can be achieved by any valid sequence of operations, output "Yes"; otherwise output "No" (both without quotes).
You can printeach character in any case (upper or lower).
Examples
input
Copy
5 8
.#.#..#.
.....#..
.#.#..#.
#.#....#
.....#..
output
Yes
input
Copy
5 5
..#..
..#..
#####
..#..
..#..
output
No
input
Copy
5 9
........#
#........
..##.#...
.......#.
....#.#.#
output
No
Note
For the firstexample, the desired setup can be produced by 3 operations, as is shown below.
For the secondexample, the desired setup cannot be produced, since in order to colour thecenter row, the third row and all columns must be selected in one operation,but after that no column can be selected again, hence it won't be possible tocolour the other cells in the center column.
题意:#是由行和列相交而成,可以有多次或者不操作,行和列不能重复选择。
#include<bits/stdc++.h>
using namespace std;
#define N 60
#define EPS 1e-5
char s[N][N];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%s", s[i] + 1);
for(int i = 1; i <= n; i++){
for(int j = i+1; j <= n;j++) {
int flag = 0;
for(int k = 1; k <= m; k++) {
if(s[i][k]=='#'&&s[j][k]=='#')
flag = 1;
}
if(flag == 1) {
for(int k = 1; k <= m; k++) {
if((s[i][k]=='#')^(s[j][k]=='#') == 1) {//因为第i,j行在同一个集合里如果同一列不相同的话就不成立
printf("No\n");
return 0;
}
}
}
}
}
printf("Yes\n");
return 0;
}
C. Three-level Laser
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
An atom of element X can exist in n distinctstates with energies E1 < E2 < ... < En. Arkady wantsto build a laser on this element, using a three-level scheme. Here is asimplified description of the scheme.
Three distinctstates i, j and k areselected, where i < j < k. After that the following processhappens:
1. initially the atom is in the state i,
2. we spend Ek - Ei energy to put the atom in thestate k,
3. the atom emits a photon with useful energy Ek - Ej andchanges its state to the state j,
4. the atom spontaneously changes its state to thestate i, losingenergy Ej - Ei,
5. the process repeats from step 1.
Let's define theenergy conversion efficiency as , i. e. the ration between theuseful energy of the photon and spent energy.
Due to somelimitations, Arkady can only choose such three states that Ek - Ei ≤ U.
Help Arkady tofind such the maximum possible energy conversion efficiency within the aboveconstraints.
Input
The first linecontains two integers n and U (3 ≤ n ≤ 105, 1 ≤ U ≤ 109) — the numberof states and the maximum possible difference between Ek and Ei.
The second linecontains a sequence of integers E1, E2, ..., En (1 ≤ E1 < E2... < En ≤ 109). It isguaranteed that all Ei are given in increasing order.
Output
If it is notpossible to choose three states that satisfy all constraints, print -1.
Otherwise, printone real number η — the maximum possible energy conversionefficiency. Your answer is considered correct its absolute or relative errordoes not exceed 10 - 9.
Formally, letyour answer be a, and the jury's answer be b. Your answer isconsidered correct if .
Examples
input
Copy
4 4
1 3 5 7
output
0.5
input
Copy
10 8
10 13 15 16 17 19 20 22 24 25
output
0.875
input
Copy
3 1
2 5 10
output
-1
Note
In the firstexample choose states 1, 2 and 3, so that the energy conversion efficiencybecomes equal to .
In the secondexample choose states 4, 5 and 9, so that the energy conversion efficiencybecomes equal to .
枚举i,Ej即为i+1,用upper_bound()找到第一个大于a[i]+m的位置pos,然后pos-1,如果pos-i>=2该位置就是满足以i为Ei 的最佳Ek.
原因:对于(x-a)/(x-b),a,b为定值,x趋近于无穷,值就接近于1,所有因取pos-1.
#include<bits/stdc++.h>
using namespace std;
#define N 110000
#define EPS 1e-6
int a[N];
int main()
{
double sum = -10000000000;
int n, m;
scanf("%d%d", &n, &m);
double tsum = sum;
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n-2; i++) { //枚举i
int flag = 0;
int pos=upper_bound(a+1,a + n + 1, a[i] + m) - a-1;
//printf("%d\n", pos);
if(pos- i < 2)
continue;
sum = max(sum, (1.0*a[pos]-a[i+1])/(a[pos]-a[i]));
}
if(sum < 1e-9)
printf("-1\n");
else
printf("%.11lf\n", sum);
return 0;
}