直达门:https://codeforces.com/contest/1525.

唠嗑:zpx好菜,还一直bb。

A. Potion-making

题目链接.

直接上图:

在这里插入图片描述
挺简单的一题,直接上代码吧。。。

#include<iostream>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<list>

#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 1e5+10,M=20;
const int inf=0x3f3f3f3f;
int n,m,t,x;

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        int f = __gcd(n, 100);
        cout << 100/f<< endl;
    }
    //system("pause");
    return 0;
}

B. Permutation Sort

题目链接.

直接上图
在这里插入图片描述

题意:

给一个为1到n的乱序数组,然后我们可以取除了整个数组除外的任意子序列改变数据的位置,输出得到上升序列的最小操作数。

题解:

贪心一点,既然求最小操作数,那么我们可以让第一个数或者最后一个数除外的序列为一个子序列考虑,注意特殊情况就可以了。

#include<iostream>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<list>

#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 1e5+10,M=20;
const int inf=0x3f3f3f3f;
int n,m,t,x;
int arr[55];

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        int flag = 0;
        cin >> n;
        for (int i = 1; i <= n;i++)
            cin >> arr[i];
        for (int i = 1; i <= n;i++){
            if(arr[i]!=i) {
                 flag = 1;
                 break;
               } 
        }
        if(flag==0){
         cout << 0 << endl;
         continue;
          }
        if(arr[1]==1||arr[n]==n){
          cout << 1 << endl;
        }
          else{
              if(arr[1]==n&&arr[n]==1)
                  cout << 3 << endl;
              else
                  cout << 2 << endl;
          }
    }
 //   system("pause");
    return 0;
}

C. Robot Collisions

题目链接.

直接上图
在这里插入图片描述

题解:

就硬模拟,注意起始点奇数和偶数的点是不用碰撞到的,因此可以分为奇数和偶数分别判断。

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x,d,t;
}e[300005];
int n,m;
int a[300005];
int l[300005];
int cmp(int x,int y){
	return e[x].x<e[y].x;
}
void solve(int o){
	int p=0;
	for(int i=1;i<=n;i++){
		if(e[l[i]].x%2==o){
			if(e[l[i]].d==1||!p){
				a[++p]=l[i];
			}
			else {
				e[l[i]].t=e[a[p]].t=(e[l[i]].x-e[a[p]].x*e[a[p]].d)/2;
				p--;
			}
		}
	}
	for(;p>=2;p-=2){
		e[a[p]].t=e[a[p-1]].t=(2*m-e[a[p]].x-e[a[p-1]].x*e[a[p-1]].d)/2;
	}
	if(p){
		e[a[p]].t=-1;
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			cin>>e[i].x;
			l[i]=i;
		}
		for(int i=1;i<=n;i++){
			char c;
			cin>>c;
			if(c=='L') e[i].d=-1;
			else e[i].d=1;
		}
		sort(l+1,l+1+n,cmp);
		solve(0),solve(1);
		for(int i=1;i<=n;i++){
			cout<<e[i].t<<" ";
		}
		cout<<endl;
	}
	return 0;
}

D. Armchairs

题目链接.

直接上图
在这里插入图片描述

题解:

挺好的一道dp,输入过程中存储好1和0的位置和数目,dp[i][j]中,i遍历每一个1,j 表示有j个0的位置可以让当前的1填充,dp[i][j]表示当前1到这 j 个0中的最小值,因此当加入一个0可以选择时,比较dp[i][j-1]和dp[i-1][j-1]+0到1的距离的大小,保留小的就好。

#include<iostream>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<list>

#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 1e5+10,M=20;
const int inf=0x3f3f3f3f;
int n,m,t,x;
int dp[5005][5005];

int main(){
   ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
   cin >> n;
   int pos0[5005], pos1[5002];
   int a=0, b=0;
   for (int i = 1; i <= n;i++){
       cin >> x;
       if(x==0)
           pos0[++a]=i;
       else
           pos1[++b]=i;
   }
   if(a==n){
           cout << 0 << endl;
           return 0;
       }
   memset(dp, inf, sizeof dp);
   for (int i = 1; i <= b; i++){
        for (int j = i; j <= a; j++){
           if (i == 1)   dp[i][j] = min(dp[i][j - 1],abs(pos1[i] - pos0[j]) );
           else dp[i][j] = min(dp[i][j - 1] , dp[i-1][j-1] + abs(pos1[i] - pos0[j]));
               }
   }
   cout << dp[b][a]<<endl;
   system("pause");
   return 0;
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