转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud
A. SwapSort
time limit per test    1 second
memory limit per test      256 megabytes
input      standard input
output      standard output

In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.

Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.

Input

The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.

Output

In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.

Sample test(s)
Input
5
5 2 5 1 4
Output
2
0 3
4 2
Input
6
10 20 20 40 60 60
Output
0
Input
2
101 100
Output
1
0 1

问如何在n次交换之内将原数组排序成一个递增数组。

分析:每次交换都使得一位换到其对应的位子即可。n^2

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int a[],id[]; vector<pair<int,int> >v;
int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n)
{
v.clear();
for(int i=;i<n;i++)cin>>a[i];
int minn=;
for(int i=;i<n-;i++)
{
minn=i;
for(int j=i+;j<n;j++)
{
if(a[j]<a[minn])
{
minn=j;
}
}
if(i!=minn)
{
swap(a[i],a[minn]);
v.PB(MP(i,minn));
}
}
cout<<v.size()<<endl;
for(int i=;i<v.size();i++)
{
cout<<v[i].first<<" "<<v[i].second<<endl;
} }
return ;
}
 
B. BerSU Ball
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.

We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.

For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.

Input

The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence a1, a2, ..., an (1 ≤ ai ≤ 100), where ai is the i-th boy's dancing skill.

Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence b1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
Input
4
1 4 6 2
5
5 1 5 7 9
Output
3
Input
4
1 2 3 4
4
10 11 12 13
Output
0
Input
5
1 1 1 1 1
3
1 2 3
Output
2

题意:男女生结对,要求相互结对的男女的skill值相差不得超过1,问,最多能组几队。

方法一:贪心,男女生分别排序一下,而后每次去一个男生,找到满足要求的skill值最小的女生,组队,若无符合要求的,则该男生不组队。

方法二:二分图最大匹配,skill值差一的男女生间加一条边,而后求最大匹配

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
int V;
const int MAX_V=;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V]; void add_edge(int u,int v)
{
G[u].PB(v);
G[v].PB(u);
} bool dfs(int v)//增广路
{
used[v]=;
for(int i=;i<G[v].size();i++)
{
int u=G[v][i],w=match[u];
if(w<||!used[w]&&dfs(w))
{
match[u]=v;
match[v]=u;
return ;
}
}
return false ;
} int hungary()
{
int res=;
CLR(match,-);
for(int v=;v<V;v++)
{
if(match[v]<)
{
CLR(used,);
if(dfs(v))
{
res++;
}
}
}
return res;
} int a[],b[];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n)
{
for(int i=;i<n;i++)cin>>a[i];
cin>>m;
for(int i=;i<m;i++)cin>>b[i];
V=n+m;
for(int i=;i<V;i++)G[i].clear();
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(abs(a[i]-b[j])<=)
{
add_edge(i,n+j);
}
}
}
cout<<hungary()<<endl;
}
return ;
}
C. Given Length and Sum of Digits...
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

The single line of the input contains a pair of integers m, s (1 ≤ m ≤ 100, 0 ≤ s ≤ 900) — the length and the sum of the digits of the required numbers.

Output

In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).

Sample test(s)
Input
2 15
Output
69 96
Input
3 0
Output
-1 -1

题意:找出n位,且各个数位上的数的和为m的最小的数和最大的数。

分析:贪心,当m<1&&n!=1或者m>9*n时,无解,输出-1,否则,最小的数为从后往前贪,尽可能取大的,同时要保证最高位上至少能取得1;最大的数为从前向后贪,尽可能取大的,直到和已经达到m;

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; int a[];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m)
{
int temp=m;
if(m>*n||(m<&&n!=))cout<<-<<" "<<-<<endl;
else
{
for(int i=n;i>;i--)
{
if(m->)
{
a[i]=;
m-=;
}
else if(i!=)
{
a[i]=m-;
m=;
}
else
{
a[i]=m;
}
}
for(int i=;i<=n;i++)
{
cout<<a[i];
}
cout<<" ";
m=temp;
for(int i=;i<=n;i++)
{
if(m>)a[i]=;
else a[i]=m;
m-=a[i];
}
for(int i=;i<=n;i++)
{
cout<<a[i];
}
cout<<endl;
}
}
return ;
}
D. Unbearable Controversy of Being
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!

Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:

Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.

Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.

