topcoder srm 702 div1 -3
1、给定一个$n*m$的矩阵,里面的数字是1到$n*m$的一个排列。一个$n*m$矩阵$A$对应一个$n*m$ 的01字符串,字符串的位置$i*m+j$为1当且仅当$A_{i,j}=i*m+j+1$。现在可以交换矩阵的两列或者两行。在任意多次操作后使得矩阵对应的字符串最大?
思路:从1到$n*m$,依次枚举。每次将当前数字填回正确位置。比较该次操作前后是否变大。不变大则不做本次操作。
#include <stdio.h>
#include <vector>
#include <string>
using namespace std; class GridSortMax
{
int a[55][55];
int b[55][55];
int N,M; void reset(int t) {
for(int i=0;i<N;++i) for(int j=0;j<M;++j) {
if(t==0) b[i][j]=a[i][j];
else a[i][j]=b[i][j];
}
} pair<int,int> get(int t) {
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
if(a[i][j]==t) return make_pair(i,j);
}
}
} void swapCol(int y1,int y2) {
for(int i=0;i<N;++i) swap(a[i][y1],a[i][y2]);
} void swapRow(int x1,int x2) {
for(int i=0;i<M;++i) swap(a[x1][i],a[x2][i]);
} string getAns() {
string s="";
for(int i=0;i<N;++i) for(int j=0;j<M;++j) {
if(a[i][j]==i*M+j+1) s+="1";
else s+="0";
}
return s;
} public:
string findMax(int n,int m,vector<int> grid)
{
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) {
a[i][j]=grid[i*m+j];
}
}
N=n;
M=m;
for(int i=1;i<=n*m;++i) {
pair<int,int> p=get(i);
int x=p.first;
int y=p.second;
int xx=(i-1)/m;
int yy=(i-1)%m;
if(x==xx&&y==yy) continue; string s0=getAns();
reset(0); if(x!=xx) swapRow(x,xx);
if(y!=yy) swapCol(y,yy); string s1=getAns();
if(s1<s0) reset(1); } return getAns(); }
};
2、(1)“()”是合法的字符串;(2)如果s是合法的,那么"(sss..ss)"是合法的,即1个或多个s外面加一层圆括号。给定字符串$S$以及 $k$,问$S$的所有合法子列的集合中(不重复),第$k$小的是哪个?$|S|<=150$,$k<=10^{9}$
思路:首先得到所有合法的字符串,然后分别枚举是不是$S$的子列。
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
using namespace std; set<string> s; void dfs(string x) {
if(x.size()>150) return;
s.insert(x);
string p="";
for(int i=0;p.size()<=150;++i) {
p+=x;
dfs("("+p+")");
}
} class RepeatedStrings
{
public:
string findKth(string S,int k)
{
dfs("()");
for(string x:s) {
int p1=0,p2=0;
while(p1<(int)S.size()&&p2<(int)x.size()) {
if(S[p1]==x[p2]) ++p1,++p2;
else ++p1;
}
if(p2>=(int)x.size()) {
if(--k==0) return x;
}
}
return "";
}
};
topcoder srm 702 div1 -3的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- 【topcoder SRM 702 DIV 2 250】TestTaking
Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
随机推荐
- gitlab数据迁移至其他gitlb服务器上
需求: A : 待迁移服务器,上边存有数据 B:接收项目得服务器,本身存在数据 验证方案: 一,搭建gitlab8.15.2 OS:rhel7.4 yum install policycoreutil ...
- Koa中设置中文Cookie值
默认情况下, 如果 ctx.cookies.set('user', '杨过', { domain: 'xxxx', path: 'xxxx', maxAge: 24 * 60 * 60 * 1000, ...
- shadow一键安装
https://blog.csdn.net/qq_4278923/article/details/80909686
- java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition的解决方案
今天在GitHub上面看到一个有意思的项目,下载下来,使用tomcat部署失败,出现异常,网上说JDK版本太高,改低,还是失败. 由于本人有个习惯,更喜欢把项目直接放入tomcat webapps 里 ...
- caffe-ssd运行create_data.sh的时候报错:SSD from caffe.proto import caffe_pb2 ImportError: No module named caffe.proto
在用voc2007和voc2012的数据训练基于caffe的SSD模型的时候,我们需要将图片数据转换成lmdb格式,运行脚本文件是SSD源码里面提供的create_data.sh(具体位置在$CAFF ...
- 《大话设计模式》c++实现 之策略模式
一.UML图 二.概念 策略模式:他定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户. 三.优点 (1)策略模式是一种定义一系列算法的方法,从 ...
- Python 3 -- 数据结构(list, dict, set,tuple )
看了<Head First Python>后,觉得写的很不错,适合新手.此处为读书笔记,方便日后查看. Python 提供了4中数据结构:list,dict,set,tuple. 每种结构 ...
- codeoforces 932A
题意: A和B在玩一个游戏,首先有一个X0 >= 3,之后选择一个小于X0的质数p,然后在找一个最小的X1 >= X0,并且p可以整除X1:之后再选择一个小于X1的质数p,然后再找一个最小 ...
- 【Hadoop学习之五】win7+Eclipse+hadoop3搭建本机开发环境
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 拓扑: 知识准备: 1.eclip ...
- Map集合——双列集合
双列集合<k, v> Map: Map 和 HashMap是无序的: LinkedHashMap是有序的: HashMap & LinkedHashMap: put方法: 其中,可 ...