西南民族大学 春季 2023 训练赛4

小石的图形

太坑了, π的精度一定要开大,不然就wa, 建议用acos(-1)或者M_PI.

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f

using namespace std;
//typedef long long ll;

const int N = 1e5+10;
int n,m,t;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    printf("%.3lf",n * n * 0.5 / M_PI);
    return 0;
}

植树造林

刚开始想的是对的,但一直觉得没有这么简单,最后不知道怎么想就交上去了,结果真是奇数输出1偶数输出2......

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f

using namespace std;
//typedef long long ll;

const int N = 1e5+10;
int n,m,t,k;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n ;
    if(n & 1)
    cout << 1 << endl;
    else
     cout << 2 <<endl; 
    return 0;
}

Forsaken给学生分组

值得注意的是,一个人一组的话,能力值最高和最低都是他自己,并不存在只有最高值而无最低值一说...

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f

using namespace std;
//typedef long long ll;

const int N = 1e5+10;
int n,m,t,k,a[N],ans;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> k;
    for(int i = 0; i < n; i++)
     cin >> a[i];
    sort(a, a + n);
    for(int i = 0;i < k ; i ++)
     ans += a[n - i - 1] - a[i];
    cout << ans << endl;
    return 0;
}

分数的运算

纯模拟,感觉是我想得太复杂了,所以第一次写多了错了()

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <utility>
#define inf 0x3f3f3f3f
#define endl '\n'
#define int long long

using namespace std;

const int N = 1e5+10;

//typedef long long ll;

int n,m,t;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int x11,x22,y11,y22;
    cin >> x11 >> y11 >> x22 >> y22;
    int d = __gcd(x11 * y22 + x22 * y11, y11 * y22);
    cout << (x11 * y22 + x22 * y11) / d << ' ' << y11 * y22 / d << endl;
    d = __gcd(x11 * y22 - x22 * y11, y11 * y22);
    if(x11 * y22 - x22 * y11 == 0)
     cout << 0 << ' ' << 0 << endl;
    else
    {
    if(x11 * y22 < x22 * y11) cout << "-";
    cout << abs(x11 * y22 - x22 * y11) / d << ' ' << y11 * y22 / d << endl;        
    }
    d = __gcd(x11 * x22, y11 * y22);
    cout << x11 * x22 / d << ' ' << y11 * y22 / d << endl;
    d = __gcd(x11 * y22 , y11 * x22);
    cout << x11 * y22 / d << ' ' << y11 * x22 / d << endl; 
    
    return 0;
}

小y的旅行

用并查集先把不在k以内的点的边连上,然后再连k以内的点的边,如果这两歌点已经连通,则ans++,即我们需要删掉的k以内的环的边数.

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define inf 0x3f3f3f3f
#define f first
#define s second 

using namespace std;
//typedef long long ll;

const int N = 1e6+10;
typedef pair<int, int> PII;
vector<PII> a;
int n,m,ans,t,k;
int fa[N];
int find(int x)
{
    if(fa[x] != x) 
     fa[x] = find(fa[x]);
    return fa[x];
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++ )
     fa[i] = i;
    for(int i = 0; i < m; i++)
    {
        int x,y;
        cin >> x >> y;
        a.push_back({x,y});
        if(find(fa[a[i].f]) != find(fa[a[i].s]) && a[i].f > k && a[i].s > k)
         fa[find(a[i].f)] = find(fa[a[i].s]); 
    }
    for(int i = 0; i < m; i++)
     {
         if(a[i].f <= k || a[i].s <= k )
         {
         if(find(a[i].f) != find(a[i].s))
          fa[find(a[i].f)] = find(a[i].s);
         else
          ans ++;
         }
         
     }
    cout << ans << endl; 
    return 0;
}

小L的数列

只找寻符合条件的最长上升子序列只能得80分,这道题可以枚举每个数的因子,p[N]用来存每个因子的能排序的长度,dp[N]则是当前元素的最长长度

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define endl '\n'
#define int long long
#define f first
#define s second

using namespace std;

const int N = 1e5+10, M = 1e6 + 10;

//typedef long long ll;
typedef pair<int,int> PII;

int n,m,t;
int v[N],dp[N],p[N];
void solve()
{
   memset(p, 0, sizeof p );
    cin >> n;
    int ans = 0;
    for(int i = 1;i <= n; i++)
        cin >> v[i];
    sort(v + 1, v + 1 + n);
    for(int  i = 1 ;i <= n;i ++)
    {
        dp[i] = 1;
      //  int maxx = 0;
        for(int j = 2; j * j <= v[i]; j++)
        {
            if(v[i] % j != 0) continue;
                dp[i] = max(dp[i], dp[p[j]] + 1) ;
                dp[i] = max(dp[i], dp[p[v[i] / j]] + 1);
        }
        for(int j = 1;j * j <= v[i]; j ++)
        {
            if(v[i] % j != 0) continue;
                p[j] = i ;
                p[v[i] / j] = i ;
        }
        ans = max(ans, dp[i]);
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int Ke_scholar;
    cin >> Ke_scholar;
    while(Ke_scholar--)
    {
        solve();
    }
    return 0;
}

 

小L的编辑器

可以用双端队列

C++ STL deque容器(详解版) (biancheng.net)

#include <iostream>
#include <cstring>
#include <queue>
#define int long long

using namespace std;

deque<char> q;

signed main() {
    ios :: sync_with_stdio(false);
    cin.tie(0) , cout.tie(0);
    
    string s , t ;
    cin >> s >> t;
    
    q.push_back(s[s.size()-1]);
    for(int i=s.size()-1; i; i--){
        if(t[i-1] == 'L'){
            q.push_back(s[i-1]);
        }
        else q.push_front(s[i-1]);
    }
    while(q.size()){
        cout << q.front();
        q.pop_front();
    }
    return 0;
}

也可以先把'L' 和'R' 分开存,然后讲'L'的字符串反向

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <utility>
#define inf 0x3f3f3f3f
#define endl '\n'
#define int long long

using namespace std;

const int N = 1e5+10;

//typedef long long ll;

int n,m,t;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    string s1,s2,s3,s4;
    cin >> s1 >> s2;
    for(int i = 0; i < s2.size(); i++)
    {
        if(s2[i] == 'R')
        s3 += s1[i];
        else 
         s4 += s1[i];
    }
    reverse(s4.begin(), s4.end());
    cout << s3 << s4 << endl;
    return 0;
}

1408

 

posted @ 2023-04-05 04:37  Ke_scholar  阅读(18)  评论(0编辑  收藏  举报