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

山东省2016acm省赛

时间:2016-06-10 17:40:39      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

A

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <list>
 5 #include <map>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstring>
 9 #include <sstream>
10 #include <string>
11 #include <cmath>
12 #include <queue>
13 using namespace std;
14 #define clc(a,b) memset(a,b,sizeof(a))
15 #define inf 0x3f3f3f3f
16 const int N=10010;
17 const int MOD = 1e9+7;
18 #define LL long long
19 void fre() {
20     freopen("in.txt","r",stdin);
21 }
22 inline int r() {
23     int x=0,f=1;char ch=getchar();
24     while(ch>9||ch<0) {if(ch==-) f=-1;ch=getchar();}
25     while(ch>=0&&ch<=9) { x=x*10+ch-0;ch=getchar();}return x*f;
26 }
27  
28 int main(){
29     int T;
30     T=r();
31     while(T--){
32         int x,y;
33         int ans;
34         x=r();
35         y=r();
36         if(x%y!=0)
37             ans=x/y+1;
38         else
39             ans=x/y;
40         cout<<ans<<endl;
41     }
42     return 0;
43 }

 

C

最短路

反向建边,记录当前点序号最小的前驱

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <list>
  5 #include <map>
  6 #include <stack>
  7 #include <vector>
  8 #include <cstring>
  9 #include <sstream>
 10 #include <string>
 11 #include <cmath>
 12 #include <queue>
 13 #include <bits/stdc++.h>
 14 using namespace std;
 15 #define clc(a,b) memset(a,b,sizeof(a))
 16 #define inf 0x3f3f3f3f
 17 const int N=100010;
 18 const int MOD = 1e9+7;
 19 #define LL long long
 20 void fre() {
 21     freopen("in.txt","r",stdin);
 22 }
 23 inline int r() {
 24     int x=0,f=1;char ch=getchar();
 25     while(ch>9||ch<0) {if(ch==-) f=-1;ch=getchar();}
 26     while(ch>=0&&ch<=9) { x=x*10+ch-0;ch=getchar();}return x*f;
 27 }
 28 int n,m;
 29 struct node{
 30     int v,c;
 31     node(int vv,int cc){
 32        v=vv;
 33        c=cc; 
 34     }
 35     node(){}
 36     bool operator <(const node &r) const{
 37         return c>r.c;
 38     }
 39 };
 40  
 41 struct edge{
 42     int v,cost;
 43     edge(int vv=0,int ccost =0):v(vv),cost(ccost){}
 44 };
 45  
 46 vector<edge>e[N];
 47 bool vis[N];
 48 int dist[N];
 49 int p[N];
 50 void dij(int start){
 51     clc(vis,false);
 52     for(int i=0;i<=n+1;i++) dist[i]=inf;
 53     priority_queue<node>q;
 54     while(!q.empty()) q.pop();
 55     dist[start]=0;
 56     q.push(node(start,0));
 57     node tmp;
 58     while(!q.empty()){
 59         tmp=q.top();
 60         q.pop();
 61         int u=tmp.v;
 62         if(vis[u]) continue;
 63         vis[u]=true;
 64         for(int i=0;i<e[u].size();i++){
 65              int v=e[u][i].v;
 66              int cost=e[u][i].cost;
 67              if(!vis[v]&&dist[v]>dist[u]+cost){
 68                 dist[v]=dist[u]+cost;
 69                 p[v]=u;
 70                 q.push(node(v,dist[v]));
 71              }
 72              else if(!vis[v]&&dist[v]==dist[u]+cost){
 73                 p[v]=min(p[v],u);
 74              }
 75         }
 76     }
 77 }
 78 void add(int u,int v,int w){
 79     e[u].push_back(edge(v,w));
 80 }
 81  
 82 void init(){
 83     for(int i=0;i<=n+1;i++){
 84         e[i].clear();
 85     }
 86     clc(p,-1);
 87 }
 88  
 89 int main(){
 90     // fre();
 91     int T;
 92     T=r();
 93     while(T--){
 94         n=r(),m=r();
 95         init();
 96         int u,v,w;
 97         while(m--){
 98             u=r(),v=r(),w=r();
 99             add(v,u,w);
100         }
101         dij(n+1);
102         if(dist[0]>=inf){
103             printf("-1\n");
104             continue;
105         }
106         else if(p[0]==n+1){
107             printf("0\n");
108             continue;
109         }
110         else
111             printf("%d\n",p[0]);
112     }
113     return 0;
114 }

 

D

归并排序技巧题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <list>
 5 #include <map>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstring>
 9 #include <sstream>
