Codeforces Round #249 (Div. 2) (ABCD ),

Source: Internet
Author: User
Tags polyline

Codeforces Round #249 (Div. 2) (ABCD ),

Match link: http://codeforces.com/contest/435


A. Queue on Bus Stoptime limit per test: 1 secondmemory limit per test: 256 megabytes

It's that time of the year when the Russians flood their countryside summer cottages (dachas) and the bus stop has a lot of people. people rarely go to the dacha on their own, it's usually a group, so the people stand in queue by groups.

The bus stop queue hasNGroups of people.I-Th group from the beginning hasAIPeople. Every 30 minutes an empty bus arrives at the bus stop, it can carry at mostMPeople. naturally, the people from the first group enter the bus first. then go the people from the second group and so on. note that the order of groups in the queue never changes. moreover, if some group cannot fit all of its members into the current bus, it waits for the next bus together with other groups standing after it in the queue.

Your task is to determine how Your buses is needed to transport allNGroups to the dacha countryside.

Input

The first line contains two integersNAndM(1 digit ≤ DigitN, Bytes,MMemory ≤ memory 100). The next line containsNIntegers:A1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limitM).

Output

Print a single integer-the number of buckets that is needed to transport allNGroups to the dacha countryside.

Sample test (s) Input
4 32 3 2 1
Output
3
Input
3 41 2 1
Output
1

There are a total of n platforms. Each station has an ai person at the beginning. A car can carry up to m people at a time. If the current car cannot hold all the people on the current station, no one on the current station will get on the bus and wait for the next one. Ask how many cars are needed for the person carrying the bus.

Question Analysis: Just follow the questions to simulate

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[105];int main(){    int n, m;    scanf("%d %d", &n, &m);    for(int i = 0; i < n; i++)        scanf("%d", &a[i]);    int ans = 1;    int now = m;    for(int i = 0; i < n;)    {        if(a[i] <= now)        {            now -= a[i];            i ++;        }        else        {            now = m;            ans ++;        }    }    printf("%d\n", ans);}



B. Pasha Maximizestime limit per test: 1 secondmemory limit per test: 256 megabytes

Pasha has a positive integerAWithout leading zeroes. Today he decided that the number is too small and he shocould make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at mostKSwaps.

Input

The single line contains two integersAAndK(1 digit ≤ DigitALimit ≤ limit 1018; 0 bytes ≤ bytesKLimit ≤ limit 100 ).

Output

Print the maximum number that Pasha can get if he makes at mostKSwaps.

Sample test (s) Input
1990 1
Output
9190
Input
300 0
Output
300
Input
1034 2
Output
3104
Input
9090000078001234 6
Output
9907000008001234

For a number a, only two adjacent numbers can be exchanged at most k times at a time, and the maximum number after the exchange is obtained.

Subject Analysis: greedy. Obviously, the greedy strategy for putting big numbers forward is wrong. The correct one should be to make the numbers at the highest level as big as possible, and start from the highest bit to the lowest level, first, find the maximum number in step k. If the maximum number in step k is not the number of the current position, then the maximum number is switched to the current end, not switched once, k minus 1


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char s[20];int k;int main(){    scanf("%s %d", s, &k);    int len = strlen(s);    int now;    for(int i = 0; i < len; i++)    {        now = i;        for(int j = i + 1; j <= i + k && j < len; j++)            if(s[j] > s[now])                now = j;        if(now != i)        {            for(int j = now; j > i; j--)            {                swap(s[j], s[j - 1]);                k --;            }        }    }    printf("%s\n", s);}



C. Cardiogramtime limit per test: 1 secondmemory limit per test: 256 megabytes

In this problem, your task is to use ASCII graphics to paint a cardiogram.

A cardiogram is a polyline with the following corners:

That is, a cardiogram is fully defined by a sequence of positive integersA1, bytes,A2, middle..., middle ,...,AN.

Your task is to paint a cardiogram by given sequenceAI.

Input

The first line contains integerN(2 cores ≤ CoresNParameter ≤ limit 1000). The next line contains the sequence of integersA1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limit 1000). It is guaranteed that the sum of allAIDoesn' t exceed1000.

Output

