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,每一行和下一行用不同的方法就能保证没有邻居. 其他的可以用爆搜,每次暴力和后面的一个编号交换并判断可行性 ...
随机推荐
- 把一个树莓派SD卡系统和文件迁移到空SD卡中
1.打开win32diskimager软件读出SD卡树莓派系统和文件到电脑的镜像文件中, 2.使用 SD card formatter 格式化SD卡 3.再用win32diskimager往空SD卡写 ...
- 【机器学习速成宝典】模型篇02线性回归【LR】(Python版)
目录 什么是线性回归 最小二乘法 一元线性回归 多元线性回归 什么是规范化 Python代码(sklearn库) 什么是线性回归(Linear regression) 引例 假设某地区租房价格只与房屋 ...
- 使用tushare获取股票实时分笔数据延时有多大
使用tushare获取股票实时分笔数据延时有多大 前几天分享了一段获取所有股票实时数据的代码,有用户积极留言,提出一个非常棒的问题:如果数据本生的延时非常严重,通过代码获取数据再快又有什么用呢? 一直 ...
- LeetCode_1114.按顺序打印(多线程)
LeetCode_1114 LeetCode-1114.按顺序打印 我们提供了一个类: public class Foo { public void one() { print("one&q ...
- lr_save_string和sprintf、lr_eval_string的使用
一.lr_save_string函数 1.该函数主要是将程序中的常量或变量保存为参数: //将常量保存为参数 lr_save_string("777","page&quo ...
- linux交叉编译Windows版本的ffmpeg
主要参考http://www.cnblogs.com/haibindev/archive/2011/12/01/2270126.html 在我的机器上编译libfaac的时候 出现问题了 输出如下 . ...
- 【零售小程序】—— webview嵌套web端项目(原生开发支付功能)
index → index.wxml 套webwiew // url 活动url bindmessage 接收信息 <web-view src='{{url}}' bindmessage='m ...
- 解决VirtualBox虚拟机装XP无声问题的简便办法
原文地址;http://www.2cto.com/os/201206/134887.html 解决VirtualBox虚拟机装XP无声问题的简便办法 解决VirtualBox虚拟机装XP无声问题的简便 ...
- NIO组件之channel
Java NIO指的是new IO ,相对OIO,也称non-blocking IO,对应四种基本IO类型中的IO多路复用,主要有有三大核心组件,Channel(管道),Buffer(缓冲区),sel ...
- 用U盘完成win10系统的安装
电脑太卡了,每次都要重装,然后每次忘记要从哪里开始动手,都要百度,仅以此篇记录下 目录 1.系统盘准备 2.从U盘启动安装 1.系统盘准备 第一步:在电脑中完成系统盘制作工具的安装,由于它是要依赖.n ...