0

0

Codeforces Round #277.5 (Div. 2)部分题解_html/css_WEB-ITnose

php中文网

php中文网

发布时间:2016-06-24 11:53:48

|

1108人浏览过

|

来源于php中文网

原创

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

55 2 5 1 4

output

20 34 2

input

610 20 20 40 60 60

output

input

2101 100

output

10 1


排序之后贪心一下

<span style="font-size:18px;">/*************************************************************************    > File Name: cf.cpp    > Author: acvcla    > QQ:     > Mail: acvcla@gmail.com     > Created Time: 2014年11月17日 星期一 23时34分13秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>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_backint A[maxn],B[maxn];std::vector<int>x,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<n;i++){				cin>>A[i];				B[i]=A[i];			}			sort(B,B+n);			for(int i=0;i<n;i++){				if(A[i]==B[i])continue;				int M=0;				for(int j=i+1;j<n;j++)if(A[j]==B[i]){					M=j;					if(B[j]==A[i]){						swap(A[i],A[j]);						x.pb(i);y.pb(j);						break;					}				}				if(A[i]==B[i])continue;				swap(A[i],A[M]);				x.pb(i);y.pb(M);			}			cout<<x.size()<<endl;			for(int i=0;i<x.size();i++){				cout<<x[i]<<' '<<y[i]<<endl;			}		}		return 0;}</span>


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

41 4 6 255 1 5 7 9

output

input

41 2 3 4410 11 12 13

output

input

51 1 1 1 131 2 3

output

直接贪心就好

<span style="font-size:18px;">/*************************************************************************    > File Name: cf.cpp    > Author: acvcla    > QQ:     > Mail: acvcla@gmail.com     > Created Time: 2014年11月17日 星期一 23时34分13秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>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_backint 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<<ans<<endl;		}		return 0;}</span>

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


贪心加细节

WowTo
WowTo

用AI建立视频知识库

下载

<span style="font-size:18px;">/*************************************************************************    > File Name: cf.cpp    > Author: acvcla    > QQ:     > Mail: acvcla@gmail.com     > Created Time: 2014年11月17日 星期一 23时34分13秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>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_backint 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"<<endl;				continue;			}			if(!s&&m>1||s>m*9){				cout<<"-1 -1"<<endl;continue;			}			ans2[m]=ans1[m]=0;			int d=s%9;			if(d==0){				if(t1==m){					cout<<ans1<<' '<<ans2<<endl;					continue;				}				ans1[0]='1';				ans1[m-t1]='8';				for(int i=m-t1-1;i>0;i--)ans1[i]='0';				for(int i=t1;i<m;i++)ans2[i]='0';			}else{				if(t1==m-1){					ans1[0]='0'+d;					ans2[m-1]='0'+d;				}else{					ans1[0]='1';					ans1[m-t1-1]='0'+d-1;					for(int i=m-t1-2;i>0;i--)ans1[i]='0';					ans2[t1]='0'+d;					for(int i=t1+1;i<m;i++)ans2[i]='0';				}			}			cout<<ans1<<' '<<ans2<<endl;		}		return 0;}</span>

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 41 22 31 44 3

output

input

4 121 21 31 42 12 32 43 13 23 44 14 24 3

output

12

暴力就好,如果u,v之间长度为2的路径有x条,且x>1,那么显然以u,v为起点和终点的四边形有c(x,2)个

<span style="font-size:18px;">/*************************************************************************    > File Name: cf.cpp    > Author: acvcla    > QQ:    > Mail: acvcla@gmail.com     > Created Time: 2014年11月17日 星期一 23时34分13秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>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_backint n,m;std::vector<int> 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<G[v].size();i++){		dfs(u,G[v][i],dist+1);	}}int main(){		ios_base::sync_with_stdio(false);		cin.tie(0);		while(cin>>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<<i<<' '<<j<<' '<<d[i][j]<<endl;					LL t=d[i][j];					ans+=(t*(t-1))/2;				}			}			cout<<ans<<endl;			}		return 0;}</span>

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 1000011

    output

    input

    4 4 1005000110101001011001

    output

    Note

    For the first test the required matrices are:

    011101110011110101

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


    按行dp,由于每行的和必须为2,所以可以在新建行的时候在列和为1或0的位置上选出两个来添加。直到最终列和全部为2.

    具体看代码

    <span style="font-size:18px;">/*************************************************************************    > File Name: cf.cpp    > Author: acvcla    > QQ:     > Mail: acvcla@gmail.com     > Created Time: 2014年11月17日 星期一 23时34分13秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>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_backLL 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;j<n;j++){					col[j+1]+=(s[j]=='1');				}			}			LL x=0,y=0;			for(int i=1;i<=n;i++){				if(col[i]==1)x++;				if(col[i]==0)y++;				if(col[i]>2){					cout<<0<<endl;					return 0;				}			}			cout<<dfs(x,y)<<endl;		}		return 0;}</span>



    HTML速学教程(入门课程)
    HTML速学教程(入门课程)

    HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

    下载

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    通义千问
    通义千问

    阿里巴巴推出的全能AI助手

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    Golang 实际项目案例:从需求到上线
    Golang 实际项目案例:从需求到上线

    《Golang 实际项目案例:从需求到上线》以真实业务场景为主线,完整覆盖需求分析、架构设计、模块拆分、编码实现、性能优化与部署上线全过程,强调工程规范与实践决策,帮助开发者打通从技术实现到系统交付的关键路径,提升独立完成 Go 项目的综合能力。

    17

    2026.02.26

    Golang Web 开发路线:构建高效后端服务
    Golang Web 开发路线:构建高效后端服务

    《Golang Web 开发路线:构建高效后端服务》围绕 Go 在后端领域的工程实践,系统讲解 Web 框架选型、路由设计、中间件机制、数据库访问与接口规范,结合高并发与可维护性思维,逐步构建稳定、高性能、易扩展的后端服务体系,帮助开发者形成完整的 Go Web 架构能力。

    17

    2026.02.26

    Golang 并发编程专题:掌握多核时代的核心技能
    Golang 并发编程专题:掌握多核时代的核心技能

    《Golang 并发编程专题:掌握多核时代的核心技能》系统讲解 Go 在并发领域的设计哲学与实践方法,深入剖析 goroutine、channel、调度模型与并发安全机制,结合真实场景与性能思维,帮助开发者构建高吞吐、低延迟、可扩展的并发程序,全面提升多核时代的工程能力。

    16

    2026.02.26

    batoto漫画官网入口与网页版访问指南
    batoto漫画官网入口与网页版访问指南

    本专题系统整理batoto漫画官方网站最新可用入口,涵盖最新官网地址、网页版登录页面及防走失访问方式说明,帮助用户快速找到batoto漫画官方平台,稳定在线阅读各类漫画内容。

    431

    2026.02.25

    Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法
    Steam官网正版入口与注册登录指南_新手快速进入游戏平台方法

    本专题系统整理Steam官网最新可用入口,涵盖网页版登录地址、新用户注册流程、账号登录方法及官方游戏商店访问说明,帮助新手玩家快速进入Steam平台,完成注册登录并管理个人游戏库。

    130

    2026.02.25

    TypeScript全栈项目架构与接口规范设计
    TypeScript全栈项目架构与接口规范设计

    本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。

    41

    2026.02.25

    Python数据处理流水线与ETL工程实战
    Python数据处理流水线与ETL工程实战

    本专题聚焦 Python 在数据工程场景下的实际应用,系统讲解 ETL 流程设计、数据抽取与清洗、批处理与增量处理方案,以及数据质量校验与异常处理机制。通过构建完整的数据处理流水线案例,帮助开发者掌握数据工程中的性能优化思路与工程化规范,为后续数据分析与机器学习提供稳定可靠的数据基础。

    15

    2026.02.25

    Java领域驱动设计(DDD)与复杂业务建模实战
    Java领域驱动设计(DDD)与复杂业务建模实战

    本专题围绕 Java 在复杂业务系统中的建模与架构设计展开,深入讲解领域驱动设计(DDD)的核心思想与落地实践。内容涵盖领域划分、聚合根设计、限界上下文、领域事件、贫血模型与充血模型对比,并结合实际业务案例,讲解如何在 Spring 体系中实现可演进的领域模型架构,帮助开发者应对复杂业务带来的系统演化挑战。

    9

    2026.02.25

    Golang 生态工具与框架:扩展开发能力
    Golang 生态工具与框架:扩展开发能力

    《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。

    21

    2026.02.24

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号