码迷,mamicode.com
首页 > 其他好文 > 详细

CF #261 DIV2 A,B,C,D

时间:2014-08-16 16:30:50      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:os   io   for   ar   问题   div   amp   size   

A 先判断邻边长度是否相等,不是就无法构成正方形,否则的话位置坐标搞一下

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <deque>
#include <map>
#define Inf 0x3f3f3f3f
#define Maxn 1000005
#define LL long long 
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int main()
{
	int x1,y1,x2,y2;
	//re;
	//wr;
	while(cin>>x1>>y1>>x2>>y2)
	{
		if(x1==x2)
		{
			cout<<x1+abs(y1-y2)<<" "<<y1<<" "<<x2+abs(y1-y2)<<" "<<y2<<endl;
		}
		else if(y1==y2)
		{
			cout<<x1<<" "<<y1+abs(x1-x2)<<" "<<x2<<" "<<y2+abs(x1-x2)<<endl;
		}
		else if(abs(x2-x1)==abs(y2-y1))
		{
			cout<<x1<<" "<<y2<<" "<<x2<<" "<<y1<<endl;
		}
		else
			puts("-1");
	}
	return 0;
}


B 注意下最大和最小相等的情况,用long long

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <deque>
#include <map>
#define Inf 0x3f3f3f3f
#define Maxn 1000005
#define LL long long 
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int main()
{
	int a[Maxn],i,Max,Min;
	LL p,q,n;
	//re;
	//wr;
	while(cin>>n)
	{
		p=0;q=0;
		Max=-1;Min=Inf;
		for(i=0;i<n;i++)
		{
			cin>>a[i];
		    Max=max(Max,a[i]);
		    Min=min(Min,a[i]);
		}
		for(i=0;i<n;i++)
		{
			if(a[i]==Max)
				p++;
			if(a[i]==Min)
				q++;
		}
		if(Max!=Min)
			cout<<Max-Min<<" "<<p*q<<endl;
		else
			cout<<0<<" "<<n*(n-1)/2<<endl;
	}
	return 0;
}

C 问题等价于构造出n个长度为d的k进制数,其实就是将0-(n-1)转化为k进制数即可,当k^d<n时无解

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <deque>
#include <map>
#define Inf 0x3f3f3f3f
#define Maxn 1000005
#define LL long long 
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int ans[1005][1005],s;
bool check(int n,int k,int d)
{
	int tmp=1;
	while(d--)
	{
		tmp*=k;
		if(tmp>=n)
			return true;
	}
	return false;
}
void T(int n,int d,int k)
{
	int p=0;
	vector<int> t;
	while(n)
	{
		t.push_back(n%k);
		n/=k;
	}
	if(t.size()<d)
	{
		int tmp=d-t.size();
		while(tmp--)
			ans[s][p++]=1;
	}
	for(int i=t.size()-1;i>=0;i--)
		ans[s][p++]=t[i]+1;
	s++;
}
int main()
{
	int n,k,d;
	//re;
	//wr;
	while(~scanf("%d%d%d",&n,&k,&d))
	{
		s=0;
		memset(ans,0,sizeof(ans));
		if(!check(n,k,d))
		{
			puts("-1");
			continue;
		}
		else
		{
			for(int i=0;i<n;i++)
				T(i,d,k);
		}
		for(int j=0;j<d;j++)
			for(int i=0;i<n;i++)
				printf("%d%c",ans[i][j],i==n-1?'\n':' ');
	}
	return 0;
}

D 预处理出每一位的f(1,i,ai)和f(j,n,aj)的值,将f(j,n,aj)加入到树状数组中,求的时候就是求sigma(sum(f1[i]-1),i>=1&&i<n)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <bitset>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <deque>
#include <map>
#define Inf 0x3f3f3f3f
#define Maxn 1000005
#define LL long long 
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
LL tree[Maxn];
void add(int pos,int n,int x)
{
	for(int i=pos;i<=n;i+=lowbit(i))
		tree[i]+=x;
}
LL sum(int pos)
{
	LL ans=0;
	for(int i=pos;i>0;i-=lowbit(i))
		ans+=tree[i];
	return ans;
}
int main()
{
	map<int,int> mp;
	int i,n,a[Maxn],f1[Maxn],f2[Maxn];
	LL ans;
	//re;
	//wr;
	while(~scanf("%d",&n))
	{
		ans=0;
		memset(f1,0,sizeof(f1));
		memset(f2,0,sizeof(f2));
		for(i=1;i<=n;i++)
			scanf("%d",&a[i]);
		mp.clear();
		for(i=1;i<=n;i++)
			mp[a[i]]++,f1[i]=mp[a[i]];
		mp.clear();
		for(i=n;i>=1;i--)
			mp[a[i]]++,f2[i]=mp[a[i]],add(f2[i],n,1);
		for(i=1;i<n;i++)
		{
			add(f2[i],n,-1);
			ans+=sum(f1[i]-1);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}


CF #261 DIV2 A,B,C,D,布布扣,bubuko.com

CF #261 DIV2 A,B,C,D

标签:os   io   for   ar   问题   div   amp   size   

原文地址:http://blog.csdn.net/hqu_fritz/article/details/38613679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!