Codeforces Round #270
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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; bool isp[1001000]; void get_prime()
{
isp[1]=isp[0]=true;
for(int i=2;i<=1000010;i++)
{
if(isp[i]==false)
for(int j=i*2;j<=1000010;j+=i)
isp[j]=true;
}
} int main()
{
get_prime();
int n;
scanf("%d",&n);
int x,y;
for(int i=2;i<=n;i++)
{
if(isp[i]==true&&isp[n-i]==true)
{
printf("%d %d\n",i,n-i);
return 0;
}
}
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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,k;
int f[2200];
int all[2200]; int main()
{
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d",f+i);
all[f[i]]++;
}
int now=0,high=1,ans=0;
for(int i=2000;i>=1;(all[i]==0)?i--:i+=0)
{
if(all[i])
{
if(now==0) high=i;
if(now+all[i]<=k)
{
now+=all[i];
all[i]=0;
}
else /// now + all[i]>k
{
int res=k-now;
all[i]-=res;
now=k;
}
}
if(now==k||i==1)
{
ans+=(high-1)*2;
now=0;high=1;
}
}
printf("%d\n",ans);
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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; int n,p[100100];
struct NME
{
char str1[60],str2[60];
}name[100100]; int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s%s",name[i].str1,name[i].str2);
//cout<<name[i].str1<<" ... "<<name[i].str2<<endl;
}
for(int i=1;i<=n;i++) scanf("%d",p+i);
int pos=-1,se=-1;
bool flag=true; if(strcmp(name[p[1]].str1,name[p[1]].str2)<0) pos=p[1],se=1;
else pos=p[1],se=2;
for(int i=2;i<=n&&flag;i++)
{
int id=p[i];
if(se==1)
{
bool ok1=false,ok2=false;
if(strcmp(name[pos].str1,name[id].str1)<0)
{
ok1=true;
}
if(strcmp(name[pos].str1,name[id].str2)<0)
{
ok2=true;
}
if(ok1&&ok2)
{
if(strcmp(name[id].str1,name[id].str2)<=0)
{
pos=id; se=1;
}
else if(strcmp(name[id].str2,name[id].str1)<0)
{
pos=id; se=2;
}
}
else if(ok1==true&&ok2==false)
{
pos=id; se=1;
}
else if(ok1==false&&ok2==true)
{
pos=id; se=2;
}
else
{
flag=false;
break;
}
}
else if(se==2)
{
bool ok1=false,ok2=false;
if(strcmp(name[pos].str2,name[id].str1)<0)
{
ok1=true;
}
if(strcmp(name[pos].str2,name[id].str2)<0)
{
ok2=true;
}
if(ok1&&ok2)
{
if(strcmp(name[id].str1,name[id].str2)<=0)
{
pos=id; se=1;
}
else if(strcmp(name[id].str2,name[id].str1)<0)
{
pos=id; se=2;
}
}
else if(ok1==true&&ok2==false)
{
pos=id; se=1;
}
else if(ok1==false&&ok2==true)
{
pos=id; se=2;
}
else
{
flag=false;
break;
}
}
}
if(flag==true) puts("YES");
else puts("NO");
return 0;
}

D. Design Tutorial: Inverse the Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder
Open 2014 Round 2C, InverseRMQ, is a good example.

Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance
matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

Input

The first line contains an integer n (1 ≤ n ≤ 2000)
— the number of nodes in that graph.

Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109)
— the distance between node i and node j.

Output

If there exists such a tree, output "YES", otherwise output "NO".

Sample test(s)
input
3
0 2 7
2 0 9
7 9 0
output
YES
input
3
1 2 7
2 0 9
7 9 0
output
NO
input
3
0 2 2
7 0 9
7 9 0
output
NO
input
3
0 1 1
1 0 1
1 1 0
output
NO
input
2
0 0
0 0
output
NO
Note

In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.

In the second example, it is impossible because d1, 1 should
be 0, but it is 1.

