2017.2其他简要题解

Posted phile的空间

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017.2其他简要题解相关的知识,希望对你有一定的参考价值。

codeforces 739A

答案一定是所有区间中最小区间长度,构造的话就把所有区间都当成那个长度即可

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 int b[100010];
 5 struct node{int x,y;} a[100010];
 6 int n,m;
 7 
 8 bool cmp(node a,node b)
 9 {
10     return a.x<b.x;
11 }
12 
13 int main()
14 {
15     scanf("%d%d",&n,&m);
16     int ans=n;
17     for (int i=1; i<=m; i++)
18     {
19         scanf("%d%d",&a[i].x,&a[i].y);
20         ans=min(ans,a[i].y-a[i].x+1);
21     }
22     sort(a+1,a+1+m,cmp);
23     for (int i=1; i<=m; i++)
24         a[i].y=a[i].x+ans-1;
25     printf("%d\\n",ans);
26     for (int i=1; i<=m; i++)
27     {
28         if (a[i].x==a[i-1].x) continue;
29         if (a[i].x>a[i-1].y)
30         {
31             for (int j=0; j<ans; j++)
32                 b[a[i].x+j]=j;
33         }
34         else {
35             int k=a[i-1].y+1;
36             for (int j=a[i-1].x; j<=a[i].x; j++)
37                 b[k++]=b[j];
38         }
39     }
40     for (int i=1; i<=n; i++) printf("%d ",b[i]);
41 }
View Code

 

codeforces 739B

做法很多,可以树上差分统计然后二分点到根这段,也可以直接用可持久化线段树维护

 1 #include<bits/stdc++.h>
 2 #define mp make_pair
 3 using namespace std;
 4 typedef long long ll;
 5 struct way{int po,next,len;} e[200010];
 6 int ans[200010],p[200010],a[200010];
 7 ll d[200010];
 8 vector< pair<ll,int> > st;
 9 int n,len;
10 
11 void add(int x,int y,int z)
12 {
13     e[++len].po=y;
14     e[len].next=p[x];
15     e[len].len=z;
16     p[x]=len;
17 }
18 
19 void dfs(int x)
20 {
21     st.push_back(mp(d[x],x));
22     int j=lower_bound(st.begin(),st.end(),mp(d[x]-a[x],0))-st.begin()-1;
23     if (j>=0) ans[st[j].second]--;
24     for (int i=p[x]; i; i=e[i].next)
25     {
26         int y=e[i].po;
27         d[y]=d[x]+e[i].len;
28         dfs(y);
29         ans[x]+=ans[y]+1;
30     }
31     st.pop_back();
32 }
33 
34 int main()
35 {
36     scanf("%d",&n);
37     for (int i=1; i<=n; i++) scanf("%d",&a[i]);
38     for (int i=2; i<=n; i++)
39     {
40         int x,y;
41         scanf("%d%d",&x,&y);
42         add(x,i,y);
43     }
44     dfs(1);
45     for (int i=1; i<=n; i++) printf("%d ",ans[i]);
46 }
View Code

 

hdu5071

vector大模拟

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 #define mp make_pair
  5 
  6 typedef long long ll;
  7 typedef pair<int,ll> pi;
  8 vector<pi> p;
  9 int x,now;
 10 
 11 int get(int x)
 12 {
 13     for (int i=0; i<p.size(); i++)
 14         if (p[i].first==x) return i;
 15     return -1;
 16 }
 17 
 18 void erase(int x)
 19 {
 20     int i=0;
 21     for( vector<pi>::iterator it=p.begin();it!=p.end(); it++,i++){
 22         if (i==x)
 23         {
 24             p.erase(it);
 25             return;
 26         }
 27     }
 28 }
 29 
 30 void add()
 31 {
 32     scanf("%d",&x);
 33     if (get(x)!=-1) puts("same priority.");
 34     else {
 35         p.push_back(mp(x,0));
 36         puts("success.");
 37     }
 38 }
 39 
 40 void close()
 41 {
 42     scanf("%d",&x);
 43     int k=get(x);
 44     if (k==-1) puts("invalid priority.");
 45     else{
 46         if(x==now) now=0;
 47         printf("close %d with %I64d.\\n",p[k].first,p[k].second);
 48         erase(k);
 49     }
 50 }
 51 void chat()
 52 {
 53     scanf("%d",&x);
 54     if (!p.size()) puts("empty.");
 55     else {
 56         if (now) p[get(now)].second+=x;
 57         else p[0].second+=x;
 58         puts("success.");
 59     }
 60 }
 61 
 62 void rot(int y)
 63 {
 64     if (y<1||y>p.size()) puts("out of range.");
 65     else{
 66         pi pp=p[y-1];
 67         erase(y-1);
 68         p.insert(p.begin(),pp);
 69         puts("success.");
 70     }
 71 }
 72 
 73 void prior()
 74 {
 75     int mx=0,k=-1;
 76     if (!p.size()) puts("empty.");
 77     else {
 78         for(int i=0;i<p.size();++i)
 79             if(p[i].first>mx) {mx=p[i].first;k=i;}
 80         rot(k+1);
 81     }
 82 }
 83 void choose()
 84 {
 85     scanf("%d",&x);
 86     int ch=get(x);
 87     if (ch==-1) puts("invalid priority.");
 88     else rot(ch+1);
 89 }
 90 
 91 void top()
 92 {
 93     scanf("%d",&x);
 94     if(get(x)==-1) puts("invalid priority.");
 95     else now=x,puts("success.");
 96 }
 97 void untop()
 98 {
 99     if (now) {now=0;puts("success.");}
100     else puts("no such person.");
101 }
102 
103 void bye()
104 {
105     if (now)
106     {
107         x=get(now);
108         if(p[x].second!=0) printf("Bye %d: %I64d\\n",p[x].first,p[x].second);
109         erase(x);
110     }
111     for (int i=0; i<p.size(); i++)
112         if (p[i].second) printf("Bye %d: %I64d\\n",p[i].first,p[i].second);
113 }
114 int main()
115 {
116     string op;
117     int cas,y,n; scanf("%d",&cas);
118     while(cas--)
119     {
120         now=0;
121         scanf("%d",&n);
122         p.clear();
123         for(int k=1; k<=n; k++)
124         {
125             cin>>op;
126             printf("Operation #%d: ",k);
127             if(op=="Add") add();
128             else if(op=="Close") close();
129             else if(op=="Chat") chat();
130             else if(op=="Rotate") {scanf("%d",&y);rot(y);}
131             else if(op=="Prior") prior();
132             else if(op=="Choose") choose();
133             else if(op=="Top") top();
134             else if(op=="Untop") untop();
135         }
136         bye();
137     }
138     return 0;
139 }
View Code

 

