zoukankan      html  css  js  c++  java
  • Codeforces Round #304 (Div. 2) 题解

    题目链接:http://codeforces.com/contest/546

    A、题意:一个士兵要买香蕉,第1个香蕉价格是k,第二个香蕉价格是2k,以此类推,现在士兵有钱n要买w个香蕉,问他需要像朋友借多少钱才能买到香蕉

    解:ans=max((1+w)*w/2*k-n, 0);

     1 /*
     2  * Problem:  
     3  * Author:  SHJWUDP
     4  * Created Time:  2015/6/24 星期三 13:24:16
     5  * File Name: 233.cpp
     6  * State: 
     7  * Memo: 
     8  */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 
    14 using namespace std;
    15 
    16 typedef long long int64;
    17 
    18 int k, n, w;
    19 int main() {
    20 #ifndef ONLINE_JUDGE
    21     freopen("in", "r", stdin);
    22     //freopen("out", "w", stdout);
    23 #endif
    24     while(~scanf("%d%d%d", &k, &n, &w)) {
    25         int64 cost=(1LL+w)*w/2*k;
    26         printf("%I64d
    ", cost<=n?0:cost-n);
    27     }
    28     return 0;
    29 }
    View Code

    B、题意:给你n个整数,你可以花费w使得一个数x变为x+w,问你最小花费多少使得这n个数各不相同

    解:排序后顺次提升即可

     1 /*
     2  * Problem:  
     3  * Author:  SHJWUDP
     4  * Created Time:  2015/6/24 星期三 13:24:16
     5  * File Name: 233.cpp
     6  * State: 
     7  * Memo: 
     8  */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <queue>
    14 
    15 using namespace std;
    16 
    17 typedef long long int64;
    18 
    19 const int MaxA=3e3+7;
    20 
    21 int n;
    22 int arr[MaxA];
    23 int main() {
    24 #ifndef ONLINE_JUDGE
    25     freopen("in", "r", stdin);
    26     //freopen("out", "w", stdout);
    27 #endif
    28     while(~scanf("%d", &n)) {
    29         for(int i=0; i<n; i++) {
    30             scanf("%d", &arr[i]);
    31         }
    32         sort(arr, arr+n);
    33         int64 ans=0;
    34         for(int i=1; i<n; i++) {
    35             if(arr[i]<=arr[i-1]) {
    36                 ans+=arr[i-1]+1-arr[i];
    37                 arr[i]=arr[i-1]+1;
    38             }
    39         }
    40         printf("%I64d
    ", ans);
    41     }
    42     return 0;
    43 }
    View Code

    C、题意:两个士兵玩卡片游戏(卡片上写有正整数),他们各有一摞顺序确定的卡片,游戏开始后,两人比较最顶端卡片上数字的大小,数字大的赢得这两张卡片,并将赢得的卡片与当次参与比较的卡片放在自己卡片堆的下方(放置顺序为自己的在下,赢得的在上),最后没有卡片者输。问在第几局分出胜负,谁赢了,如果没人赢,输出-1

    解:吃我10w次模拟辣,hhh  ps:正解 大概是记忆化+bfs

     1 /*
     2  * Problem:  
     3  * Author:  SHJWUDP
     4  * Created Time:  2015/6/24 星期三 13:24:16
     5  * File Name: 233.cpp
     6  * State: 
     7  * Memo: 
     8  */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <queue>
    14 
    15 using namespace std;
    16 
    17 typedef long long int64;
    18 
    19 const int MaxA=3e3+7;
    20 
    21 int n;
    22 int mxA, mxB;
    23 int main() {
    24 #ifndef ONLINE_JUDGE
    25     freopen("in", "r", stdin);
    26     //freopen("out", "w", stdout);
    27 #endif
    28     while(~scanf("%d", &n)) {
    29         int num;
    30         scanf("%d", &num);
    31         queue<int> A;
    32         for(int i=0; i<num; i++) {
    33             int x;
    34             scanf("%d", &x);
    35             A.push(x);
    36         }
    37         scanf("%d", &num);
    38         queue<int> B;
    39         for(int i=0; i<num; i++) {
    40             int x;
    41             scanf("%d", &x);
    42             B.push(x);
    43         }
    44         int cnt=1e5+7, ans=0;
    45         while(cnt-- && !A.empty() && !B.empty()) {
    46             int a=A.front(); A.pop();
    47             int b=B.front(); B.pop();
    48             if(a>b) {
    49                 A.push(b); A.push(a);
    50             } else {
    51                 B.push(a); B.push(b);
    52             }
    53             ans++;
    54         }
    55         if(A.empty()) {
    56             printf("%d 2
    ", ans);
    57         } else if(B.empty()) printf("%d 1
    ", ans);
    58         else puts("-1");
    59     }
    60     return 0;
    61 }
    View Code

    D、题意:对于一个正整数n,它的分数为它的最小因子数,现在给你a,b,(a>b),告诉你n=a!/b!,问你n的分数

    解:类似打质数表

     1 /*
     2  * Problem:  
     3  * Author:  SHJWUDP
     4  * Created Time:  2015/6/24 星期三 13:24:16
     5  * File Name: 233.cpp
     6  * State: 
     7  * Memo: 
     8  */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <algorithm>
    13 #include <queue>
    14 
    15 using namespace std;
    16 
    17 typedef long long int64;
    18 
    19 const int MaxA=5e6+7;
    20 
    21 int A, B;
    22 int64 table[MaxA];
    23 int main() {
    24 #ifndef ONLINE_JUDGE
    25     freopen("in", "r", stdin);
    26     //freopen("out", "w", stdout);
    27 #endif
    28     vector<int> prime;
    29     memset(table, -1, sizeof(table));
    30     table[1]=0;
    31     for(int i=2; i<MaxA; i++) {
    32         if(table[i]==-1) {
    33             table[i]=1;
    34             prime.push_back(i);
    35         }
    36         for(int j=0, lim=prime.size(); j<lim; j++) {
    37             int64 tmp=int64(i)*prime[j];
    38             if(tmp<MaxA) {
    39                 table[tmp]=table[i]+1;
    40             } else break;
    41         }
    42         table[i]+=table[i-1];
    43     }
    44     int T;
    45     scanf("%d", &T);
    46     while(T--) {
    47         scanf("%d%d", &A, &B);
    48         printf("%I64d
    ", table[A]-table[B]);
    49     }
    50     return 0;
    51 }
    View Code

    E、题意:有n个城市之间有m条路连接着,每个城市驻守着士兵,现在要改变驻守策略(即改变每个城市驻守士兵的数量),但是每个城市的士兵只能向相邻城市移动一次,问你能否成功,如果成功输出移动方法

    解:网络流最大流,将每个城市拆成两点,分别代表入口和出口,

    源点对任何点入口建原驻守士兵数量容量边

    任何点的入口对自己的出口建立INF容量边

    如果a与b间有路,那么建立a入口到b出口的INF容量边

    任何点的出口对汇点建变防后驻守士兵数量容量边

    那么最大流等于总士兵数时移动可行

      1 /*
      2  * Problem:  
      3  * Author:  SHJWUDP
      4  * Created Time:  2015/7/5 星期日 12:05:43
      5  * File Name: 233.cpp
      6  * State: 
      7  * Memo: 
      8  */
      9 #include <iostream>
     10 #include <cstdio>
     11 #include <cstring>
     12 #include <algorithm>
     13 #include <queue>
     14 
     15 #define all(o) o.begin(), o.end()
     16 
     17 using namespace std;
     18 
     19 typedef vector<int> vi;
     20 typedef long long int64;
     21 
     22 const int INF=0x7f7f7f7f;
     23 
     24 const int MaxA=100+7;
     25 const int MaxB=200+7;
     26 const int MaxC=MaxA*3+MaxB*2;
     27 
     28 struct Edge {
     29     int v, cap;
     30 };
     31 
     32 struct Dinic {
     33     int n, m, s, t; 
     34     vector<Edge> edges;
     35     vector<vector<int> > g;
     36     vector<int> cur, d;
     37 
     38     void init(int n) {
     39         this->n=n;
     40         m=0;
     41         g.resize(n);
     42         cur.resize(n);
     43         d.resize(n);
     44     }
     45 
     46     void add(int u, int v, int cap, bool rev=0) {
     47         edges.push_back((Edge){v, cap});
     48         edges.push_back((Edge){u, rev?cap:0});
     49         m=(int)edges.size();
     50         g[u].push_back(m-2);
     51         g[v].push_back(m-1);
     52     }
     53 
     54     bool BFS() {
     55         fill(d.begin(), d.end(), -1);
     56         queue<int> Q;
     57         Q.push(s);
     58         d[s]=0;
     59         while(!Q.empty()) {
     60             int u=Q.front(); Q.pop();
     61             for(int i=(int)g[u].size()-1; i>=0; i--) {
     62                 Edge& e=edges[g[u][i]];
     63                 if(d[e.v]==-1 && e.cap>0) {
     64                     d[e.v]=d[u]+1;
     65                     Q.push(e.v);
     66                 }
     67             }
     68         }
     69         return d[t]!=-1;
     70     }
     71 
     72     int augment(int u, int a) {
     73         if(u==t || a==0) return a;
     74         int flow=0, f;
     75         for(int i=cur[u]; i>=0; i--) {
     76             Edge& e=edges[g[u][i]];
     77             if(d[e.v]==d[u]+1 && (f=augment(e.v, min(a, e.cap)))>0) {
     78                 e.cap-=f; edges[g[u][i]^1].cap+=f;
     79                 a-=f; flow+=f;
     80                 if(a==0) break;
     81             }
     82         }
     83         return flow;
     84     }
     85 
     86     int maxFlow(int s, int t) {
     87         this->s=s; this->t=t;
     88         int flow=0;
     89         while(BFS()) {
     90             for(int i=0; i<n; i++) cur[i]=(int)g[i].size()-1;
     91             flow+=augment(s, INF);
     92         }
     93         return flow;
     94     }
     95 
     96     friend ostream& operator<<(ostream& os, Dinic &mf) {
     97         os<<"		#begin"<<endl;
     98         for(int i=0; i<mf.m; i++) {
     99             Edge& e=mf.edges[i];
    100             os<<e.v<<"	"<<e.cap<<endl;
    101         }
    102         os<<"		#end"<<endl;
    103         return os;
    104     }
    105 };
    106 
    107 int N, M;
    108 int main() {
    109 #ifndef ONLINE_JUDGE
    110     freopen("in", "r", stdin);
    111     //freopen("out", "w", stdout);
    112 #endif
    113     while(~scanf("%d%d", &N, &M)) {
    114         vi arr1(N), arr2(N);
    115         int s=N<<1, t=s+1;
    116         Dinic mf; mf.init(t+1);
    117         for(int i=0; i<N; i++) {
    118             scanf("%d", &arr1[i]);
    119         }
    120         for(int i=0; i<N; i++) {
    121             scanf("%d", &arr2[i]);
    122         }
    123         for(int i=0; i<N; i++) {
    124             mf.add(s, i, arr1[i]);
    125             mf.add(N+i, t, arr2[i]);
    126             mf.add(i, N+i, INF);
    127         }
    128         for(int i=0; i<M; i++) {
    129             int a, b;
    130             scanf("%d%d", &a, &b), a--, b--;
    131             mf.add(a, N+b, INF);
    132             mf.add(b, N+a, INF);
    133         }
    134         vector<Edge> oedges=mf.edges;
    135         int flow=mf.maxFlow(s, t);
    136         //cout<<mf;
    137         if(flow!=accumulate(all(arr1), 0) || flow!=accumulate(all(arr2), 0)) {
    138             puts("NO"); continue;
    139         }
    140         vector<vi > ans(N, vi(N));
    141         for(int u=0; u<N; u++) {
    142             for(int i=mf.g[u].size()-1; i>=0; i--) {
    143                 int p=mf.g[u][i];
    144                 int v=oedges[p].v;
    145                 if(N<=v && v<N+N) {
    146                     ans[u][v-N]=oedges[p].cap-mf.edges[p].cap;
    147                 }
    148             }
    149         }
    150         puts("YES");
    151         for(int i=0; i<N; i++) {
    152             for(int j=0; j<N; j++) {
    153                 printf("%d%c", ans[i][j], " 
    "[j==N-1]);
    154             }
    155         }
    156     }
    157     return 0;
    158 }
    View Code
  • 相关阅读:
    eclipse连接远程hadoop集群开发时0700问题解决方案
    螺旋线
    双曲抛物面
    双曲抛物面
    工业相机标定相关知识整理
    高科技 stuff
    高科技 stuff
    杜甫诗百首
    杜甫诗百首
    经典纪录片
  • 原文地址:https://www.cnblogs.com/shjwudp/p/4622045.html
Copyright © 2011-2022 走看看