wenbao与cf

选择一个区间反转使得1最多

http://codeforces.com/contest/327/problem/A

 

弱鸡代码

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int sum[120], sum2[120], a[120], b[120];
 4 int main(){
 5     int n, ma = -1;
 6     cin>>n;
 7     for(int i = 1; i <= n; i++){
 8         cin>>a[i];
 9         sum[i] = sum[i-1] + a[i];
10         b[i] = 1 - a[i];
11         sum2[i] = sum2[i-1] + b[i];
12     }
13     for(int i = 1; i <= n; i++){
14         for(int j = i; j <=n ; j++){
15             ma = max(sum[i-1] + sum[n] - sum[j] + sum2[j] - sum2[i-1], ma);
16         }
17     }
18     cout<<ma<<endl;
19 }

大神代码

 

 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,l=0,k=0,s=0,i,a;
 6     cin>>n;
 7     for(i=1;i<=n;i++)
 8     {
 9         cin>>a;if(a==1) {l++;if(s>0) s--;}else s++; if(s>k) k=s;
10     }
11     if(k==0) {cout<<l-1;return 0;}
12     cout<<l+k;
13 }

 

思路很重要,其实就是找最长的0.。。。。。。。

 

 

@找规律题

  啦啦啦啦啦~~~从样例里面寻找到新大陆!!

  

1     2 + 2*1 = 4 ----> 2/2 = 1;
2     2 + 17*2 = 36 ----> 6/3 = 2;
3     3 + 46*3 = 144 ----> 12/4 = 3;
4     12 + 97*4 = 400 ----> 20/5 = 4; 
5     so ----------------->
6     X = n*(n+1)*(n+1) - (n-1);

 

  

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

 

1 #include <stdio.h>
2 long long x;
3 int main(){
4     scanf("%I64d", &x); 
5     printf("2\n", 2);
6     for(long long i = 2; i <= x; i++) 
7         printf("%I64d\n", i*(i+1)*(i+1)-(i-1));
8 }

 

 

@   http://codeforces.com/problemset/problem/722/C

   逐次删除元素,求区间最大和

 

 1 #include <iostream>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn = 1e5+10;
 5 ll a[maxn], sum[maxn], b[maxn], l[maxn], r[maxn], vis[maxn], c[maxn], n, ma;
 6 int main(){
 7     std::ios::sync_with_stdio(false);
 8     cin.tie(0), cout.tie(0);
 9     cin>>n;
10     for(int i = 1; i <= n; i++)    cin>>a[i];
11     for(int i = 1; i <= n; i++) cin>>b[i];
12     for(int i = n; i > 1; i--){
13         if(vis[b[i]-1] && vis[b[i]+1]){
14             r[l[b[i]-1]] = r[b[i]+1], l[r[b[i]+1]] = l[b[i]-1];
15             ma =sum[r[b[i]-1]] = sum[l[b[i]-1]] = sum[l[b[i]+1]] = sum[r[b[i]+1]] = sum[l[b[i]-1]] + sum[r[b[i]+1]] +a[b[i]];
16         }else if(vis[b[i]-1]){
17             l[b[i]] = l[b[i]-1], r[b[i]] = b[i], r[l[b[i]-1]] = b[i];
18             ma  = sum[r[b[i]-1]] = sum[l[b[i]-1]] = sum[l[b[i]]] =  sum[r[b[i]]] = sum[l[b[i]-1]] + a[b[i]];
19         }else if(vis[b[i]+1]){
20             l[b[i]] = b[i], r[b[i]] = r[b[i]+1], l[r[b[i]+1]] = b[i];
21             ma = sum[r[b[i]]] =  sum[l[b[i]]] = sum[r[b[i]+1]] = sum[l[b[i]+1]] = sum[r[b[i]+1]] + a[b[i]];
22         }else{
23             l[b[i]] = r[b[i]] = b[i];
24             ma = sum[l[b[i]]] = sum[r[b[i]]] = a[b[i]];
25         }
26         c[i] = max(ma, c[i+1]);
27         vis[b[i]] = 1;
28     }
29     for(int i = 2; i <= n; i++) cout<<c[i]<<endl;
30     cout<<0<<endl;
31     return 0;
32 }

 

 

