Cattle off the third game

Topic
C:
Violent search

#include<bits/stdc++.h>
using namespace std;
int n, k, r, ans;
struct ty
{
    
    
    int x, y, r;
}a[20], pos[10];
bool xiangjiao(int i, int j)
{
    
    
	// 两圆相切   可以用勾股定理判断
    if ((a[i].x-pos[j].x)*(a[i].x-pos[j].x)+(a[i].y-pos[j].y)*(a[i].y-pos[j].y) <= (a[i].r+r)*(a[i].r+r)) 
	return 1;
    return 0;
}
int calc()
{
    
    
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    	for (int j = 1; j <= k; j++)
    	{
    
    
        	if (xiangjiao(i, j))
        	{
    
    
          	  cnt++;
          	  break;
   // 此时存了 k 个点,一但某个所给的点可以被k个点中某个打到,就break
   //  保证了一个点不会被重复计数 
        	}
   		}
    return cnt;
}

void dfs(int dep)
{
    
    
    if (dep > k)// 存好所选的k个点,去判断这k个点最多消灭多少敌人 
    {
    
    
        ans = max(ans, calc());
        return ;
    }
    for (int i = -7; i <= 7; i++)
    	for (int j = -7; j <= 7; j++)
    	{
    
    
       	 	pos[dep].x = i;
       		pos[dep].y = j;
       	 	dfs(dep + 1);
   	 	}
	}
int main()
{
    
    
    scanf("%d %d %d", &n, &k, &r);
    for (int i = 1; i <= n; i++)
        scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].r);
    dfs(1);
    cout << ans << endl;
    return 0;
}

G question
and check set

#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Max = (int)1e5 + 9; 
int t,n,m;
//int bcj[Max];
struct xpy
{
    
    
	ll a;
	int x;
}bcj[1000009];
int Find(int x)
{
    
    
	int r = x, j, k = x;
	while(r != bcj[r].x) r = bcj[r].x; // 寻找根节点 
	while(k != r)  
	{
    
    
		j = bcj[k].x;  // 暂存此时k的父节点 
		bcj[k].x = r;  //  将k的父节点改为根节点
		bcj[r].a = max(bcj[k].a,bcj[r].a);
		k = j;  //  k移到父节点   直至全改为根节点  
	}
	return r;
}
void Union(int x,int y)
{
    
    
	x = Find(x);//  x变成x的根节点 
	y = Find(y);//   y变成y的根节点 
	if(x != y) bcj[x].x = y;
}
void work()
{
    
    	
	for(int i = 1; i <= n; i++) 
		{
    
    
			scanf("%lld", &bcj[i].a);
			bcj[i].x = i;
		}
	int x, y, t;
	while(m--)
	{
    
    
		scanf("%d %d", &x, &y);
		Union(x, y);
	}
	
	for(int i = 1; i <= n; i++) Find(i);// 更新根节点  保证这个集合的最大值更新到根节点身上

	ll sum = 0;
	for(int i = 1; i <= n; i++)
	{
    
    
		sum += bcj[Find(i)].a;
		//printf("%d ", Find(i));
	}
	cout << sum << endl;
}
int main()
{
    
    
	
	scanf("%d %d", &n, &m);
	work();
	return 0;
}

/*
6 4
1 2 3 4 5 6
1 2
2 3
4 5
5 6
*/

I problem
method one
dynamic programming
Insert picture description here

#include<bits/stdc++.h>

using namespace std;
const int N= 1e6+7;
int n,a[N],last[N], f[N];
int main()
{
    
    
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
    
    
        scanf("%d", &a[i]);
        // 用数组last存某一个数字上一次出现的位置
        if(last[a[i]] != 0) f[i] = max(f[i-1], f[last[a[i]]]+1);
        else f[i] = f[i-1];
        last[a[i]] = i;
    }
    cout << f[n];
    return 0;
}




Method Two
Greedy

#include <bits/stdc++.h>
using namespace std;
int a[1000010];//
map<int, int> m;
int main()
{
    
    
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    long long sum = 0;
    for (int i = 1; i <= n; i++)
    {
    
    
    //    cout<<i<<" "<<a[i]<<" "<<m[a[i]]<< endl;
        if (m[a[i]])
        {
    
    
            sum++;
            m.clear();
            
        }
        m[a[i]]=1;
        
    }
    cout<<sum<<endl;
    return 0;
}

Question J:
Game
Move forward from the final state and find some rules

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int Max = 1000009;
int n;
int sum1,sum2;
ll a[Max];
int main()
{
    
    
	int b;
	cin >> n;
	int sum = 0;
	for(int i = 1; i <= n; ++i)
	{
    
    
		int q;
		scanf("%d", &q);
		if(q % 2  == 0) sum++;
	}
	if(n == 1 && sum == 0) cout << "NiuNiu\n";
	else if(n == 1 && sum == 1) cout << "NiuMei\n";
	else if(sum >= 2) cout << "NiuMei\n";
	else 
	{
    
    
		if(n % 2 != 0) cout << "NiuMei\n";
		else cout << "NiuNiu\n";
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/cosx_/article/details/113742005