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 ...
随机推荐
- 使用 Cordova+Visual Studio 创建跨平台移动应用(1)
1简介 本章节是关于Visual Studio Tools for Apache Cordova的,目前此产品只发布了预览版.Visual Studio for Apache Cordova帮助熟悉V ...
- UVA - 12130 Summits
Description Problem G - Summits Time limit: 8 seconds You recently started working for the largest m ...
- SQL学习之--触发器
USE [learn2] GO /****** Object: Trigger [dbo].[trigger_AdClass] Script Date: 09/30/2014 09:01:03 *** ...
- chrome(转)
阅读目录 Chrome的隐身模式 Chrome下各种组合键 Chrome的about指令 chrome://accessibility 查看浏览器当前访问的标签 chrome://appcac ...
- Android Framework 其中A记录
一个简短的引论 以往的研究太偏应用层的功能,实现了,原则上不进入非常理解,现在,研究人员framework该框架层. 创纪录的 1.下载源代码,文件夹例如以下: 2.Android系统的层次例如以下: ...
- Hello ASP.NET5
2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio2015(已更新upda ...
- UIButton 文字图片排列
UIButton缺省值是:图画-文字水平,所以我们并不需要调整. 1.写作-图画 水平显示,以前的文本,图片后再次 [btn setTitleEdgeInsets:UIEdgeInsetsMake(0 ...
- sdut 上机练习8面向对象编程(对象的数组)
上机练习8面向对象编程(对象的数组) Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 利用类对象数组完毕N个学生数据(学号是字符串类型.成绩是整型)的输入 ...
- Linux管道通信
1.Linux内部自己实现了管道的同步,但多个读或者多个写之间的互斥,还需要自己实现.
- 网络爬虫WebCrawler(1)-Http网页内容抓取
在windows在下面C++由Http协议抓取网页的内容: 首先介绍了两个重要的包(平时linux在开源包,在windows下一个被称为动态链接库dll):curl包和pthreads_dll,其中c ...