比赛链接: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. JAVA编程思想(2) - 操作符(一)

    "在最底层,Java中的数据是通过操作符来操作的." 1. 使用Java操作符 -操作符接受一个或者多个參数,并生成一个新值,參数的形式与普通的方法调用不用,但效果是同样的.加号和 ...

  2. ASP.NET 运行

    ASP.NET 运行 对于ASP.NET开发,排在前五的话题离不开请求生命周期.像什么Cache.身份认证.Role管理.Routing映射,微软到底在请求过程中干了哪些隐秘的事,现在是时候揭晓了.抛 ...

  3. ArcPad 10 的安装部署

    ArcPad是安装在手持设备或者移动终端的一个外业ArcGIS产品,也就是说ArcPad是Esri的一款软件产品,而不是硬件设备哦.尽管不比ArcGIS Desktop功能复杂缤纷,可是对于野外作业. ...

  4. EL字符串表达式和常用功能用途拦截

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ${wj ...

  5. 【Java GUI】Java面板基础:JPanel

    有两个面板,常见的面板(JPanel)和滚动面板(JScrollPane) Jpanel 面板是一种常见的容器,JPanel的作用是实现接口层次结构,面放入一些组件.也能够在上面绘画,将放有组件和有画 ...

  6. [原创].NET 业务框架开发实战之八 业务层Mapping的选择策略

    原文:[原创].NET 业务框架开发实战之八 业务层Mapping的选择策略 .NET 业务框架开发实战之八 业务层Mapping的选择策略 前言:在上一篇文章中提到了mapping,感觉很像在重新实 ...

  7. c++学籍管理系统

    程序在编译时出错(vc++ 6.0) 求哪位大神帮忙改改 #include<iostream> #include <string> #include<conio.h> ...

  8. Nancy 框架

    Nancy 框架 Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE ,  GET ,  HEAD ,  OPTIO ...

  9. Gradle 载入中 Android 下一个.so档

    1.在project下新建 jni/libs  目录   . jni 是和原来的libs  同级 ,将全部的.so文件放入 新建的libs文件下 2.在build.gradle 文件里新增下面内容到a ...

  10. secureCRT使用退格键(backspace)出现^H解决的方法

    刚新装了python-3.4.1,使用secureCRT连接上去,可是我在进入后,输入回格键时,屏幕显示的是^H,这个让人受不了.最终在网上找到了解决的方法,仅仅要改动一下secureCRT的配置就可 ...