Programming Thinking and Practice CSP-M4

TT count ducks

Title

On this day, TT felt uncomfortable at home because of the epidemic. One hour after catching cats in the cloud, TT decided to go to the nearby hills to play. TT came to a small lake and saw many ducks playing by the lake. TT was suddenly envious. At this time he found that each duck was different, or had a different feather, or a different personality. TT opened a map<duck, integer> tong in his mind, turning the ducks into numbers. Now he was curious, how many ducks mapped to the number of digits with different numbers less than k.

Problem solving ideas

Violence judges each digit of each number and calculates the number of different numbers.

Code

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n, k;
int cntall;
void diff(long long temp){
	if(k > 10){
		cntall++;
		return;
	}
	bool cnt[10];
	memset(cnt, 0, sizeof(cnt));
	int cnt1 = 0;
	while(temp){
		int a = temp % 10;
		if(!cnt[a]){
			cnt[a] = true;
			cnt1++;
			if(cnt1 >= k)
				return;
		}
		temp /= 10;
	}
	cntall++;
}
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> k;
	for(int i = 0; i < n; ++i){
		long long temp;
		cin >> temp;
		diff(temp);
	}
	cout << cntall << endl;
	return 0;
}

ZJM wants to resist cosmic rays

Title

According to rumours, 2020 is a year of concentrated cosmic rays, which is inseparable from the mysterious cosmic dog! But Ruishen and Dongdong were busy confronting the Cosmic Dog, and the work of resisting cosmic rays fell to ZJM. Assuming that the emission point of cosmic rays is located on a plane, ZJM has obtained all the emission points of cosmic rays by special means, and their coordinates are all integers. And ZJM needs to construct a protective cover, this protective cover is a circle, the center is located at a cosmic ray emission point. At the same time, because most
all the funds are allocated to Ruishen, so ZJM must save money and make a protective cover with the smallest area. When ZJM decided, Dongdong came to ZJM to fight against Cosmic Dog, so ZJM threw the question to you~

Problem solving ideas

It is still violence, each point is enumerated as the center of the circle, and the maximum value of the distance to all other points is the radius of the circle, and the minimum value of the radius of all circles is taken (note the square)

Code

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int n;
double ans = 1e11;
int mini = -1;
struct P{
	double x, y;
}point[1010];
bool cmp(P a, P b){
	if(a.x != b.x) return a.x < b.x;
	return a.y < b.y;
}
int main() {
	cin >> n;
	for(int i = 0; i < n; ++i)
		cin >> point[i].x >> point[i].y;
	sort(point, point + n, cmp);
	for(int i = 0; i < n; ++i){
		double max1 = 0;
		for(int j = 0; j < n; ++j){
			if(j == i) continue;
			double t = 1.0 * (point[j].x - point[i].x) * (point[j].x - point[i].x) + 1.0 * (point[j].y - point[i].y) * (point[j].y - point[i].y);
			max1 = max(t, max1);
		}
		if(max1 < ans){
			mini = i;
			ans = max1;
		}
	}
	printf("%.2lf %.2lf\n", point[mini].x, point[mini].y);
	printf("%.2lf\n", ans);
    return 0;
}

Crisis of Cosmic Dog

Title

In Twishen vs. Cosmic Ray, we learned the power of Cosmic Dogs. Although Cosmic Dogs are fierce, they have a very cute girlfriend.
Recently, his girlfriend got some numbers, and at the same time, she still likes trees, so she plans to put the numbers into a tree.
On this day, she is almost finished spelling it out, and at the same time she and her friends have an appointment to go out on vacation. The greedy space dog accidentally ate all the branches of the tree. So fear surrounds the Cosmic Dog. He wants to restore the entire tree now, but it only knows that this tree is a binary search tree, and the gcd (greatest common divisor) of the two nodes connected to any tree edge exceeds 1.
But Cosmic Dog only emits cosmic rays. He comes to ask for your help and ask if you can help him solve this problem.

Problem solving ideas

