寒假第一周 1.11 --- 1.17

比暑假还长的寒假---

 

1.11

cf 608d

http://codeforces.com/contest/608/problem/D

不会做,看的题解

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 505;
 8 int a[maxn],dp[maxn][maxn];
 9 int n;
10 const int INF = (1<<30)-1;
11 
12 int dfs(int l,int r){
13     if(l >= r) return 1;
14     if(dp[l][r]) return dp[l][r];
15     int res = INF;
16     if(a[l] == a[r]) res = min(res,dfs(l+1,r-1));
17     for(int i = l;i < r;i++){
18         res = min(res,dfs(l,i) + dfs(i+1,r));
19     }
20     return dp[l][r] = res;
21 }
22 
23 int main(){
24     while(scanf("%d",&n) != EOF){
25         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
26         memset(dp,0,sizeof(dp));
27         printf("%d\n",dfs(1,n));
28     }
29     return 0;
30 }
View Code

 

cf 296c

http://codeforces.com/problemset/problem/296/C

先以为得用 线段树的区间更新,,后来发现可以不用--

把区间更新学一下下叭---再来写一下

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 #define lp (p<<1)
 7 #define rp (p << 1|1)
 8 #define getmid(l,r) (l+(r-l)/2)
 9 
10 typedef long long LL;
11 const int maxn = 5e5+5;
12 
13 struct node{
14     int l,r;
15     int d;
16 }b[maxn];
17 
18 node c[maxn];
19 int cnt[maxn];
20 int p[maxn];
21 LL a[maxn];
22 LL add[maxn];
23 LL q[maxn];
24 int n,m,k;
25 
26 void solve(){
27     memset(cnt,0,sizeof(cnt));
28     for(int i = 1;i <= k;i++){
29         cnt[c[i].l]++;
30         cnt[c[i].r+1]--;
31     }
32 
33     /*for(int i = 1;i <= m;i++){
34         printf("cnt[%d] = %d\n",i,cnt[i]);
35     }*/
36 
37     for(int i = 1;i <= m;i++) cnt[i] = cnt[i-1] + cnt[i];
38 
39     /*for(int i = 1;i <= m;i++){
40         printf("cnt[%d] = %d\n",i,cnt[i]);
41     }*/
42     memset(add,0,sizeof(add));
43     for(int i = 1;i <= m;i++){
44         if(cnt[i] == 0) continue;
45         add[b[i].l] += 1LL*cnt[i]*b[i].d;
46         add[b[i].r+1] -= 1LL*cnt[i]*b[i].d;
47     }
48 
49     for(int i = 1;i <= n;i++) add[i] = add[i-1] + add[i];
50     for(int i = 1;i <= n;i++){
51         a[i] += add[i];
52     }
53     for(int i = 1;i <= n;i++) printf("%I64d ",a[i]);
54     printf("\n");
55 }
56 
57 int main(){
58     while(scanf("%d %d %d",&n,&m,&k) != EOF){
59         for(int i = 1;i <= n;i++) scanf("%I64d",&a[i]);
60         for(int i = 1;i <= m;i++){
61             scanf("%d %d %d",&b[i].l,&b[i].r,&b[i].d);
62         }
63         for(int i = 1;i <= k;i++){
64             scanf("%d %d",&c[i].l,&c[i].r);
65         }
66         solve();
67     }
68     return 0;
69 }
View Code

 

1.12

cf 616d

http://codeforces.com/contest/616/problem/D

发现真的不会写尺取阿---

wa了一下午 ---sad---

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<set>
 7 using namespace std;
 8 
 9 typedef pair<int,int> pii;
10 const int maxn = 1e6+5;
11 int a[maxn];
12 int n,k;
13 
14 void solve(){
15     pii res;
16     multiset<int> ss;
17     set<int> s;
18     int len = 0;
19     int j = 1,i = 1;
20     while(j <= n){
21         while(j <= n && s.size() <= k){
22                 if(s.size() == k){
23                     if(!s.count(a[j])) break;
24                 }
25                 s.insert(a[j]);
26         //        printf("i = %d  j = %d  s.size() = %d\n",i,j,s.size());
27                 ss.insert(a[j]);
28                 if(s.size() > k) break;
29                 j++;
30 
31         }
32         if(j-i > len && s.size() <= k){
33             len = j-i;
34             res = make_pair(i,j-1);
35         }
36        if(ss.count(a[i]) == 1){
37             //printf("------\n");
38         s.erase(a[i]);
39        }
40        int x = a[i];
41        multiset<int>::iterator pos = ss.find(x);
42        ss.erase(pos);
43        i++;
44       // printf("=== i = %d  j = %d\n",i,j);
45     }
46     printf("%d %d\n",res.first,res.second);
47 }
48 
49 int main(){
50     while(scanf("%d %d",&n,&k) != EOF){
51         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
52         solve();
53     }
54     return 0;
55 }
View Code

 

