【 Codeforces Round #502】

A. The Rank

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.

There are nn students, each of them has a unique id (from 11 to nn). Thomas's id is 11. Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids.

In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids.

Please help John find out the rank of his son.

Input

The first line contains a single integer nn (1≤n≤10001≤n≤1000) — the number of students.

Each of the next nn lines contains four integers aiai, bibi, cici, and didi (0≤ai,bi,ci,di≤1000≤ai,bi,ci,di≤100) — the grades of the ii-th student on English, German, Math, and History. The id of the ii-th student is equal to ii.

Output

Print the rank of Thomas Smith. Thomas's id is 11.

Examples

input

Copy

5
100 98 100 100
100 100 100 100
100 100 99 99
90 99 90 100
100 98 60 99

output

Copy

2

input

Copy

6
100 80 90 99
60 60 60 60
90 60 100 60
60 100 60 80
100 100 0 100
0 0 0 0

output

Copy

1

Note

In the first sample, the students got total scores: 398398, 400400, 398398, 379379, and 357357. Among the 55 students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is 22.

In the second sample, the students got total scores: 369369, 240240, 310310, 300300, 300300, and 00. Among the 66 students, Thomas got the highest score, so his rank is 11.

排序,水过。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct st
{
	int e,g,m,h,id;
}s[maxn];
bool cmp(st a,st b)
{
	if(a.e+a.g+a.h+a.m==b.e+b.g+b.h+b.m)
	{
		return a.id<b.id;
	}
	return a.e+a.g+a.h+a.m>b.e+b.g+b.h+b.m;
}

int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		s[i].id=i;
		cin>>s[i].e>>s[i].g>>s[i].m>>s[i].h;
	}
	sort(s+1,s+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		if(s[i].id==1) 
		{
			cout<<i<<endl;
			break;
		}
	}
	return 0;
}

B. The Bits

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question:

Given two binary numbers aa and bb of length nn. How many different ways of swapping two digits in aa (only in aa, not bb) so that bitwise OR of these two numbers will be changed? In other words, let cc be the bitwise OR of aa and bb, you need to find the number of ways of swapping two bits in aa so that bitwise OR will not be equal to cc.

Note that binary numbers can contain leading zeros so that length of each number is exactly nn.

Bitwise OR is a binary operation. A result is a binary number which contains a one in each digit if there is a one in at least one of the two numbers. For example, 010102010102 OR 100112100112 = 110112110112.

Well, to your surprise, you are not Rudolf, and you don't need to help him…… You are the security staff! Please find the number of ways of swapping two bits in aa so that bitwise OR will be changed.

Input

The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of bits in each number.

The second line contains a binary number aa of length nn.

The third line contains a binary number bb of length nn.

Output

Print the number of ways to swap two bits in aa so that bitwise OR will be changed.

Examples

input

Copy

5
01011
11001

output

Copy

4

input

Copy

6
011000
010011

output

Copy

6

Note

In the first sample, you can swap bits that have indexes (1,4)(1,4), (2,3)(2,3), (3,4)(3,4), and (3,5)(3,5).

In the second example, you can swap bits that have indexes (1,2)(1,2), (1,3)(1,3), (2,4)(2,4), (3,4)(3,4), (3,5)(3,5), and (3,6)(3,6).

数学题(我数学真的很差QAQ)

t00\cdot t10+t00\cdot t11+t01\cdot t10

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+77;
#define INF 0x3f3f3f3f
char a[maxn],b[maxn];
int main(){
	ll n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<n;i++) cin>>b[i];
	ll n1,n2,n3,n4;
	n1=n2=n3=n4=0; 
	for(int i=0;i<n;++i){
		if(a[i] == '0' && b[i] == '0') n1++;
		else if(a[i] == '0' && b[i] == '1') n2++;
		else if(a[i] == '1' && b[i] == '0') n3++;
		else n4++;
	}
	cout<<n1*n3+n2*n3+n1*n4<<endl;
	return 0;
} 

C. The Phone Number

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!

The only thing Mrs. Smith remembered was that any permutation of nn can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.

The sequence of nn integers is called a permutation if it contains all integers from 11 to nn exactly once.

The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS).

A subsequence ai1,ai2,…,aikai1,ai2,…,aik where 1≤i1<i2<…<ik≤n1≤i1<i2<…<ik≤n is called increasing if ai1<ai2<ai3<…<aikai1<ai2<ai3<…<aik. If ai1>ai2>ai3>…>aikai1>ai2>ai3>…>aik, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.

For example, if there is a permutation [6,4,1,7,2,3,5][6,4,1,7,2,3,5], LIS of this permutation will be [1,2,3,5][1,2,3,5], so the length of LIS is equal to 44. LDScan be [6,4,1][6,4,1], [6,4,2][6,4,2], or [6,4,3][6,4,3], so the length of LDS is 33.

Note, the lengths of LIS and LDS can be different.

So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.

Input

The only line contains one integer nn (1≤n≤1051≤n≤105) — the length of permutation that you need to build.

Output

Print a permutation that gives a minimum sum of lengths of LIS and LDS.

If there are multiple answers, print any.

Examples

input

Copy

4

output

Copy

3 4 1 2

input

Copy

2

output

Copy

2 1

Note

In the first sample, you can build a permutation [3,4,1,2][3,4,1,2]. LIS is [3,4][3,4] (or [1,2][1,2]), so the length of LIS is equal to 22. LDS can be ony of [3,1][3,1], [4,2][4,2], [3,2][3,2], or [4,1][4,1]. The length of LDS is also equal to 22. The sum is equal to 44. Note that [3,4,1,2][3,4,1,2] is not the only permutation that is valid.

In the second sample, you can build a permutation [2,1][2,1]. LIS is [1][1] (or [2][2]), so the length of LIS is equal to 11. LDS is [2,1][2,1], so the length of LDS is equal to 22. The sum is equal to 33. Note that permutation [1,2][1,2] is also valid.

每隔\LARGE ^{\sqrt{n}}就把这些数翻转一下即可,这样就能使LIS+LDS最小。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+77;
#define INF 0x3f3f3f3f
stack<int> st;
int main(){
	ll n;
	cin>>n;
	int d=sqrt(n);
	for(int i=1;i<=n;i++)
	{
		st.push(i);
		if(i%d==0)
		{
			while(!st.empty())
			{
				cout<<st.top()<<" ";
				st.pop();
			}
		}
	}
		while(!st.empty())
		{
			cout<<st.top()<<" ";
			st.pop();
		}
	cout<<endl;
	return 0;
} 
//12
//3 2 1 6 5 4 9 8 7 12 11 10

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/81530158
今日推荐