Codeforces Round #378 (Div. 2) B && codeforces 733B(暴力)
2016-11-01 14:47
381 查看
B. Parade
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian
population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th
column consists of li soldiers,
who start to march from left leg, and risoldiers,
who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march
from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal|L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this
columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index iand
swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 ≤ n ≤ 105) —
the number of columns.
The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) —
the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if
the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
input
output
input
output
input
output
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal5 + 8 + 10 = 23,
and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal5 + 8 + 3 = 16,
and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
题意:给你两堆数,交换其中对应的两个数字,让他们的差值更大,让你告诉他换哪组数,如果已经最大了就直接输出0;
思路1:真的换过来,注意缓过来之后还要换回去
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1e5 + 5;
int l[maxn], r[maxn], a[maxn];
int cmp(int a, int b)
{
return a < b;
}
int main()
{
int n, L = 0, R = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &l[i], &r[i]);
L += l[i];
R += r[i];
}
int index, max1 = abs(L-R), flag = 0;
for(int i = 1; i <= n; i++)
{
L = L - l[i] + r[i];
R = R - r[i] + l[i];
// cout << L << endl << R << endl;
if(abs(L-R) > max1)
{
flag = 1;
max1 = abs(L-R);
index = i;
}
L = L - r[i] + l[i]; //还要换回去
R = R - l[i] + r[i];
}
if(flag)
cout << index << endl;
else
cout << 0 << endl;
return 0;
}
思路2:记录每组当前的差值,两堆数交换就是总的差值减去这组数据差值*2,因为,如果l[i] - r[i] = a > 0,交换之后肯定就是-a了,这样总的差值就要先-a(假设没有这两个数),再-a(交换成功)。同理 a < 0#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int l[maxn], r[maxn], d[maxn], n;
int main(void)
{
while(cin >> n)
{
int lsum = 0, rsum = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &l[i], &r[i]);
lsum += l[i];
rsum += r[i];
d[i] = l[i]-r[i]; //记录每组数据
}
int del = lsum-rsum;
int ans = abs(del), index = 0;
for(int i = 1; i <= n; i++)
{
if(abs(del-2*d[i]) > ans) //直接用差值-这组数据差值的2倍就行
{
ans = abs(del-2*d[i]);
index = i;
}
}
cout << index << endl;
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian
population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.
There will be n columns participating in the parade, the i-th
column consists of li soldiers,
who start to march from left leg, and risoldiers,
who start to march from right leg.
The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march
from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal|L - R|.
No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this
columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index iand
swap values li and ri.
Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
Input
The first line contains single integer n (1 ≤ n ≤ 105) —
the number of columns.
The next n lines contain the pairs of integers li and ri (1 ≤ li, ri ≤ 500) —
the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Output
Print single integer k — the number of the column in which soldiers need to change the leg from which they start to march, or 0 if
the maximum beauty is already reached.
Consider that columns are numbered from 1 to n in the order they are given in the input data.
If there are several answers, print any of them.
Examples
input
3 5 6 8 9 10 3
output
3
input
2 6 5 5 6
output
1
input
6
5 9
1 34 8
4 5
23 54
12 32
output
0
Note
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal5 + 8 + 10 = 23,
and from the right leg — 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.
If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal5 + 8 + 3 = 16,
and who march from the right leg — 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.
It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
题意:给你两堆数,交换其中对应的两个数字,让他们的差值更大,让你告诉他换哪组数,如果已经最大了就直接输出0;
思路1:真的换过来,注意缓过来之后还要换回去
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1e5 + 5;
int l[maxn], r[maxn], a[maxn];
int cmp(int a, int b)
{
return a < b;
}
int main()
{
int n, L = 0, R = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &l[i], &r[i]);
L += l[i];
R += r[i];
}
int index, max1 = abs(L-R), flag = 0;
for(int i = 1; i <= n; i++)
{
L = L - l[i] + r[i];
R = R - r[i] + l[i];
// cout << L << endl << R << endl;
if(abs(L-R) > max1)
{
flag = 1;
max1 = abs(L-R);
index = i;
}
L = L - r[i] + l[i]; //还要换回去
R = R - l[i] + r[i];
}
if(flag)
cout << index << endl;
else
cout << 0 << endl;
return 0;
}
思路2:记录每组当前的差值,两堆数交换就是总的差值减去这组数据差值*2,因为,如果l[i] - r[i] = a > 0,交换之后肯定就是-a了,这样总的差值就要先-a(假设没有这两个数),再-a(交换成功)。同理 a < 0#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int l[maxn], r[maxn], d[maxn], n;
int main(void)
{
while(cin >> n)
{
int lsum = 0, rsum = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &l[i], &r[i]);
lsum += l[i];
rsum += r[i];
d[i] = l[i]-r[i]; //记录每组数据
}
int del = lsum-rsum;
int ans = abs(del), index = 0;
for(int i = 1; i <= n; i++)
{
if(abs(del-2*d[i]) > ans) //直接用差值-这组数据差值的2倍就行
{
ans = abs(del-2*d[i]);
index = i;
}
}
cout << index << endl;
}
return 0;
}
相关文章推荐
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】
- Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】
- [广义后缀自动机] BZOJ 3473 字符串 & BZOJ 3277 串 & Codeforces 204E #129 (Div. 1) E. Little Elephant and Strings
- Codeforces Round #376 (Div. 2) F. Video Cards 数学 & 暴力
- 交换a串的位置让b.c串出现最多次 暴力 Codeforces Div. 2 B. ZgukistringZ
- 暴力字符串 Codeforces Div. 2 A Mr. Kitayuta's Gift
- Codeforces 495 B. Modular Equations && Codeforces Round #282 (Div. 2)
- Codeforces Round #383 (Div. 2) && Codeforces 742 B (标记技巧)
- Codeforces Round #363(Div. 2) A. Launch of Collider【模拟】 && B. One Bomb【暴力】
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)
- [组合数] 51Nod 1161 Partial Sums & Codeforces 223C #138 (Div. 1) Partial Sums
- Codeforces 136 divIID && zoj 3633 && HDU 4358
- Codeforces 716 B. Complete the Word(暴力)——Codeforces Round #372 (Div. 2)
- [数论] 51Nod 1217 Minimum Modular & Codeforces 303C #183 (Div. 1) Minimum Modular
- codeforces-16A-A. Flag (C && 简单模拟 && 暴力农夫山泉)
- Codeforces 495 A. Digital Counterr && Codeforces Round #282 (Div. 2)
- 付忠庆的练习小笔记-Codeforces #277.5 Div2 A&D
- CodeForces - 633D Fibonacci-ish (map&暴力)
- Codeforces Round #378 (Div. 2) C && Codeforces 733C (模拟)详解(两种写法)