第十七次codeforces竞技结束 #270 Div 2

这一场居然是七道题,当时还吓了一跳,只做了3道题怎么破之类的……

但是,嘛,稳定三道题是很重要的啦~不要在意这些细节~~~


A. Design Tutorial: Learn from Math

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.

For example, there is a statement called the "Goldbach's conjecture". It says: "each even number no less than four can be expressed as the sum of two primes". Let's modify it. How about a statement like that: "each integer no less than 12 can be expressed as the sum of two composite numbers." Not like the Goldbach's conjecture, I can prove this theorem.

You are given an integer n no less than 12, express it as a sum of two composite numbers.

Input

The only line contains an integer n (12 ≤ n ≤ 106).

Output

Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them.

Sample test(s)
input
12
output
4 8
input
15
output
6 9
input
23
output
8 15
input
1000000
output
500000 500000
Note

In the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well.

In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number.


我被这道题弄得简直哭了要…… 当时被坑得半死……

其实想多了啦大家~ 因为2以外的偶数都是合数对不对^_^,所以,奇数的话就是9 + (N-9),偶数的话是4 + (N-4)~~~

8018751 Sep/29/2014 03:38 Accepted
8018742 Sep/29/2014 03:36 Compilation error
8011448 Sep/28/2014 21:25 Wrong answer on test 32
7999693 Sep/28/2014 19:48 Hacked

Code(先给大家看看当时我的水代码):

#include <cmath> 
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = (int)1e6;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

int n,prime[N]={0},nump=0;
int isNotPrime[N]={1,1};

bool cmp(const int a, const int b)
{
	return a > b;
}

void getpri()
{
	for(int i=2;i<=N/2;i++)
	{
		if(!isNotPrime[i]) prime[nump++]=i;//cout<<i<<' ';
		for(int j=0;j<nump && i*prime[j]<N;j++)
		{
			isNotPrime[i*prime[j]]=1;
			if(!(i%prime[j]))break;
		}
	}
}

int main()
{
	cin>>n;
	int pos=1;
	getpri();
	
	for(int i=4;i<=n/2;i++)
	{
		if(i> prime[pos])pos++;
		if(i==prime[pos])continue;
		if(count(prime,prime+55555,n-i)==1)continue;
		else
		{
			cout<<i<<" "<<n-i<<endl;
			return 0;
		}
	}
	
	return 0;
}


嘛,实际上~

Code:

bool f=(n%2==1);
cout<<f?9:4<<" "<<f?n-9:n-4<<endl;
#include <iostream>
#include <algorithm>
using namespace std;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

int main()
{
	int n;	cin>>n;
	if(n%2==0)cout<<4<<" "<<n-4<<endl;
	else cout<<9<<" "<<n-9<<endl;
	return 0;
}

B. Design Tutorial: Learn from Life

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.

Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the fi-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).

What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator.

The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person.

Output

Output a single integer — the minimal time needed to achieve the goal.

Sample test(s)
input
3 2
2 3 4
output
8
input
4 2
50 100 50 100
output
296
input
10 3
2 2 2 2 2 2 2 2 2 2
output
8
Note

In first sample, an optimal solution is:

  1. The elevator takes up person #1 and person #2.
  2. It goes to the 2nd floor.
  3. Both people go out of the elevator.
  4. The elevator goes back to the 1st floor.
  5. Then the elevator takes up person #3.
  6. And it goes to the 2nd floor.
  7. It picks up person #2.
  8. Then it goes to the 3rd floor.
  9. Person #2 goes out.
  10. Then it goes to the 4th floor, where person #3 goes out.
  11. The elevator goes back to the 1st floor.


有一帮人想坐电梯上楼,如何安排可以让电梯经历的楼层数最少呢~

这道题正着想略烦……但是倒着来~~^_^

我们采取倒放的方式,先把楼层最高的几个送上去,下来的路上送第二第三……顺次的,到底层再送现在最高的几个(当然是装满)

这样一来就可以保证总楼层经历最少~

Code:

#include <cmath> 
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

bool cmp(const int a, const int b)
{
	return a > b;
}

int des[2048];

int main()
{
	int n,k;	cin>>n>>k;
	memset(des,0,sizeof des);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&des[i]);
	}
	sort(des,des+n,cmp);
	int rest=k,t=0;
	for(int i=0;i<n;i++)
	{
		if(rest>0)
		{
			if(rest==k) t+=2*(des[i]-1);
			rest--;
		}
		if(rest==0) rest=k;
		//cout<<i<<":"<<rest<<":"<<t<<endl;
	}
	cout<<t<<endl;
	return 0;
}

C. Design Tutorial: Make It Nondeterministic

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.

Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?

More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: .

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of people.

The next n lines each contains two strings. The i-th line contains strings fi and si (1 ≤ |fi|, |si| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.

The next line contains n distinct integers: p1, p2, ..., pn (1 ≤ pi ≤ n).

Output

If it is possible, output "YES", otherwise output "NO".

Sample test(s)
input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
output
NO
input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
output
YES
input
2
galileo galilei
nicolaus copernicus
2 1
output
YES
input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
output
NO
input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
output
YES
Note

In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.

In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.



这道题的意思是说每个人都有两个名字,可以任选其中一种来作为排序用,是否存在一种选择,令他们的名字可以按照给出的顺序字典序排序。

那么,我们就按照字典序把这所有的名字都排好序标记上是属于谁的名字,然后看是否存在满足这种条件的子序列即可~

Code:

#include <cmath> 
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<string,int> psi;
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))

struct cmp
{  
    bool operator()(psi a,psi b) 
    {  
        if(a.first!=b.first)
            return a.first>b.first;  
    }  
};  


priority_queue <psi,vector<psi>,cmp> p_q;

int main()
{
	int n;	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string s1,s2;
		cin>>s1>>s2;
		p_q.push(psi(s1,i));
		p_q.push(psi(s2,i));
	}
	//cout<<p_q.top().first<<"\t"<< p_q.top().second<<endl;
	for(int i=1;i<=n;i++)
	{
		int pos;	cin>>pos;
		while(!p_q.empty() && p_q.top().second!=pos) p_q.pop();
		if(p_q.empty())
		{
			cout<<"NO"<<endl;
			return 0;
		}
		p_q.pop();
	}
	cout<<"YES"<<endl;
	return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值