1.13

cf 615c

http://codeforces.com/problemset/problem/615/C

还是不懂dp的做法,,最长公共前缀是什么阿--sad--

每次贪心取能够匹配到的最长的子串,一直到匹配完b串

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 
 8 typedef pair<int,int> pii;
 9 const int maxn = 3005;
10 char a[maxn],b[maxn],ra[maxn];
11 int lena,lenb;
12 int stl,edl;
13 int str,edr;
14 int pos;
15 
16 void worklb(char *s){
17     int res = 0;
18     stl = 0,edl = 0;
19     int len = 0;
20     for(int i = 1;i <= lena;i++){
21         for(int j = i;j <= lena;j++){
22             if(s[j] != b[pos+j-i]){
23                 break;
24             }
25             if(j-i+1 > len){
26                 stl = i;
27                 edl = j;
28                 len = j-i+1;
29             }
30             //printf("- i = %d  j = %d\n",i,j);
31         }
32     }
33 }
34 
35 void workub(char *s){
36     int res = 0;
37     str = 0,edr = 0;
38     int len = 0;
39     for(int i = 1;i <= lena;i++){
40         for(int j = i;j <= lena;j++){
41             if(s[j] != b[pos+j-i]){
42                 break;
43             }
44             if(j-i+1 > len){
45                 len = j-i+1;
46                 str = i;
47                 edr = j;
48             }
49            // printf("-- i = %d  j = %d\n",i,j);
50         }
51     }
52 }
53 
54 void solve(){
55     vector<pii> ans;
56     for(int i = 1;i <= lena;i++) ra[i] = a[lena-i+1];
57     pos = 1;
58     int lb,ub;
59     int cnt = 0;
60     while(pos <= lenb){
61         worklb(a);
62         workub(ra);
63         //printf("stl = %d  edl = %d\n",stl,edl);
64         //printf("str = %d  edr = %d\n",str,edr);
65         if(edl == 0 && stl == 0 && str == 0 && edr == 0){
66             puts("-1");
67             return;
68         }
69         if(edl-stl >= edr-str){
70             ans.push_back(make_pair(stl,edl));
71             pos += edl-stl+1;
72           //  printf("-pos = %d\n",pos);
73         }
74         else{
75             ans.push_back(make_pair(lena-str+1,lena-edr+1));
76             pos += edr-str+1;
77            // printf("--pos = %d\n",pos);
78         }
79        // printf("pos = %d\n",pos);
80     }
81     printf("%d\n",ans.size());
82     for(int i = 0;i < ans.size();i++){
83         printf("%d %d\n",ans[i].first,ans[i].second);
84     }
85 }
86 
87 int main(){
88     scanf("%s",a+1);
89     scanf("%s",b+1);
90     lena = strlen(a+1);
91     lenb = strlen(b+1);
92     solve();
93 }
View Code

 

cf 611e

http://codeforces.com/contest/611/problem/E

先是将每场战争从大到小排序,然后分别从两边贪

wa 8 ---------

然后,,,能力值最弱的优先,耗费的人数最少的优先

