Codeforces Round #249 (Div. 2) (ABCD题解)

2024-03-20 14:08

本文主要是介绍Codeforces Round #249 (Div. 2) (ABCD题解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

比赛链接:http://codeforces.com/contest/435


A. Queue on Bus Stop
time limit per test:1 second
memory 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 has n groups of people. The i-th group from the beginning has ai people. Every 30 minutes an empty bus arrives at the bus stop, it can carry at most m people. 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 many buses is needed to transport all n groups to the dacha countryside.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100). The next line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ m).

Output

Print a single integer — the number of buses that is needed to transport all n groups to the dacha countryside.

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

题目大意:一共n个站台,每个站台一开始有ai个人,一辆车一次最多载m个人,如果当前这辆车不能装下当前站的所有人,则当前站的人都不上车而等下一辆,问搭载所有的人需要几辆车

题目分析:照着题意模拟即可

#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 Maximizes
time limit per test:1 second
memory limit per test:256 megabytes

Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should 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 most k swaps.

Input

The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most k swaps.

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

题目大意:给一个数a,每次只能交换相邻两个数,最多交换k次,求交换后的最大数字

题目分析:贪心问题,显然大数字往前放的贪心策略是错的,正确的应该是尽量让高位的数字大,从最高位开始向低位找,先找到k步之内的最大数,如果k步之内的最大数不是当前位置的数,那么把最大数交换到当前为止,没交换一次,k减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. Cardiogram
time limit per test:1 second
memory 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 integers a1, a2, ..., an.

Your task is to paint a cardiogram by given sequence ai.

Input

The first line contains integer n (2 ≤ n ≤ 1000). The next line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 1000). It is guaranteed that the sum of all ai doesn't exceed 1000.

Output

Print max |yi - yj| lines (where yk is the y coordinate of the k-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
5
3 1 2 5 1
Output
      / \      / \ /   \     /       \   
 /         \  \ / 
Input
3
1 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


题目大意:这题意着实醒神。。。给一个数列,按其顺序一上一下打印图形

题目分析:因为还好ai最大是1000,可以用一个2000*2000的矩阵存起来,然后就模拟吧,注意动态取竖直方向的最大最小值,没想到cf还有这样的题


#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 --;elsey ++;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 Grid
time limit per test:4 seconds
memory limit per test:256 megabytes

You are given an n × m grid, 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 size 3 × 5. Four nodes of this grid are black, the other 11 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 integers n and m (2 ≤ n, m ≤ 400). Each of the following n lines contain m characters (zeros and ones) — the description of the grid. If the j-th character in the i-th line equals zero, then the node on the i-th horizontal line and on the j-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 5
10000
10010
00001
Output
20
Input
2 2
00
00
Output
4
Input
2 2
11
11
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.


题目大意:给个0/1矩阵,0的地方可以相互连接,问矩阵中共有多少个三角形,注意三角形的边必须是垂直水平或者斜45度的

题目分析:做的特别纠结的一题,根据题意显然所有三角形都是直角三角形,所以可以直接枚举直角来做,我说下我的做法吧。。。

我是枚举边做的,首先预处理出每个点往其周围的8个方向可以延伸的最远距离(距离及点数),用dp[i][j][0-7]表示,然后分成两种情况,第一种是只有一个边是斜45度方向的,枚举全部情况,注意这种情况我是按45度角枚举的,所以最后要除2,然后是有两条斜45度方向边的情况,这种情况是按90度枚举的,所以不会重复,其实直接全部枚举直角就可以了,我做的时候调来调去给调乱了,就搞成了这样


#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);
}


这篇关于Codeforces Round #249 (Div. 2) (ABCD题解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829705

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述