When rhombi are compared, the order of intersections b and d doesn't matter.

Input

The first line of the input contains a pair of integers n, m (1 ≤ n ≤ 3000, 0 ≤ m ≤ 30000) — the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 ≤ ai, bi ≤ n;ai ≠ bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.

It is not guaranteed that you can get from any intersection to any other one.

Output

Print the required number of "damn rhombi".

Sample test(s)
Input
5 4
1 2
2 3
1 4
4 3
Output
1
Input
4 12
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
Output
12

题意:求满足要求的菱形的数目。

枚举菱形的起点和终点两个结点,每次找出中间结点的数目cnt,则这两个结点为起点和终点的方案数C(cnt,2)。不断累加即可。

由于边数不超过30000

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=;
vector<int>g[maxn];
vector<int>rg[maxn];
int deg[maxn];
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m)
{
int u,v;
for(int i=;i<m;i++)
{
cin>>u>>v;
u--,v--;
g[u].push_back(v);
rg[v].push_back(u);
}
long long ans=;
for(int i=;i<n;i++)
{
memset(deg,,sizeof(deg));
for(int j=;j<g[i].size();j++)deg[g[i][j]]++; for(int j=;j<n;j++)
{
long long cnt=;
if(i==j)continue;
for(int k=;k<rg[j].size();k++)
{
if(deg[rg[j][k]])cnt++;
}
if(cnt>)ans+=(cnt-)*cnt/;
} }
cout<<ans<<endl; }
return ;
}
E. Hiking
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the i-th rest point the distance from the start equals xi, and its picturesqueness equals bi. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates xi.

Every day the traveler wants to cover the distance l. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance l every day and visit the most picturesque places.

Let's assume that if the traveler covers distance rj in a day, then he feels frustration , and his total frustration over the hike is calculated as the total frustration on all days.

Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used.

The traveler's path must end in the farthest rest point.

Input

The first line of the input contains integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 105) — the number of rest points and the optimal length of one day path.

Then n lines follow, each line describes one rest point as a pair of integers xi, bi (1 ≤ xi, bi ≤ 106). No two rest points have the same xi, the lines are given in the order of strictly increasing xi.

Output

Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to n in the order of increasing xi. The last printed number must be equal to n.

Sample test(s)
Input
5 9
10 10
20 10
30 1
31 5
40 10
Output
1 2 4 5 
Note

In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as

函数为单调函数,则可用二分。

不断二分答案然后判断此答案下的最佳方案所产生的误差即可

 #include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
int n,l;
const int maxn=;
const double eps=1e-;
int d[maxn],p[maxn];
double dp[maxn];
int pre[maxn];
double check(double mid)
{
memset(pre,,sizeof());
for(int i=;i<=n;i++)
{
dp[i]=1e10;
for(int j=;j<i;j++)
{
double temp=dp[j]+sqrt(abs(.+d[i]-d[j]-l))-mid*p[i];
if(temp<dp[i])
{
dp[i]=temp;
pre[i]=j;
}
}
}
return dp[n];
}
void dfs(int x)
{
if(pre[x])dfs(pre[x]);
cout<<x<<" ";
} int main()
{
ios::sync_with_stdio(false);
//freopen("a.in","r",stdin);
cin>>n>>l;
for(int i=;i<=n;i++)cin>>d[i]>>p[i];
double l=,r=1e10;
while(r-l>eps)
{
double mid=(l+r)/2.0;
if(check(mid)>eps)l=mid;
else r=mid;
}
dfs(n);
cout<<endl;
return ;
}
F. Special Matrices
time limit per test  1 second
memory limit per test  256 megabytes
input  standard input
output  standard output

An n × n square matrix is special, if:

  • it is binary, that is, each cell contains either a 0, or a 1;
  • the number of ones in each row and column equals 2.

You are given n and the first m rows of the matrix. Print the number of special n × n matrices, such that the first m rows coincide with the given ones.

As the required value can be rather large, print the remainder after dividing the value by the given number mod.

Input

The first line of the input contains three integers n, m, mod (2 ≤ n ≤ 500, 0 ≤ m ≤ n, 2 ≤ mod ≤ 109). Then m lines follow, each of them contains n characters — the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'. Each column of the given m × n table contains at most two numbers one.