然后,在打败最强的之后,剩下的枪手的分配,应该是找到能力值和它最相近的t[i]

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<set>
  6 using namespace std;
  7 
  8 const int maxn = 2e5+5;
  9 int t[maxn];
 10 int n,a,b,c;
 11 
 12 void solve(){
 13     int d[3];
 14     d[0] = a;d[1] = b;d[2] = c;
 15     sort(d,d+3);
 16     a = d[0];b = d[1];c = d[2];
 17     sort(t+1,t+n+1);
 18 
 19   /*  for(int i = 1;i <= n;i++) printf("%d ",t[i]);
 20     printf("\n");*/
 21     multiset<int> s;
 22     for(int i = 1;i <= n;i++){
 23         s.insert(t[i]);
 24     }
 25     if((a+b+c) < t[n]){
 26         puts("-1");
 27         return;
 28     }
 29     int cc = 0;
 30     multiset<int>::iterator it;
 31     multiset<int>::iterator itt;
 32     while(s.size() != 0){
 33             it = s.end();it--;
 34             int x = *it;
 35             int y;
 36             s.erase(it);
 37             cc++;
 38             if(s.size() == 0) break;
 39             if(a >= x){
 40                if(s.size() != 0) {
 41                     itt = s.begin();
 42                     s.erase(itt);
 43                }
 44                 if(s.size() != 0) {
 45                     itt = s.begin();
 46                     s.erase(itt);
 47                 }
 48                 if(s.size() == 0) break;
 49             }
 50             else if(b >= x){
 51                 itt = s.upper_bound(a);
 52                 if(itt == s.begin()){
 53                     if(a >= *itt)
 54                     s.erase(itt);
 55                 }
 56                 else{
 57                     itt--;
 58                     s.erase(itt);
 59                 }
 60                 if(s.size() == 0) break;
 61                 itt = s.upper_bound(c);
 62                 if(itt == s.begin()){
 63                     if(c >= *itt) s.erase(itt);
 64                 }
 65                 else{
 66                     itt--;
 67                     s.erase(itt);
 68                 }
 69                 if(s.size() == 0) break;
 70             }
 71             else if(c >= x){
 72                 int lb = 0,ub = 0;
 73                 itt = s.upper_bound(a);
 74                 if(itt == s.begin()){
 75                     if(a >= *itt){
 76                         s.erase(itt);
 77                         lb = 1;
 78                     }
 79                 }
 80                 else{
 81                     itt--;
 82                     lb = 1;
 83                     s.erase(itt);
 84                 }
 85                 if(s.size() == 0) break;
 86                 itt = s.upper_bound(b);
 87                 if(itt == s.begin()){
 88                     if(b >= *itt){
 89                         s.erase(itt);
 90                         ub = 1;
 91                     }
 92                 }
 93                 else{
 94                     itt--;
 95                     ub = 1;
 96                     s.erase(itt);
 97                 }
 98                 if(s.size() == 0) break;
 99                 if(lb == 0 && ub == 0){
100                     itt = s.upper_bound(a+b);
101                     if(itt == s.begin()){
102                         if((a+b) >= *itt){
103                             s.erase(itt);
104                         }
105                     }
106                     else{
107                         itt--;
108                         s.erase(itt);
109                     }
110                     if(s.size() == 0) break;
111                 }
112 
113                /* printf("---debug--\n");
114                 for(multiset<int>::iterator p = s.begin();p != s.end();p++){
115                 printf("%d ",*p);
116                 }
117                 printf("---end---\n");*/
118 
119             }
120             else if((a+b) >= x){
121                 itt = s.upper_bound(c);
122                 if(itt == s.begin()){
123                         if(c >= *itt){
124                             s.erase(itt);
125                         }
126                 }
127                 else{
128                     itt--;
129                     s.erase(itt);
130                 }
131                 if(s.size() == 0) break;
132 
133             }
134             else if((a+c) >= x){
135                 itt = s.upper_bound(b);
136                 if(itt == s.begin()){
137                         if(b >= *itt){
138                             s.erase(itt);
139                         }
140                 }
141                 else{
142                     itt--;
143                     s.erase(itt);
144                 }
145                 if(s.size() == 0) break;
146             }
147             else if((b+c) >= x){
148                 itt = s.upper_bound(a);
149                 if(itt == s.begin()){
150                         if(a >= *itt){
151                             s.erase(itt);
152                         }
153                 }
154                 else{
155                     itt--;
156                     s.erase(itt);
157                 }
158                 if(s.size() == 0) break;
159             }
160             else{
161                 int y = 1;
162             }
163           /*  printf("s.size() = %d  cc = %d\n",s.size(),cc);
164             for(multiset<int>::iterator p = s.begin();p != s.end();p++){
165                 printf("%d ",*p);
166             }
167             printf("----\n");*/
168     }
169     printf("%d\n",cc);
170 }
171 
172 int main(){
173   //  freopen("in.txt","r",stdin);
174     //freopen("out.txt","w",stdout);
175     while(scanf("%d",&n) != EOF){
176         scanf("%d %d %d",&a,&b,&c);
177         for(int i = 1;i <= n;i++) scanf("%d",&t[i]);
178         solve();
179     }
180     return 0;
181 }
View Code

 

hdu 1024

将 n 个数 分成 连续的m 段,问这m段最大的和是多少

不会做---