PrintMax|YIAccept-Encoding-YJ| Lines (whereYKIsYCoordinate ofK-Th point of the polyline), in each line print characters. each character must equal either (slash), '\» (backslash),'» (space ). the printed image must be the image of the given polyline. please study the test samples for better understanding of how to print a cardiogram.

Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty.

Sample test (s) Input
53 1 2 5 1
Output
      / \        / \ /   \      /       \    /         \            \ / 
Input
31 5 1
Output
 / \       \       \       \       \ / 
Note

Due to the technical reasons the answers for the samples cannot be copied from the statement. We 've attached two text documents with the answers below.

Http://assets.codeforces.com/rounds/435/1.txt

Http://assets.codeforces.com/rounds/435/2.txt


The meaning of this question is truly refreshing... For a series, print the image in order.

Problem Analysis: because the maximum ai size is 1000, you can use a 2000*2000 matrix to store it. Then, simulate it. Pay attention to dynamically taking the maximum and minimum values in the vertical direction, I didn't expect cf to have such questions.


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char g[2005][2005];int main(){    int n;    scanf("%d", &n);    int sign = 1;    int maxy = 1000, miny = 1000;    int x = -1, y = 1000;    memset(g, 0, sizeof(g));    while(n--)    {        int a;        scanf("%d", &a);        if(sign == 1)            y --;        else            y ++;        for(int i = 1; i <= a; i++)        {            if(sign == 1)            {                y ++;                x ++;                g[x][y] = '/';            }            else            {                y --;                x ++;                g[x][y] = '\\';            }        }        maxy = max(maxy, y);        miny = min(miny, y);        sign *= -1;    }    for(int i = 0; i <= x; i++)        for(int j = miny; j <= maxy; j++)            if(!g[i][j])                g[i][j] = ' ';            int cnt =0 ;    for(int j = maxy; j >= miny; j--)    {        for(int i = 0; i <= x; i++)            printf("%c", g[i][j]);        printf("\n");    }}



D. Special Gridtime limit per test: 4 secondsmemory limit per test: 256 megabytes

You are givenNLimit × limitMGrid, some of its nodes are black, the others are white. Moreover, it's not an ordinary grid-each unit square of the grid has painted diagonals.

The figure below is an example of such grid of size3 limit x limit 5. Four nodes of this grid are black, the other11 nodes are white.

Your task is to count the number of such triangles on the given grid that:

  • The corners match the white nodes, and the area is positive;
  • All sides go along the grid lines (horizontal, vertical or diagonal );
  • No side contains black nodes.
Input

The first line contains two integersNAndM(2 cores ≤ CoresN, Bytes,MLimit ≤ limit 400). Each of the followingNLines containMCharacters (zeros and ones)-the description of the grid. IfJ-Th character inI-Th line equals zero, then the node onI-Th horizontal line and onJ-Th vertical line is painted white. Otherwise, the node is painted black.

The horizontal lines are numbered starting from one from top to bottom, the vertical lines are numbered starting from one from left to right.

Output

Print a single integer-the number of required triangles.

Sample test (s) Input
3 5100001001000001
Output
20
Input
2 20000
Output
4
Input
2 21111
Output
0
Note

The figure below shows red and blue triangles. they are the examples of the required triangles in the first sample. one of the invalid triangles is painted green. it is invalid because not all sides go along the grid lines.


Question: to give a 0/1 matrix, where 0 can be connected to each other, and ask how many triangles are in the Matrix. Note that the sides of the triangle must be vertical horizontal or oblique 45 degrees.

Question Analysis: According to the question, it is clear that all triangles are right triangles, so you can directly enumerate right triangles. Let me explain my practice...

