赞
踩
前天晚上写的一场牛客上比赛,虽然只写出了三道,但比起之前的成绩感觉自己明显有了一点进步了,继续努力吧,
- #include<bits/stdc++.h>
- using namespace std;
- int a[100005];
- int main(){
- int n;
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>a[i];
- }
- sort(a+1,a+n+1);
- int flag=0;
- for(int i=1;i<=n-2;i++){
- if(a[i]==a[i+1]&&a[i+1]==a[i+2])flag=1;
- }
- if(flag)cout<<"YES"<<endl;
- else cout<<"NO"<<endl;
- return 0;
- }

签到题,我是排序后看看有没有三个连续相等的数字。更好的做法应该是用桶排看看有没有出现次数超过三的数字,其实都差不多。
- #include <bits/stdc++.h>
- using namespace std;
-
- int main()
- {
- int n,x,f=1,a=0;cin>>n;
- for(int i=0;i<n;i++)
- {
- cin>>x;
- if(x==0) f=0;
- }
- if(f==0) cout<<"NO"<<endl;
- else cout<<"YES"<<endl;
- return 0;
- }
又一道签到题,只要包含0就不是好数组。
- #include<bits/stdc++.h>
- using namespace std;
- int n,x;
- const long long mod=1e9+7;
- typedef long long ll;
- ll quick(ll a,ll b){
- ll res=1;
- while(b){
- if(b%2)res=res*a%mod;
- a=a*a%mod;
- b/=2;
- }
- return res;
- }
- ll f[10005];
- void init(){
- f[0]=1;
- for(int i=1;i<=10005;i++){
- f[i]=f[i-1]*i%mod;
- }
- }
- ll inv(ll x){
- return quick(x,mod-2)%mod;
- }
- int main(){
- init();
- cin>>n>>x;
- ll cnt=(ll)sqrt(x);
- if(cnt<n){
- cout<<0<<endl;
- return 0;
- }
- //cout<<f[cnt]<<" "<<inv(f[n])<<" "<<inv(f[cnt-n])<<endl;
- ll ans=(f[cnt]*((inv(f[n])%mod*inv(f[cnt-n]%mod))%mod))%mod;
- cout<<ans<<endl;
- return 0;
- }

预处理阶乘和逆元(费马小定理),然后求组合数,注意要开long long,并且在计算过程中一定要时时取模否则会暴。
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int a[15][15],b[15][15];
- bool book[15][15][10005];
- ll n,m,p;
- ll quick(ll a,ll b,ll mod){
- ll res=1;
- while(b){
- if(b%2)res=res*a%mod;
- a=a*a%mod;
- b/=2;
- }
- return res;
- }
- int ne[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
- typedef struct stu{
- int x;
- int y;
- int z;
- int c;
- }node;
- int ans=1e9+7;
- queue<node> q;
- void bfs(int x,int y,int z,int c){
- node k={x,y,z,c};
- q.push(k);
- while(!q.empty()){
- k=q.front();
- q.pop();
- if(book[k.x][k.y][k.c%(p-1)])continue;
- book[k.x][k.y][k.c%(p-1)]=true;
- if(k.x==n&&k.y==m&&k.c%(p-1)==0){
- ans=min(k.z,ans);
- break;
- }
- for(int i=0;i<4;i++){
- int nx=k.x+ne[i][0];
- int ny=k.y+ne[i][1];
- if(nx>=1&&nx<=n&&ny>=1&&ny<=m){
- //cout<<nx<<" "<<ny<<" "<<k.z+1<<" "<<k.c+a[nx][ny]<<endl;
- int cc=(k.c+a[nx][ny])%(p-1);
- node l={nx,ny,k.z+1,cc};
- q.push(l);
- }
- }
- }
- }
- int main(){
- cin>>n>>m>>p;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- cin>>a[i][j];
- }
- }
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- cin>>b[i][j];
- }
- }
- bfs(1,1,0,a[1][1]);
- if(ans==1e9+7)cout<<-1;
- else cout<<ans;
- return 0;
- }

首先咱们先看c的表式方式
首先我们要知道我们最后要求的数是对(p-1)取模的数,我们完全可以在输入的时候就将图上的数直接取模。然后我们再对Cij进行简化,很容易发现对(p-1)取模一直都为1。
(由于
可以将展开为
)
然后用bfs跑分层图,第一、二维为x,y,第三维为走到这个点时的计数器上的数对(p-1)取模的结果(这样能避免多走但计数器取模后一样的情况)。具体写法见上。如果能搜到答案就输出,不然就输出-1.
- #include<bits/stdc++.h>
- using namespace std;
- int a[100005];
- int pre[100005],sum[100005][101];
- int main(){
- int n;
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>a[i];
- if(a[i]<0)pre[i]=pre[i-1]-a[i];
- else pre[i]=pre[i-1];
- for(int j=1;j<=100;j++){
- sum[i][j]=sum[i-1][j]+(a[i]==j);
- }
- }
- int q;
- cin>>q;
- for(int i=1;i<=q;i++){
- int l,r,tmp=0;
- cin>>l>>r;
- int t=pre[r]-pre[l-1];
- int res=0;
- int cnt=0;
- for(int j=1;j<=100;j++){
- int k=sum[r][j]-sum[l-1][j];
- if(k!=0){
- if(k*j+res>=t){
- cnt+=(t-res)/j;
- break;
- }
- else cnt+=k;res+=k*j;
- //cout<<" "<<t<<" "<<cnt<<" "<<res<<endl;
- }
- }
- cout<<cnt+1<<endl;
- }
- return 0;
- }

这题是我看了他发的答案才写出来的,咱们首先得要知道他的A类价值数是连续的,可能又相等的值,但一定是一个区间内的所有数,考虑 A 类价值最小的方案,是从小到大排序序列 a。然后进行n 次,将序列 a 的最后一个数字移动到最前面。会发现这样操作序列 a 的 A 类价值是单调不减的,并且每次只移动一个数,增量只可能是0 或者 1。所以可以得出一个结论,A 类价值是连续的。所以我们最后只需要求他的最大A的值r和最小A值l,答案就为r-l+1。上界即为正数个数,下界就是从小到大排的第一个正数前缀到尾的距离。
看着好高级。
我还是太菜了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。