dp[i][j] 表示前 i 个数 分成 j 段 能够得到最大的和(选取第 j 个数)

然后 dp[i][j] = max(dp[i-1][j-1] + a[i],dp[k][j-1] + a[i]) (1 <= k <= i)

看的题解做的--

但是wa 了好久--- 

是 因为直接输出 的dp[n] (按照这个dp的定义,必须选取第n个数),应该再和没有选取第n个数的取一下最大值

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 #define INF 0x7fffffff
 8 typedef long long LL;
 9 //const long long INF = (1<<62)-1;
10 const int maxn = 1e6+5;
11 int n,m;
12 int a[maxn],b[maxn],dp[maxn];
13 
14 void solve(){
15     int tmp;
16     memset(dp,0,sizeof(dp));
17     memset(b,0,sizeof(b));
18     for(int i = 1;i <= m;i++){
19         tmp = -INF;
20         for(int j = i;j <= n;j++){
21             dp[j] = max(dp[j-1]+a[j],b[j-1]+a[j]);
22             b[j-1] = tmp;
23             tmp = max(tmp,dp[j]);
24           //  printf("dp[%d] = %d\n",j,dp[j]);
25         }
26     }
27     dp[n] = max(dp[n],tmp);
28     printf("%d\n",dp[n]);
29 }
30 
31 int main(){
32     while(scanf("%d %d",&m,&n) != EOF){
33         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
34         solve();
35     }
36     return 0;
37 }
View Code

 

hdu 1029

用map统计个数 --(话说为什么会出现在 dp 的分类里面-----)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int maxn = 1e6+5;
10 int a[maxn];
11 int n;
12 
13 void solve(){
14     map<int,int> h;
15     int c = (n+1)/2;
16     for(int i = 1;i <= n;i++){
17         h[a[i]]++;
18         if(h[a[i]] >= c){
19             printf("%d\n",a[i]);
20             return;
21         }
22     }
23 }
24 
25 int main(){
26     while(scanf("%d",&n) != EOF){
27         for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
28         solve();
29     }
30     return 0;
31 }
View Code

 

1.14

cf 294b

http://codeforces.com/problemset/problem/294/B

暴力枚举两种厚度的书分别垂直放了多少本---

dp 的做法没有想出来---再补叭-

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 105;
 8 struct node{
 9     int t,w;
10 };
11 
12 node a[maxn],b[maxn];
13 int n;
14 
15 int cmp(node n1,node n2){
16     return n1.w > n2.w;
17 }
18 
19 int c1,c2;
20 
21 void solve(){
22     if(c1 != 0) sort(a+1,a+c1+1,cmp);
23     if(c2 != 0) sort(b+1,b+c2+1,cmp);
24     int ans = (1<<30)-1;
25     for(int i = 0;i <= c1;i++){
26         for(int j = 0;j <= c2;j++){
27             int lb = i+2*j;
28             int ub = 0;
29             for(int p = i+1;p <= c1;p++){
30                 ub += a[p].w;
31             }
32             for(int q = j+1;q <= c2;q++){
33                 ub += b[q].w;
34             }
35             if(lb >= ub){
36                 ans = min(ans,lb);
37             }
38         }
39     }
40     printf("%d\n",ans);
41 }
42 
43 int main(){
44     while(scanf("%d",&n) != EOF){
45          c1 = 0;
46          c2 = 0;
47          int x,y;
48         for(int i = 1;i <= n;i++){
49             scanf("%d %d",&x,&y);
50             if(x == 1) {
51                 a[++c1].t = x;
52                 a[c1].w = y;
53             }
54             else{
55                 b[++c2].t = x;
56                 b[c2].w = y;
57             }
58         }
59         solve();
60     }
61     return 0;
62 }
View Code

 

cf 294c

http://codeforces.com/contest/294/problem/C