小艾代码:

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 #define ll long long
 5 ll a[100005],numl[100005],numr[100005],b[100005],l[100005],r[100005],ans[100005];
 6 ll Max(ll a,ll b){
 7     return a>b?a:b;
 8 }
 9 int main(){
10     int n;
11     long long sum=0;
12     scanf("%d",&n);
13     for(int i=1;i<=n;i++){
14         scanf("%lld",&a[i]);
15     }
16     for(int i=n;i>=1;i--) scanf("%lld",&b[i]);
17     for(int i=0;i<=n+1;i++){
18         if(i>0) l[i]=i-1;
19         if(i<n+1) r[i]=i+1;
20     }
21     for(int i=1;i<=n;i++){
22         int x=b[i];
23         long long num=numr[l[x]]+numl[r[x]]+a[x];
24         numl[r[x]]=num;
25         numr[l[x]]=num;
26         l[r[x]]=l[x];
27         r[l[x]]=r[x];
28         sum=Max(sum,num);
29         ans[i]=sum;
30     }
31     for(int i=n-1;i>=0;i--) printf("%lld\n",ans[i]);
32 
33 }

 

 

 

X73代码:

 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int N=1e5+5;
 5 
 6 ll a[N],b[N];
 7 ll sum[N],res[N],mark[N];
 8 set<ll>s;
 9 int pre[N];
10 int Find(int v)
11 {
12     if(pre[v]!=v)
13         pre[v]=Find(pre[v]);
14     return pre[v];
15 }
16 
17 int main()
18 {
19     int n;
20     scanf("%d",&n);
21     for(int i=1;i<=n;i++)pre[i]=i;
22     for(int i=1;i<=n;i++)scanf("%I64d",a+i);
23     for(int i=1;i<=n;i++)scanf("%I64d",b+i);
24     res[n]=0;
25     mark[b[n]]=1;
26     sum[b[n]]=a[b[n]];
27     s.insert(a[b[n]]);
28     for(int i=n-1;i>=1;i--)
29     {
30         res[i]=*s.rbegin();
31         int t=b[i];
32         if(mark[t+1]&&!mark[t-1])
33         {
34             int u=Find(t+1);
35             pre[t]=u;
36             sum[u]+=a[t];
37             s.insert(sum[u]);
38         }
39         else if(!mark[t+1]&&mark[t-1])
40         {
41             int u=Find(t-1);
42             pre[t]=u;
43             sum[u]+=a[t];
44             s.insert(sum[u]);
45         }
46         else if(mark[t+1]&&mark[t-1])
47         {
48             int u=Find(t+1);
49             int v=Find(t-1);
50             pre[u]=v;
51             pre[t]=v;
52             sum[v]+=sum[u]+a[t];
53             s.insert(sum[v]);
54         }
55         else
56         {
57             sum[t]+=a[t];
58             s.insert(a[t]);
59         }
60         mark[t]=1;
61     }
62     for(int i=1;i<=n;i++)
63         printf("%I64d\n",res[i]);
64     return 0;
65 }

 

 

 

