1. 程式人生 > 實用技巧 >2020牛客暑期多校訓練第三場部分

2020牛客暑期多校訓練第三場部分

L 、 Problem L is the Only Lovely Problem

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
  ll x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
int main()
{
  string s ;
  cin >> s ;
  string a = "lovely" ;
  for(int i = 0 ;i < 6 ;i ++ ) {
    if(i >= s.size()) break ;
    if(s[i] < 'a') s[i] += 32 ;
    if(s[i] != a[i]) return 0 * puts("ugly") ;
   }
  return 0 * puts("lovely") ;
}
/*
*/

B、Classical String Problem

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 2e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
  ll x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
char s[N] ;
int main()
{
  scanf("%s" , s) ;
  int n = in() ;
  int len = strlen(s) ;
  int l = 0 , r = len - 1 ;
  for(int i = 1; i <= n ;i ++ ) {
    char c[2] ;
    int x ;
    scanf("%s%d" , c , &x) ;
    if(c[0] == 'A') printf("%c\n" , s[((l + x - 1) % len + len) % len]) ;
    else l += x , r += x , (l %= len + len) %= len , (r %= len + len) %= len ;
  }
  return 0 ;
}
/*
*/

A、Clam and Fish

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 2e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
char s[N] ;
void work(){
  int n ;
  scanf("%d" , &n) ;
  int ans = 0 , res = 0 ;
  scanf("%s" , s) ;
  for(int i = 0 ;i < n ;i ++) {
    if(s[i] >= '2') {
      ans ++ ;
    }
    if(s[i] == '0') {
        if(res) res -- , ans ++ ;
    }
    if(s[i] == '1') 
        res ++ ;
  }
  printf("%d\n" , ans +  res / 2) ;
  return ;
}
int main()
{
  int t ;
  scanf("%d" , &t) ;
  while(t --) work() ;
  return 0 ;
}
/*
*/

c、Operation Love

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-5 , pi = acos(-1) ;
typedef pair<double , double> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
  ll x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
PII a[N] ;
int len(PII a , PII b){
  double x = a.x - b.x , y = a.y - b.y ;
  return sqrt(x * x + y * y + 0.5) ;
}
double cross(PII a , PII b){
  return a.x * b.y - a.y * b.x ;
}
double area() {
  double ans = 0 ;
  for(int i = 0 ;i < 20 ;i ++)
    ans += cross(a[i] , a[(i + 1) % 20]) ;
  return ans ;
}
void work()
{
  vector<int> ans ;
  for(int i = 0; i < 20 ;i ++ ) cin >> a[i].x >> a[i].y ;
  if(area() > 0) {
    for(int i = 0; i < 20 ;i ++) {
      if(len(a[i] , a[(i + 1) % 20]) == 9) {
        if(len(a[(i + 1) % 20] , a[(i + 2) % 20]) == 8) cout << "right" << endl ;
        else cout << "left" << endl ;
        return ;
      }
    }
  }else {

    for(int i = 0; i < 20 ;i ++) {
      if(len(a[i] , a[(i + 1) % 20]) == 9) {
        if(len(a[(i + 1) % 20] , a[(i + 2) % 20]) == 8) cout << "left" << endl ;
        else cout << "right" << endl ;
        return ;
      }
    }
  }
  return ;
}
int main()
{
  int n = in() ;
  while(n --) work() ;
  return 0 ;
}
/*
*/

E、Two Matchings

首先

\[p_{p[i]} = i, 那麼p_i = p[i]\\例如p_5 = 3,那麼就是i = 3, p_3 = 5 \]

根據上述,發現一個目標n的排列裡面,都是一個一個的環,並且每個環的長度為2 , 題目要求兩個那個matching串和最小,就貪心使每個串的和比較小,也就是找個最小和次小,那麼第一個matching最小的話,肯定是直接將a排序,a[1] < a[2] < a[3] < a[4] , 這個貢獻就是a[i + 1] - a[i] , i += 2, 那麼次小的怎麼算呢

\[a[1] < a[2] < a[3] < a[4] < a[5] < a[6] < a[7] < a[8] \]

\[最小的安排是a[2] - a[1] , a[4] - a[3] , a[6] - a[5] \]

貪心講:次小的應該是錯開一個減,例如a[3] - a[1] , a[4] - a[2],但是列舉6個時候應該是a[6] - a[4] ,. a[5] - a[2] , a[3] - a[1], 如下圖, 那麼8個時候怎麼錯開,發現就是兩個4錯開,10個時候錯開的話就是一個4和一個6組合,下面也就是dp了,每次dp就max一下上面兩種錯開方式的最大值

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
ll in()
{
  ll x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
ll a[N] , dp[N] ;
void work(){
  int n = in() ;
  for(int i = 1; i <= n ;i ++ ) a[i] = in() , dp[i] = 1e18 ;
  sort(a + 1 , a + n + 1) ;
  ll ans = 0 ;
  for(int i = 1; i <= n ;i += 2) {
    ans += a[i + 1] - a[i] ;
  }
  dp[0] = 0 ;
  for(int i = 4; i <= n ;i ++ ) {
    dp[i] = min(dp[i] , dp[i - 4] + a[i] - a[i - 2] + a[i - 1] - a[i - 3]) ;
    if(i >= 6)
     dp[i] = min(dp[i] , dp[i - 6] + a[i] - a[i - 2] + a[i - 1] - a[i - 4] + a[i - 3] - a[i - 5]) ;
  }
  printf("%lld\n" , dp[n] + ans) ;
  return ;
}
int main()
{
  int n = in() ;
  while(n --) work() ;
  return 0 ;
}
/*
*/