A-小红喜欢1_牛客周赛 Round 57 (nowcoder.com)
思路:
简单记录一下
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int a[N];
void slove()
{
int flag = 0;
for (int i = 0; i < 5; i++)
{
int x;
cin >> x;
if (x == 1)
flag = i + 1;
}
cout << flag;
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
B-小红的树切割_牛客周赛 Round 57 (nowcoder.com)
思路:
有相同的就切边
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int a[N];
void slove()
{
int n;
cin >> n;
string s;
cin >> s;
s = " " + s;
int ans = 0;
for (int i = 1; i <= n - 1; i++)
{
int u, v;
cin >> u >> v;
if (s[u] == s[v])
ans++;
}
cout << ans;
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
C-小红的双好数(easy)_牛客周赛 Round 57 (nowcoder.com)
思路:
推一下发现 只要n大于等于三 总可以又2进制和n进制表现,1的时候除了1进制,其它也可以表现,唯独2时,输出NO。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int a[N];
void slove()
{
int n;
cin >> n;
if (n == 1)
{
cout << "YES\n";
cout << 2 << " " << 3;
}
else if (n == 2)
cout << "NO\n";
else
{
cout << "YES\n";
cout << 2 << " " << n;
}
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
D-小红的线段_牛客周赛 Round 57 (nowcoder.com)
思路:
将所有的点分为:在线上、在线中、在线下。线上和线下的点优先相连,必与直线相交;多的和线中的点相连,也与直线相交;若线上和线下的点先用完则线中的点相连,也必相交。若线中的点先用完,只剩下线上的点或者线下的点,即不可相连与直线相交
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#define int long long
using namespace std;
const int N = 1e3 + 10;
int a[N];
void slove()
{
vector<int> v1, v2, v3;
int n, k, b;
cin >> n >> k >> b;
for (int i = 1; i <= 2*n; i++)
{
int x, y;
cin >> x >> y;
if (k * x + b > y)//线上
v1.push_back(i);
else if (k * x + b == y)//线中
v2.push_back(i);
else//线下
v3.push_back(i);
}
//cout << v1.size() << " " << v2.size() << " " << v3.size() << "\n";
int ans = 0;
if (v1.size() > v3.size())//多的在v3里
swap(v1, v3);
vector<pair<int, int>>q;
for (int i = 0; i < v1.size(); i++)// 上下的点相连必相交
{
q.push_back({ v1[i], v3[i] });
}
int j,i;
for (i = v1.size(),j=0; i < v3.size()&&j<v2.size(); i++,j++)//线上的点和其它点相连,必和直线相交
{
q.push_back({ v3[i],v2[j] });
}
vector<pair<int, int>> f;
if (j!=v2.size())//线上的点 没用完 线上的点相连 必相交
{
for (int i = j; i < v2.size(); i++)
{
q.push_back({ v2[i],v2[++i] });
}
}
else
{
for (; i < v3.size(); i++)//多的点 不能相交
{
f.push_back({ v3[i],v3[++i] });
}
}
cout << q.size() << "\n";
for (int i = 0; i < f.size(); i++)
{
cout << f[i].first << " " << f[i].second << " N\n";
}
for (int i = 0; i < q.size(); i++)
{
cout << q[i].first << " " << q[i].second << " Y\n";
}
}
signed main()
{
int t = 1;
//cin >> t;
while (t--)
{
slove();
}
}
F-小红的数组操作_牛客周赛 Round 57 (nowcoder.com)
思路:
线段树
代码:
#include <iostream>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, cnt[N], q, tot;
int b[N];
struct Node
{
int l; int r;
int mi;
} tr[N << 2];
void pushup(int u)
{
tr[u].mi = min(tr[u << 1].mi, tr[u << 1 | 1].mi);
}
void build(int u, int l, int r)
{
tr[u].l = l; tr[u].r = r;
if (l == r)
{
tr[u] = {l, r, b[l]};
}
else
{
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void modify(int u, int x, int v)
{
if (tr[u].l == x && tr[u].r == x)
{
tr[u].mi = v;
}
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
if (x > mid) modify(u << 1 | 1, x, v);
pushup(u);
}
}
int query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r) return tr[u].mi;
else
{
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid)
return query(u << 1, l, r);
else if (l > mid)
return query(u << 1 | 1, l ,r);
else
{
auto left = query(u << 1, l, r);
auto right = query(u << 1 | 1, l, r);
return min(left, right);
}
}
}
signed main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
int x; cin >> x;
cnt[i] = cnt[i - 1] + x;
for (int j = 1; j <= x; j ++)
{
int y; cin >> y;
b[ ++ tot] = y;
}
}
build(1, 1, tot);
cin >> q;
while (q --)
{
int op; cin >> op;
if (op == 1)
{
int i, j, x;
cin >> i >> j >> x;
modify(1, cnt[i - 1] + j, x);
} else
{
int i; cin >> i;
cout << query(1, 1, cnt[i]) << endl;
}
}
}