@  http://codeforces.com/contest/723/problem/D

    dfs

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char a[60][60];
 4 int flag[60][60], n, m, k, num, t = 0, mark, dir[4][2] = {0,1,1,0,0,-1,-1,0};
 5 pair<int ,pair<int,int> > p[2500];
 6 void dfs(int x, int y){
 7     if(x == 0 || x == n-1 || y == 0 || y == m-1) mark = 0;
 8     for(int i = 0; i < 4; i++){
 9         int xx = x + dir[i][0];
10         int yy = y + dir[i][1];
11         if(xx >= 0 && xx < n && yy >= 0 && yy < m && a[xx][yy] == '.' && !flag[xx][yy]){
12             flag[xx][yy] = 1;
13             num++;
14             dfs(xx, yy);
15         }
16     }
17 }
18 void lalala(int x, int y){
19     a[x][y] = '*';
20     for(int i = 0; i < 4; i++){
21         int xx = x + dir[i][0];
22         int yy = y + dir[i][1];
23         if(xx >= 0 && xx < n && yy >= 0 && yy < m && a[xx][yy] == '.') lalala(xx,yy);
24     }
25 }
26 int main(){
27     cin >> n>> m >> k;
28     for(int  i = 0; i < n; i++) cin>>a[i];
29     for(int i = 0; i < n; i++){
30         for(int j = 0; j < m; j++){
31             if(a[i][j] == '.' && !flag[i][j]){
32                 flag[i][j] = 1, num = 1, mark = 1, dfs(i,j);
33                 if(mark) p[t].first = num, p[t].second.first = i, p[t].second.second = j, t++; 
34             }
35         }
36     }
37     sort(p, p+t);
38     int sum = 0;
39     for(int i = 0; i < t-k; i++) sum += p[i].first, lalala(p[i].second.first, p[i].second.second);
40     cout<<sum<<endl;
41     for(int i = 0; i < n; i++) cout<<a[i]<<endl;
42 }

 

 

@  http://codeforces.com/contest/722/problem/D

 

想了很久没有思路。。。垃圾

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 map<int, int> m;
 5 priority_queue<int> pq;
 6 int main(){
 7     int n, flag = 0, x;
 8     cin>>n;
 9     for(int i = 0; i < n; i++){
10         cin>>x;
11         m[x] = 1;
12         pq.push(x);
13     }
14     while(1){
15         x = pq.top();
16         while(x&&m[x]) x/=2;
17         if(!x) break;
18         else pq.pop(),pq.push(x), m[x] = 1;
19     }
20     while(!pq.empty()){
21         cout<<pq.top()<<" ";
22         pq.pop();
23     }
24     return 0;
25 }

 

 

 

