codeforces 刷题记录-2022-11-22

codeforces 刷题记录-2022-11-22

1.题目:

  1. 318 - A - Even and Odds
  2. 160 - A - Twins

1.1题目描述:

A. Even Odds

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.

Input

The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 1012).

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print the number that will stand at the position number k after Volodya’s manipulations.

Examples

input

Copy

10 3

output

5

input

7 7

output

6

Note

In the first sample Volodya’s sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5.

1.2.解题思路:

这道题解题的过程当中WA了两发,一次是首次尝试,将奇数和偶数分成两个数组进行存储然后根据题目输入的个数进行输出;另一次是将奇数和偶数进行分开两个数组进行存储,然后在进行合并(此方法可行,但是遇到大整数会超内存)。

正确的解法是:不用存储(从根本上避免了大量空间存储导致的超内存),直接对存储奇数和存储偶数(数组中间的两边)进行找规律,通过规律求得对应位置的数。

1.3 AC代码:

//author: Unlome
// Problem: A. Even Odds
// Contest: Codeforces - Codeforces Round #188 (Div. 2)
// URL: https://codeforces.com/contest/318/problem/A?f0a28=1
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include
using namespace std;
typedef long long ll;//使用long long 因为题目要求2^12
int main()
{
    ll n,k;
    cin>>n>>k;
    ll res=0;
    if(k<=(n+1)/2){//如果是位于左边的奇数
        cout<<(k*2)-1<

2.1题目描述:

A. Twins

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Imagine that you have a twin brother or sister. Having another person that looks exactly like you seems very unusual. It’s hard to say if having something of an alter ego is good or bad. And if you do have a twin, then you very well know what it’s like.

Now let’s imagine a typical morning in your family. You haven’t woken up yet, and Mom is already going to work. She has been so hasty that she has nearly forgotten to leave the two of her darling children some money to buy lunches in the school cafeteria. She fished in the purse and found some number of coins, or to be exact, n coins of arbitrary values a1, a2, …, an. But as Mom was running out of time, she didn’t split the coins for you two. So she scribbled a note asking you to split the money equally.

As you woke up, you found Mom’s coins and read her note. “But why split the money equally?” — you thought. After all, your twin is sleeping and he won’t know anything. So you decided to act like that: pick for yourself some subset of coins so that the sum of values of your coins is strictly larger than the sum of values of the remaining coins that your twin will have. However, you correctly thought that if you take too many coins, the twin will suspect the deception. So, you’ve decided to stick to the following strategy to avoid suspicions: you take the minimum number of coins, whose sum of values is strictly more than the sum of values of the remaining coins. On this basis, determine what minimum number of coins you need to take to divide them in the described manner.

Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of coins. The second line contains a sequence of n integers a1, a2, …, an (1 ≤ ai ≤ 100) — the coins’ values. All numbers are separated with spaces.

Output
In the single line print the single number — the minimum needed number of coins.

Examples

input

2
3 3

output

2

input

3
2 1 2

output

2

Note

In the first sample you will have to take 2 coins (you and your twin have sums equal to 6, 0 correspondingly). If you take 1 coin, you get sums 3, 3. If you take 0 coins, you get sums 0, 6. Those variants do not satisfy you as your sum should be strictly more that your twins’ sum.

In the second sample one coin isn’t enough for us, too. You can pick coins with values 1, 2 or 2, 2. In any case, the minimum number of coins equals 2.

2.2解题思路:

这道题是一道比较简单的题,所以直接过掉了,按题目的意思是需要把饼干分成两堆,使得一堆数量最少,且大于另一堆,所以想到对整个饼干进行排序,从大到小(贪心,因为需要最少的饼干),从第一个开始同时记录左边堆的总和,右边堆的总和。当左边>右边的时候,输出当前的左边的值,结束。

2.3AC代码:

// author: Unlome
// Problem: A. Twins
// Contest: Codeforces - Codeforces Round #111 (Div. 2)
// URL: https://codeforces.com/contest/160/problem/A?f0a28=1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include
using namespace std;
bool cmp(int a,int b){
	return a>b;
}
int main()
{
	int n,sum=0;
	cin>>n;
	vectora;
	for(int i=0;i>t;
		a.push_back(t);
		sum+=t;//记录所有饼干的总和
	}
	sort(a.begin(),a.end(),cmp);
	int c=0,nu=0;
	for(int i=0;isum)break;
	}
	cout<

3总结:

总体而言,第一题的思路有点小问题,下次可以多考虑一下时间复杂度和空间复杂度与输入数据范围的关系(比如第一题的超内存)。

你可能感兴趣的:(算法,刷题,CodeForces刷题记录,c++,算法)