程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #277.5 (Div. 2)部分題解

Codeforces Round #277.5 (Div. 2)部分題解

編輯:C++入門知識

Codeforces Round #277.5 (Div. 2)部分題解


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 kswaps, 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

排序之後貪心一下

/*************************************************************************
    > File Name: cf.cpp
    > Author: acvcla
    > QQ: 
    > Mail: [email protected] 
    > Created Time: 2014年11月17日 星期一 23時34分13秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 3e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int A[maxn],B[maxn];
std::vectorx,y;
int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(0);
		int n;
		int ans=0;
		while(cin>>n){
			x.clear();y.clear();
			for(int i=0;i>A[i];
				B[i]=A[i];
			}
			sort(B,B+n);
			for(int i=0;i

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 sequencea1,?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
直接貪心就好

/*************************************************************************
    > File Name: cf.cpp
    > Author: acvcla
    > QQ: 
    > Mail: [email protected] 
    > Created Time: 2014年11月17日 星期一 23時34分13秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 1e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int A[maxn],B[maxn];
int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(0);
		int n,m;
		while(cin>>n){
			memset(A,0,sizeof A);
			memset(B,0,sizeof B);
			int x;
			rep(i,1,n){
				cin>>x;
				++A[x];
			}
			cin>>m;
			rep(i,1,m){
				cin>>x;
				++B[x];
			}
			int ans=0;
			rep(i,1,100){
				if(A[i]<=0)continue;
				ans+=min(A[i],B[i-1]+B[i]+B[i+1]);
				if(A[i]>=B[i-1]+B[i]+B[i+1])B[i-1]=B[i]=B[i+1]=0;
				else{
					if(B[i-1]>0&&A[i]>0){
						int t=B[i-1];
						B[i-1]=max(B[i-1]-A[i],0);
						A[i]=max(A[i]-t,0);
					}
					if(B[i]>0&&A[i]>0){
						int t=B[i];
						B[i]=max(B[i]-A[i],0);
						A[i]=max(A[i]-t,0);
					}
					if(B[i+1]>0&&A[i]>0){
						int t=B[i+1];
						B[i+1]=max(B[i+1]-A[i],0);
						A[i]=max(A[i]-t,0);
					}
				}
			}
			cout<
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

貪心加細節

/*************************************************************************
    > File Name: cf.cpp
    > Author: acvcla
    > QQ: 
    > Mail: [email protected] 
    > Created Time: 2014年11月17日 星期一 23時34分13秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int main(){
		ios_base::sync_with_stdio(false);
		cin.tie(0);
		int m,s;
		char ans1[200],ans2[200];
		while(cin>>m>>s){
			bool ok=true;
			int t1=s/9;
			for(int i=0;i<150;i++)ans2[i]=ans1[i]='9';
			if(s==0&&m==1){
				cout<<"0 0"<1||s>m*9){
				cout<<"-1 -1"<0;i--)ans1[i]='0';
					ans2[t1]='0'+d;
					for(int i=t1+1;i
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
暴力就好,如果u,v之間長度為2的路徑有x條,且x>1,那麼顯然以u,v為起點和終點的四邊形有c(x,2)個

/*************************************************************************
    > File Name: cf.cpp
    > Author: acvcla
    > QQ:
    > Mail: [email protected] 
    > Created Time: 2014年11月17日 星期一 23時34分13秒
 ************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 3e3 + 10;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
int n,m;
std::vector G[maxn];
int d[maxn][maxn];
void dfs(int u,int v,int dist){
	if(dist>2)return;
	if(dist==2){
		++d[u][v];return ;
	}
	for(int i=0;i>n>>m){
			for(int i=1;i<=n;i++)G[i].clear();
			memset(d,0,sizeof d);
			int u,v;
			for(int i=1;i<=m;i++){
				cin>>u>>v;
				G[u].pb(v);
			}
			LL ans=0;
			for(int i=1;i<=n;i++)dfs(i,i,0);
			for(int i=1;i<=n;i++){
				for(int j=1;j<=n;j++){
					if(d[i][j]<2||j==i)continue;
					//cout<







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 numbermod.

    Input

    The first line of the input contains three integers n, m, mod (2?≤?n?≤?500, 0?≤?m?≤?n, 2?≤?mod?≤?109). Thenm 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?×?ntable 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.


    按行dp,由於每行的和必須為2,所以可以在新建行的時候在列和為1或0的位置上選出兩個來添加。直到最終列和全部為2.

    具體看代碼

    /*************************************************************************
        > File Name: cf.cpp
        > Author: acvcla
        > QQ: 
        > Mail: [email protected] 
        > Created Time: 2014年11月17日 星期一 23時34分13秒
     ************************************************************************/
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    typedef long long LL;
    const int maxn = 500+10;
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define pb push_back
    LL d[maxn][maxn],n,m,mod;
    bool vis[maxn][maxn];
    int col[maxn];
    char s[maxn];
    LL cn2(LL n){
    	return n*(n-1)/2;
    }
    LL dfs(LL x,LL y){//當前還有x列為1,y列為0,到達目標狀態的方案種數,之所以是每次選2個是因為每行的和都必須為2
    	if(x==0&&y==0)return 1;
    	if(vis[x][y])return d[x][y];
    	d[x][y]=0;
    	vis[x][y]=1;
    	if(x>=2)d[x][y]+=cn2(x)*dfs(x-2,y)%mod;//選出為1的兩列讓其加上1,此時和為1的為x-2,和為0的為y
    	if(y>=2)d[x][y]+=cn2(y)*dfs(x+2,y-2)%mod;//選出為0的兩列讓其加上1,此時和為1的為x+2,和為0的為y-2
    	if(x>=1&&y>=1)d[x][y]+=x*y*dfs(x,y-1)%mod;//各選一列加上1,此時和為1的為x,和為0的為y-1
    	return d[x][y]%=mod;
    }
    int main(){
    		ios_base::sync_with_stdio(false);
    		cin.tie(0);
    		while(cin>>n>>m>>mod){
    			memset(vis,0,sizeof vis);
    			memset(col,0,sizeof col);
    			rep(i,1,m){
    				cin>>s;
    				for(int j=0;j2){
    					cout<<0<


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved