Codeforces Round #704 (Div. 2) DE
PS:构造场真是要我命
D
a * 0 and b * 1, use these numbers to generate two number x, y that x- y has k * 1
思路
首先k==0比较好构造
然后发现a + b - 2 < k || a == 0 || b == 1是不行的 直接no
其余的情况
当k <= a时
我们可以x构造成1111…110000…00的形式
y构造成1111…11000…00100…00,第一段连续的0有k个
当k > a时
x如上构造
y可以构造成111…10111…1000…01
最后两段连续的1和0的个数为k
代码
#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
//#include <unordered_map>
using namespace std;
#define dbg(x) cerr << #x " = " << x << endl;
typedef pair<int, int> P;
typedef long long ll;
#define FIN freopen("in.txt", "r", stdin);
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int a, b, k;
cin >> a >> b >> k;
if(k == 0)
{
cout << "Yes" << endl;
for(int i = 1; i <= b; i++) cout << 1;
for(int i = 1; i <= a; i++) cout << 0;
cout <<endl;
for(int i = 1; i <= b; i++) cout << 1;
for(int i = 1; i <= a; i++) cout << 0;
cout <<endl;
return 0;
}
if(a + b - 2 < k || a == 0 || b == 1)
{
cout << "NO" << endl;
return 0;
}
cout << "Yes" << endl;
for(int i = 0; i < b; i++)
{
cout << 1;
}
for(int i = 0; i < a; i++)
{
cout << 0;
}
cout << endl;
if(k <= a)
{
for(int i = 0; i < b-1; i++)
{
cout << 1;
}
for(int i = 0; i < k; i++)
{
cout << 0;
}
cout << 1;
for(int i = 1; i <= a - k; i++)
{
cout << 0;
}
cout << endl;
return 0;
}
else
{
for(int i = 1; i <= b - (k - a) - 1; i++)
{
cout << 1;
}
cout << 0;
for(int i = 1; i <= k - a; i++)
{
cout << 1;
}
for(int i = 1; i <= a-1; i++)
{
cout << 0;
}
cout << 1 <<endl;
}
return 0;
}
E
给n个数列,每个数列有m个数,请找出一个数列,使得该数列与每个数列不同项只有2个
思路
dfs……嗯改
用第一个数组和后面的数组相比
当不同的数<=2,不管
当不同的数>4,no
else
暴力修改每一个数,然后dfs
其实我不会分析这个复杂度……不过确实不是指数的,感觉应该是n*m
ps:当跳出循环嵌套的时候goto真好用,不过最好还是少用或者按顺序用比较好
代码
#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
//#include <unordered_map>
using namespace std;
#define dbg(x) cerr << #x " = " << x << endl;
typedef pair<int, int> P;
typedef long long ll;
#define FIN freopen("in.txt", "r", stdin);
const int MAXN = 2.5e5+5;
vector<int> v[MAXN];
int ans[MAXN];
int n, m;
bool dfs(int dep)
{
dbg(dep);
if(dep >= 3) return 0;
int flg = 1;
for(int j = 1; j < n; j++)
{
vector<int> pos;
for(int i = 0; i < m; i++)
{
if(v[0][i] != v[j][i])
{
pos.push_back(i);
}
}
int dif = pos.size();
dbg(dif);
if(dif <= 2) continue;
else if(dif > 4) return 0;
if(dep == 2) return 0;//剪枝一下比较好
for(int k = 0; k < pos.size(); k++)
{
int org = v[0][pos[k]];
v[0][pos[k]] = v[j][pos[k]];
if(dfs(dep+1))
{
flg = 1;
goto End;
}
else
{
v[0][pos[k]] = org;
}
}
flg = 0;
break;
}
End:
if(flg)
{
for(int i = 0; i < m; i++)
{
ans[i] = v[0][i];
}
}
return flg;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
int num;
cin >> num;
v[i].push_back(num);
}
}
int tmp = dfs(0);
if(tmp)
{
cout << "Yes" <<endl;
for(int i = 0; i < m; i++)
{
cout << ans[i] << ' ';
}
cout <<endl;
}
else
{
cout <<"No" <<endl;
}
return 0;
}