@  http://codeforces.com/contest/721/problem/C

    费用小于等于T游览的城市尽量多

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <string.h>
 4 using namespace std;
 5 const int maxn = 5005;
 6 int n, m, t;
 7 int node = 0, head[maxn], dp[maxn][maxn], parent[maxn][maxn];
 8 struct Node {
 9     int from, to, next, w;
10 } Edge[maxn];
11 void init(){
12     memset(head, -1, sizeof(head));
13     for(int i = 0; i <= n; i ++){
14         for(int j = 0; j <= n; j ++){
15             dp[i][j] = t+1;
16         }
17     }
18     dp[1][1] = 0;
19 }
20 void addedge(int u, int v, int w){
21     Edge[node].from = u;
22     Edge[node].to = v;
23     Edge[node].w = w;
24     Edge[node].next = head[u];
25     head[u] = node ++;
26 }
27 void print(int pos){
28     cout << pos <<endl;
29     int id = n;
30     vector<int> v;
31     v.push_back(n);
32     for(int i = pos; i > 1; i--){
33         v.push_back(parent[i][id]);
34         id = parent[i][id];
35     }
36     for(int i = v.size() - 1; i >= 0; i--) cout << v[i] <<" ";
37 }
38 void solve(){
39     int v, u, w, pos;
40     for(int i = 2; i <= n; i++){
41         for(int j = 0; j < m; j++){
42             u = Edge[j].from, v = Edge[j].to, w = Edge[j].w;
43             if(dp[i-1][u] + w < dp[i][v]){
44                 dp[i][v] = dp[i-1][u] + w;
45                 parent[i][v] = u;
46             }
47         }
48         if(dp[i][n] <= t) pos = i;
49     }
50     print(pos);
51 }
52 int main(){
53     cin >> n >> m >> t;
54     init();
55     int u, v, w;
56     for(int i = 0; i < m; i++) {
57         cin >> u >> v >> w;
58         addedge(u, v, w);
59     }
60     solve();
61     return 0;
62 }

 

 

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 const int maxn = 5005;
 5 int u[maxn], v[maxn], w[maxn], dp[maxn][maxn], parent[maxn][maxn], x[maxn];
 6 int main(){
 7     int n, m, t, pos;
 8     cin >> n >> m >> t;
 9     for(int i = 0; i < m; i ++){
10         cin >> u[i] >> v[i] >> w[i];
11     }
12     memset(dp, 0x3f, sizeof(dp));
13     dp[1][1] = 0;
14     for(int i = 2; i <= n; i ++){
15         for(int j = 0; j < m; j ++){
16             if(dp[i-1][u[j]] + w[j] < dp[i][v[j]]){
17                 dp[i][v[j]] = dp[i-1][u[j]] + w[j];
18                 parent[i][v[j]] = u[j];
19             }
20         }
21         if(dp[i][n] <= t) pos = i;
22     }
23     int id = n;
24     for(int i = pos; i >= 1; i--){
25         x[i] = id;
26         id = parent[i][id];
27     }
28     cout << pos <<endl;
29     for(int i = 1; i <= pos; i ++) cout << x[i]<<" ";
30     return 0;
31 }

 

 

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

  内切球(W的爽啊)

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 1e5+10;
 5 struct Node{
 6     long long x, y, z, w;
 7 }T[maxn];
 8 long long cmp(Node a, Node b){
 9     if(a.y == b.y && a.z == b.z) return a.x < b.x;
10     if(a.y == b.y) return a.z < b.z;
11     return a.y < b.y;
12 }
13 int main(){
14     long long n, mi = -1, a[4], num, num1, num2;
15     cin >> n;
16     for(int i = 0; i < n; i++){
17         cin >> a[0] >> a[1] >> a[2];
18         sort(a, a+3);
19         T[i].x = a[0], T[i].y = a[1], T[i].z = a[2], T[i].w = i+1;
20         if(a[0] > mi){
21             mi = a[0];
22             num = i+1;
23         }
24     }
25     sort(T, T+n, cmp);
26     long long mm = -1;
27     for(int i = 1; i < n; i++){
28         if(T[i].y == T[i-1].y && T[i].z == T[i-1].z){
29             long long t = min(T[i].x+T[i-1].x, T[i].y);
30             if(t > mm){
31                 mm = t;
32                 num1 = T[i].w, num2 = T[i-1].w;
33             }
34         }
35     }
36     if(mi >= mm) {
37         cout<<1<<endl<<num<<endl;
38     }else{
39         cout<<2<<endl<<num1<<" "<<num2<<endl;
40     }
41     return 0;
42 }

 

 

 

好屌

 

http://codeforces.com/contest/738/problem/A

 

1 #include <bits/stdc++.h>
2 using namespace std;
3 
4 int main() {
5   string s; cin >> s >> s;
6   cout << regex_replace(s, regex("o(go)+"), "***") << '\n';
7   return 0;
8 }

 

 

 

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

 

投票。。。。。。

 

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <map>
 5 using namespace std;
 6 const int maxn = 2e5+10;
 7 vector<int> v[maxn];
 8 int vv[maxn], b[maxn];
 9 map<int, int > m;
10 map<int, int > :: iterator it;
11 int main(){
12     int n, q, x, y;
13     scanf("%d", &n);
14     for(int i = 0; i < n; i++){
15         scanf("%d%d", &x, &y);
16         v[x].push_back(y);
17         vv[x] = y;
18     }
19     m[0] = 0;
20     for(int i = 1; i <= n; i++){
21         if(vv[x]){
22             m[vv[i]] = i;
23         }
24     }
25     scanf("%d", &q);
26     while(q--){
27         int x, xx;
28         scanf("%d", &x);
29         for(int i = 0; i < x; i++){
30             scanf("%d", &xx);
31             b[i] = xx;
32             if(vv[xx]) m.erase(vv[xx]);
33         }
34         it = m.end();
35         it --;
36         if(it == m.begin()){
37             printf("0 0\n");
38         }else{
39             int itt = it->second;
40             it--;
41             printf("%d %d\n", itt, *lower_bound(v[itt].begin(), v[itt].end(), it->first));
42         }
43         for(int i = 0; i < x; i++){
44             if(vv[b[i]]) m[vv[b[i]]] = b[i];
45         }
46     }
47     return 0;
48 }

 

 

 

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

 