没想出来,看的题解

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 const int maxn = 1000+5;
10 const int mod = 1e9+7;
11 
12 int n,m;
13 LL fac[maxn+5],afac[maxn+5];
14 int a[maxn],cnt[maxn];
15 int vis[maxn];
16 
17 LL Q_pow(LL x,LL y){
18     LL res = 1;
19     x %= mod;
20     while(y){
21         if(y&1) res = res*x%mod;
22         x = x*x%mod;
23         y >>= 1;
24     }
25     return res;
26 }
27 
28 void Pre(){
29     fac[0] = 1;
30     for(int i = 1;i <= maxn;i++){
31         fac[i] = fac[i-1]*1LL*i%mod;
32     }
33     afac[maxn] = Q_pow(fac[maxn],mod-2);
34     for(int i = maxn;i >= 1;i--) afac[i-1] = afac[i]*i%mod;
35 }
36 
37 void solve(){
38     int pos = 0;
39     LL ans = 1;
40     for(int i = 1;i <= n;){
41         int j = i;
42         if(vis[i]){
43             pos = i;
44             i++;
45             continue;
46         }
47         while(!vis[j] && j <= n) j++;
48         int lb = j-i;
49         ans = ans*afac[lb]%mod;
50         //printf("i = %d  j = %d\n",i,j);
51         if(vis[pos] && vis[j]){
52             ans = ans*Q_pow(2,lb-1)%mod;
53         }
54         i = j;
55     }
56     ans = ans*fac[n-m]%mod;
57     printf("%I64d\n",ans);
58 }
59 
60 int main(){
61     Pre();
62 
63    /* for(int i = 1;i <= 10;i++){
64         printf("fac[%d] = %I64d  afac[%d] = %I64d\n",i,fac[i],i,afac[i]);
65     }*/
66 
67     while(scanf("%d %d",&n,&m) != EOF){
68         memset(vis,0,sizeof(vis));
69         for(int i = 1;i <= m;i++) {
70             scanf("%d",&a[i]);
71             vis[a[i]] = 1;
72         }
73         solve();
74     }
75     return 0;
76 }
View Code

 

1.15

今天好颓- -

被hack得好惨阿---

cf 614a

http://codeforces.com/problemset/problem/614/A

可以

p[i-1]*k > r

的时候,移项来判断是不是大于 p[i-1] > r/k

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 LL l,r,k;
10 LL p[10005];
11 
12 void solve(){
13     p[0] = 1;
14     int c = 0;
15     if(l == 1){
16         printf("1 ");
17         c++;
18     }
19     for(int i = 1;i < 100;i++){
20         LL tmp = 1LL*r/k;
21         if(p[i-1] > tmp) break;
22         p[i] = p[i-1]*k;
23         if(p[i] >= l && p[i] <= r) {
24             printf("%I64d ",p[i]);
25             c++;
26         }
27     }
28     if(c == 0) puts("-1");
29 }
30 
31 int main(){
32     while(scanf("%I64d %I64d %I64d",&l,&r,&k) != EOF){
33         solve();
34     }
35     return 0;
36 }
View Code

 

另外是一神教的办法,两边取一下对数

log(p[i-1]) + log(k) > log(r)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<vector>
 7 using namespace std;
 8 
 9 typedef long long LL;
10 LL l,r,k;
11 LL p[10005];
12 
13 void solve(){
14     p[0] = 1;
15     int c = 0;
16     if(l == 1){
17         printf("1 ");
18         c++;
19     }
20     for(int i = 1;i < 100;i++){
21         double lb = log(p[i-1]) + log(k);
22         double ub = log(r);
23        // printf("i = %d  lb = %lf  ub = %lf\n",i,lb,ub);
24         double cha = lb - ub;
25         if(cha > 1e-7 ) {
26            // printf("lb = %lf  ub = %lf cha = %lf\n",lb,ub,cha);
27             break;
28         }
29         p[i] = p[i-1]*k;
30         //printf("p[%d] = %I64d  ---",i,p[i]);
31         if(p[i] >= l && p[i] <= r) {
32             printf("%I64d ",p[i]);
33             c++;
34         }
35     }
36     if(c == 0) puts("-1");
37 }
38 
39 int main(){
40     while(scanf("%I64d %I64d %I64d",&l,&r,&k) != EOF){
41         solve();
42     }
43     return 0;
44 }
View Code

 

cf 614c

http://codeforces.com/contest/614/problem/C

貌似求个圆环

然后 r = min(点到线段的距离)

R = max(P 到 点的距离)