Output

Print the remainder after dividing the required value by number mod.

Sample test(s)
Input
3 1 1000
011
Output
2
Input
4 4 100500
0110
1010
0101
1001
Output
1
Note

For the first test the required matrices are:

011
101
110 011
110
101

In the second test the required matrix is already fully given, so the answer is 1.

题意,对于一个n*n的矩阵,已经给出m行,问填满整个矩阵共有多少方案,其中,矩阵有以下要求:所填元素非0即1,每行每列有且必须有两个1

分析:dp。

dp[i][j]表示填满前(i/2+j)行有i列1个1,j列2个1的矩阵的方案数

递推方程为dp[i][j]=dp[i+2][j-2]*(i+2)*(i+1)/2+dp[i][j-1]*i*(n-i-j+1)+dp[i-2][j]*(n-i-j+2)*(n-i-j+1)/2;

注意好边界即可

 #include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
ll dp[][];
int deg[];
int x,y;
ll n,m,MOD;
ll dfs(ll a,ll b)
{
if(a<||b<y)return ;
if(dp[a][b]>=)return dp[a][b];
if(b==y&&a<x)return ;
dp[a][b]=;
dp[a][b]+=dfs(a+,b-)*(a+)*(a+)/;
dp[a][b]%=MOD;
dp[a][b]+=dfs(a,b-)*a*(n-a-b+);
dp[a][b]%=MOD;
dp[a][b]+=dfs(a-,b)*(n-a-b+)*(n-a-b+)/;
dp[a][b]%=MOD;
return dp[a][b];
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m>>MOD)
{
int a=,b;
char ch;
for(int i=;i<m;i++)
{
b=;
for(int j=;j<n;j++)
{
cin>>ch;
if(ch=='')
deg[j]++,b++;
}
if(b!=)a=;
}
if(a)
{
cout<<<<endl;
continue;
}
CLR(dp,-);
a=,b=;
for(int i=;i<n;i++)
{
if(deg[i]==)a++;
else if(deg[i]==)b++;
}
dp[a][b]=;
x=a,y=b;
cout<<dfs(,n)<<endl;
}
return ;
}

Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解的更多相关文章

  1. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  2. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  3. Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  4. Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

  5. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  6. Codeforces Round #277.5 (Div. 2)-B. BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

  7. Codeforces Round #277.5 (Div. 2)-A. SwapSort

    http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...

  8. Codeforces Round #277.5 (Div. 2)-D

    题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include< ...

  9. Codeforces Round #277.5 (Div. 2)B——BerSU Ball

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Mysql存储过程分析

    为了搞明白为什么mysql的存储过程是高效的,我们需要理解mysql的执行流程是什么,当输入sql语句之后,mysql会先进行sql语句语法正确性检查,然后再进行编译,然后才执行,最后把结果返回.如下 ...

  2. IE CSS Bug 系列

    1.[IE CSS Bug系列]IE6&IE7图片链接无效 <!doctype html> <html> <head> <meta charset=& ...

  3. hdu 5363Key Set

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363 Problem Description soda has a set S with n inte ...

  4. 利用Azure Redis Cache构建百万量级缓存读写

    Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...

  5. Codeforces 414B Mashmokh and ACM

    http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...

  6. BZOJ 1927 星际竞速

    http://www.lydsy.com/JudgeOnline/problem.php?id=1927 思路:把一个点拆成两个点, S->i 费用0,流量1 (代表这个点可以移动到其他点所必备 ...

  7. BZOJ 2599 Race(树分治)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2599 题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 题意:每次 ...

  8. 时刻注意QT与Windows系统的不同(惨痛教训)

    其实就一条:WINAPI使用'\',而QT使用'/'.如果程序立刻崩溃的话,是很难看出端倪的,所以要时刻注意...

  9. OS error set

    Failed to resolve/decode supposed IPv4 source addres Failed to resolve/decode supposed IPv4 source a ...

  10. Gradle Tips#1-tasks

    原文链接 以这篇博客開始,我将写一系列关于Gradle的文章,用来记录接触Gradle构建脚本以来我所理解的Gradle. 今天要讲的就是Gradle tasks以及task的配置和运行.可能有的读者 ...