Codeforces Round #304 (Div. 2)

这次打得较差,差点就绿了。。。

B题,这B题竟然WA了,再看看就发现一个很严重的错误。。--!

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int num[3050];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&num[i]);
        }
        sort(num,num+n);
        int cnt=0,ans=0;
        for(int i=1;i<n;i++){
            if(num[i]==num[i-1]){
                cnt++;
            }
            else {
                int k=num[i]-num[i-1]-1;
                if(k>=cnt){
                    ans=ans+(1+cnt)*cnt/2;
                    cnt=0;
                }
                else{
                    ans=ans+(1+k)*k/2;
                    cnt-=k;
                }
                ans=ans+cnt*(k+1);
            }
        }
        if(cnt>0){
            ans=ans+(1+cnt)*cnt/2;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

D题,可以预处理出各个数个含的质数的个数,可以用循环处理,求前缀和。循环是,从2开始,d记录下它有没有质因子,d=0则证明它当前是素数,第二层对j=i*k这样的数求它含有i的个数。如果d不为0,说明它不是素数,且由于一直循环,所以处理到它时就是已经把它所有质因子个数算出来了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
const int N=5000000;
int d[N+10];
int s[N+10];

void initial(){
    memset(d,0,sizeof(d));
    s[0]=s[1]=0;
    for(int i=2;i<=N;i++){
        if(d[i]==0){
            d[i]++;
            for(int j=i+i;j<=N;j+=i){
                int c=j;
                while(c%i==0){
                    d[j]++;
                    c/=i;
                }
            }
        }
        s[i]=s[i-1]+d[i];
    }
}

int main(){
    initial();
    int T,a,b;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&a,&b);
        printf("%d\n",s[a]-s[b]);
    }
    return 0;
}

  

E题,二分图网络流

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
/*
这道在开始也在想网络流,但是,看到只能移动一条边时,就无语了。。群里一讨论,对哦,二分图拆点建边,
这样就是一条边了。。。T_T我果然是渣。。 s->i建一条a[i]的边,i+n->e建一条b[i]的边。u-v就按u->v‘,v->u‘。
注意加上u->u‘
*/

const int INF=0x3f3f3f3f;
const int MAXN=300;//点数的最大值
const int MAXM=2000;//边数的最大值

struct Node
{
    int from,to,next;
    int cap;
}edge[MAXM];
int tol;

int dep[MAXN];//dep为点的层次
int head[MAXN];
int flow[MAXN][MAXN];
int n,m;
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)//第一条变下标必须为偶数
{
    edge[tol].from=u;
    edge[tol].to=v; edge[tol].cap=w;  edge[tol].next=head[u];
    head[u]=tol++;
    edge[tol].from=v;
    edge[tol].to=u;
    edge[tol].cap=0;
    edge[tol].next=head[v];
    head[v]=tol++;
}

int BFS(int start,int end)
{
    int que[MAXN];
    int front,rear; front=rear=0;
    memset(dep,-1,sizeof(dep));
    que[rear++]=start;
    dep[start]=0;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==MAXN)front=0;
        for(int i= head[u];i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>0&& dep[v]==-1)
            {
                dep[v]=dep[u]+1;
                que[rear++]=v;
                if(rear>=MAXN) rear=0;
                if(v==end)return 1;
            }
        }
    }
    return 0;
}
int dinic(int start,int end)
{
    int res=0;
    int top;
    int stack[MAXN];//stack为栈,存储当前增广路
    int cur[MAXN];//存储当前点的后继
    while(BFS(start,end))
    {
        memcpy(cur,head, sizeof(head));
        int u=start;
        top=0;
        while(1)
        {
            if(u==end)         //到达汇点
            {
                int min=INF;
                int loc;
               for(int i=0;i<top;i++)

                  if(min>edge[stack[i]].cap)
                  {
                      min=edge[stack[i]].cap;
                      loc=i;
                  }
                for(int i=0;i<top;i++){
                    edge[stack[i]].cap-=min;
                    edge[stack[i]^1].cap+=min;
                }
                res+=min;
                top=loc;                // 以最小边的起点为源点再寻找其他增广路径
                u=edge[stack[top]].from;
            }
            for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)         //寻找可增广的路径,即确定edge[]中以u为起点可增广的i的第一条边的位置
              if(edge[i].cap!=0 && dep[u]+1==dep[edge[i].to])
                 break;
            if(cur[u]!=-1)
            {
                stack [top++]=cur[u];
                u=edge[cur[u]].to;
            }
            else
            {
                if(top==0) break;   //不能再增广
                dep[u]=-1;
                u= edge[stack [--top] ].from;   //退栈寻找下一条
            }
        }
    }
    return res;
}

