您的位置:首页 > 其它

Coder-Strike 2014 - Finals Div 2

2014-04-23 17:41 281 查看
B题:

输入一个只含‘A’-‘Z’的字符串,要求每个字符是对称的且是回文串。

#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stack>
#include <vector>
#include <string.h>
#include <stdio.h>
using namespace std;
int hash[26];

void init()
{
    memset(hash,0,sizeof(hash));
    for(int i = 0; i < 26; i++)
    {
        if(i+'A' == 'A' || i+'A' == 'H' || i+'A' == 'I' || i+'A' == 'M' || i+'A' == 'O'
        || i+'A' == 'T' || i+'A' == 'U' || i+'A' == 'V' || i+'A' == 'W' || i+'A' == 'X'
        || i+'A' == 'Y')
            hash[i] = 1;
    }
}

int main()
{
    char s[100010];
    int len;
    init();
    while(~scanf("%s",s))
    {
        len = strlen(s);
        int flag = 1;

        for(int i = 0; i < len; i++)
        {
            if(s[i] != s[len-1-i] || hash[s[i]-'A'] == 0)
            {
                flag = 0;
                break;
            }
        }

        if(flag == 0)
            printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}


C题:

题意:输入n和p,接下来有n行,第i行两个a,b,代表第i个人认为a和b是嫌疑犯。领导要从中选出两个嫌疑犯,问有多少种选法使至少有p个人同意他的决定。某个人同意定义为这两个人中至少有一个人是他认为的嫌疑犯。

思路:拿一个数组存每个嫌疑犯被怀疑的次数,升序排序,对第i 个嫌疑犯,二分查找[i+1,n]到第j个人满足i和j被怀疑的次数之和大于等于p,那么j之后的与i之和肯定大于等于p。求得ans。但结果偏大,因为可能有同一个人怀疑i和j。那么要更新ans。

学习:

lower_bound():

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置



map和pair的应用。

#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string.h>
#define LL long long
#define _LL __int64

using namespace std;
const int INF = 0x3f3f3f3f;

map < pair<int, int>, int > mp;
int n,p;
int c1[300010],c2[300010];
_LL ans;

int main()
{
	int i,j,a,b;
	scanf("%d %d",&n,&p);
	memset(c1,0,sizeof(c1));
	memset(c2,0,sizeof(c2));

	for(i = 1; i <= n; i++)
	{
		scanf("%d %d",&a,&b);
		if(a > b)
			swap(a,b);

		c1[a]++;
		c1[b]++;
		c2[a]++;
		c2[b]++;
		
		mp[ make_pair(a,b) ] += 1;
	}

	sort(c1+1,c1+1+n);
	map< pair<int,int>,int>::iterator it;
	ans = 0;
	for(i = 1; i <= n; i++)
	{
		int add = lower_bound(c1+1+i,c1+1+n,p-c1[i]) - c1; //lower_bound函数进行二分查找第一个大于等于p-c[i]的位置
		ans += n+1-add;
	}

	for(it = mp.begin(); it != mp.end(); it++)
	{
		a = it->first.first;
		b = it->first.second;
		if(c2[a]+ c2[b] >= p && c2[a] + c2[b] - mp[make_pair(a,b)] < p) //不满足的要减去
			ans--;
	}
	
	printf("%I64d\n",ans);
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: