Daliy Algorithm -- day 100
Nothing to fear
种一棵树最好的时间是十年前,其次是现在!
那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~
2020.8.5
人一我十,人十我百,追逐青春的梦想,怀着自信的心,永不言弃!
Trouble Sort
这题是这样的,如果这个序列中存在符号相反的数(\(b_{i} \not= b_{j}\))则一定能够排好序.
所以我们只需要检查序列中是否出现了异号元素。但是要排除一种特殊情况
- 序列中不存在异号元素,但是序列已经是一个非递减序列。这种情况输出YES
- 序列中同时存在异号元素输出"YES"
- 其他情况输出"NO"
#include <bits/stdc++.h>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
int test;
void slove()
{
int n;cin >> n;
vector<int> a(n+1, 0) , b(n + 1, 0);
bool f1 = 0 , f0 = 0;
for(int i = 0;i < n;i ++)cin >> a[i];
for(int i = 0;i < n;i ++){
cin >> b[i];
if(b[i])f1 = 1;
else f0 = 1;
}
if((f1 && f0) || n == 1)
{
cout << "YES" << endl;
return ;
}
bool flag = 0;
if(f1 || f0)
{
for(int i = 0;i <= n - 2;i ++)
{
if(a[i] > a[i + 1])
flag = 1;
}
if(!flag)
{
cout << "YES" << endl;
return;
}
}
cout << "NO" << endl;
}
int main()
{
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed; // 在iomanip中
#endif
SIS;
cin >> test;
while(test--)
{
slove();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
Matrix Game
其实就是数一下一共有多少个行和列都为空的单元,如果存奇数个,那么先手一定会赢,否则后手赢。
#include <bits/stdc++.h>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
using namespace std;
typedef long long ll;
const int MAX = 0x7ffffff;
const int N = 55;
int test;
int a[N][N];
void slove()
{
int n , m;
cin >> n >> m;
set<int> r , c;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
cin >> a[i][j];
if(a[i][j] == 1)
r.insert(i) , c.insert(j);
}
}
int mn = min(n - r.size() , m - c.size());
if(mn & 1)cout << "Ashish" << endl;
else cout << "Vivek" << endl;
}
int main()
{
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed; // 在iomanip中
#endif
SIS;
cin >> test;
while(test--)
{
slove();
}
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
}
食物链
扩展域并查集,终于搞定一道题了 , nice!
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int f[N];
int n , m;
int find(int x)
{
if(f[x] != x)f[x] = find(f[x]);
return f[x];
}
void Union(int a,int b)
{
int A = find(a) , B = find(b);
f[A] = B;
}
bool checkTong(int x,int y)
{
if(find(x + n + n) == find(y))return false;
if(find(x) == find(y + n + n))return false;
return true;
}
bool checkMao(int x,int y)
{
if(x == y)return false;
if(find(x) == find(y))return false;
if(find(x) == find(y + n + n))return false;
return true;
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= 3 * n ;i ++)f[i] = i;
int ans = 0 , d , x , y;
for(int i = 0;i < m;i ++)
{
scanf("%d %d %d",&d,&x,&y);
if(x > n || y > n){
ans++;
continue;
}
if(d == 1)
{
if(checkTong(x , y))
{
Union(x + n + n, y + n + n);
Union(x , y);
Union(x + n , y + n);
}else ans++;
}else{
if(checkMao(x , y))
{
Union(x + n + n, y);
Union(x , y + n);
Union(x + n , y + n + n);
}else ans++;
}
}
cout << ans << endl;
return 0;
}