I do this by enumeration. First, we can pre-process the maximum distance (distance and points) that each vertex can extend to the eight directions around it ), it is represented by dp [I] [j] [0-7] And then divided into two cases. The first case is that only one side is in the 45-degree oblique direction, and all conditions are enumerated, note that in this case, I enumerate Based on the 45 degree angle, so I have to divide it by 2, and then there are two sides with an oblique 45 degree direction. In this case, the enumeration is based on 90 degrees, so we won't repeat it. In fact, we can simply enumerate all the right angles. When I make the call, I make the call to make the call messy.


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 405;int dp[MAX][MAX][8], g[MAX][MAX];char s[MAX][MAX];int main(){    int n, m;    scanf("%d %d", &n, &m);    memset(dp, 0, sizeof(dp));    for(int i = 1; i <= n; i++)    {           scanf("%s", s[i] + 1);        for(int j = 1; j <= m; j++)        {            g[i][j] = s[i][j] - '0';            if(!g[i][j])                for(int k = 0; k < 8; k++)                    dp[i][j][k] = 1;        }    }    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            if(dp[i - 1][j][0] && dp[i][j][0])                dp[i][j][0] += dp[i - 1][j][0];            if(dp[i - 1][j - 1][7] && dp[i][j][7])                dp[i][j][7] += dp[i - 1][j - 1][7];            if(dp[i][j - 1][6] && dp[i][j][6])                dp[i][j][6] += dp[i][j - 1][6];        }        for(int j = m; j >= 1; j--)        {            if(dp[i - 1][j + 1][1] && dp[i][j][1])                dp[i][j][1] += dp[i - 1][j + 1][1];            if(dp[i][j + 1][2] && dp[i][j][2])                dp[i][j][2] += dp[i][j + 1][2];        }    }    for(int i = n; i >= 1; i--)    {        for(int j = 1; j <= m; j++)        {            if(dp[i + 1][j][4] && dp[i][j][4])                dp[i][j][4] += dp[i + 1][j][4];            if(dp[i + 1][j - 1][5] && dp[i][j][5])                dp[i][j][5] += dp[i + 1][j - 1][5];        }        for(int j = m; j >= 1; j--)        {            if(dp[i + 1][j + 1][3] && dp[i][j][3])                dp[i][j][3] += dp[i + 1][j + 1][3];        }    }    for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)            for(int k = 0; k < 8; k++)                dp[i][j][k] -= 1;    int ans1 = 0;    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            for(int k = 1; k <= max(n, m); k++)            {                if(dp[i][j][0] >= k && dp[i][j][7] >= k && i - k >= 1 && dp[i - k][j][6] >= k)                    ans1 ++;                if(dp[i][j][0] >= k && dp[i][j][1] >= k && i - k >= 1 && dp[i - k][j][2] >= k)                    ans1 ++;                if(dp[i][j][2] >= k && dp[i][j][1] >= k && j + k <= m && dp[i][j + k][0] >= k)                    ans1 ++;                if(dp[i][j][2] >= k && dp[i][j][3] >= k && j + k <= m && dp[i][j + k][4] >= k)                    ans1 ++;                if(dp[i][j][4] >= k && dp[i][j][3] >= k && i + k <= n && dp[i + k][j][2] >= k)                    ans1 ++;                if(dp[i][j][4] >= k && dp[i][j][5] >= k && i + k <= n && dp[i + k][j][6] >= k)                    ans1 ++;                if(dp[i][j][6] >= k && dp[i][j][5] >= k && j - k >= 1 && dp[i][j - k][4] >= k)                    ans1 ++;                if(dp[i][j][6] >= k && dp[i][j][7] >= k && j - k >= 1 && dp[i][j - k][0] >= k)                    ans1 ++;            }        }    }    ans1 /= 2;    int ans2 = 0;    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            for(int k = 1; k <= max(n, m); k++)            {                if(dp[i][j][1] >= k && dp[i][j][3] >= k && i - k >= 1 && j + k <= m && dp[i - k][j + k][4] >= 2 * k)                    ans2 ++;                if(dp[i][j][5] >= k && dp[i][j][3] >= k && i + k <= n && j - k >= 1 && dp[i + k][j - k][2] >= 2 * k)                    ans2 ++;                if(dp[i][j][5] >= k && dp[i][j][7] >= k && i - k >= 1 && j - k >= 1 && dp[i - k][j - k][4] >= 2 * k)                    ans2 ++;                if(dp[i][j][1] >= k && dp[i][j][7] >= k && i - k >= 1 && j - k >= 1 && dp[i - k][j - k][2] >= 2 * k)                    ans2 ++;            }        }    }    printf("%d\n", ans1 + ans2);}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.