zoukankan      html  css  js  c++  java
  • 洛谷七月月赛

    A题

    分析:直接贪心即可,注意要long long

     1 #include "iostream"
     2 #include "cstdio"
     3 #include "cstring"
     4 #include "string"
     5 using namespace std;
     6 const int maxn=1e5+10;
     7 long long a[maxn],x;
     8 int n;
     9 int main()
    10 {
    11     cin>>n>>x;
    12     for(int i=0;i<n;i++)
    13     scanf("%lld",&a[i]);
    14     long long ans=0;
    15     for(int i=1;i<n;i++){
    16         if(a[i]+a[i-1]>x){
    17             long long num=a[i]+a[i-1]-x;
    18             if(a[i]>=num){
    19                 a[i]-=num;
    20                 ans+=num;
    21             }else{
    22                 long long tt=num-a[i];
    23                 a[i-1]-=tt;
    24                 a[i]=0;
    25                 ans+=num;
    26             }
    27         }
    28     }
    29     cout<<ans<<endl;
    30     return 0;
    31 }
    View Code

    B题

    分析:直接BFS即可,注意因为这个地方有跳变的情况,但是只能跳变一次,所以我们设置两个状态,移动过去的标记为0,跳变过去的标记为1

     1 #include "iostream"
     2 #include "cstdio"
     3 #include "cstring"
     4 #include "queue"
     5 using namespace std;
     6 const int maxn=1100;
     7 int vis[maxn][maxn][2];
     8 int h,w,d,r;
     9 char s[maxn][maxn];
    10 int dx[]={1,-1,0,0};
    11 int dy[]={0,0,1,-1};
    12 struct Node{
    13     int x,y,flag,step;
    14 };
    15 int bfs(){
    16     Node t;
    17     t.x=1,t.y=1,t.flag=0,t.step=0;
    18     queue<Node>p;
    19     vis[t.x][t.y][t.flag]=1;
    20     p.push(t);
    21     while(!p.empty()){
    22         Node e,f;
    23         e=p.front(); p.pop();
    24         if((vis[e.x][e.y][0]||vis[e.x][e.y][1])&&(e.x==h&&e.y==w))
    25         return e.step;
    26         for(int i=0;i<4;i++){
    27             int x=e.x+dx[i],y=e.y+dy[i];
    28             if(x<=0||x>h||y<=0||y>w)  continue;
    29             if(s[x][y]=='#'||vis[x][y][e.flag])  continue;
    30             vis[x][y][e.flag]=1;
    31             f.x=x,f.y=y,f.flag=e.flag,f.step=e.step+1;
    32             p.push(f);
    33         }
    34         if(e.flag==0){
    35             int xx=e.x+d,yy=e.y+r;
    36             if(xx<=0||xx>h||yy<=0||yy>w) continue;
    37             if(s[xx][yy]=='#'||vis[xx][yy][1])  continue;
    38             vis[xx][yy][1]=1;
    39             f.x=xx,f.y=yy,f.flag=1,f.step=e.step+1;
    40             p.push(f);
    41         }
    42     }
    43     return -1;
    44 }
    45 int main()
    46 {
    47     cin>>h>>w>>d>>r;
    48     getchar();
    49     for(int i=1;i<=h;i++){
    50         for(int j=1;j<=w;j++)
    51         scanf("%c",&s[i][j]);
    52         getchar();
    53     }
    54     memset(vis,0,sizeof(vis));
    55     cout<<bfs()<<endl;
    56 }
    View Code

    C题

    分析:这个题比较考验开脑洞的能力,首先我们要先按照住宿从前往后排序,其次这个公交站必须建在有住宿的点上,然后我们在脑补一下每次移动有多少人会+1,多少人会-1,则我们可以得到最终建在正好占总人数1/2的那个住宿处

     1 #include "iostream"
     2 #include "cstdio"
     3 #include "cstring"
     4 #include "string"
     5 #include "algorithm"
     6 using namespace std;
     7 const int maxn=1e5+10;
     8 long long L;
     9 int n;
    10 typedef pair<long long,long long> Node;
    11 Node p[maxn];
    12 int main()
    13 {
    14     cin>>L>>n;
    15     cout<<(2>>1)<<endl; 
    16     for(int i=1;i<=n;i++)
    17     cin>>p[i].first>>p[i].second;
    18     sort(p+1,p+1+n);
    19     long long s=0;
    20     for(int i=1;i<=n;i++)
    21     s+=p[i].second;
    22     s=(s+1)>>1;
    23     long long o=0,l=0;
    24     int b=0;
    25     for(int i=1;i<=n;i++){
    26         o+=p[i].second;
    27         if(!b&&o>=s) b=1,l=p[i].first;
    28     }
    29     long long ans=0;
    30     for(int i=1;i<=n;i++){
    31         if(p[i].first<l) ans+=(l-p[i].first)*(p[i].second);
    32         else ans-=(l-p[i].first)*(p[i].second);
    33     }
    34     cout<<ans<<endl;
    35 } 
    View Code
  • 相关阅读:
    AS3 判断双击事件
    php 数据类型转换与比较
    几行几列算法
    CCNode的属性说明
    bitmapdata的知识点
    addFrameScript用法
    TweenMax.allTo
    flash TweenMax用法
    flash流媒体资料
    c实现windows socket
  • 原文地址:https://www.cnblogs.com/wolf940509/p/7105873.html
Copyright © 2011-2022 走看看