2023-2024-1 ACM集训队每周程序设计竞赛(3)题解

A.游戏机

思路:判断三个字符是否相等即可

代码如下:

  1. #include<bits/stdc++.h>  
  2. using namespace std;  
  3. #define ll long long  
  4. int main(){  
  5.     string a; cin >> a;  
  6.     if(a[0] == a[1] && a[0] == a[2]) puts("Won");  
  7.     else puts("Lost");  
  8.     return 0;  
  9. }  

B.小星喝酒

思路:遍历计算

代码如下:

  1. #include<bits/stdc++.h>  
  2. using namespace std;  
  3. typedef long long ll;  
  4. const int maxn = 1e6 + 7;  
  5. const ll mod = 1e9 + 7;  
  6.   
  7. int main()  
  8. {  
  9.     int n, x;  
  10.     cin >> n >> x;  
  11.     int ans = -1;  
  12.     ll cnt = 0;  
  13.     for(int i = 0; i < n; ++i){  
  14.         int a, b;  
  15.         cin >> a >> b;  
  16.         cnt += a * b;  
  17.         if(ans == -1){  
  18.             if(cnt > x * 100)ans = i + 1;  
  19.         }  
  20.     }  
  21.     cout << ans << "\n";  
  22.     return 0;  
  23. }  

C.取水果

思路:暴力可以通过,单调栈时间复杂度更好

以下是单调栈代码:

  1. #include <bits/stdc++.h>  
  2. using namespace std;  
  3. const int N = 10010;  
  4. int n;  
  5. int h[N];  
  6. int q[N], tot;  
  7. int l[N], r[N];  
  8. int main()  
  9. {  
  10.   cin >> n;  
  11.   for (int i = 1; i <= n; i ++ ) cin >> h[i];  
  12.   h[0] = h[n + 1] = -1;  
  13.   q[0] = 0; tot = 0;  
  14.   for (int i = 1; i <= n; i ++ )  
  15.   {  
  16.     while (tot >= 1 && h[i] <= h[q[tot]]) tot --;  
  17.     l[i] = q[tot];  
  18.     q[++ tot] = i;  
  19.   }  
  20.   q[0] = n + 1, tot = 0;  
  21.   for (int i = n; i >= 1; i -- )  
  22.   {  
  23.     while (tot >= 1 && h[i] <= h[q[tot]]) tot --;  
  24.     r[i] = q[tot];  
  25.     q[++ tot] = i;  
  26.   }  
  27.   int s = 0;  
  28.   for (int i = 1; i <= n; i ++ )  
  29.     s = max(s, h[i] * (r[i] - l[i] - 1));  
  30.   cout << s << endl;  
  31. }  

D.与或问题

思路:线性dp
dp[i][0/1]=>第i个位置为0/1的种数

代码如下:

  1. #include <bits/stdc++.h>  
  2. #define int long long  
  3. using namespace std;  
  4. const int mod = 1e9 + 7;  
  5. const int maxn = 1e6 + 10;  
  6.   
  7. int dp[100][2];  
  8. string s[100];  
  9.   
  10. void solve() {  
  11.     int n;  
  12.     cin>>n;  
  13.     for (int i = 1; i <=n; ++i) {  
  14.         cin>>s[i];  
  15.     }  
  16.     dp[0][0]=dp[0][1]=1;  
  17.     for (int i = 1; i <=n; ++i) {  
  18.         if(s[i][0]=='A'){  
  19.             dp[i][1]=dp[i-1][1];  
  20.             dp[i][0]=dp[i-1][0]*2+dp[i-1][1];  
  21.         } else{  
  22.             dp[i][0]=dp[i-1][0];  
  23.             dp[i][1]=dp[i-1][1]*2+dp[i-1][0];  
  24.         }  
  25.     }  
  26.     cout<<dp[n][1];  
  27. }  
  28.   
  29. signed main() {  
  30. //    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);  
  31.     int _ = 1;  
  32. //    cin >> _;  
  33.     while (_--) {  
  34.         solve();  
  35.     }  
  36.     return 0;  
  37. }  

E.旋转与翻转问题

思路:

    1.( x , y )顺时针旋转90°后变成 ( y , − x )
    2. ( x , y )逆时针旋转90°后变成 ( − y , x )
    3. ( x , y ) 以 x = p对称时变成 ( 2 p − x , y )
    4.( x , y ) 以 y = p对称时变成 ( x , 2 p − y )

有了这个我们只需要 记录每次旋转的变换参数, 后面直接代入x, y的坐标就能求出答案了。
 

