其他分享
首页 > 其他分享> > #706 (Div. 2) A B (思维 C 贪心

#706 (Div. 2) A B (思维 C 贪心

作者:互联网

A. Split it!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
char s[109];
void work()
{
	cin >> n >> k;
	scanf("%s", s+1);
	if(n % 2 == 0 && n / 2 == k)
	{
		cout << "NO\n";return;
	}
	for(int i = 1; i <= k; ++i)
		if(s[i] != s[n+1-i])
		{
			cout << "NO\n";return;
		}
	cout << "YES\n";
}
int main()
{
	int t;
	cin >> t;
    while(t--)
        work();
    return 0;
}

B. Max and Mex
0 1 2 3 4 … 这样的输出 n + k
否则找到 mex和max,求一下这个 mid,如果原来有就输出 n + 1,否则就输出 n

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 9;
int n, k;
int a[maxn];

void work()
{
	map<int,int> ma;
    int mex = 0;
    scanf("%d %d", &n, &k);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), ma[a[i]] = 1;
    sort(a + 1, a + n + 1);
    bool f = 1;
    for(int i = 1; i <= n; ++i) if(a[i] != i - 1) {
    	f = 0, mex = i - 1;break;
	}
    if(f)
        printf("%d\n", n + k);
    else
    {
        int mx = *max_element(a+1,a+1+n), x;
        x = (mx + mex + 1) / 2;
        if(!ma[x] && k) printf("%d\n",n + 1);
        else printf("%d\n", n);
    }
}

int main()
{
   int T; scanf("%d", &T);
   for(int ca = 1; ca <= T; ++ca)
       work();
    return 0;
}

C. Diamond Miner
贪心,离原点近的之间连起来

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 9;
int n, k;
double a[maxn], b[maxn];
bool cmp(double x, double y)
{
	return abs(x) < abs(y);
}
double f(double x, double y)
{
	return sqrt(x*x+y*y);
}
void work()
{
	int c1 = 0, c2 = 0;
	cin >> n;
	for(int i = 1; i <= n*2; ++i)
	{
		double x, y;
		scanf("%lf %lf", &x, &y);
		if(x) a[++c1] = x;
		else b[++c2] = y;
	}
	double ans = 0;
	sort(a+1,a+1+n,cmp);sort(b+1,b+1+n,cmp);
	for(int i = 1; i <= n; ++i)
	{
		ans += f(a[i], b[i]);
	}
	printf("%.9f\n", ans);
}

int main()
{
   int T; scanf("%d", &T);
   for(int ca = 1; ca <= T; ++ca)
       work();
    return 0;
}

标签:int,double,706,long,maxn,printf,Div,scanf,贪心
来源: https://blog.csdn.net/cosx_/article/details/118877251