Berland Football Cup starts really soon! Commentators from all over the world come to the event.
Organizers have already built nn commentary boxes. mm regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.
If nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.
Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.
What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm)?
The only line contains four integer numbers nn, mm, aa and bb (1≤n,m≤10121≤n,m≤1012, 1≤a,b≤1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.
Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm). It is allowed that the final number of the boxes is equal to 00.
9 7 3 8
15
2 7 3 7
14
30 6 17 19
0
In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.
In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles for the each of them.
In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 55
boxes.
【分析】比较一下 n % m * b和 (m - n % m) * a的大小即可
【代码】
#include
using namespace std;
long long min(long long a, long long b)
{
if (a > b) return b;
return a;
}
int main()
{
long long n, m, ans, c;
int a, b;
scanf("%I64d%I64d%d%d", &n, &m, &a, &b);
c = n % m;
printf("%I64d", min(c * b, (m - c) * a));
return 0;
}
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.
The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and ai≤aj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jjif ai>ajai>aj and ai≤aj+Kai≤aj+K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101––––,55,54][101,53,42,102,101_,55,54] →→ [101,53–––,42,102,55,54][101,53_,42,102,55,54] →→ [101––––,42,102,55,54][101_,42,102,55,54] →→ [42,102,55,54–––][42,102,55,54_] →→ [42,102,55][42,102,55]. In total there are 33 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
The first line contains two space separated positive integers nn and KK (1≤n≤2⋅1051≤n≤2⋅105, 1≤K≤1061≤K≤106) — number of bacteria and intergalactic constant KK.
The second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — sizes of bacteria you have.
Print the only integer — minimal possible number of bacteria can remain.
7 1 101 53 42 102 101 55 54
3
6 5 20 15 10 15 20 25
1
7 1000000 1 1 1 1 1 1 1
7
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20,15,10,15,20–––,25][20,15,10,15,20_,25] →→ [20,15,10,15–––,25][20,15,10,15_,25] →→ [20,15,10–––,25][20,15,10_,25] →→[20,15–––,25][20,15_,25] →→ [20–––,25][20_,25] →→ [25][25].
In the third example no bacteria can swallow any other bacteria.
【代码】
#include
#include
using namespace std;
priority_queue, greater> q;
int main()
{
int n, k, i, x, a1, a2, cnt = 1;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; ++i)
{
scanf("%d", &x);
q.push(x);
}
a1 = q.top();
q.pop();
if (q.empty())
{
printf("1");
}
else
{
while (!q.empty())
{
a2 = q.top();
q.pop();
if (a2 > a1 && a1 + k >= a2)
n -= cnt, cnt = 1;
else if (a2 > a1) cnt = 1;
else if (a2 == a1) ++cnt;
a1 = a2;
}
printf("%d", n);
}
return 0;
}
A bracket sequence is a string containing only characters "(" and ")".
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
You are given nn bracket sequences s1,s2,…,sns1,s2,…,sn. Calculate the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".
If si+sjsi+sj and sj+sisj+si are regular bracket sequences and i≠ji≠j, then both pairs (i,j)(i,j) and (j,i)(j,i) must be counted in the answer. Also, if si+sisi+si is a regular bracket sequence, the pair (i,i)(i,i) must be counted in the answer.
The first line contains one integer n(1≤n≤3⋅105)n(1≤n≤3⋅105) — the number of bracket sequences. The following nn lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 3⋅1053⋅105.
In the single line print a single integer — the number of pairs i,j(1≤i,j≤n)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence.
3 ) () (
2
2 () ()
4
In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).
In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).
#include
#include
#define ZJ 310000
using namespace std;
long long d[710000];
char s[310000];
int js[310000];
int main()
{
int i, j, n, cnt;
bool a, b;
long long ans = 0;
scanf("%d", &n);
for (i = 1; i <= n; ++i)
{
a = 0;
cnt = 0;
scanf("%s", s);
for (j = 0; j < strlen(s); ++j)
{
if (s[j] == '(') ++cnt;
else --cnt;
js[j + 1] = cnt;
}
for (j = 1; j <= strlen(s); ++j)
{
if (js[j] < 0 && js[strlen(s)] > js[j]) a = 1;
js[j] = 0;
}
if (a) continue;
else d[ZJ + cnt]++;
}
for (i = 0; i <= ZJ; ++i) ans += d[ZJ + i] * d[ZJ - i];
printf("%I64d", ans);
return 0;
}
Given three numbers n,a,bn,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to aa, and the number of components in its complement is bb. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.
In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.
The adjacency matrix of an undirected graph is a square matrix of size nn consisting only of "0" and "1", where nn is the number of vertices of the graph and the ii-th row and the ii-th column correspond to the ii-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 11 if and only if the ii-th and jj-th vertices in the graph are connected by an edge.
A connected component is a set of vertices XX such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to XX violates this rule.
The complement or inverse of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are not adjacent in GG.
In a single line, three numbers are given n,a,b(1≤n≤1000,1≤a,b≤n)n,a,b(1≤n≤1000,1≤a,b≤n): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.
If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).
Otherwise, on the first line, print "YES"(without quotes). In each of the next nn lines, output nn digits such that jj-th digit of ii-th line must be 11 if and only if there is an edge between vertices ii and jj in GG (and 00 otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes.
If there are several matrices that satisfy the conditions — output any of them.
3 1 2
YES 001 001 110
3 3 3
NO
【代码】
#include
using namespace std;
int d[1100][1100];
int main()
{
int i, j, k, a, b, n;
scanf("%d%d%d", &n, &a, &b);
if (a != 1 && b != 1)
{
printf("NO");
}
else
{
if (a == 1 && b == 1)
{
if (n == 1) printf("YES\n0");
else if (n == 2 || n == 3) printf("NO");
else
{
printf("YES\n");
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
if (j - 1 == i) d[i][j] = d[j][i] = 1;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; ++j) printf("%d", d[i][j]);
printf("\n");
}
}
}
else if (b == 1)
{
printf("YES\n");
for (i = a; i < n; ++i) d[i][i + 1] = d[i + 1][i] = 1;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; ++j) printf("%d", d[i][j]);
printf("\n");
}
}
else
{
printf("YES\n");
for (i = b; i < n; ++i) d[i][i + 1] = d[i + 1][i] = 1;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; ++j)
if (i != j) printf("%d", !d[i][j]);
else printf("%d", d[i][j]);
printf("\n");
}
}
}
return 0;
}