Codeforces Round #261 (Div. 2)

A. Pashmak and Garden http://codeforces.com/contest/459/problem/A

水题,没过,一字之差,细节决定成败

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 void out(int a,int b,int c,int d){
 5     printf("%d %d %d %d\n",a,b,c,d);
 6 }
 7 int main(){
 8     int x1,y1,x2,y2;
 9     while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)){
10         int sx=min(x1,x2);
11         int sy=min(y1,y2);
12         int bx=max(x1,x2);
13         int by=max(y1,y2);
14         int dx=bx-sx;
15         int dy=by-sy;
16         if(dx&&dy&&dx!=dy){
17             puts("-1");
18             continue;
19         }
20         if(dx&&dy){
21             if((x1<x2&&y1<y2)||(x2<x1&&y2<y1)){
22                 out(sx,by,bx,sy);
23                 continue;
24             }
25             out(sx,sy,bx,by);
26             continue;
27         }
28         if(dx){
29             out(sx,sy+dx,bx,by+dx);
30             continue;
31         }
32         out(sx+dy,sy,bx+dy,by);
33     }
34     return 0;
35 }
View Code

 

 B. Pashmak and Flowers http://codeforces.com/contest/459/problem/B

分类讨论

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 typedef __int64 LL;
 5 const int M=200010;
 6 int a[M];
 7 int main(){
 8     int n;
 9     while(~scanf("%d",&n)){
10         for(int i=0;i<n;i++){
11             scanf("%d",&a[i]);
12         }
13         int big=a[0],sma=a[0];
14         for(int i=1;i<n;i++){
15             big=max(big,a[i]);
16             sma=min(sma,a[i]);
17         }
18         printf("%d ",big-sma);
19         if(big==sma){
20             printf("%I64d\n",(LL)n*(n-1)/2);
21             continue;
22         }
23         LL sumbig=0,sumsma=0;
24         for(int i=0;i<n;i++){
25             if(a[i]==big) sumbig++;
26             else if(a[i]==sma) sumsma++;
27         }
28         printf("%I64d\n",sumbig*sumsma);
29     }
30     return 0;
31 }
View Code

 

C. Pashmak and Buses

分类讨论,可构造就有多种解了,我是按照高精度加法从1111构造到kkkk,

 1 #include<cstdio>
 2 const int M=1024;
 3 int a[M][M];
 4 int main(){
 5     int n,k,d;
 6     while(~scanf("%d%d%d",&n,&k,&d)){
 7         int tmp=1;
 8         bool flag=false;
 9         for(int i=0;i<d;i++){
10             tmp*=k;
11             if(tmp>=n){
12                 flag=true;
13                 break;
14             }
15         }
16         if(!flag){
17             puts("-1");
18             continue;
19         }
20         for(int j=1;j<=d;j++){
21             a[1][j]=1;
22         }
23         for(int i=2;i<=n;i++){
24             for(int j=1;j<=d;j++){
25                 a[i][j]=a[i-1][j];
26             }
27             a[i][d]++;
28             for(int j=d;j>=1;j--){
29                 if(a[i][j]>k){
30                     a[i][j]=1;
31                     a[i][j-1]++;
32                 }
33             }
34         }
35         for(int j=1;j<=d;j++){
36             for(int i=1;i<=n;i++){
37                 printf("%d ",a[i][j]);
38             }
39             puts("");
40         }
41     }
42     return 0;
43 }
View Code

 

 D. Pashmak and Parmida's problem http://codeforces.com/contest/459/problem/D

