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

A. Kitahara Haruki's Gift
time limit per test:1 second
memory limit per test:256 megabytes

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?

Input

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.

Output

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).

Sample test(s)
Input
3
100 200 100
Output
YES
Input
4
100 100 100 200
Output
NO
Note

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");
}
B. Kuriyama Mirai's Stones
time limit per test:2 seconds
memory limit per test:256 megabytes

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:

  1. She will tell you two numbers,
    l and r (1 ≤ l ≤ r ≤ n), and you should tell her.
  2. 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.

Input

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.

Output

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.

Sample test(s)
Input
6
6 4 2 7 2 7
3
2 3 6
1 3 4
1 1 6
Output
24
9
28
Input
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
Output
10
15
5
15
5
5
2
12
3
5
Note

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]);
}
}
C. Ryouko's Memory Note
time limit per test:1 second
memory limit per test:256 megabytes

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.

Input

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).

Output

Print a single integer — the minimum number of pages Ryouko needs to turn.

Sample test(s)
Input
4 6
1 2 3 4 3 2
Output
3
Input
10 5
9 4 3 8 8
Output
6
Note

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);
}
D. Nanami's Digital Board
time limit per test:1 second
memory limit per test:256 megabytes

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?

Input

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.
Output

For each query, print a single line containing one integer — the answer to Nanami's query.

Sample test(s)
Input
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
Output
0
2
6
Input
3 3 4
1 1 1
1 1 1
1 1 1
2 2 2
1 2 2
2 1 1
2 2 1
Output
6
3
3
Note

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解决问题的方法)的更多相关文章

  1. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  4. Codeforces Round #449 (Div. 2)ABCD

    又掉分了0 0. A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note

    题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...

  6. Codeforces Round #248 (Div. 2)C 题

    题目:http://codeforces.com/contest/433/problem/C 没想到做法就各种纠结, 今天做的都快疯掉了, 太弱了, 等题解一出,就各种恍然大悟 不应该不应该 正文: ...

  7. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. SP2010 3D标签云Web部分--很酷的效果,强烈推荐!!

    SP2010 3D标签云Web部分--很酷的效果.强烈推荐! ! 项目描述叙事         基于简单Flash的3D标签云Web部件.SP Server 2010使用. 建立在内置标签云Web部件 ...

  2. linux 下安装jdk及配置jdk环境图解

    linux 下安装jdk及配置jdk环境图解 一:先检測是否已安装了JDK 运行命令: # rpm -qa|grep jdk  或   # rpm -q jdk  或  #find / -name j ...

  3. js字的数目的计算方法(与word计算公式为)

    [背景] 用户往往需要一定数量的单词填写必填字段限制,但js由value.length取出来的往往差异很大,与实际的话.通常真正的用户抱怨.很显然,我没有写那么多字,但系统提示超过字数限制.然后,我学 ...

  4. android看不见main函数怎么办?程序异常了,能够不提示“xxx软件停止执行”吗?

    今天遇到了这个问题,分享一下解决方式. android没有main 函数,自然也就不存在main里面加入异常处理来实现全局异常捕获的方案.那android程序有全局异常补货的解决方式吗? 答案是有的: ...

  5. 【Android进阶】使用第三方平台ShareSDK实现新浪微博的一键分享功能

    在公司最近的一个项目中,需要实现一键分享功能,在这里我使用的是第三方平台ShareSDK,将使用经验与大家分享 先看效果图 主界面 分享界面 由于第一次使用,所以需要先进行新浪授权,授权界面 分享结果 ...

  6. Swift学习笔记7:关闭

    闭包 捕 引用和将其存储在任意常量和变量的上下文. Swift 你会管理 捕获 过程中涉及到的内存操作. 在 函数 章节中介绍的全局和嵌套函数实际上也是特殊的闭包,闭包採取例如以下三种形式之中的一个: ...

  7. 使用shell/python获取hostname/fqdn释疑(转)

    一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...

  8. 巧妙设备MTU的大小,轻松提网速

    MTU是什么? "MTU=最大传输单元 单位:字节" 我们在使用互联网时进行的各种网络操作,都是通过一个又一个"数据包"传输来实现的.而MTU指定了网络中可数据 ...

  9. 解决 Error:No suitable device found: no device found for connection &quot;System eth0&quot;

    一.底 我们安装在虚拟机,.想模拟几台server.这时就想直接复制已经有的安装好的虚拟机.这样比較省事,不要在反复的安装虚拟机并配置JAVA环境,省掉做相同的事情,这时直接复制,这样之前配置的JAV ...

  10. vs2013提交github代码

    vs2013的新特性之一就是可以方便的通过集成的git工具管理git代码.今天简单给大家演示 在github新建仓库 复制仓库地址 克隆仓库到本地(这一步非常重要,只有顺利获取github的code, ...