不知道了.....

 还是去抄了一个板-

  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <cmath>
  4 #include <vector>
  5 using namespace std;
  6 //lrj计算几何模板
  7 struct Point
  8 {
  9     double x, y;
 10     Point(double x=0, double y=0) :x(x),y(y) {}
 11 };
 12 typedef Point Vector;
 13 
 14 Point read_point(void)
 15 {
 16     double x, y;
 17     scanf("%lf%lf", &x, &y);
 18     return Point(x, y);
 19 }
 20 
 21 const double EPS = 1e-10;
 22 
 23 //向量+向量=向量 点+向量=点
 24 Vector operator + (Vector A, Vector B)    { return Vector(A.x + B.x, A.y + B.y); }
 25 
 26 //向量-向量=向量 点-点=向量
 27 Vector operator - (Vector A, Vector B)    { return Vector(A.x - B.x, A.y - B.y); }
 28 
 29 //向量*数=向量
 30 Vector operator * (Vector A, double p)    { return Vector(A.x*p, A.y*p); }
 31 
 32 //向量/数=向量
 33 Vector operator / (Vector A, double p)    { return Vector(A.x/p, A.y/p); }
 34 
 35 bool operator < (const Point& a, const Point& b)
 36 { return a.x < b.x || (a.x == b.x && a.y < b.y); }
 37 
 38 int dcmp(double x)
 39 { if(fabs(x) < EPS) return 0; else return x < 0 ? -1 : 1; }
 40 
 41 bool operator == (const Point& a, const Point& b)
 42 { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
 43 
 44 /**********************基本运算**********************/
 45 
 46 //点积
 47 double Dot(Vector A, Vector B)
 48 { return A.x*B.x + A.y*B.y; }
 49 //向量的模
 50 double Length(Vector A)    { return sqrt(Dot(A, A)); }
 51 
 52 //向量的夹角,返回值为弧度
 53 double Angle(Vector A, Vector B)
 54 { return acos(Dot(A, B) / Length(A) / Length(B)); }
 55 
 56 //叉积
 57 double Cross(Vector A, Vector B)
 58 { return A.x*B.y - A.y*B.x; }
 59 
 60 //向量AB叉乘AC的有向面积
 61 double Area2(Point A, Point B, Point C)
 62 { return Cross(B-A, C-A); }
 63 
 64 //向量A旋转rad弧度
 65 Vector VRotate(Vector A, double rad)
 66 {
 67     return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
 68 }
 69 
 70 //将B点绕A点旋转rad弧度
 71 Point PRotate(Point A, Point B, double rad)
 72 {
 73     return A + VRotate(B-A, rad);
 74 }
 75 
 76 //求向量A向左旋转90°的单位法向量,调用前确保A不是零向量
 77 Vector Normal(Vector A)
 78 {
 79     double l = Length(A);
 80     return Vector(-A.y/l, A.x/l);
 81 }
 82 
 83 /**********************点和直线**********************/
 84 
 85 //求直线P + tv 和 Q + tw的交点,调用前要确保两条直线有唯一交点
 86 Point GetLineIntersection(Point P, Vector v, Point Q, Vector w)
 87 {
 88     Vector u = P - Q;
 89     double t = Cross(w, u) / Cross(v, w);
 90     return P + v*t;
 91 }//在精度要求极高的情况下,可以自定义分数类
 92 
 93 //P点到直线AB的距离
 94 double DistanceToLine(Point P, Point A, Point B)
 95 {
 96     Vector v1 = B - A, v2 = P - A;
 97     return fabs(Cross(v1, v2)) / Length(v1);    //不加绝对值是有向距离
 98 }
 99 
100 //点到线段的距离
101 double DistanceToSegment(Point P, Point A, Point B)
102 {
103     if(A == B)    return Length(P - A);
104     Vector v1 = B - A, v2 = P - A, v3 = P - B;
105     if(dcmp(Dot(v1, v2)) < 0)    return Length(v2);
106     else if(dcmp(Dot(v1, v3)) > 0)    return Length(v3);
107     else return fabs(Cross(v1, v2)) / Length(v1);
108 }
109 
110 //点在直线上的射影
111 Point GetLineProjection(Point P, Point A, Point B)
112 {
113     Vector v = B - A;
114     return A + v * (Dot(v, P - A) / Dot(v, v));
115 }
116 
117 //线段“规范”相交判定
118 bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2)
119 {
120     double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
121     double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
122     return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
123 }
124 
125 //判断点是否在线段上
126 bool OnSegment(Point P, Point a1, Point a2)
127 {
128     Vector v1 = a1 - P, v2 = a2 - P;
129     return dcmp(Cross(v1, v2)) == 0 && dcmp(Dot(v1, v2)) < 0;
130 }
131 
132 //求多边形面积
133 double PolygonArea(Point* P, int n)
134 {
135     double ans = 0.0;
136     for(int i = 1; i < n - 1; ++i)
137         ans += Cross(P[i]-P[0], P[i+1]-P[0]);
138     return ans/2;
139 }
140 
141 int n;
142 const int maxn = 1e6+5;
143 
144 Point P;
145 Point t[maxn];
146 
147 void solve(){
148     double r = 1.0*1e9;
149     double R = -1.0*1e9;
150     for(int i = 0;i < n;i++){
151         double dis = sqrt((t[i].x-P.x)*(t[i].x - P.x) + (t[i].y - P.y)*(t[i].y - P.y));
152         R = max(R,dis);
153         double dist = DistanceToSegment(P,t[i],t[(i+1)%n]);
154         r = min(r,dist);
155         //printf("R = %lf  r = %lf\n",R,r);
156     }
157     double ans = 3.14159265357*(R*R-r*r);
158     printf("%.9lf\n",ans);
159 }
160 
161 int main(){
162     while(scanf("%d",&n) != EOF){
163         scanf("%lf %lf",&P.x,&P.y);
164         for(int i = 0;i < n;i++){
165             scanf("%lf %lf",&t[i].x,&t[i].y);
166         }
167         solve();
168     }
169     return 0;
170 }
View Code

 