In the third example, it is impossible because d1, 2 should
equal d2, 1.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; const int maxn=2222; int n;
LL d[maxn][maxn]; int main()
{
scanf("%d",&n);
bool flag=true;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%I64d",&d[i][j]);
if(i==j&&d[i][j]) flag=false;
}
}
for(int i=1;i<=n&&flag;i++)
for(int j=1;j<=n&&flag;j++)
{
if(d[i][j]!=d[j][i]) flag=false;
if(i!=j&&d[i][j]==0) flag=false;
}
if(flag==false)
{
puts("NO");
return 0;
}
for(int i=1;i<=n&&flag;i++)
{
LL MINI=(1LL<<62),pos=-1;
for(int j=1;j<=n;j++)
{
if(d[i][j]<MINI&&i!=j)
{
MINI=d[i][j]; pos=j;
}
}
for(int j=1;j<=n&&flag;j++)
{
if(j==i||j==pos) continue;
if(abs(d[pos][j]-d[i][j])!=d[i][pos])
{
flag=false;
}
}
}
if(flag==false)
{
puts("NO");
}
else
{
puts("YES");
}
return 0;
}

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

Codeforces Round #270 A~D的更多相关文章

  1. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  2. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  3. Codeforces Round #270 1001

    Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...

  4. Codeforces Round #270(利用prim算法)

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...

  5. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  6. 多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))

    题目链接:http://codeforces.com/contest/472/problem/A 题目: 题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和.此题则是将其修改为:一个大于 ...

  7. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  8. Codeforces Round #270 D C B A

    谈论最激烈的莫过于D题了! 看过的两种做法不得不ORZ,特别第二种,简直神一样!!!!! 1th:构造最小生成树. 我们提取所有的边出来按边排序,因为每次我们知道边的权值>0, 之后每次把边加入 ...

  9. Codeforces Round #270

    A 题意:给出一个数n,求满足a+b=n,且a+b均为合数的a,b 方法一:可以直接枚举i,n-i,判断a,n-i是否为合数 #include<iostream> #include< ...

随机推荐

  1. C++学习之虚继承

    http://blog.csdn.net/wangxingbao4227/article/details/6772579 C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同 ...

  2. setInterval(code, time)中code传递参数办法

    1.使用setInterval的场景 有时我们需要隔一定的时间执行一个方法,这时就会用到setInterval,但是由于这个方法是浏览器模拟出的Timer线程,在调用我们方法时不能为其传递参数. 2. ...

  3. VS2008非托管C++调用wcf(WebService)服务

    在Visual Studio 2008以及以后版本中,微软停止了非托管C++的直接WebService引用.不过ATL Server代码已经托管到开源网站上,我们可以找到ATL Server的源代码, ...

  4. QT自定义对象导入JavaScript脚本使用

    1.对象 项目属性要添加 QT += script自定义的对象头文件如下,实现正常就好,记得脚本里要调用的方法一定要定义在public slots:下,要不然调用时提示该对象没有*方法   #ifnd ...

  5. 《算法导论》读书笔记之排序算法—Merge Sort 归并排序算法

    自从打ACM以来也算是用归并排序了好久,现在就写一篇博客来介绍一下这个算法吧 :) 图片来自维基百科,显示了完整的归并排序过程.例如数组{38, 27, 43, 3, 9, 82, 10}. 在算法导 ...

  6. 经验总结:按需加载JS和css

    项目中做过这样的事情:所有页面都通过SSI指令 include这样一份public-js.shtml, 用来引入涉及到的js(包括公共的脚本 验证插件 自定义组件等),但是一些没有交互效果的页面根本不 ...

  7. MySQL Cluster-备份恢复初步测试

    参考文档   http://blog.chinaunix.net/uid-20639775-id-1617795.html  http://xxtianxiaxing.iteye.com/blog/5 ...

  8. java学习之线程的操作方法

    package com.gh.thread; /** * 线程实现的两种方法 * 1.继承thread类 * 2.实现Runnable接口 * @author ganhang * */ public ...

  9. sharepoint 2013 附件控件FileUpload怎样检验是否为图片的方法

    记录一下关于附件控件FileUpload怎样检验是否为图片的方法: function checkImg() { var fileObj =document.getElementById('<%= ...

  10. java.util.zip.Deflater 压缩 inflater解压 实例

    原文:java压缩解压缩类实例[转] package com.example.helloworld; import java.io.ByteArrayOutputStream; import java ...