2020牛客暑期多校训练营(第三场)题解

L Problem L is the Only Lovely Problem

题目链接

https://ac.nowcoder.com/acm/contest/5668/L

思路

没什么好说的,签到题

代码

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
// #define TDS_ACM_LOCAL

void solve(){
    string s;
    cin>>s;
    for(int i=0; i<6; i++)  s[i]=tolower(s[i]);
    if(s.substr(0,6)=="lovely") cout<<"lovely"<<endl;
    else cout<<"ugly"<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}

B Classical String Problem

题目链接

https://ac.nowcoder.com/acm/contest/5668/B

思路

将字符串s视作首尾相连的圈,每次对s进行‘M’操作时,将初始对应与0的下标k移动

代码

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
char s[2000009];
char c;
int n, x, k=0, len;
int main(){
    scanf("%s", &s);
    scanf("%d", &n);
    getchar();
    len=strlen(s);
    while(n--){
        scanf("%c %d", &c, &x);
        getchar();
        if(c=='A')
            printf("%c\n", s[(k+x-1)%len]);
        else{
            k+=x;
			if(k>=len)
				k%=len;
			else if(k<0)
				k+=len;
        }
    }
    return 0;
}

A Clam and Fish

题目链接

https://ac.nowcoder.com/acm/contest/5668/A

思路

当遇到type2和type3时,因为有鱼且只能进行一次操作,所以直接抓鱼即可
type0的情况有饵料就用
type1的情况要考虑是钓鱼还是制作饵料,根据后面剩余的type0和type1来决定,有两种方法
1、从早到晚考虑
能做饵料就做,不能做就钓鱼,最后如果饵料有剩余的x,可以将后面的x/2的做饵料的机会拿来钓鱼
2、从晚到早考虑
遇到type0就记录为抓鱼的地点,往前搜索可以做饵料的机会即可抓鱼,当没有type0的时候并且遇到type1时,可以记录该点为抓鱼的地点,往前搜索可以制作饵料的地方

代码

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const double pi = acos(-1.0);
#define INF 0x7f7f7f
// #define TDS_ACM_LOCAL
int t, n, ans, ans0;
char s[2000009];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>n;
        cin>>s;
        ans=0, ans0=0;
        for(int i=n-1; i>=0; i--){
            if(s[i]=='0'){
                ans0++;
            }
            else if(s[i]=='1'){
                if(ans0==0)
                    ans0++;
                else if(ans0>0){
                    ans0--;
                    ans++;
                }
            }
            else if(s[i]=='2'){
                ans++;
            }
            else if(s[i]=='3'){
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C Operation Love(计算几何,叉积

题目链接

https://ac.nowcoder.com/acm/contest/5668/C

思路

找出最长的两边,9和8,利用叉积计算两边的连接顺序
求为右手的情况(先8后9)
1、先找到9再找到8,则叉积求得的方向应该为逆时针
2、先找到8再找到9,则叉积求得的方向应该为顺时针

在这里插入图片描述

代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const double pi = acos(-1.0);
#define INF 0x7f7f7f
// #define TDS_ACM_LOCAL
struct node
{
    double x, y;
}p[21];
double eps=1e-5;
double dis(node p1, node p2){
    return (sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
double cross(node a, node b, node c){
    return (a.x-b.x)*(a.y-c.y)-(a.y-b.y)*(a.x-c.x);
}

void solve(){
    int flag=0;
    for(int i=0; i<20; i++) cin>>p[i].x>>p[i].y;
    for(int i=0; i<20; i++){
        if((fabs(dis(p[i], p[(i+1)%20])-9)<eps)&&(fabs(dis(p[(i+1)%20], p[(i+2)%20])-8)<eps))
            if(cross(p[i], p[(i+1)%20], p[(i+2)%20])>0)
                flag=1;
        if((fabs(dis(p[i], p[(i+1)%20])-8)<eps)&&(fabs(dis(p[(i+1)%20], p[(i+2)%20])-9)<eps))
            if(cross(p[i], p[(i+1)%20], p[(i+2)%20])<0)
                flag=1;
    }
    if(flag)
        cout<<"right"<<endl;
    else
        cout<<"left"<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xmyrzb/article/details/107445242