1. 程式人生 > >Educational Codeforces Round 23 A-F 補題

Educational Codeforces Round 23 A-F 補題

als nal 枚舉 typedef code col sca 復雜 long long

A Treasure Hunt

註意負數和0的特殊處理。。 水題。。 然而又被Hack了 嗎的智障

#include<bits/stdc++.h>
using namespace std;







int main()
{
 int sa,sb,da,db,x,y;
 scanf("%d%d%d%d%d%d",&sa,&sb,&da,&db,&x,&y);
 sa=da-sa;sb=db-sb;
 if(sa<0)sa*=-1;
 if(sb<0)sb*=-1;
 if(x<0)x*=-1;
 if(y<0)y*=-1;
 if((sa!=0&&x==0)||(sb!=0&&y==0)){printf("NO\n");return 0;}
 if((sa==0)&&(sb==0)){printf("YES\n");return 0;}
 if(((sa%x)!=0)||((sb%y)!=0)){printf("NO\n");return 0;}
 if((((max(sa/x,sb/y))-(min(sa/x,sb/y)))%2)!=0){printf("NO\n");return 0;}
 printf("YES\n");
 return 0;
}

B Makes And The Product

排序後求一下組合數就好了。。

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N=2e5;
LL num[N];
map<LL,LL>ma;




LL C(LL n,LL m)
{
 LL ans=1;
 for(int i=0;i<m;i++)
 	ans*=n-i;
 for(int i=m;i>=2;i--)
 	ans/=i;
 return ans;
}
int main()
{
 int n;
 scanf("%d",&n);
 for(int i=0;i<n;i++)scanf("%I64d",num+i);
 sort(num,num+n);
 LL ans=1;
 for(int i=0;i<3;i++)
 	ma[num[i]]++;
 LL l=ma[num[2]];
 for(int i=3;i<n&&num[i]==num[2];i++)
 	{
 	 l++;
	}
 ans=ans*C(l,ma[num[2]]);
 printf("%I64d\n",ans);
 return 0;
}

C Really Big Numbers

肯定存在某個數k 大於k的數都是big number... 二分找最小的k就好了

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;


LL sum(LL x)
{
 LL dex=1;
 LL ans=0;
 while(x>0)
 	{
 	 ans+=((x%10)*(dex-1));
 	 x/=10;dex*=10;
	}
 return ans;
}



int main()
{
 LL n,s;
 scanf("%I64d%I64d",&n,&s);
 LL l=1,r=n;
 LL i;
 bool flag=false;
 LL ri;
 while(l<=r&&r<=n)
 	{
 	  i=(l+r)>>1;
 	 if(sum(i)>=s){flag=true;ri=i;r=i-1;}
 	 	else {l=i+1;}
	}
if(flag) {printf("%I64d\n",(n-ri+1));return 0;}
 	else printf("0\n");
 return 0;
}

D Imbalanced Array

暴力枚舉每個區間 復雜度O(n^2)起步 顯然是不可以的。

那麽只能考慮每個位置對答案做的貢獻。即它是多少個區間的最值?

用棧維護,從前往後一個一個堆棧,並且保證棧中的所有元素構成不上升序列即可。

復雜度O(n)技巧性還是很強的。。

#include <bits/stdc++.h>
using namespace std;
long long n,pos[1000005],neg[1000005];
long long solve(long long a[]){
	stack<pair<int,long long>> s;
	a[n]=1e8;
	s.push({-1,1e9});
	long long sum=0;
	for(int i=0;i<=n;s.push({i,a[i]}),++i)
		while(a[i]>s.top().second){
			auto p=s.top();
			s.pop();
			auto p2=s.top();
			sum+=p.second*(i-p.first)*(p.first-p2.first);
		}
	return sum;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=0;i<n;neg[i]=(-1)*pos[i],++i)
		cin>>pos[i];
	cout<<solve(pos)+solve(neg);
}

E Choosing The Commander

F MEX Queries

Educational Codeforces Round 23 A-F 補題