首页 > 其他分享 >UNIQUE VISION Programming Contest 2022 Winter(AtCoder Beginner Contest 283)

UNIQUE VISION Programming Contest 2022 Winter(AtCoder Beginner Contest 283)

时间:2022-12-31 20:11:37浏览次数:67  
标签:std AtCoder Winter Contest int min ++ vector ans

A - Power

Given integers A and B, print the value A^B.

基础不解答

B - First Query Problem

基础不解答

C - Cash Register

Takahashi is a cashier.
There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 00. Whenever he types the key 00, the displayed number is multiplied by 100; whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.
Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?

Solution

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main(){
	string s;
	cin>>s;
	int len = s.length();
	int ans = len;
	int t = 0;
	for(int i = 0; i < len;){
		if(s[i] == '0'){
			int j;
			for(j = i+1;j < len;j++){
				if(s[j] != '0'){
					break;
				}
			}
			if(j - i > 1){
				t = j - i;
				t = t/2;
				ans = ans - t;
			}
			i = j;
		}
		else{
			i++;
		}
	}
	printf("%d\n",ans);
	return 0;
}

D - Scope

D - Scope (atcoder.jp)

Solution

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int b[100005];
int main(){
	string str;
	cin>>str;
	stack<char> st;
	int len = str.length();
	bool flag = true;
	memset(b,0,sizeof(b));
	for(int i = 0;i < len;i++){
		if(str[i] == '('){
			st.push(str[i]);
		}else if(str[i] != ')' && str[i] != '('){
			int t = str[i];
			if(b[t] != 0){
				flag = false;
				break;
			}else{
				b[t]++;
			}
		}else if(str[i] == ')'){
			memset(b,0,sizeof(b));
			if(st.size() == 0){
				flag = false;
				break;
			}else{
				st.pop();
			}
		}
	}
	if(flag == false){
		printf("No\n");
	}else{
		printf("Yes\n");
	}
	return 0;
}

E - Don't Isolate Elements

E - Don't Isolate Elements (atcoder.jp)

Solution

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 1000000010;
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
int main(){
	int h,w;
	cin>>h>>w;
	vector<vector<int>> a(h,vector<int>(w));
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			cin>>a[i][j];
		}
	}
	vector<vector<vector<int>>> dp(h,vector<vector<int>>(2, vector<int>(2,inf)));
	dp[0][0][0] = 0;
	dp[0][0][1] = 1;
	for(int i = 1;i < h;i++){
		for(int j = 0;j < 2;j++){
			for(int k = 0;k < 2;k++){
				for(int l = 0;l < 2;l++){
					vector<int> x(w,-1);//初始化为-1,为了在i=1时可以作比较
					if(i != 1){
						x = a[i-2];//此时x可以表示矩阵中的一行了
					}
					if(j){//j代表i-2行进行了操作
						for(int m = 0;m < w;m++){
							x[m] = 1-x[m];
						}
					}
					vector<int> y = a[i-1];//表示i-1行
					if(k){//k表示i-1行操作没有
						for(int m = 0;m < w;m++){
							y[m] = 1-y[m];
						}
					}
					vector<int> z = a[i];
					if(l){
						for(int m = 0;m < w;m++){
							z[m] = 1-z[m];
						}
					}
					//3行变完,开始判断是否合法
					bool flag = true;
					for(int m = 0;m < w;m++){
						if(y[m] != z[m] && x[m] != y[m] && (m == 0 || y[m] != y[m-1]) && (m == w-1 || y[m] != y[m+1])){
							flag = false;
						}
					}
					if(i == h-1){
						for(int m = 0;m < w;m++){
							if(z[m] != y[m] && (m == 0 || z[m] != z[m-1]) && (m == w-1 || z[m] != z[m+1])){
								flag = false;
							}
						}
					}//判断完
					if(flag){//假如合法
						chmin(dp[i][k][l],dp[i-1][j][k] + l);//加l判断是否变了
					}
				}
			}
		}
	}
	//dp跑完;
	int ans = inf;
	for(int j = 0;j < 2;j++){
		for(int k = 0;k < 2;k++){
			chmin(ans, dp[h-1][j][k]);
		}
	}
	if(ans == inf){
		cout<<-1<<endl;
	}else{
		cout<<ans<<endl;
	}
	return 0;
}

F - Permutation Distance

F - Permutation Distance (atcoder.jp)

Solution

给出两种解答:
1.双重循环减枝:第一重循环我们对i进行遍历来求Di的值,第二重循环,我们设变量jj表示下一个数组元素对i的相对距离。然后每次进行更新。
作ans = ∞,j = 1。进行下列变换:

\[ans = min(ans,|P_i-P_{i-j}| + j)(当i > j时) \]

\[ans = min(ans,|P_i - P_{i+j}| + j)(当i + j <= n时) \]

然后每次j自增1,当j > ans的时候结束循环输出当前i对应的ansDi

#include <bits/stdc++.h>
using namespace std;
int main(){    
	int N;
    cin >> N;
    vector<int> P(N+1);
    for(int i = 1;i <= N;i++){
    	cin>>P[i];
	}
    for(int i = 1;i <= N;i++){
    	int res = 2*N;
    	for(int j = 1;j <= res;j++){
    		if(i  > j) res = min(res, abs(P[i] - P[i-j]) + j);
    		if(i + j <= N) res = min(res, abs(P[i] - P[j+i]) + j);
		}
		cout<< res <<" ";
	}
	return 0;
}

时间复杂度为\(O(N\sqrt(N))\)

解答2:将题目中的绝对值打开,可以得到四种情况:

