codeforces-1139 (div2)

A.如果第i个数字是偶数,总贡献就加上i

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)  
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))  
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);  
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);  
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long  
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second 
typedef vector<int> VI;
int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();}
while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;}
const double eps = 1e-9;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; 
int N,M,K;
char str[maxn];
int main(){
    Sca(N);
    scanf("%s",str + 1);
    LL ans = 0;
    for(int i = 1; i <= N ; i ++){
        if(!((str[i] - '0') & 1)) ans += i;
    }
    Prl(ans);
    return 0;
}
A

 

B.从后往前贪心,总是取当年能取的最大的。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)  
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))  
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);  
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);  
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long  
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second 
typedef vector<int> VI;
int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();}
while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;}
const double eps = 1e-9;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; 
int N,M,K;
LL a[maxn];
int main(){
    Sca(N);
    for(int i = 1; i <= N ; i ++){
        Scl(a[i]);        
    }
    LL now = INF;
    LL ans = 0;
    for(int i = N; i >= 1 && now; i --){
        a[i] = min(a[i],now - 1);
        ans += a[i];
        now = a[i];
    }
    Prl(ans);
    return 0;
}
B

 

C.总个数是N ^ K,考虑所有不符合条件的情况,显然黑边两边的点是不可以出现在同一序列的的,所以去掉所有的黑边,并查集维护所有联通块的点,对于每个联通快,不合理的情况是size ^ K

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)  
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))  
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);  
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);  
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long  
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second 
typedef vector<int> VI;
int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();}
while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;}
const double eps = 1e-9;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; 
int N,M,K;
int fa[maxn],Size[maxn];
void init(){
    for(int i = 0 ; i <= N; i ++){
        fa[i] = i;
        Size[i] = 1;
    }
}
LL cul(LL a,LL b){
    LL ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return ans;
}
int find(int t){
    if(fa[t] == t) return t;
    return fa[t] = find(fa[t]);
}
void Union(int a,int b){
    a = find(a); b = find(b);
    if(a == b) return;
    fa[a] = b;
    Size[b] += Size[a];
}
int main(){
    Sca2(N,K); init();
    for(int i = 1; i <= N - 1; i ++){
        int u,v,w; Sca3(u,v,w);
        if(!w) Union(u,v);
    }
    LL sum = cul(N,K);
    for(int i = 1; i <= N ; i ++){
        if(fa[i] == i){
            sum = (sum - cul(Size[i],K) + mod) % mod;
        }
    }
    Prl(sum);
    return 0;
}
C

 

E.显然反向考虑,不断往上加比较容易。

考虑到整个图实际上类似于一个二分图,对于每一次加就做一次类似寻找增广路的操作寻找能否增加答案即可。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)  
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))  
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);  
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);  
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long  
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second 
typedef vector<int> VI;
int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();}
while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;}
const double eps = 1e-9;
const int maxn = 5010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7; 
int N,M,K,Q;
PII node[maxn];
int vis[maxn];
int q[maxn];
struct Edge{
    int to,next;
}edge[maxn];
int head[maxn],tot;
void init(){
    for(int i = 0 ; i < maxn; i ++) head[i] = -1;
    tot = 0;
}
void add(int u,int v){
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
int linker[maxn];
int Stack[maxn],top;

bool dfs(int x){
    for(int i = head[x]; ~i ; i = edge[i].next){
        if(vis[edge[i].to]) continue;
        vis[edge[i].to] = 1;
        Stack[++top] = edge[i].to;
        if(linker[edge[i].to] == -1 || dfs(linker[edge[i].to])){
            linker[edge[i].to] = x;
            return true;
        }
    }
    return false;
}
bool find(int x){
    for(int i = 1; i <= top; i ++) vis[Stack[i]] = 0;
    top = 0;
    return dfs(x);
}
int ans[maxn];
int main(){
    Sca2(N,M); init();
    for(int i = 1; i <= M; i ++) linker[i] = -1;
    for(int i = 1; i <= N; i ++) node[i].fi = read();
    for(int i = 1; i <= N; i ++) node[i].se = read();
    Sca(Q);
    for(int i = 1; i <= Q; i ++){
        q[i] = read();
        vis[q[i]] = 1;
    }
    for(int i = 1; i <= N ; i ++){
        if(!vis[i]){
            add(node[i].fi,node[i].se);
        }
        vis[i] = 0;
    }
    int cnt = 0;
    while(find(cnt)) cnt++;
    ans[Q] = cnt;
    for(int i = Q - 1;i >= 1 ; i --){
        add(node[q[i + 1]].fi,node[q[i + 1]].se);
        while(find(cnt)) cnt++;
        ans[i] = cnt;
    }
    for(int i = 1; i <= Q; i ++) Pri(ans[i]);
    return 0;
}
E

 

posted @ 2019-04-08 18:34  Hugh_Locke  阅读(267)  评论(0编辑  收藏  举报