Codeforces Round #363 (Div. 2)

最近太忙了时间不够, 4个1a 记录一下代码 剩4题有空补做吧

 

http://codeforces.com/contest/699

 

A

 1 //#define txtout
 2 //#define debug
 3 #include<bits/stdc++.h>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=2e5+10;
11 int n;
12 char a[M];
13 int b[M];
14 int solve(){
15     int answer=inf;
16     for(int i=0;i<n;i++){
17         if(a[i]=='L') continue;
18         int last=i;
19         for(int j=i;j<n;j++){
20             if(a[j]=='L') break;
21             last=j;
22         }
23         if(last==n-1) break;
24         answer=min(answer,(b[last+1]-b[last])/2);
25         i=last;
26     }
27     if(answer==inf) answer=-1;
28     return answer;
29 }
30 int main(){
31     #ifdef txtout
32     freopen("in.txt","r",stdin);
33     freopen("out.txt","w",stdout);
34     #endif // txtout
35     while(~scanf("%d%s",&n,a)){
36         for(int i=0;i<n;i++){
37             scanf("%d",&b[i]);
38         }
39         printf("%d\n",solve());
40     }
41     return 0;
42 }
View Code

 

B

 1 //#define txtout
 2 //#define debug
 3 #include<bits/stdc++.h>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=1e3+10;
11 int n,m;
12 char a[M][M];
13 int sum;
14 int row[M];
15 int clum[M];
16 void init(){
17     mt(row,0);
18     mt(clum,0);
19     sum=0;
20     for(int i=0;i<n;i++){
21         for(int j=0;j<m;j++){
22             if(a[i][j]=='.') continue;
23             sum++;
24             row[i]++;
25             clum[j]++;
26         }
27     }
28 }
29 void solve(){
30     init();
31     for(int i=0;i<n;i++){
32         for(int j=0;j<m;j++){
33             int temp=sum-row[i]-clum[j];
34             if(a[i][j]=='*') temp++;
35             if(temp==0){
36                 puts("YES");
37                 printf("%d %d\n",i+1,j+1);
38                 return ;
39             }
40         }
41     }
42     puts("NO");
43 }
44 int main(){
45     #ifdef txtout
46     freopen("in.txt","r",stdin);
47     freopen("out.txt","w",stdout);
48     #endif // txtout
49     while(~scanf("%d%d",&n,&m)){
50         for(int i=0;i<n;i++){
51             scanf("%s",a[i]);
52         }
53         solve();
54     }
55     return 0;
56 }
View Code

 

C

 1 //#define txtout
 2 //#define debug
 3 #include<bits/stdc++.h>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef long long LL;
 7 const double pi=acos(-1.0);
 8 const double eps=1e-8;
 9 const int inf=0x3f3f3f3f;
10 const int M=1e2+10;
11 int n;
12 int a[M];
13 int dp[M][4];
14 int solve(){
15     for(int i=0;i<n;i++){
16         for(int j=0;j<3;j++){
17             dp[i][j]=inf;
18         }
19     }
20     dp[0][2]=1;
21     for(int i=0;i<2;i++){
22         if((a[0]>>i)&1){
23             dp[0][i]=0;
24         }
25     }
26     for(int i=0;i<n-1;i++){
27         for(int j=0;j<3;j++){
28             dp[i+1][2]=min(dp[i+1][2],dp[i][j]+1);
29             for(int k=0;k<2;k++){
30                 if((a[i+1]>>k)&1){
31                     dp[i+1][k]=min(dp[i+1][k],dp[i][2]);
32                     dp[i+1][k]=min(dp[i+1][k],dp[i][k^1]);
33                 }
34             }
35         }
36     }
37     int result=inf;
38     for(int j=0;j<3;j++){
39         result=min(result,dp[n-1][j]);
40     }
41     return result;
42 }
43 int main(){
44     #ifdef txtout
45     freopen("in.txt","r",stdin);
46     freopen("out.txt","w",stdout);
47     #endif // txtout
48     while(~scanf("%d",&n)){
49         for(int i=0;i<n;i++){
50             scanf("%d",&a[i]);
51         }
52         printf("%d\n",solve());
53     }
54     return 0;
55 }
View Code

 

 

D

  1 //#define txtout
  2 //#define debug
  3 #include<bits/stdc++.h>
  4 #define mt(a,b) memset(a,b,sizeof(a))
  5 using namespace std;
  6 typedef long long LL;
  7 const double pi=acos(-1.0);
  8 const double eps=1e-8;
  9 const int inf=0x3f3f3f3f;
 10 const int M=2e5+10;
 11 int n;
 12 int a[M];
 13 bool tabu[M];
 14 int du[M];
 15 queue<int> q;
 16 void init(){
 17     for(int i=1;i<=n;i++){
 18         tabu[i]=false;
 19         du[i]=0;
 20     }
 21     for(int i=1;i<=n;i++){
 22         du[a[i]]++;
 23     }
 24     while(!q.empty()) q.pop();
 25     for(int i=1;i<=n;i++){
 26         if(du[i]==0) q.push(i);
 27     }
 28     while(!q.empty()){
 29         int u=q.front();
 30         q.pop();
 31 
 32         #ifdef debug
 33         printf("front =  %d\n",u);
 34         #endif // debug
 35 
 36         tabu[u]=true;
 37         du[a[u]]--;
 38         if(du[a[u]]==0){
 39             q.push(a[u]);
 40         }
 41     }
 42 }
 43 int solve(){
 44     init();
 45     #ifdef debug
 46     for(int i=1;i<=n;i++){
 47         printf("%d ",tabu[i]);
 48     }
 49     puts("");
 50     #endif // debug
 51     int sum=0;
 52     int root=-1;
 53     for(int i=1;i<=n;i++){
 54         if(a[i]==i){
 55             root=i;
 56             tabu[i]=true;
 57             break;
 58         }
 59     }
 60     if(root==-1){
 61         for(int i=1;i<=n;i++){
 62             if(tabu[i]) continue;
 63             root=i;
 64             while(true){
 65                 tabu[a[i]]=true;
 66                 i=a[i];
 67                 if(i==root) break;
 68             }
 69             a[i]=i;
 70             sum++;
 71             break;
 72         }
 73     }
 74     for(int i=1;i<=n;i++){
 75         if(tabu[i]) continue;
 76         int s=i,t=i;
 77         while(true){
 78             tabu[a[t]]=true;
 79             t=a[t];
 80             if(t==s) break;
 81         }
 82         a[s]=root;
 83         sum++;
 84     }
 85     return sum;
 86 }
 87 int main(){
 88     #ifdef txtout
 89     freopen("in.txt","r",stdin);
 90     freopen("out.txt","w",stdout);
 91     #endif // txtout
 92     while(~scanf("%d",&n)){
 93         for(int i=1;i<=n;i++){
 94             scanf("%d",&a[i]);
 95         }
 96         printf("%d\n",solve());
 97         for(int i=1;i<=n;i++){
 98             printf("%d%c",a[i],i==n?'\n':' ');
 99         }
100     }
101     return 0;
102 }
View Code

 

 

 

end

posted on 2016-07-20 22:15  gaolzzxin  阅读(134)  评论(0编辑  收藏  举报