1.16

神经兮兮地在计蒜客看了一下午python

有一道题目是这样的--

输入 A ,B,C

再输出 A+B+C

为什么用它说的 raw_input()不行阿---

后来从 cf 扒了一个输入才通过-----诶--------------

---------------昏割线-----------------

cf 313 c

http://codeforces.com/contest/313/problem/C

先排序,再统计能够整除4的幂的数 的次数,把次数排序后,和原数组相乘

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 const int maxn = 5e6+5;
 9 int p[maxn],a[maxn],b[maxn];
10 int n;
11 
12 void solve(){
13     int k;
14     for(int i = 0;i <= 14;i++){
15         if(n == p[i]){
16             k = i;
17             break;
18         }
19     }
20    // printf("k = %d\n",k);
21     LL ans = 0;
22     sort(a+1,a+n+1);
23     memset(b,0,sizeof(b));
24     for(int i = 1;i <= n;i++){
25         for(int j = 0;j <= k;j++){
26             if(i%p[j] == 0) b[i]++;
27         }
28     }
29     sort(b+1,b+n+1);
30     for(int i = 1;i <= n;i++){
31         ans += 1LL*a[i]*b[i];
32     }
33     printf("%I64d\n",ans);
34 }
35 
36 int main(){
37     p[0] = 1;
38     for(int i = 1;i <= 14;i++) {
39         p[i] = p[i-1]*4;
40        // printf("p[%d] = %d\n",i,p[i]);
41     }
42     while(scanf("%d",&n) != EOF){
43         //    printf("n = %d\n",n);
44         for(int i = 1;i <= n;i++){
45             scanf("%d",&a[i]);
46         //    printf("a[%d] = %d\n",i,a[i]);
47         }
48         solve();
49     }
50     return 0;
51 }
View Code

 

1.17

cf 289c

http://codeforces.com/problemset/problem/289/C

分 n-k 的奇偶构造一下

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 1e6+5;
 8 char s[maxn];
 9 int n,k;
10 
11 
12 void solve(){
13     char t[maxn];
14     if(n < k){
15         puts("-1");
16         return;
17     }
18     if(n != 1 && k == 1){
19         puts("-1");
20         return;
21     }
22 
23     if(n == k){
24         for(int i = 0;i < n;i++){
25             char c = 'a'+i;
26             printf("%c",c);
27         }
28         printf("\n");
29         return;
30     }
31     if(k == 2){
32         for(int i = 1;i <= n;i++){
33             if(i%2) printf("a");
34             else printf("b");
35         }
36         printf("\n");
37         return;
38     }
39 
40     if((n-k)%2 == 0){
41         for(int i = 1;i <= n-k;i++){
42             if(i%2 == 1) t[i] = 'a';
43             else t[i] = 'b';
44         }
45         for(int i = n-k+1;i <= n;i++){
46             char c = 'a' + i-(n-k+1);
47             t[i] = c;
48         }
49     }
50     else{
51         k--;
52         for(int i = 1;i <= n-k;i++){
53             if(i%2 == 1) t[i] = 'a';
54             else t[i] = 'b';
55         }
56         for(int i = n-k+1;i <= n;i++){
57             int z = i-(n-k+1);
58             if(z == 0) t[i] = 'a';
59             else{
60                 z++;
61                 t[i] = 'a'+z;
62             }
63         }
64     }
65     for(int i = 1;i <= n;i++) printf("%c",t[i]);
66   //  printf("%s",t+1);
67     printf("\n");
68 }
69 
70 int main(){
71     while(scanf("%d %d",&n,&k) != EOF){
72         solve();
73     }
74     return 0;
75 }
View Code

 