This is a question of interval dp, we enumerate the interval [ l , r ] [l, r] , For any interval [ l , r ] [l,r] In other words, the parent node of the upper layer must be l 1 l−1 or r + 1 r+1
For each interval, enumerate a root node, divide the interval into two sub-intervals, the left half and the right half, then the parent nodes of these two sub-intervals are enumerated for us Of this point.
we use d p [ i ] [ j ] [ 0 ] dp[i][j][0] Representation interval [ l , r ] [l,r] Does the formed subtree have a scheme that satisfies the value of its root node and a l 1 a_{l−1} Not relatively prime
d p [ i ] [ j ] [ 1 ] dp[i][j][1] Representation interval [ l , r ] [l,r] Does the formed subtree have a scheme that satisfies the value of its root node and a r + 1 a_{r+1} Not relatively prime
transfer is enumeration k ( l k r ) k(l≤k≤r)
if d p [ l ] [ k 1 ] [ 1 ] = 1 dp[l][k−1][1]=1 And d p [ k + 1 ] [ r ] [ 0 ] = 1 dp[k+1][r][0]=1 And a k a_k versus a l 1 a_{l-1} Relatively prime, then d p [ l ] [ r ] [ 0 ] = 1 dp[l][r][0]=1 ;
if d p [ l ] [ k 1 ] [ 1 ] = 1 dp[l][k−1][1]=1 And d p [ k + 1 ] [ r ] [ 0 ] = 1 dp[k+1][r][0]=1 And a k a_k versus a r + 1 a_{r+1} Relatively prime, then d p [ l ] [ r ] [ 1 ] = 1 dp[l][r][1]=1 ;
To prevent double calculation of gcd, the gcd between all nodes can be preprocessed and stored in a two-dimensional array.

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[705], dp[705][705][2], GCD[705][705];
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}
int main() {
	int T;
	cin >> T;
	while(T--){
		int n;
		cin >> n;
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= n; i++)
			cin >> a[i];
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				GCD[i][j] = gcd(a[i], a[j]);
		for(int i = 1; i <= n; i++){
			if(GCD[i][i + 1] != 1){
				dp[i + 1][i + 1][0] = 1;
				dp[i][i][1] = 1;
			}
		}
		for(int i = 1; i < n; i++){
			for(int l = 1; l + i <= n; l++){
				int r = l + i;
				for(int k = l; k <= r; k++){
					if((k == l || dp[l][k-1][1]) && (dp[k+1][r][0] || k == r)){
						if(l != 1 && GCD[l - 1][k] != 1)
							dp[l][r][0] = 1;
						if(r != n && GCD[k][r + 1] != 1)
							dp[l][r][1] = 1;
					}
				}
			}
		}
		bool flag = false;
		if(dp[2][n][0] || dp[1][n-1][1]) flag = true;
		for(int i = 2; i < n - 1; i++){
			if(dp[1][i - 1][1] && dp[i + 1][n][0])
				flag = true;
		}
		if(!flag) cout << "No\n";
		else cout << "Yes\n";
	}
    return 0;
}

Intelligent Recommendation

Programming Thinking and Practice CSP-M4 Supplementary Questions (1/2/Smart Class)

TT count ducks Time limit Space limit 1S 256MB Title description On this day, TT felt uncomfortable at home because of the epidemic. After catching cats in the cloud for an hour, TT decided to go to t...

Shandong University Programming Design Thinking CSP Model Test M4

A. TT count ducks Title description On this day, TT felt uncomfortable at home because of the epidemic. After catching cats in the cloud for an hour, TT decided to go to the nearby hills. TT came to a...

Programming Thinking and Practice CSP-M1

Programming Thinking and Practice CSP-M1 Problem A-Cuckoo East's Adventure description: Gu Gu Dong is a playful child. One day, he got a magical circle from ancient ruins. This ring is made up of alph...

Programming Thinking and Practice CSP-M2

Article Directory Programming Thinking and Practice CSP-M2 Problem A- HRZ sequence Description Input Output Sample Input Sample Onput Note Idea Codes Problem B-HRZ Learn English Description Input Outp...

Programming Thinking and Practice CSP-M3

Article Directory Problem A-Ruishen's sequence Description Input Output Sample Input Sample Output Idea Codes Problem B- Master of Eliminating Music Description Input Output Sample Input1 Sample Outpu...

More Recommendation

Program design thinking and practice CSP-M4 B-ZJM must resist cosmic rays

Title description: According to rumours, 2020 will be a year of concentrated cosmic rays, which cannot be separated from the mysterious cosmic dog! But Ruishen and Dongdong are busy In the face-to-fac...

Programming Thinking and Practice CSP-M1 Exercises

Problem A-Cuckoo East's Adventure Title Gu Gu Dong is a playful child. One day, he got a magical circle from ancient ruins. This ring is made up of alphabets that are connected end to end. There is a ...

Programming Thinking and Practice CSP-M1 Supplement

A-Cuckoo East's Adventure title Gu Gu Dong is a playful kid. One day, he got a magical circle from ancient ruins. This ring is made up of alphabets that are connected end to end. There is a pointer on...

Programming Thinking and Practice CSP Simulation 1-Drawing

Question name 201512-3 drawing Title Input Line 1 has three integers m, n and q. m and n represent the width and height of the canvas, in characters. q represents the number of drawing operations. Lin...

Summary of classes and objects, static variables, instance variables, Final and Static modifiers

Summary of classes and objects, static variables, instance variables, Final and Static modifiers Class and object kind:Collection with the same characteristics, abstract template Object:Things with so...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top