int bef[MAXN],aft[MAXN];

int main(){
	 int s,e;
	 while(scanf("%d%d",&n,&m)!=EOF){
	 	init();
	 	int sum=0; s=0; e=2*n+1;
	 	memset(flow,0,sizeof(flow));
	 	for(int i=1;i<=n;i++){
	 		scanf("%d",&bef[i]);
	 		sum+=bef[i];
	 	}
	 	int cp=sum;
	 	for(int i=1;i<=n;i++){
	 		scanf("%d",&aft[i]);
	 		sum-=aft[i];
	 	}
	 	for(int i=1;i<=n;i++){
	 		addedge(s,i,bef[i]);
	 		addedge(i+n,e,aft[i]);
	 		addedge(i,i+n,INF);
	 	}
	 	int u,v;
	 	for(int i=1;i<=m;i++){
	 		scanf("%d%d",&u,&v);
	 		addedge(u,v+n,INF);
	 		addedge(v,u+n,INF);
	 	}
	 	if(sum!=0){
	 		printf("NO");
	 	}
	 	else{
	 		int res=dinic(s,e);
	 		if(res!=cp){
	 			puts("NO");
	 		}
	 		else{
	 			puts("YES");
	 			for(int i=n+1;i<=2*n;i++){
	 				int v=i-n;
	 				for(int ei=head[i];ei!=-1;ei=edge[ei].next){
	 					int u=edge[ei].to;
	 					if(u<e){
	 						flow[u][v]=edge[ei].cap;
	 					}
	 				}
	 			}
	 			for(int i=1;i<=n;i++){
	 				printf("%d",flow[i][1]);
	 				for(int j=2;j<=n;j++)
	 				printf(" %d",flow[i][j]);
	 				printf("\n");
	 			}
	 		}
	 	}
	 }
}

  

时间: 2024-08-29 01:56:15

Codeforces Round #304 (Div. 2)的相关文章

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

题目传送门 1 /* 2 水题:ans = (1+2+3+...+n) * k - n,开long long 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 12 int main(void) //Codeforces Roun

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;

queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

题目传送门 1 /* 2 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 3 queue容器:模拟上述过程,当次数达到最大值时判断为-1 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #in

D-Soldier and Number Game(CF546D) Codeforces Round #304 (Div. 2)

D. Soldier and Number Game Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of ch

Codeforces Round #304 (Div. 2) -----CF546

A. Soldier and Bananas A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana). He has n dollars. How many dol

Codeforces Round #304 (Div. 2)——D素数筛+dp——Soldier and Number Game

Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x

codeforces水题100道 第五题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas (math)

题目链接:http://www.codeforces.com/problemset/problem/546/A题意:一个人现在有n元,它买第i根香蕉需要i*k元,问他要买w根香蕉的话,需要问他的朋友借多少钱?C++代码: #include <iostream> using namespace std; int n, k, w; int main() { cin >> k >> n >> w; cout << max(0, w*(w+1)/2*k-

Codeforces Round #304 (Div. 2)——1002—— Soldier and Badges

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one o