Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)
Kitahara Haruki has bought
n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.
Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given
to Ogiso Setsuna.
But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?
The first line contains an integer
n (1 ≤ n ≤ 100) — the number of apples. The second line containsn integersw1, w2, ..., wn
(wi = 100 or
wi = 200), wherewi is the weight of thei-th apple.
In a single line print "YES" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print "NO" (without the
quotes).
- 3
- 100 200 100
- YES
- 4
- 100 100 100 200
- NO
In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.
题目大意:有100克的苹果和200克的苹果若干。每一个苹果都不能切开,问是否能均分给两个人
题目分析:n最大100。
。随便搞。计算100克和200克的个数然后枚举全部可能
- #include <cstdio>
- int main()
- {
- int n;
- scanf("%d", &n);
- int cnt1 = 0, cnt2 = 0;
- for(int i = 0; i < n; i++)
- {
- int num;
- scanf("%d", &num);
- if(num == 100)
- cnt1 ++;
- else
- cnt2 ++;
- }
- bool f = false;
- for(int i = 0; i <= cnt1; i++)
- {
- for(int j = 0; j <= cnt2; j++)
- {
- if(i * 100 + j * 200 == (cnt1 - i) * 100 + (cnt2 - j) * 200)
- {
- f = true;
- break;
- }
- }
- if(f)
- break;
- }
- if(f)
- printf("YES\n");
- else
- printf("NO\n");
- }
Kuriyama Mirai has killed many monsters and got many (namelyn) stones. She numbers the stones from1 to
n. The cost of thei-th stone is
vi. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions:
- She will tell you two numbers,
l and r (1 ≤ l ≤ r ≤ n), and you should tell her.
- Let ui be the cost of thei-th cheapest stone (the cost that will be on thei-th
place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers,l andr (1 ≤ l ≤ r ≤ n), and you should tell her.
For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy.
The first line contains an integer
n (1 ≤ n ≤ 105). The second line containsn integers:v1, v2, ..., vn (1 ≤ vi ≤ 109)
— costs of the stones.
The third line contains an integer
m (1 ≤ m ≤ 105) — the number of Kuriyama Mirai's questions. Then followm lines, each line contains three integerstype,l
andr (1 ≤ l ≤ r ≤ n; 1 ≤ type ≤ 2), describing a question. Iftype equal to1, then you should output the answer for
the first question, else you should output the answer for the second one.
Print m lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input.
- 6
- 6 4 2 7 2 7
- 3
- 2 3 6
- 1 3 4
- 1 1 6
- 24
- 9
- 28
- 4
- 5 5 2 3
- 10
- 1 2 4
- 2 1 4
- 1 1 1
- 2 1 4
- 2 1 2
- 1 1 1
- 1 3 3
- 1 1 3
- 1 4 4
- 1 2 2
- 10
- 15
- 5
- 15
- 5
- 5
- 2
- 12
- 3
- 5
Please note that the answers to the questions may overflow 32-bit integer type.
题目大意:n个数字,m个查询。1表示查询下标l到r的和。2表示查询从小到大排完序下标l到r的和
题目分析:这还用分析个啥。。。
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #define ll long long
- using namespace std;
- int const MAX = 1e5 + 5;
- ll a[MAX], b[MAX];
- ll sum1[MAX], sum2[MAX];
- int main()
- {
- int n;
- scanf("%d", &n);
- for(int i = 1; i <= n; i++)
- {
- scanf("%I64d", &a[i]);
- b[i] = a[i];
- }
- sort(b + 1, b + n + 1);
- memset(sum1, 0, sizeof(sum1));
- memset(sum2, 0, sizeof(sum2));
- for(int i = 1; i <= n; i++)
- {
- sum1[i] = sum1[i - 1] + a[i];
- sum2[i] = sum2[i - 1] + b[i];
- }
- int q;
- scanf("%d", &q);
- while(q--)
- {
- int t, l, r;
- scanf("%d %d %d", &t, &l, &r);
- if(t == 1)
- printf("%I64d\n", sum1[r] - sum1[l - 1]);
- else
- printf("%I64d\n", sum2[r] - sum2[l - 1]);
- }
- }
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, calledRyouko's Memory Note. She writes
what she sees and what she hears on the notebook, and the notebook became her memory.
Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to
analyze, which is tough work.
Ryouko's notebook consists of
n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from pagex to pagey,|x - y|
pages should be turned. During analyzing, Ryouko needsm pieces of information, thei-th piece of information is on page
ai. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is.
Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges pagex to pagey,
she would copy all the information on pagex toy (1 ≤ x, y ≤ n), and consequently, all elements in sequencea that
wasx would becomey. Note thatx can be equal toy, in which case no changes take place.
Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.
The first line of input contains two integers
n and m (1 ≤ n, m ≤ 105).
The next line contains m integers separated by spaces:a1, a2, ..., am(1 ≤ ai ≤ n).
Print a single integer — the minimum number of pages Ryouko needs to turn.
- 4 6
- 1 2 3 4 3 2
- 3
- 10 5
- 9 4 3 8 8
- 6
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequencea becomes{1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to
turn is|1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.
In the second sample, optimal solution is achieved by merging page 9 to 4.
题目大意:给一些数字,仅仅能替换一个数字(这里是所有替换。比方用2替换3就是把所有的3都换成2),要求目标值最小。求这个最小的目标值。目标值为
题目分析:中位数定理。用vector记录每一个相邻的关系(注意仅仅存相邻数字不同的,不然排序取中位数会出错),然后枚举每组的中位数计算改变了的目标值,最后取目标值的最小值
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #define ll long long
- using namespace std;
- int const MAX = 1e5 + 5;
- ll a[MAX];
- vector <ll> vt[MAX];
- int main()
- {
- int n, m;
- ll ma = 0, dif = 0;
- scanf("%d %d", &n, &m);
- for(int i = 0; i < m; i++)
- {
- scanf("%I64d", &a[i]);
- ma = max(ma, a[i]);
- }
- for(int i = 0; i < m - 1; i++)
- {
- if(a[i] == a[i + 1])
- continue;
- dif += abs(a[i + 1] - a[i]);
- vt[a[i]].push_back(a[i + 1]);
- vt[a[i + 1]].push_back(a[i]);
- }
- ll ans = dif;
- for(int i = 1; i <= ma; i++)
- {
- ll tmp = dif;
- int sz = vt[i].size();
- if(sz == 0)
- continue;
- sort(vt[i].begin(), vt[i].end());
- int mid = vt[i][sz / 2];
- for(int j = 0; j < sz; j++)
- tmp = tmp + abs(vt[i][j] - mid) - abs(i - vt[i][j]);
- ans = min(ans, tmp);
- }
- printf("%I64d\n", ans);
- }
Nanami is an expert at playing games. This day, Nanami's good friend Hajime invited her to watch a game of baseball. Unwilling as she was, she followed him to the stadium. But Nanami had no interest in the game, so she looked
around to see if there was something that might interest her. That's when she saw the digital board at one end of the stadium.
The digital board is n pixels in height andm pixels in width, every pixel is either light or dark. The pixels are described by its coordinate. Thej-th
pixel of the i-th line is pixel(i, j). The board displays messages by switching a combination of pixels to light, and the rest to dark. Nanami notices that the state of
the pixels on the board changes from time to time. At certain times, certain pixels on the board may switch from light to dark, or from dark to light.
Nanami wonders, what is the area of the biggest light block such that a specific pixel is on its side. A light block is a sub-rectangle of the board, in which all pixels are light. Pixel(i, j)
belongs to a side of sub-rectangle with(x1, y1) and(x2, y2)
as its upper-left and lower-right vertex if and only if it satisfies the logical condition:
((i = x1 ori = x2) and (y1 ≤ j ≤ y2))
or ((j = y1 or
j = y2) and (x1 ≤ i ≤ x2)).
Nanami has all the history of changing pixels, also she has some questions of the described type, can you answer them?
The first line contains three space-separated integersn,
m andq (1 ≤ n, m, q ≤ 1000) — the height and width of the digital board, and the number of operations.
Then follow n lines, each line containingm space-separated integers. The
j-th integer of the
i-th line isai, j — the initial state of pixel(i, j).
- If ai, j = 0, pixel(i, j) is initially dark.
- If ai, j = 1, pixel(i, j) is initially light.
Then follow q lines, each line containing three space-separated integersop,
x, andy (1 ≤ op ≤ 2; 1 ≤ x ≤ n; 1 ≤ y ≤ m), describing an operation.
- If op = 1, the pixel at(x, y) changes its state (from light to dark or from dark to light).
- If op = 2, Nanami queries the biggest light block with pixel(x, y) on its side.
For each query, print a single line containing one integer — the answer to Nanami's query.
- 3 4 5
- 0 1 1 0
- 1 0 0 1
- 0 1 1 0
- 2 2 2
- 2 1 2
- 1 2 2
- 1 2 3
- 2 2 2
- 0
- 2
- 6
- 3 3 4
- 1 1 1
- 1 1 1
- 1 1 1
- 2 2 2
- 1 2 2
- 2 1 1
- 2 2 1
- 6
- 3
- 3
Consider the first sample.
The first query specifies pixel
(2, 2), which is dark itself, so there are no valid light blocks, thus the answer is 0.
The second query specifies pixel
(1, 2). The biggest light block is the block with (1, 2) as its upper-left vertex and(1, 3) as its lower-right vertex.
The last query specifies pixel
(2, 2), which became light in the third operation. The biggest light block is the block with(1, 2) as its upper-left vertex and
(3, 3) as its lower-right vertex.
题目大意:给一个n*m的0/1矩阵,q组操作,1表示改变(x,y)点的值(0变1,1变0),2表示查询点(x,y)所在矩形的最大面积
题目分析:直接模拟,四个数组。up,down,left,right分别记录当前行/列向四个方向延伸的1的个数
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int const MAX = 1005;
- int n, m, q;
- int g[MAX][MAX];
- int up[MAX][MAX], down[MAX][MAX], left[MAX][MAX], right[MAX][MAX];
- int cnt[MAX];
- void pre()
- {
- for(int i = 1; i <= n; i++)
- {
- for(int j = 1; j <= m; j++)
- {
- if(g[i][j] == 0)
- up[i][j] = left[i][j] = 0;
- else
- {
- up[i][j] = up[i - 1][j] + 1;
- left[i][j] = left[i][j - 1] + 1;
- }
- }
- }
- for(int i = n; i >= 1; i--)
- {
- for(int j = m; j >= 1; j--)
- {
- if(g[i][j] == 0)
- down[i][j] = right[i][j] = 0;
- else
- {
- down[i][j] = down[i + 1][j] + 1;
- right[i][j] = right[i][j + 1] + 1;
- }
- }
- }
- }
- void change(int x, int y)
- {
- g[x][y] ^= 1;
- for(int i = 1; i <= n; i++)
- {
- if(g[i][y] == 0)
- up[i][y] = 0;
- else
- up[i][y] = up[i - 1][y] + 1;
- }
- for(int i = 1; i <= m; i++)
- {
- if(g[x][i] == 0)
- left[x][i] = 0;
- else
- left[x][i] = left[x][i - 1] + 1;
- }
- for(int i = n; i >= 1; i--)
- {
- if(g[i][y] == 0)
- down[i][y] = 0;
- else
- down[i][y] = down[i + 1][y] + 1;
- }
- for(int i = m; i >= 1; i--)
- {
- if(g[x][i] == 0)
- right[x][i] = 0;
- else
- right[x][i] = right[x][i + 1] + 1;
- }
- }
- int get_ma(int ma, int pos)
- {
- int ans = 0;
- for(int i = pos + 1; i <= ma; i++)
- cnt[i] = min(cnt[i - 1], cnt[i]);
- for(int i = pos - 1; i >= 1; i--)
- cnt[i] = min(cnt[i + 1], cnt[i]);
- int l = pos, r = pos;
- for(int i = cnt[pos]; i >= 1; i--)
- {
- while(cnt[l - 1] >= i && l > 1)
- l --;
- while(cnt[r + 1] >= i && r < ma)
- r ++;
- ans = max(ans, (r - l + 1) * i);
- }
- return ans;
- }
- int cal(int x, int y)
- {
- int ans = 0;
- for(int i = 1; i <= n; i++)
- cnt[i] = left[i][y];
- ans = max(ans, get_ma(n, x));
- for(int i = 1; i <= n; i++)
- cnt[i] = right[i][y];
- ans = max(ans, get_ma(n, x));
- for(int i = 1; i <= m; i++)
- cnt[i] = up[x][i];
- ans = max(ans, get_ma(m, y));
- for(int i = 1; i <= m; i++)
- cnt[i] = down[x][i];
- ans = max(ans, get_ma(m, y));
- return ans;
- }
- int main()
- {
- memset(g, 0, sizeof(g));
- scanf("%d %d %d", &n, &m, &q);
- for(int i = 1; i <= n; i++)
- for(int j = 1; j <= m; j++)
- scanf("%d", &g[i][j]);
- pre();
- while(q--)
- {
- int op, x, y;
- scanf("%d %d %d", &op, &x, &y);
- if(op == 1)
- change(x, y);
- else
- printf("%d\n", cal(x, y));
- }
- }
版权声明:本文博主原创文章,博客,未经同意不得转载。
Codeforces Round #248 (Div. 2) (ABCD解决问题的方法)的更多相关文章
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- Codeforces Round #449 (Div. 2)ABCD
又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...
- Codeforces Round #248 (Div. 2)C 题
题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...
- Codeforces Round #143 (Div. 2) (ABCD 思维场)
题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...
- Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和
B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...
- Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题
A. Ryouko's Memory Note 题目连接: http://www.codeforces.com/contest/434/problem/A Description Ryouko is ...
随机推荐
- Java 的布局管理器GridBagLayout的使用方法(转)
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...
- UVA 11402 - Ahoy, Pirates!(段树)
UVA 11402 - Ahoy, Pirates! 题目链接 题意:总的来说意思就是给一个01串,然后有3种操作 1.把一个区间变成1 2.把一个区间变成0 3.把一个区间翻转(0变1,1变0) 思 ...
- 【Linux探索之旅】开宗明义+第一部分第一课:什么是Linux?
内容简介 1.课程大纲 2.第一部分第一课:什么是Linux? 3.第一部分第二课预告:下载Linux,免费的噢! 开宗明义 我们总听到别人说:Linux挺复杂的,是给那些追求逼格的程序员用的.咱 ...
- 【C语言探索之旅】 第二部分第三课:数组
内容简介 1.课程大纲 2.第二部分第三课: 数组 3.第二部分第四课预告:字符串 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. C语 ...
- Android-Service组件
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/40381109 原文:http://developer.android.com ...
- 大数据的胖哥的方式(9)- 金融业数据仓库的逻辑模型FS-LDM
介绍: 大数据是不是海市蜃楼,来自小橡子只是意淫奥克斯,大数据的发展,而且要从头开始,基于大数据建设国家.项目-level数据中心行业将越来越多,大数据仅供技术,而非溶液,临数据组织模式,数据逻辑模式 ...
- Ant—Ant标签解释
采用ant命令必须写ant命令脚本,脚本由非常多Ant标签组成.现在总结一下我也遇到过Ant标签: 版权声明:本文博主原创文章,博客,未经同意不得转载.
- Windows创建的基本含义和进程的进程的内核
过程 1 这意味着过程: 1.1 一个是在操作系统的内核对象管理处理. 的统计信息的地方. 1.2 还有一个是地址空间.它包括全部可运行模块或DL L 模块的代码和数据.它还包括动态内存分配的 ...
- 大约cocos2d-X 3.x使用引擎版本自带的物理引擎Physics
今天打算用BOX2D物理引擎, 我想我以前听说过一些时间cocos2d-X在3.0版本封装自己的物理引擎Physics, 听名字很霸气量, 这的确是一个比BOX2D非常多( 毕竟是基于BOX2D封装的 ...
- HDU 5012 Dice (BFS)
事实上是非常水的一道bfs,用字符串表示每一个状态,map判重就ok了. 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5012 #include&l ...