hdu5073

方差贪心,最后答案一定是连续的n-k个组成的惯量,注意精度保护

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long ll;
 5 int n,k,a[100010];
 6 int main()
 7 {
 8     int cas;
 9     scanf("%d",&cas);
10     while (cas--)
11     {
12         scanf("%d%d",&n,&k);
13         for (int i=1; i<=n; i++)
14             scanf("%d",&a[i]);
15         sort(a+1,a+1+n);
16         if (k>=n-1)
17         {
18             puts("0");
19             continue;
20         }
21         ll s1=0,s2=0;
22         for (int i=1; i<=n-k; i++)
23         {
24             s2+=1ll*a[i]*a[i];
25             s1+=a[i];
26         }
27         ll ans=1ll*(n-k)*s2-s1*s1;
28         for (int i=n-k+1; i<=n; i++)
29         {
30             s2+=1ll*a[i]*a[i]-1ll*a[i-n+k]*a[i-n+k];
31             s1+=a[i]-a[i-n+k];
32             ans=min(ans,1ll*(n-k)*s2-s1*s1);
33         }
34         printf("%.10lf\\n",1.0*ans/(n-k));
35     }
36 }
View Code

 

hdu5074

简单dp

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 const int inf=-1000000007;
 5 int f[2][60],a[60][60],n,m;
 6 int main()
 7 {
 8     int cas;
 9     scanf("%d",&cas);
10     while (cas--)
11     {
12         scanf("%d%d",&n,&m);
13         for (int i=1; i<=m; i++)
14             for (int j=1; j<=m; j++)
15                 scanf("%d",&a[i][j]);
16         for (int i=1; i<=m; i++) f[0][i]=inf;
17         int x;
18         scanf("%d",&x);
19         if (x==-1) memset(f[0],0,sizeof(f[0])); else f[0][x]=0;
20         int p=0;
21         for (int i=2; i<=n; i++)
22         {
23             p^=1;
24             for (int j=1; j<=m; j++) f[p][j]=inf;
25             scanf("%d",&x);
26             if (x>-1)
27             {
28                 for (int j=1; j<=m; j++)
29                     f[p][x]=max(f[p][x],f[p^1][j]+a[j][x]);
30             }
31             else {
32                 for (int j=1; j<=m; j++)
33                     for (int k=1; k<=m; k++)
34                         f[p][k]=max(f[p][k],f[p^1][j]+a[j][k]);
35             }
36         }
37         int ans=0;
38         for (int i=1; i<=m; i++) ans=max(ans,f[p][i]);
39         printf("%d\\n",ans);
40     }
41 }
View Code

 

hdu5078

水题简单模拟

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 double sqr(double x)
 6 {
 7     return x*x;
 8 }
 9 
10 double dis(int x,int y,int x0,int y0)
11 {
12     return sqrt(sqr(x-x0)+sqr(y-y0));
13 }
14 
15 int main()
16 {
17     int cas,n;
18     scanf("%d",&cas);
19     while (cas--)
20     {
21         scanf("%d",&n);
22         double ans=0;
23         int x0,y0,x,y,t,t0;
24         scanf("%d%d%d",&t0,&x0,&y0);
25         for (int i=2; i<=n; i++)
26         {
27             scanf("%d%d%d",&t,&x,&y);
28             ans=max(ans,dis(x,y,x0,y0)/(t-t0));
29             x0=x,y0=y,t0=t;
30         }
31         printf("%.10lf\\n",ans);
32     }
33 }
View Code

 

hdu5077

暴力dfs+剪枝,然后打表