\[\begin{aligned} D_i &= \underset {j≠i}{min}​(|P_i-P_j|+|i-j|)\\ &= min\{\underset{j<i}{min}(|P_i- P_j| + i-j),\underset{j>i}{min}(|P_i-P_j|+j-i)\}\\ &= min\{\underset{j<i,P_j<P_i}{min}(P_i-P_j+i-j),\underset{j<i,P_j>P_i}{min}(P_j-P_i+i-j),\underset{j>i,P_j<P_i}{min}(P_i-P_j+j-i),\underset{j>i,P_j>P_i}{min}()P_j-P_i+j-i\}\\ &= min\{P_i+i-\underset{j<i,P_j<P_i}{max}((P_j+j)),\underset{j<i,P_j>P_i}{min}((P_J+j)-(P_i-i)),P_i-i-\underset{j>i,P_j<P_i}{max}((P_j-j)),\underset{j>i,P_j>P_i}{min}((P_j+j)-(P_i+i))\} \end{aligned} \]

G - Partial Xor Enumeration

G - Partial Xor Enumeration (atcoder.jp)

这是一道线性基多次求第k大元素的题。套线性基板子就行。

Solution

#include <bits/stdc++.h>
 
using i64 = long long;
 
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N;
    i64 L, R;
    std::cin >> N >> L >> R;
    L--;
    
    std::vector<i64> A(N);
    for (int i = 0; i < N; i++) {
        std::cin >> A[i];
    }
    
    std::array<i64, 60> t{};
    for (int i = 0; i < N; i++) {
        for (int j = 59; j >= 0; j--) {
            if (A[i] >> j & 1) {
                if (t[j]) {
                    A[i] ^= t[j];
                } else {
                    t[j] = A[i];
                    break;
                }
            }
        }
    }
    
    for (int i = 59; i >= 0; i--) {
        if (t[i]) {
            for (int j = i + 1; j < 60; j++) {
                if (t[j] >> i & 1) t[j] ^= t[i];
            }
        }
    }
    
    for (i64 i = L; i < R; i++) {
        i64 ans = 0;
        i64 x = i;
        for (int j = 0; j < 60; j++) {
            if (t[j]) {
                if (x & 1) ans ^= t[j];
                x /= 2;
            }
        }
        std::cout << ans << " \n"[i == R - 1];
    }
    
    return 0;
}

Ex - Popcount Sum

Ex - Popcount Sum (atcoder.jp)

这道题是求在\(1-N\)中模\(M\)等于\(R\)的整数的二进制表示中,\(1\)的个数的和。
现在有下面一条结论:

对于正整数\(A\),

Solution


标签:std,AtCoder,Winter,Contest,int,min,++,vector,ans
From: https://www.cnblogs.com/TTS-TTS/p/17017181.html

相关文章

  • LF Professional及WINTERACTER产品简介
    LF专业版v7.9LFProfessionalv7.8将32/64位Rainier编译器与经典的Lahey/FujitsuLF95编译器相结合!Rainier完全符合Fortran95/90/77标准,并广泛支持Fortran2003......
  • [AtCoder Regular Contest 077] F: SS (arc077F)
    原题链接​​​https://arc077.contest.atcoder.jp/tasks/arc077_d​​Description定义偶串为这个字符串前一半和后一半相同(abadabad)定义函数f(S)表示在S这个字符串后......
  • 【JZWinter Camp 2017】欠题小结
    2017.1.12Day1【GDKOI2017模拟】T2[JZOJ4938]序列T3[JZOJ4939]平均数2017.1.13Day2【NOIP2017提高组模拟】T2[JZOJ3824][CFRCC2014warmupDiv.1D]渴2017.1......
  • [AtCoder Grand Contest 018] D: Tree and Hamilton Path (agc018D)
    原题链接​​​https://agc018.contest.atcoder.jp/tasks/agc018_d​​Description给出一棵N个点带边权的树现在有一个N个点的完全图,一条边x,y的长度是这两点的在树上最短......
  • [AtCoder Grand Contest 071] E: Jigsaw (agc071E)
    原题链接​​​https://agc017.contest.atcoder.jp/tasks/agc017_e​​Description给出N块拼图每块拼图宽度为3,高度为相同的H拼图由3个宽度为1的部分拼接而成,第一部分......
  • HZNU Winter Trainning 7 补题 - Zeoy
    CodeForces-1660C题目传送门:https://vjudge.net/contest/535955#problem/C题意:询问一个字符串最少删去几个字符,能够把这个字符串变成aabbccdd这种两两相同的字符串题......
  • 2022 winter training.1
    A-SearchingLocalMinimumhttps://codeforces.com/problemset/problem/1480/C题意交互题有一个1~n的序列最多询问100次问i位置上的数是什么最后要找出一个局部最......
  • AtCoder-abc230_g GCD Permutation 容斥
    J-GCDPermutation传送门:J-GCDPermutation知识点:素数筛、容斥定理、gcd题意:长度为n的一个排列a中,求满足\(gcd(i,j)!=1且gcd(a_i,a_j)!=1\)的i,j对数。i,j可以......
  • AtCoder Beginner Contest 239总结
    由于比赛延期了一个星期,今天才打,恰巧我记错了时间,本来是8:00,我记成9:00,然后·········幸运的是我还是把前四题做出来了(intwentyminutes),由于时间问题,我......
  • Atcoder Beginner Contest 244总结
    这次的Rating凉了………………这次出乎T3意料的考了交互题,虽然很简单,但卡了我好久……T4考了一个神奇的东西,我用骗分大法水了过去……一看排名发现有快3000人得了1000分......