10 #include <string>
11 #include <cmath>
12 #include <queue>
13 using namespace std;
14 #define clc(a,b) memset(a,b,sizeof(a))
15 #define inf 0x3f3f3f3f
16 const int N=200010;
17 const int MOD = 1e9+7;
18 #define LL long long
19 void fre() {
20     freopen("in.txt","r",stdin);
21 }
22 inline int r() {
23     int x=0,f=1;char ch=getchar();
24     while(ch>9||ch<0) {if(ch==-) f=-1;ch=getchar();}
25     while(ch>=0&&ch<=9) { x=x*10+ch-0;ch=getchar();}return x*f;
26 }
27 struct node{
28     int s,w,num;
29 }p[N];
30 int n,R,q;
31 node a[N],b[N];
32  
33 bool cmp(const node &a,const node &b){
34    return (a.s==b.s)?(a.num<b.num):(a.s>b.s);
35 }
36  
37 void work(){
38     int ai=1,bi=1;
39     for(int i=1;i<=2*n;i+=2){
40         if(p[i].w>p[i+1].w){
41             p[i].s++;
42             a[ai++]=p[i];
43             b[bi++]=p[i+1];
44         }
45         else{
46             p[i+1].s++;
47             a[ai++]=p[i+1];
48             b[bi++]=p[i];
49         }
50     }
51     int i=1,j=1,k=1;
52     while(i<ai&&j<bi){
53         if(cmp(a[i],b[j])){
54             p[k++]=a[i++];
55         }
56         else
57             p[k++]=b[j++];
58     }
59     while(i<ai) p[k++]=a[i++];
60     while(j<bi) p[k++]=b[j++];
61 }
62  
63 int main(){
64     // fre();
65     int T;
66     T=r();
67     while(T--){
68          n=r(),R=r(),q=r();
69          for(int i=1;i<=2*n;i++){
70             int x;
71             x=r();
72             p[i].s=x;
73             p[i].num=i;
74          }
75          for(int i=1;i<=2*n;i++){
76             int x;
77             x=r();
78             p[i].w=x;
79          }
80          sort(p+1,p+1+2*n,cmp);
81          for(int i=1;i<=R;i++){
82             work();
83          }
84          printf("%d\n",p[q].num);
85     }
86     return 0;
87 }

 

F

dp记忆话搜索

dp[i][j][k][inx][last]:当前三种水果分别有i j k个的时候且当前放第inx类的水果,持续了last天的方案数

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <list>
 5 #include <map>
 6 #include <stack>
 7 #include <vector>
 8 #include <cstring>
 9 #include <sstream>
10 #include <string>
11 #include <cmath>
12 #include <queue>
13 using namespace std;
14 #define clc(a,b) memset(a,b,sizeof(a))
15 #define inf 0x3f3f3f3f
16 const int N=10010;
17 const int MOD = 1e9+7;
18 #define LL long long
19 void fre() {
20     freopen("in.txt","r",stdin);
21 }
22 inline int r() {
23     int x=0,f=1;char ch=getchar();
24     while(ch>9||ch<0) {if(ch==-) f=-1;ch=getchar();}
25     while(ch>=0&&ch<=9) { x=x*10+ch-0;ch=getchar();}return x*f;
26 }
27  
28 int num;
29 int dp[51][51][51][3][51];
30 int a[3],b[3];
31  
32 int dfs(int n,int a0,int a1,int a2,int inx,int last){
33     LL ans=0;
34     if(dp[a0][a1][a2][inx][last]!=-1) return dp[a0][a1][a2][inx][last];
35     if(n==num) return dp[a0][a1][a2][inx][last]=1;
36     if(inx==-1){
37        if(b[0]>0&&a0>=1) ans+=dfs(n+1,a0-1,a1,a2,0,1);     
38        if(b[1]>0&&a1>=1) ans+=dfs(n+1,a0,a1-1,a2,1,1);
39        if(b[2]>0&&a2>=1) ans+=dfs(n+1,a0,a1,a2-1,2,1);   
40     }
41     else{
42        if(inx==0){
43           if(last+1<=b[0]&&a0>=1) ans+=dfs(n+1,a0-1,a1,a2,0,last+1);  
44           if(b[1]>0&&a1>=1) ans+=dfs(n+1,a0,a1-1,a2,1,1);
45           if(b[2]>0&&a2>=1) ans+=dfs(n+1,a0,a1,a2-1,2,1);
46         } 
47        else if(inx==1){
48           if(last+1<=b[1]&&a1>=1) ans+=dfs(n+1,a0,a1-1,a2,1,last+1);
49           if(b[0]>0&&a0>=1) ans+=dfs(n+1,a0-1,a1,a2,0,1);
50           if(b[2]>0&&a2>=1) ans+=dfs(n+1,a0,a1,a2-1,2,1);
51        }
52        else{
53           if(last+1<=b[2]&&a2>=1) ans+=dfs(n+1,a0,a1,a2-1,2,last+1);
54           if(b[0]>0&&a0>=1) ans+=dfs(n+1,a0-1,a1,a2,0,1);
55           if(b[1]>0&&a1>=1) ans+=dfs(n+1,a0,a1-1,a2,1,1);
56        }
57     }
58     ans%=MOD;        
59     return dp[a0][a1][a2][inx][last]=ans;    
60 }
61  
62 int main(){
63     int T;
64     T=r();
65     while(T--){
66        clc(dp,-1);
67        for(int i=0;i<3;i++){
68           // scanf("%d",&a[i]);
69           int x;
70           x=r();
71           a[i]=x;
72        }
73        for(int i=0;i<3;i++){
74            // scanf("%d",&b[i]);
75            int x;
76            x=r();
77            b[i]=x;
78        }
79        num=a[0]+a[1]+a[2];
80        printf("%d\n",dfs(0,a[0],a[1],a[2],-1,0));
81     }
82     return 0;
83 }

 

山东省2016acm省赛

标签:

原文地址:http://www.cnblogs.com/ITUPC/p/5573854.html

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