Codeforces Round #304 (Div. 2)

A. Soldier and Bananas
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A soldier wants to buy w bananas in the shop. He has to payk dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to payi·k dollars for the i-th banana).

He has n dollars. How many dollars does he have to borrow from his friend soldier to buyw bananas?

Input

The first line contains three positive integers k, n, w (1  ≤  k, w  ≤  1000,0 ≤ n ≤ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.

Output

Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output0.

Sample test(s)
Input
3 17 4
Output
13

分析:求一下等差数列前w项之和Sn,然后判断Sn与n的大小即可。(ps:比赛的时候忘了比较,直接输出Sn-n以致wa了一发,简直傻逼了)。
题目链接: http://codeforces.com/contest/546/problem/A
代码清单:
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<ctime>
#include<cctype>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

ll k,n,w;

void input(){
    cin>>k>>n>>w;
}

void solve(){
    if(w*(k+w*k)/2>n)
        cout<<w*(k+w*k)/2-n<<endl;
    else cout<<"0"<<endl;
}

int main(){
    input();
    solve();
    return 0;
}

B. Soldier and Badges
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Colonel has n badges. He wants to give one badge to every of hisn 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 of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.

Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.

Input

First line of input consists of one integer n (1 ≤ n ≤ 3000).

Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.

Output

Output single integer — minimum amount of coins the colonel has to pay.

Sample test(s)
Input
4
1 3 1 4
Output
1
Input
5
1 2 3 2 5
Output
2
Note

In first sample test we can increase factor of first badge by 1.

In second sample test we can increase factors of the second and the third badge by1.


分析:先排序,然后对一个数如果出现c次(c>1),那么就对其中c-1个进行操作,一直加1直到出现一个新的数,记得把新的数也标记。(ps:比赛时忘了标记,又傻逼了)。
题目链接: http://codeforces.com/contest/546/problem/B
代码清单:
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<ctime>
#include<cctype>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

int n;
int a[3005];
int vis[100000];
int ans=0;

void input(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        vis[a[i]]++;
    }
}

void solve(){
    sort(a,a+n);
    for(int i=0;i<n;i++){
        int x=a[i];
        if(vis[a[i]]>1){
            while(vis[x]){
                x++;
                ans++;
            }
            vis[x]=1;
            vis[a[i]]--;
        }
    }
    cout<<ans<<endl;
}

int main(){
    input();
    solve();
    return 0;
}

C. Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins thisfight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

You have to calculate how many fights will happen and who will win the game, or state that game won't end.

Input

First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then followk1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

All card values are different.

Output

If somebody wins in this game, print 2 integers where the first one stands for the number offights before end of game and the second one is1 or 2 showing which player has won.

If the game won't end and will continue forever output  - 1.

Sample test(s)
Input
4
2 1 3
2 4 2
Output
6 2
Input
3
1 2
2 1 3
Output
-1
Note

First sample:

Second sample:


分析:就是按部就班,题目怎么说就怎么做。比赛是我用set集合来标记导致TLE。然而并不需要标记也是可以过的,判断的时候可以判断循环次数大于一个极大值(20000000)即可视为无解。也可以用hash,但是我想了好久都不知道如何hash,然后随便取了个mod=3031,咦,居然神奇地过了。
题目链接: http://codeforces.com/contest/546/problem/C
代码清单:

(1) hash法
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<ctime>
#include<cctype>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const int maxn = 3050 ;
const int mod = 3031 ;

queue<int>p;
queue<int>q;
int n;
int k1,k2,x;
int an=0,bn=0,ans=0;
bool ok=false;
bool vis[maxn][maxn];

void input(){
    scanf("%d",&n);
    scanf("%d",&k1);
    for(int i=0;i<k1;i++){
        scanf("%d",&x);
        p.push(x);
        an=an*10+x;
    }
    scanf("%d",&k2);
    for(int i=0;i<k2;i++){
        scanf("%d",&x);
        q.push(x);
        bn=bn*10+x;
    }
}

void solve(){
    vis[an%mod][bn%mod]=true;
    while(!p.empty()&&!q.empty()){
        ans++;
        int pp=p.front(); p.pop();
        int qq=q.front(); q.pop();
        int a=1,b=1;
        while(a*10<an) a*=10;
        an=an%a;
        while(b*10<bn) b*=10;
        bn=bn%b;
        if(pp>qq){
            p.push(qq);
            p.push(pp);
            an=an*100+qq*10+pp;
        }
        else{
            q.push(pp);
            q.push(qq);
            bn=bn*100+pp*10+qq;
        }
        a=an%mod; b=bn%mod;
        if(!vis[a][b]) vis[a][b]=true;
        else{ ok=true; break; }
    }
    if(ok) printf("-1\n");
    else{
        printf("%d ",ans);
        if(p.size()) printf("1\n");
        else printf("2\n");
    }
}

int main(){
    input();
    solve();
    return 0;

}

(2) 暴力模拟
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<ctime>
#include<cctype>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int maxn = 20000000;
queue<int>p;
queue<int>q;

int n;
int k1,k2,x;
int ans=0;
bool ok=false;

void input(){
    scanf("%d",&n);
    scanf("%d",&k1);
    for(int i=0;i<k1;i++){
        scanf("%d",&x);
        p.push(x);
    }
    scanf("%d",&k2);
    for(int i=0;i<k2;i++){
        scanf("%d",&x);
        q.push(x);
    }
}