圣诞老人送礼物

  上下左右四个点中三个点就可以确定一个点;;fuck

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 map<char, int> m;
 4 int main(){
 5     int n, sum = 1;
 6     string str;
 7     cin >> n >> str;
 8     for(int i = 0; i < n; i++){
 9         m[str[i]] ++;
10         if((m['L'] > 0&& m['R'] > 0) ||(m['U'] > 0 && m['D'] > 0)){
11             sum ++;
12             m['L'] = 0, m['R'] = 0, m['U'] = 0, m['D'] = 0;
13             m[str[i]] ++;
14         }
15     }
16     printf("%d\n", sum);
17     return 0;
18 }

 

 

大神就是这样出神入化

 

 1 #include<cstdio>
 2 #include<cstring>
 3 char s[200010];
 4 bool b[10];
 5 int d[1000];
 6 int main()
 7 {
 8     int n,i,ans;
 9     scanf("%d%s",&n,s+1);
10     ans=0;
11     d['L'-'A'+1]=0;
12     d['U'-'A'+1]=1;
13     d['D'-'A'+1]=2;
14     d['R'-'A'+1]=3;
15     for (i=1;i<=n;i++)
16     {
17         if (b[3-d[s[i]-'A'+1]])
18         {
19             ans++;
20             b[0]=b[1]=b[2]=b[3]=0;
21         }
22         b[d[s[i]-'A'+1]]=1;
23     }
24     printf("%d\n",ans+1);
25 }

 

 

http://codeforces.com/contest/750/problem/A

 

自己太菜。。。。。别人可以一分钟就秒的题硬是做了二十多分钟。。。。垃圾。。。

 

自己的代码:

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main(){
 4     int n, m, sum = 0, a = 5;
 5     scanf("%d%d", &n, &m);
 6     m = 240 - m;
 7     if((10+(n-1)*5)*n/2 < m){
 8         printf("%d\n", n);
 9         return 0;
10     }
11     for(int i = 0; ; i++){
12         if(sum == m){
13             printf("%d\n", i);
14             return 0;
15         }
16         if(sum > m){
17             printf("%d\n", i-1);
18             return 0;
19         }
20         sum += a;
21         a+=5;
22     }
23     return 0;
24 }

 

大大神代码:

 

 1 #include <stdio.h>
 2 #include <iostream>
 3 using namespace std;
 4 int main(){
 5     int n, m, num = 0;
 6     scanf("%d %d", &n, &m);
 7     for(int i = 1; i <= n; i++){
 8         if(m+5*i <= 240){
 9             num ++;
10             m += 5*i;
11         }
12     }
13     printf("%d\n", num);
14 }

 

模拟。。。什么是模拟,。。,,。,。,按照实际的思路写(当你面对的时候会怎么办)

 

 

 

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

 

菜如狗

    cf排名

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 0x3f3f3f3f;
 4 int main(){
 5     int n, a = maxn, b = -maxn, sum = 0, m , t;
 6     scanf("%d", &n);
 7     for(int i = 0; i < n; i++){
 8         scanf("%d %d", &m, &t);
 9         if(t == 1){
10             a = min(a, sum);
11         }else{
12             b = max(b, sum);
13         }
14         sum += m;
15     }
16     if(a <= b){
17         puts("Impossible");
18         return 0;
19     }
20     if(b == -maxn){
21         puts("Infinity");
22         return 0;
23     }
24     printf("%d\n", 1899 - b + sum);
25     return 0;
26 }

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:46  wenbao  阅读(181)  评论(0编辑  收藏  举报