cf 614d

http://codeforces.com/problemset/problem/614/D

先对a数组从小到大排序后,枚举达到 A 的个数,(a值大的优先),

再二分剩下的,使得前 x 个的值一样(前x个这时候是作为最小值)

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<vector>
  6 using namespace std;
  7 
  8 typedef long long LL;
  9 const int maxn = 1e6+5;
 10 int a[maxn];
 11 LL p[maxn];
 12 int n,A,cf,cm;
 13 LL m,y;
 14 LL sum[maxn];
 15 LL aa[maxn];
 16 
 17 int ok(int x){
 18     return y >= p[x];
 19 }
 20 
 21 struct node{
 22     int u;
 23     int id;
 24 }q[maxn];
 25 
 26 int cmp(node n1,node n2){
 27     return n1.u < n2.u;
 28 }
 29 
 30 int cmp0(node n1,node n2){
 31     return n1.id < n2.id;
 32 }
 33 
 34 void solve(){
 35     memset(p,0,sizeof(p));
 36     sort(a+1,a+n+1);
 37     p[1] = 0;
 38     for(int i = 2;i <= n;i++){
 39         p[i] = p[i-1] + 1LL*(i-1)*(a[i]-a[i-1]);
 40      //   printf("p[%d] = %d\n",i,p[i]);
 41     }
 42     for(int i = 1;i <= n;i++) aa[i] = a[n-i+1];
 43     sum[0] = 0;
 44     for(int i = 1;i <= n;i++) {
 45         sum[i] = sum[i-1] + 1LL*aa[i];
 46       //  printf("sum[%d] = %I64d\n",i,sum[i]);
 47     }
 48 
 49     int lb,ub,mid;
 50     LL ans = 0;
 51     LL tmp;
 52     int flag = 0;
 53     int ed = 0,edd = 0,cnt = 0;
 54     for(int i = 0;i <= n;i++){
 55         lb = 0;
 56         ub = n-i;
 57         y = m-1LL*i*A + sum[i];
 58         if(y < 0) continue;
 59         tmp = 1LL*i*cf;
 60         if(i == n){
 61             tmp += 1LL*cm*A;
 62             flag = 1;
 63             ans = max(ans,tmp);
 64            // printf("----tmp = %d\n",tmp);
 65             continue;
 66         }
 67         int pos = -1;
 68         for(int k = 0;k <= 50;k++){
 69             mid = (lb+ub)/2;
 70             if(ok(mid)){
 71                 lb = mid+1;
 72                 pos = max(pos,mid);
 73             }
 74             else ub = mid;
 75          //   printf("i = %d  lb = %d  ub = %d  mid = %d\n",i,lb,ub,mid);
 76         }
 77         int minn = min((y-p[pos])/pos + a[pos],1LL*A);
 78         tmp += 1LL*minn*cm;
 79         if(tmp > ans) ed = minn,edd = pos,cnt = i;
 80         ans =  max(ans,tmp);
 81       // printf("i = %d pos = %d y = %I64d  tmp = %I64d\n",i,pos,y,tmp);
 82     }
 83     printf("%I64d\n",ans);
 84     if(flag){
 85         int w = A;
 86         for(int i = 1;i <= n;i++) printf("%d ",w);
 87         printf("\n");
 88         return;
 89     }
 90     sort(q+1,q+n+1,cmp);
 91     for(int i = 1;i <= edd;i++) q[i].u = ed;
 92     for(int i = n;i >= 1;i--){
 93         if(cnt) q[i].u = A,cnt--;
 94         if(cnt == 0) break;
 95     }
 96     sort(q+1,q+n+1,cmp0);
 97     for(int i = 1;i <= n;i++) printf("%d ",q[i].u);
 98     printf("\n");
 99 }
100 
101 int main(){
102     while(scanf("%d %d %d %d %I64d",&n,&A,&cf,&cm,&m) != EOF){
103         for(int i = 1;i <= n;i++){
104             scanf("%d",&a[i]);
105             q[i].u = a[i];
106             q[i].id = i;
107         }
108         solve();
109     }
110     return 0;
111 }
View Code

 

转载于:https://www.cnblogs.com/wuyuewoniu/p/5122440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值