朴素n^2,树状数组优化nlogn

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<map>
 5 #define mt(a,b) memset(a,b,sizeof(a))
 6 using namespace std;
 7 typedef __int64 LL;
 8 const int M=1000010;
 9 class One_Tree_Array { //一维树状数组
10     typedef int typev;
11     typev a[M];
12 public:
13     void init() {
14         mt(a,0);
15     }
16     int lowb(int t) {
17         return t&(-t);
18     }
19     void add(int i,typev v) {
20         for(; i<M; a[i]+=v,i+=lowb(i));
21     }
22     typev sum(int i) {
23         typev s=0;
24         for(; i>0; s+=a[i],i-=lowb(i));
25         return s;
26     }
27 }gx;
28 int a[M],b[M],l[M],r[M];
29 map<int,int> mp;
30 int main(){
31     int n;
32     while(~scanf("%d",&n)){
33         for(int i=0;i<n;i++){
34             scanf("%d",&a[i]);
35             b[i]=a[i];
36         }
37         sort(b,b+n);
38         int lb=unique(b,b+n)-b;
39         mp.clear();
40         for(int i=0;i<lb;i++){
41             mp[b[i]]=i;
42         }
43         for(int i=0;i<n;i++){
44             b[i]=mp[a[i]];
45         }
46         gx.init();
47         mt(r,0);
48         for(int i=n-1;i>=0;i--){
49             r[b[i]]++;
50             gx.add(r[b[i]],1);
51         }
52         mt(l,0);
53         LL ans=0;
54         for(int i=0;i<n;i++){
55             l[b[i]]++;
56             gx.add(r[b[i]],-1);
57             r[b[i]]--;
58             ans+=gx.sum(l[b[i]]-1);
59         }
60         printf("%I64d\n",ans);
61     }
62     return 0;
63 }
View Code

 当然树状数组也可以掏出线段树替代之

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<map>
 5 #define mt(a,b) memset(a,b,sizeof(a))
 6 #define lrrt int L,int R,int rt
 7 #define iall 1,M,1
 8 #define imid int mid=(L+R)>>1
 9 #define lson L,mid,rt<<1
10 #define rson mid+1,R,rt<<1|1
11 using namespace std;
12 typedef __int64 LL;
13 const int M=1000010;
14 int tree[M<<2];
15 void pushup(int rt){
16     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
17 }
18 void update(int x,int val,lrrt){
19     if(L==R){
20         tree[rt]+=val;
21         return ;
22     }
23     imid;
24     if(mid>=x) update(x,val,lson);
25     else       update(x,val,rson);
26     pushup(rt);
27 }
28 int query(int x,int y,lrrt){
29     if(x<=L&&R<=y) return tree[rt];
30     imid;
31     int ans=0;
32     if(mid>=x) ans+=query(x,y,lson);
33     if(mid<y)  ans+=query(x,y,rson);
34     return ans;
35 }
36 int a[M],b[M],l[M],r[M];
37 map<int,int> mp;
38 int main(){
39     int n;
40     while(~scanf("%d",&n)){
41         for(int i=0;i<n;i++){
42             scanf("%d",&a[i]);
43             b[i]=a[i];
44         }
45         sort(b,b+n);
46         int lb=unique(b,b+n)-b;
47         mp.clear();
48         for(int i=0;i<lb;i++){
49             mp[b[i]]=i;
50         }
51         for(int i=0;i<n;i++){
52             b[i]=mp[a[i]];
53         }
54         mt(tree,0);
55         mt(r,0);
56         for(int i=n-1;i>=0;i--){
57             r[b[i]]++;
58             update(r[b[i]],1,iall);
59         }
60         mt(l,0);
61         LL ans=0;
62         for(int i=0;i<n;i++){
63             l[b[i]]++;
64             update(r[b[i]],-1,iall);
65             r[b[i]]--;
66             if(l[b[i]]-1>=1)
67             ans+=query(1,l[b[i]]-1,iall);
68         }
69         printf("%I64d\n",ans);
70     }
71     return 0;
72 }
View Code

 E. Pashmak and Graph http://codeforces.com/contest/459/problem/E

找到单调性,然后dp

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 const int M=300010;
 7 struct E{
 8     int u,v,w;
 9     friend bool operator <(E a,E b){
10         return a.w<b.w;
11     }
12 }e[M];
13 int dpnow[M],dppre[M];
14 int main(){
15     int n,m;
16     while(~scanf("%d%d",&n,&m)){
17         for(int i=0;i<m;i++){
18             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
19         }
20         sort(e,e+m);
21         mt(dppre,0);
22         for(int i=0,j;i<m;i++){
23             for(j=i+1;j<m&&e[i].w==e[j].w;j++);
24             for(int k=i;k<j;k++){
25                 dpnow[e[k].u]=dppre[e[k].u];
26                 dpnow[e[k].v]=dppre[e[k].v];
27             }
28             for(int k=i;k<j;k++){
29                 dpnow[e[k].v]=max(dpnow[e[k].v],dppre[e[k].u]+1);
30             }
31             for(int k=i;k<j;k++){
32                 dppre[e[k].u]=dpnow[e[k].u];
33                 dppre[e[k].v]=dpnow[e[k].v];
34             }
35             i=j-1;
36         }
37         int ans=0;
38         for(int i=1;i<=n;i++){
39             ans=max(ans,dppre[i]);
40         }
41         printf("%d\n",ans);
42     }
43     return 0;
44 }
View Code

 

 

end

 

posted on 2014-08-17 10:13  gaolzzxin  阅读(155)  评论(0编辑  收藏  举报