代码如下:

  1. #include<bits/stdc++.h>  
  2.   
  3. using namespace std;  
  4. const int N = 2e5 + 7;  
  5.   
  6. typedef long long ll;  
  7.   
  8. ll n, x[N], y[N], m, q;  
  9.   
  10. ll a[N], b[N], fza[N], fzb[N], visa[N], visb[N];  
  11.   
  12. int main() {  
  13.     ios::sync_with_stdio(0);  
  14.     cin >> n;  
  15.     for (int i = 1; i <= n; i++) {  
  16.         cin >> x[i] >> y[i];  
  17.     }  
  18.     cin >> m;  
  19.     a[0] = 0, b[0] = 0;  
  20.     fza[0] = fzb[0] = 1;  
  21.     visa[0] = 1, visb[0] = 2;  
  22.     for (int i = 1; i <= m; i++) {  
  23.         int op;  
  24.         cin >> op;  
  25.         if (op == 3) {  
  26.             ll p;  
  27.             cin >> p;  
  28.             a[i] = 2 * p - a[i - 1];  
  29.             b[i] = b[i - 1];  
  30.             visa[i] = visa[i - 1];  
  31.             visb[i] = visb[i - 1];  
  32.             fza[i] = -fza[i - 1];  
  33.             fzb[i] = fzb[i - 1];  
  34.         } else if (op == 4) {  
  35.             ll p;  
  36.             cin >> p;  
  37.             b[i] = 2 * p - b[i - 1];  
  38.             fzb[i] = -fzb[i - 1];  
  39.             a[i] = a[i - 1];  
  40.             visa[i] = visa[i - 1];  
  41.             visb[i] = visb[i - 1];  
  42.             fza[i] = fza[i - 1];  
  43.          } else if (op == 2) {  
  44.             if (visa[i - 1] == 1) {  
  45.                 visa[i] = 2;  
  46.                 visb[i] = 1;  
  47.             } else {  
  48.                 visa[i] = 1;  
  49.                 visb[i] = 2;  
  50.             }  
  51.             a[i] = b[i - 1];  
  52.             b[i] = a[i - 1];  
  53.             fza[i] = fzb[i - 1];  
  54.             fzb[i] = fza[i - 1];  
  55.             fza[i] = -fza[i];  
  56.             a[i] = -a[i];  
  57.   
  58.   
  59.         } else {  
  60.             if (visa[i - 1] == 1) {  
  61.                 visa[i] = 2;  
  62.                 visb[i] = 1;  
  63.             } else {  
  64.                 visa[i] = 1;  
  65.                 visb[i] = 2;  
  66.             }  
  67.             a[i] = b[i - 1];  
  68.             b[i] = a[i - 1];  
  69.             fza[i] = fzb[i - 1];  
  70.             fzb[i] = fza[i - 1];  
  71.             fzb[i] = -fzb[i];  
  72.             b[i] = -b[i];  
  73.         }  
  74.     }  
  75.     int q; cin >> q;  
  76.     while (q--) {  
  77.         int A, B;  
  78.         cin >> A >> B;  
  79.         ll ansx = x[B], ansy = y[B];  
  80.         if (visa[A] == 1) {  
  81.             ansx = ansx * fza[A] + a[A];  
  82.             ansy = ansy * fzb[A] + b[A];  
  83.               
  84.   
  85.         } else {  
  86.             ll cnt = ansx;  
  87.             ansx = ansy * fza[A] + a[A];  
  88.             ansy = cnt * fzb[A] + b[A];  
  89.               
  90.         }  
  91.         cout << ansx << " " << ansy << endl;  
  92.     }  
  93.   
  94. }  

F.移位计数

思路:

我们可以先通过归并排序求出原序列的逆序对。然后每一次把序列的第一个数轮换到序列末尾,看逆序对数是多少。每一次只需要找到序列第一个数在全排列中的位置,进行相应的更新即可,可以使用二分查找。
代码如下:

  1. #include <bits/stdc++.h>  
  2. using namespace std;  
  3.   
  4. typedef long long ll;  
  5. int n;  
  6. int a[300010];  
  7. int b[300010];  
  8. int c[300010];  
  9. int d[300010];  
  10. ll cnt;  
  11.   
  12. void merge_sort(int l, int r){  
  13.     if(l + 1 >= r)//涓€涓厓绱犱笉鐢ㄦ帓搴?  
  14.         return ;  
  15.     int mid = (l+r)/2;  
  16.     merge_sort(l, mid), merge_sort(mid, r);  
  17.     int l1 = l, r1= mid;  
  18.     int l2 = mid, r2 = r;  
  19.     int tot = l;  
  20.     while(l1 < r1 || l2 < r2){  
  21.         if((l1 < r1 && a[l1] <= a[l2]) || l2 >= r2){  
  22.             b[tot++] = a[l1++];  
  23.         }  
  24.         else{  
  25.             b[tot++] = a[l2++];  
  26.             cnt += 1ll*(r1 - l1);  
  27.         }  
  28.     }  
  29.   
  30.     for(int i = l; i < r; i++)  
  31.         a[i] = b[i];      
  32. }  
  33.   
  34. int main(){  
  35.     cin >> n;  
  36.     for(int i = 0; i < n; i++){  
  37.         cin >> a[i];  
  38.         c[i] = a[i];  
  39.         d[i] = i;  
  40.     }  
  41.     cnt = 0;  
  42.     merge_sort(0, n);  
  43.     cout << cnt << "\n";  
  44.     for(int k = 0; k < n-1; k++){  
  45.         int t = lower_bound(d, d+n, c[k]) - d;  
  46.         cnt -= 1ll*t;  
  47.         cnt += 1ll*(n - 1 - t);  
  48.         cout << cnt << "\n";  
  49.     }  
  50.     return 0;  
  51. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值