VP*2 :P
A. Equal or Not Equal
n个数围城环,一行n个字母表示a[i]和a[i+1]的关系,特别的,i==n时,判断的是a[1]和a[n]的大小关系。给出这个判断是否相等的字符串,问是否存在这样一个数组满足该条件。
思路:一开始考虑的是最后一个字母和第一个字母的关系,特判了一波结果错了,忘了这是一个环,任何的位置都可以被视为开头或结尾。得出结论:一个字符串中仅含一个N的情况不能构造。
AC Code:
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0', ch = getchar();
}
x *= f;
}
template <typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int t;
std::string s;
int main() {
// freopen("test.in","r",stdin);
// freopen("output.in", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>t;
while(t--){
std::cin>>s;
int cnt=0;
int len=s.length();
for(int i=0;i<len;i++){
if(s[i]=='N') cnt++;
}
if(cnt==1)std::cout<<"NO"<<'\n';
else std::cout<<"YES"<<'\n';
}
return 0;
}
B. Triangles on a Rectangle
给出一个矩形,它的对角位于(0,0)和(w,h),给出一些位于矩形四条边上的点,选择三个点且有两个要位于同一条边上,问怎样选择使得三个点构造的三角形面积最大,输出面积的两倍。
思路:两个在同一条边上的点连成的线段一定是三角形的底,其对边到该底的高度最大,对于每一条底边,其高是确定的底边最长则需要判断取哪两个点差值最大,把分别以四条边为底的面积算出来取最大值即可。
AC Code:
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0', ch = getchar();
}
x *= f;
}
template <typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
ll t,w,h;
int main() {
// freopen("test.in","r",stdin);
// freopen("output.in", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>t;
while(t--){
std::cin>>w>>h;
ll k1,k2,k3,k4,x,ans1,ans2,ans3,ans4;
std::cin>>k1;
ll minn=1e9,maxn=-1;
for(ll i=1;i<=k1;i++){
std::cin>>x;
minn=std::min(minn,x);
maxn=std::max(maxn,x);
}
ans1=(maxn-minn)*h;
minn=1e9,maxn=-1;
std::cin>>k2;
for(ll i=1;i<=k2;i++){
std::cin>>x;
minn=std::min(minn,x);
maxn=std::max(maxn,x);
}
ans2=(maxn-minn)*h;
minn=1e9,maxn=-1;
std::cin>>k3;
for(ll i=1;i<=k3;i++){
std::cin>>x;
minn=std::min(minn,x);
maxn=std::max(maxn,x);
}
ans3=(maxn-minn)*w;
minn=1e9,maxn=-1;
std::cin>>k4;
for(ll i=1;i<=k4;i++){
std::cin>>x;
minn=std::min(minn,x);
maxn=std::max(maxn,x);
}
ans4=(maxn-minn)*w;
std::cout<<std::max({ans1,ans2,ans3,ans4})<<'\n';
}
return 0;
}
os:啊记得开long long。。。
C. BA-String
给出一个字符串,仅由“a”和“*”组成,“*”可由0~k个b替换,要求构造出字典序第x小的字符串。
思路: 利用进制思想去考虑。可以把每一位“*”加“a”视为一个k进制数,根据字典序的含义,对于字符串倒序后处理,每遇到一个“a”,加入前面“*”所代表的的k进制数取余所得,最后把构造的字符串取反即可。
AC Code:
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0', ch = getchar();
}
x *= f;
}
template <typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
ll t,n,k,x;
std::string s;
int main() {
// freopen("test.in","r",stdin);
// freopen("output.in", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>t;
while(t--){
std::cin>>n>>k>>x;
std::cin>>s;
--x;
reverse(s.begin(),s.end());
int cnt=0;
std::string ans;
for(auto u:s){
if(u=='a'){
ans+=std::string(x%(cnt*k+1),'b');
x/=(cnt*k+1); //加1为了防止模0没什么意义,这一串是进制计算
ans+='a';
cnt=0;
}
else cnt++;
}
ans+=std::string(x%(cnt*k+1),'b');
reverse(ans.begin(),ans.end());
std::cout<<ans<<'\n';
}
return 0;
}
os:进制思想在之前的解题过程中有所涉及,这种思想要学习!
E. Replace the Numbers
给出q次操作,每次可以向数组中加入一个数,或者将数组中现有的某一个数全部替换为另一个数,求操作完成后所得到的数组。
思路: 解法很奇妙。用并查集存被修改的数,把最后修改的作为根节点,所对应的子节点最后的值均为该根节点。
AC Code:
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0', ch = getchar();
}
x *= f;
}
template <typename T>
void write(T x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + '0');
}
#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;
int q, a[N], cnt, fa[N];
int main() {
// freopen("test.in","r",stdin);
// freopen("output.in", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::vector<std::pair<int, int>>vec[N];
std::cin >> q;
while (q--) {
int x;
std::cin >> x;
if (x == 1) {
std::cin >> a[++cnt];
} else {
int n, m;
std::cin >> n >> m;
std::pair<int, int>p;
p = std::make_pair(n, m);
vec[cnt].push_back(p);
}
}
for (int i = 1; i < N; i++)
fa[i] = i;
for (int i = cnt; i >= 1; i--) {
reverse(vec[i].begin(), vec[i].end());
for (auto [x, y] : vec[i]) {//这个写法对于结构体和pair均适用
fa[x] = fa[y];
}
a[i] = fa[a[i]];
}
for (int i = 1; i <= cnt; i++) {
std::cout << a[i] << " \n"[i == cnt];
}
return 0;
}
os:代码并不难写,主要是并查集的应用,之前用并查集维护的题做过一个,并查集主要维护两个元素之间的关系。上次那个题是判断乘法除法的优先级,用并查集维护。
若有错误请指教orzorz