void solve(){
    while(!p.empty()&&!q.empty()){
        ans++;
        if(ans==maxn) break;
        int pp=p.front(); p.pop();
        int qq=q.front(); q.pop();

        if(pp>qq){
            p.push(qq);
            p.push(pp);
        }
        else{
            q.push(pp);
            q.push(qq);
        }
    }
    if(ans==maxn) printf("-1\n");
    else{
        printf("%d ",ans);
        if(p.size()) printf("1\n");
        else printf("2\n");
    }
}
int main(){
    input();
    solve();
    return 0;

}

D. Soldier and Number Game
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two soldiers are playing a game. At the beginning first of them chooses a positive integern 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 integerx > 1, such that n is divisible byx and replacing n withn / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.

To make the game more interesting, first soldier chooses n of form a! / b! for some positive integera and b (a ≥ b). Here byk! we denote the factorial of k that is defined as a product of all positive integers not large thank.

What is the maximum possible score of the second soldier?

Input

First line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.

Then follow t lines, each contains pair of integersa and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value ofn for a game.

Output

For each game output a maximum score that the second soldier can get.

Sample test(s)
Input
2
3 1
6 3
Output
2
5

分析:先打表找出所有数的素因子的个数,然后求前缀和,此前缀和cost[a]即代表了a!的素因子之和,然后输出cost[a] - cost[b] 即可。
题目链接: http://codeforces.com/contest/546/problem/D
代码清单:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxn = 5000000 + 5;

int t,a,b;
ll cost[maxn];
bool isPrime[maxn];

void init(){
    memset(cost,0,sizeof(cost));
    memset(isPrime,true,sizeof(isPrime));
    isPrime[1]=false;
    for(int i=2;i<=maxn;i++){
        if(isPrime[i]){
            for(int j=i;j<=maxn;j+=i){
                int num=j;
                while(num%i==0){
                    cost[j]++;
                    num/=i;
                }
                isPrime[j]=false;
            }
        }
    }

    for(int i=2;i<=maxn;i++)
        cost[i]=cost[i]+cost[i-1];
}

void input(){
    scanf("%d%d",&a,&b);
}

void solve(){
    printf("%I64d\n",cost[a]-cost[b]);
}

int main(){
    init();
    scanf("%d",&t);
    while(t--){
        input();
        solve();
    }return 0;
}

E. Soldier and Traveling
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of thei-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by atmoving along at most one road.

Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.

Input

First line of input consists of two integers n andm (1 ≤ n ≤ 100,0 ≤ m ≤ 200).

Next line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100).

Next line contains n integers b1, b2, ..., bn (0 ≤ bi ≤ 100).

Then m lines follow, each of them consists of two integersp and q (1 ≤ p, q ≤ n,p ≠ q) denoting that there is an undirected road between citiesp and q.

It is guaranteed that there is at most one road between each pair of cities.

Output

If the conditions can not be met output single word "NO".

Otherwise output word "YES" and then n lines, each of them consisting of n integers. Number in thei-th line in the j-th column should denote how many soldiers should road from cityi to city j (ifi ≠ j) or how many soldiers should stay in cityi (if i = j).

If there are several possible answers you may output any of them.

Sample test(s)
Input
4 4
1 2 6 3
3 5 3 1
1 2
2 3
3 4
4 2
Output
YES
1 0 0 0 
2 0 0 0 
0 5 1 0 
0 0 2 1 
Input
2 0
1 2
2 1
Output
NO

分析:最大流。然而我并不知道此题是最大流,然而就算知道了我也并不知道怎么写。下面贡献一下我室友的代码。唉,图论方面的东西懂得太少了,是要抓紧学习了。
题目链接: http://codeforces.com/contest/546/problem/E
代码清单:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
#define INF  100000000
struct edge{
   int to;
   int cap;
   int rav;
};
vector<edge> G[205];
int level[205];
int iter[205];
int n;
void add_edge(int from,int to,int cap)
{
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s)
{
    queue<int> q;
    memset(level,-1,sizeof(level));
    level[s]=0;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++)
        {
            edge &e=G[u][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[u]+1;
                q.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t) return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rav].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0)
            flow+=f;
    }
}
int a[105];
int b[105];
int vis[105][105];
int main()
{
    int m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&b[i]);
    int sum=0;
    int sum1=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i];
        sum1+=b[i];
        add_edge(i,n+i,a[i]);
        add_edge(0,i,a[i]);
        add_edge(i,2*n+1,b[i]);
    }
    if(sum!=sum1)
    {
        printf("NO\n");
        return 0;
    }
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        add_edge(n+l,r,a[l]);
        add_edge(n+r,l,a[r]);
    }
    int ans=max_flow(0,2*n+1);
    if(sum!=ans) printf("NO\n");
    else
    {
        printf("YES\n");
        for(int i=n+1;i<=2*n;i++)
        {
            for(int j=0;j<G[i].size();j++)
            {
                edge &e=G[i][j];
                vis[i-n][e.to]=a[i-n]-e.cap;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                printf("%d",vis[i][j]);
                if(j==n) printf("\n");
                else printf(" ");
            }
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值