首页 > 其他分享 >第一次双周赛

第一次双周赛

时间:2022-11-26 10:55:06浏览次数:46  
标签:int double mid 第一次 -- while 双周 calc

https://pintia.cn/problem-sets/1591416544356323328/exam/problems/1591417091146764289

T1

只需要判断前后有没有L,在把这里涂成C最后输出即可,i = 0 要特判

#include<bits/stdc++.h>
using namespace std;
string s ;
int n ;
int main() {
	cin >> n ;
	cin >> s;
	for(int i = 0 ; i < s.size() ; i ++){
		if(s[i] == 'L')continue;
		if(i == 0 && s[i + 1] != 'L')s[i] = 'C';
		if(s[i + 1] != 'L' && s[i - 1] != 'L')s[i] = 'C';
	}
	for(int i = 0 ; i < s.size() ; i ++){
		cout << s[i];
	}
} 

T2

将每一位映射到其数数值,可以用map也可以用s - '0',不必转换成十进制运算,直接在十六进制下相乘,最后判断每一位是否大于16即可

#include<bits/stdc++.h>
using namespace std;
int c1[10002];
int c2[10002];
int c3[10002];
string s1 , s2;
int main () {
  cin >> s1 >> s2;
   for(int i = s1.size() - 1 ; i >= 0  ;i --){
        if('0' <= s1[i] && s1[i] <= '9'){
        	c1[s1.size() - 1 - i] = s1[i] - '0';
		}
		else c1[s1.size() - 1 - i] = s1[i] - 'A' + 10;
   }
    for(int i = s2.size() - 1 ; i >= 0  ;i --){
        if('0' <= s2[i] && s2[i] <= '9'){
        	c2[s2.size() - 1 - i] = s2[i] - '0';
		}
		else c2[s2.size() - 1 - i] = s2[i] - 'A' + 10;
   }
   int lc = s1.size() + s2.size() ;
   for(int i = 0 ; i < s1.size()  ; i ++){
   	for(int j = 0 ; j < s2.size()  ; j ++){
   		c3[i + j] += c1[i] * c2[j];
	   }
   }
   for(int i = 0 ; i < lc  ; i ++){
   	c3[i + 1] += c3[i] / 16;
   	c3[i] %= 16;
   }
   while(lc > 0 && c3[lc] == 0){
   	lc --;
   }
   for(int i = lc ; i >= 0 ; i --){
    if(c3[i] <= 9)cout << c3[i];
    if(c3[i] >= 10)cout << char(65 + (c3[i] - 10));
	}
}

T3

这一题显然二分,然后注意的是开枪那一秒是不算的,我们循环可以用时间当循环变量,记录每一秒干什么,然后判断即可。

#include<bits/stdc++.h>
using namespace std;
int n , m;
int a[100020];
int b[100020];
int check(int x){
	for(int i = 1; i <= n ; i  ++){
    	a[i] = b[i];
	}
	int pos = 1;
	int ans = 0;
	bool v = 1;
	int cnt = 0;
	int k = a[n];
   for(int t = 1 ; t <= k ; ){
      if(v == 1 && a[pos] <= m){
    	pos ++;
    	if(pos == n + 1)return 1 ;
		v = 0;
		a[pos] = a[pos] - t + 1;
		if(a[pos] <= 0)return 0;
		continue ;
	  }
	  while(v == 1 && a[pos] > m){
	  	t ++;
		a[pos] -- ;
	  } 
	  if(v == 0){
	  	v = 1;
	  	t += x ;
	  	a[pos] = a[pos] - x;
	  	if(a[pos] < 0)return 0;
	  }
   }
}
int main () {
	cin >> n >> m;
	for(int i = 1 ; i <= n ; i ++){
		cin >> a[i];
	    b[i] = a[i];
	}
	sort(a + 1 , a + 1 + n);
	sort(b + 1 , b + 1 + n);
	int l = 1 ,  r = 1e8;
    while(l < r){
    	int mid = (l + r + 1) / 2 ;
    	if(check(mid))l = mid;
    	else r = mid - 1 ;
	}
    cout << l ;
}

T4

模拟链表加数字反转,用map储存链表地址,最后补齐五位要%05d

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
map <int,int> m;
int f[N + 5],t[N + 5];
int a[N + 5],b[N + 5];
int cnt = 0;
void res(int l,int r){
	for(int i = l;i <= r;i ++)
		b[i] = a[r - (i - l)];
	for(int i = l;i <= r;i ++)
		a[i] = b[i];
}
int main(){
	int s,n,k;
	cin >> s >> n >> k;
	for(int i = 1;i <= n;i ++){
		int tmp,a,b;
		cin >> a >> tmp >> b;
		m[a] = tmp;
		t[a] = b;
		f[b] = a;
	}
	while(t[s] != -1){
		a[++ cnt] = s;
		s = t[s];
	}
	a[++ cnt] = s;
	int st = 1,cnt1 = 0;
	int st1 = st;
	while(st <= cnt){
		if(st % k == 0)
			res(st1,st),st1 = st + 1;
		st ++;
	}
	for(int i = 1;i <= cnt;i ++){
		if(i != cnt) printf("%05d %d %05d\n",a[i],m[a[i]],a[i + 1]);
		else printf("%05d %d -1",a[i],m[a[i]]);
	}
	return 0;
}

T5

直接循环暴力即可数据有点水,正解是求出极值点,然后找出单调区间,进行三次二分。

#include<bits/stdc++.h>
using namespace std;
int T ;
double a , b , c , d , p , q;
double calc(double x){
	return a * x * x * x + b * x * x + c * x + d;
}
int cnt ;
double k , h; 
int main () {
	cin >> T;
	while(T --){
		cin >> a >> b >> c >> d >> p >> q;
		k = min((-2 * b + sqrt(4 * b * b - 12 * a * c)) / (6 * a) ,(-2 * b - sqrt(4 * b * b - 12 * a * c)) / (6 * a) ) ;
		h = max((-2 * b + sqrt(4 * b * b - 12 * a * c)) / (6 * a) ,(-2 * b - sqrt(4 * b * b - 12 * a * c)) / (6 * a) ) ;
		double l = p , r = k ;
		while(l + 1e-6 < r){
	    double 	mid = (l + r) / 2;
	    if(calc(mid) * calc(l) < 0){
	    	r = mid ;
		} else l = mid ;
	}
	   printf("%.6f " , l);
	   l = k , r = h;
	   	while(l + 1e-6 < r){
	    double 	mid = (l + r) / 2;
	    if(calc(mid) * calc(l) < 0){
	    	r = mid ;
		} else l = mid ;
	}
	   printf("%.6f " , l);
	   l = h , r = q;
	   	while(l + 1e-6 < r){
	    double 	mid = (l + r) / 2;
	    if(calc(mid) * calc(l) < 0){
	    	r = mid ;
		} else l = mid ;
	}
	   printf("%.6f " , l);
 }
}

标签:int,double,mid,第一次,--,while,双周,calc
From: https://www.cnblogs.com/wmjlzw1314/p/16927053.html

相关文章