Codeforces 907 矩阵编号不相邻构造 团操作状压DFS
A.
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
int main()
{
//freopen("out1.txt", "w", stdout);
int v1, v2, v3, vm;
int anser;
cin >> v1 >> v2 >> v3 >> vm;
int flag = ;
if (vm >= v2)
{
cout << - << endl;
exit();
}
for (int i = v3; i <= * v3; i++)
{
if (i >= vm && i <= * vm)
{
flag = ;
anser = i;
break;
}
}
if (flag)
{
cout << * v1 << endl;
cout << * v2 << endl;
cout << anser << endl;
}
else
{
cout << - << endl;
}
}
B.
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
char f[][];
bool judge(int x)
{
int flag = ;
int cur = (x - ) / ;
x = x % ;
if (x == )
{
x = ;
}
int cur1 = x - ;
// cout << "y" << " " << cur1 * 3 + 1 << " " << cur1 * 3 + 3 << endl;
// cout << "x" << " " << cur * 3 + 1 << " " << cur * 3 + 3 << endl;
for (int i = cur * + ; i <= cur * + ; i++)
{
for (int j = cur1 * + ; j <= cur1 * + ; j++)
{
if (f[i][j] == '.')
{
f[i][j] = '!';
flag = ;
}
}
}
if (flag)
{
return false;
}
else
{
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
if (f[i][j] == '.')
{
f[i][j] = '!';
}
}
}
return true;
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out1.txt", "w", stdout);
char now;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
scanf("%c", &now);
if (now != '.' && now != 'x' && now != 'o')
{
j--;
}
else
{
f[i][j] = now;
}
}
}
int x, y;
cin >> x >> y;
x = x % ;
if (x == )
{
x = ;
}
y = y % ;
if (y == )
{
y = ;
}
int want = (x - ) * + y;
//cout << want << endl;
judge(want);
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
cout << f[i][j];
if (j % == && j % != )
{
cout << " ";
}
if (j % == )
{
cout << endl;
}
}
if (i % == )
{
cout << endl;
}
}
}
C.
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
int flag = ;
int cur = ;
char letter = '.';
int num[];
int test[];
unordered_set<char> ans;
int main()
{
//freopen("out1.txt", "w", stdout);
ios::sync_with_stdio();
cin.tie();
cout.tie(); int n;
int anser = ;
cin >> n;
char a;
string b;
for (int i = ; i < ; i++)
{
ans.insert('a' + i);
}
for (int i = ; i <= n; i++)
{
cin >> a >> b;
if (ans.size() == )
{
if (a == '!')
{
anser++;
}
else if (a == '?')
{
if (b[] == letter)
{
break;
}
else if(!ans.count(b[]))
{
anser++;
}
}
continue;
}
if (a == '!')
{ unordered_set<char> now;
for (char j : b)
{
now.insert(j);
}
for (auto it = ans.begin(); it != ans.end();)
{
if (!now.count(*it))
{
ans.erase(it++);
}
else
{
it++;
}
}
}
else
{
for (char j : b)
{
ans.erase(j);
}
}
}
cout << anser << endl;
}
D.给你一个N*M的矩阵 每位的编号为(i-1)*m+j 要求你重新排列 使得重新排列后每个数与原来相邻的数互不相邻 不存在则输出-1
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
int n, m;
int ans[];
int pop = ;
int now[];
int f[][];
int num[][];
int cur[][];
int flagf = ;
stack<int> s;
void dfs(int x, int y)
{
if (x == n * m + )
{
flagf = ;
for (int i = n; i >= ; i--)
{
for (int j = m; j >= ; j--)
{
s.push(cur[i][j]);
}
}
return ;
}
if (flagf)
{
return ;
}
int aimx = (x - ) / m + ;
int aimy = (x - ) % m + ;
for (int i = ; i <= n * m; i++)
{
if ((y & ( << i)) == )
{
if ((!num[cur[aimx - ][aimy]][i]) && (!num[cur[aimx][aimy - ]][i]))
{
int numcur = y | ( << i);
//cout << aimx << " " << aimy << " " << i << endl;
cur[aimx][aimy] = i;
dfs(x + , numcur);
}
}
}
}
void solve()
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
f[i][j] = (i - ) * m + j;
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
for (int k = ; k <= ; k++)
{
int dx = i + turn[k][];
int dy = j + turn[k][];
if (f[dx][dy] != )
{
num[f[i][j]][f[dx][dy]] = num[f[dx][dy]][f[i][j]] = ;
}
} }
}
dfs(, );
if (flagf)
{
cout << "YES" << endl;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
cout << s.top() << " ";
s.pop();
}
cout << endl;
}
}
else
{
cout << "NO" << endl;
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out1.txt", "w", stdout);
while (cin >> n >> m)
{
if (n <= && m <= )
{
solve();
exit();
}
if (n < m)
{
printf("YES\n");
for (int i = ; i <= n; i++)
{
if (i & )
{
for (int j = (i - ) * m + ; j <= i * m; j += )
{
cout << j << " ";
}
for (int j = (i - ) * m + ; j <= i * m; j += )
{
cout << j << " ";
}
cout << endl;
}
else
{
for (int j = (i - ) * m + ; j <= i * m; j += )
{
cout << j << " ";
}
for (int j = (i - ) * m + ; j <= i * m; j += )
{
cout << j << " ";
}
cout << endl;
}
}
}
else
{
printf("YES\n");
for (int i = ; i <= m; i++)
{
if (i & )
{
now[i] = ;
}
else
{
now[i] = ;
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
ans[pop++] = (now[j] - ) * m + j;
now[j] += ;
if (now[j] > n)
{
now[j] = (j & ) + ;
}
//cout<<now[j]<<endl;
}
}
for (int i = ; i < pop; i++)
{
printf("%d", ans[i]);
if (i % m == )
{
printf("\n");
}
else
{
printf(" ");
}
}
}
}
}
E.状压DFS
#include <bits/stdc++.h>
#define PI acos(-1.0)
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define pb push_back
#define inf 1e9
//std::ios::sync_with_stdio(false);
using namespace std;
//priority_queue<int,vector<int>,greater<int>> que;
const double eps = 1.0e-8;
typedef pair<int, int> pairint;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + ;
const int maxm = ;
const int turn[][] = {{, }, { -, }, {, }, {, -}};
//next_permutation
ll mod = 1e9 + ;
int n, m;
int num[][];
stack<int> cur, ans;
int anser = ;
void dfs(int now)
{
if (cur.size() >= ans.size())
{
return ;
}
if (now == n)
{
for (int i = ; i < n; i++)
{
if (num[n][i] != ( << n) - )
{
return ;
}
}
ans = cur;
return ;
}
memcpy(num[now + ], num[now], sizeof(num[now]));
dfs(now + );
for (int i = ; i < n; i++)
{
if (( << i) & num[now][now])
{
num[now + ][i] |= num[now][now];
}
}
cur.push(now);
dfs(now + );
cur.pop();
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out1.txt", "w", stdout);
cin >> n >> m;
int pa, pb;
for (int i = ; i < n; i++)
{
num[][i] = << i;
//cout<<p[i]<<endl;
ans.push(i);
}
for (int i = ; i <= m; i++)
{
scanf("%d %d", &pa, &pb);
pa--, pb--;
num[][pa] |= << pb;
num[][pb] |= << pa;
}
//pf();
dfs();
cout << ans.size() << endl;
while (!ans.empty())
{
cout << ans.top() + << " ";
ans.pop();
}
cout << endl;
}
Codeforces 907 矩阵编号不相邻构造 团操作状压DFS的更多相关文章
- Xor-sequences CodeForces - 691E || 矩阵快速幂
Xor-sequences CodeForces - 691E 题意:在有n个数的数列中选k个数(可以重复选,可以不按顺序)形成一个数列,使得任意相邻两个数异或的结果转换成二进制后其中1的个数是三的倍 ...
- Educational Codeforces Round 12 D. Simple Subset 最大团
D. Simple Subset 题目连接: http://www.codeforces.com/contest/665/problem/D Description A tuple of positi ...
- Codeforces 610C:Harmony Analysis(构造)
[题目链接] http://codeforces.com/problemset/problem/610/C [题目大意] 构造出2^n个由1和-1组成的串使得其两两点积为0 [题解] 我们可以构造这样 ...
- Codeforces 804E The same permutation(构造)
[题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出 ...
- Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)
题目链接 http://codeforces.com/contest/1276/problem/C 题解 嗯,比赛结束前3min想到做法然后rush不出来了--比赛结束后又写了15min才过-- 以下 ...
- CodeForces 450B 矩阵
A - Jzzhu and Sequences Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- codeforces 477B B. Dreamoon and Sets(构造)
题目链接: B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
K. Perpetuum Mobile Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- Codeforces 906B. Seating of Students(构造+DFS)
行和列>4的可以直接构造,只要交叉着放就好了,比如1 3 5 2 4和2 4 1 3 5,每一行和下一行用不同的方法就能保证没有邻居. 其他的可以用爆搜,每次暴力和后面的一个编号交换并判断可行性 ...
随机推荐
- rtmp协议分析
最近需要做一个rtmp服务器,着手分析一下rtmp协议,开干. rtmp握手 这个推荐一篇文章讲解得比较透彻http://blog.sina.com.cn/s/blog_676e11660102v8b ...
- ORACLE 错误案例—ORA-27102: out of memory
SQL> startupORA-27102: out of memoryLinux-x86_64 Error: 28: No space left on deviceAdditional inf ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_01.mybatis课程介绍
- Ubuntu vimrc 和 bashrc 配置
先上效果图,把vimrc 和bashrc 备份一下.. vimrc: map <F9> :call SaveInputData()<CR> func! SaveInputDat ...
- wsl 下安装docker
docker for windows本身其实是可以直接用的,但是仍然有很多不足,比如说:权限问题.没有docker.sock文件.文件编码问题等.而win10自带的wsl可以非常完美地解决这些问题. ...
- Linux安装python3.6 和pip
Linux下安装Python3.6和第三方库 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!! ...
- Android专项测试监控资源
版本号 V 1.1.0 Android性能测试分为两类:1.一类为rom版本(系统)的性能测试2.一类为应用app的性能测试(本次主要关注点为app的性能测试) Android的app性能测试包括的测 ...
- shoi 魔法树
Harry Potter新学了一种魔法:可以改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术.这棵果树共有N个节点,其中节点0是根节点,每个节点u的父亲记为fa[u],保证有fa ...
- tensorflow学习之tf.truncated_normal和tf.random_noraml的区别
tf版本1.13.1,CPU 最近在tf里新学了一个函数,一查发现和tf.random_normal差不多,于是记录一下.. 1.首先是tf.truncated_normal函数 tf.truncat ...
- springboot项目中使用maven resources
maven resource 组件可以把pom的变量替换到相关的resouces目录中的资源文件变量 示例项目:内容中心 (文章管理) 生成jar包,生成docker ,生成k8s文件 1.项目结构 ...