nenu contest

http://vjudge.net/vjudge/contest/view.action?cid=54393#overview

 

 A n^2能过 对第二个n我二分了一下,快了一点点,nlogn

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 double judge[128];
 5 int main(){
 6     for(int i=0;i<100;i++){
 7         judge[i]=i*10;
 8     }
 9     double now;
10     int n,s;
11     while(~scanf("%d%d",&n,&s)){
12         int sum=0;
13         for(int i=0;i<n;i++){
14             scanf("%lf",&now);
15             int L=lower_bound(judge,judge+100,now)-judge;
16             if(judge[L]>now) L--;
17             if(judge[L]<=now&&now<=judge[L]+5) sum++;
18         }
19         printf("%.1f\n",sum*(60+0.2*s));
20     }
21     return 0;
22 }
View Code

 A LEE的on的方法也很值得借鉴,竟然比我二分的慢,不知为何,数据小吧

 1 #include<cstdio>
 2 const double eps = 1e-8;
 3 int main() {
 4     int n, s, cnt;
 5     double t;
 6     while (~scanf("%d%d", &n, &s)) {
 7         cnt = 0;
 8         while (n--) {
 9             scanf("%lf", &t);
10             int T = int(t);
11             if (T % 10 < 5 || (T % 10 == 5 && (t-T) < eps)) {
12                 cnt++;
13             }
14         }
15         printf("%.1f\n", cnt*(60 + 0.2*s));
16     }
17     return 0;
18 }
View Code

 

B %X scanf printf 16进制输入输出

 1 #include<cstdio>
 2 #include<cstring>
 3 const int M=1010;
 4 char op[M];
 5 int main(){
 6     int t,sa,sb;
 7     while(~scanf("%d",&t)){
 8         while(t--){
 9             scanf("%X%X",&sa,&sb);
10             getchar();
11             gets(op);
12             int lp=strlen(op);
13             for(int i=0;i<lp;i++){
14                 if(op[i]=='8'){
15                     if(op[i+3]=='A'){
16                         sa+=sb;
17                     }
18                     else{
19                         sb+=sa;
20                     }
21                     i+=8;
22                 }
23                 else if(op[i]=='F'){
24                     if(op[i+3]=='A'){
25                         sa+=sa;
26                     }
27                     else{
28                         sb+=sb;
29                     }
30                     i+=5;
31                 }
32             }
33             printf("%X %X\n",sa,sb);
34         }
35     }
36     return 0;
37 }
View Code

 

C 比赛时n^2logn 900ms压线过,事实上nlogn就行了

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<map>
 5 #define mt(a,b) memset(a,b,sizeof(a))
 6 using namespace std;
 7 const int M=128;
 8 class UnionFindSet {  //并查集
 9     int par[M];
10 public:
11     void init() {
12         mt(par,-1);
13     }
14     int getroot(int x) {
15         int i=x,j=x,temp;
16         while(par[i]>=0) i=par[i];
17         while(j!=i) {
18             temp=par[j];
19             par[j]=i;
20             j=temp;
21         }
22         return i;
23     }
24     bool unite(int x,int y) {
25         int p=getroot(x);
26         int q=getroot(y);
27         if(p==q)return false;
28         if(par[p]>par[q]) {
29             par[q]+=par[p];
30             par[p]=q;
31         } else {
32             par[p]+=par[q];
33             par[q]=p;
34         }
35         return true;
36     }
37 }gx;
38 map<string,int> mp;
39 string str;
40 int main(){
41     int n,m;
42     char op[32];
43     while(~scanf("%d",&n)){
44         bool flag=false;
45         gx.init();
46         mp.clear();
47         for(int i=1;i<=n;i++){
48             scanf("%d",&m);
49             while(m--){
50                 flag=true;
51                 scanf("%s",op);
52                 str=(string)op;
53                 if(!mp[str]){
54                     mp[str]=i;
55                 }
56                 else{
57                     gx.unite(mp[str],i);
58                 }
59             }
60         }
61         if(!flag){
62             printf("%d\n",n);
63             continue;
64         }
65         int ans=0;
66         for(int i=1;i<=n;i++){
67             if(i==gx.getroot(i)) ans++;
68         }
69         printf("%d\n",ans-1);
70     }
71     return 0;
72 }
View Code

 

 

I n^2能过400ms on单调队列62ms

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<algorithm>
 4 using namespace std;
 5 const int M=1010;
 6 int a[M],b[M];
 7 int main(){
 8     int n,m;
 9     while(~scanf("%d%d",&n,&m)){
10         for(int i=0;i<n;i++){
11             scanf("%d",&a[i]);
12         }
13         for(int i=0;i<m;i++){
14             scanf("%d",&b[i]);
15         }
16         int la=0,lb=0,ans=1e9;
17         while(la!=n&&lb!=m){
18             ans=min(ans,abs(a[la]-b[lb]));
19             if(a[la]>b[lb]) lb++;
20             else la++;
21         }
22         printf("%d\n",ans);
23     }
24     return 0;
25 }
View Code

 

 

J  on 水题

 1 #include<cstdio>
 2 typedef __int64 LL;
 3 const int M=1000010;
 4 char a[M][4];
 5 int main(){
 6     int n;
 7     while(~scanf("%d",&n)){
 8         for(int i=0;i<n;i++){
 9             scanf("%s",a[i]);
10         }
11         LL ans=0;
12         LL sum=0;
13         for(int i=n-1;i>=0;i--){
14             if(a[i][0]=='W') ans+=sum;
15             else sum++;
16         }
17         printf("%I64d\n",ans);
18     }
19     return 0;
20 }
View Code

 

posted on 2014-08-20 21:58  gaolzzxin  阅读(190)  评论(0编辑  收藏  举报