Codeforces Round #257 (Div. 2)

A. Jzzhu and Children

There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.

Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:

  1. Give m candies to the first child of the line.
  2. If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home.
  3. Repeat the first two steps while the line is not empty.

Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?

Input

The first line contains two integers n, m (1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100).

Output

Output a single integer, representing the number of the last child.

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

Let's consider the first sample.

Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.

Child 4 is the last one who goes home.



#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=10010;
int a[maxn],b[maxn];
int n,m;
int main()
{

    int cnt=0;
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]%m)a[i]=a[i]/m+1;
        else a[i]/=m;
    }
    int ans=0,min1=-1;
    for(int i=0;i<n;i++)
    {
        if(a[i]>=min1)
        {
            ans=i;
            min1=a[i];
        }
    }
    printf("%d\n",ans+1);
    return 0;

}

D. Jzzhu and Cities

Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.

Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.

Input

The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).

Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ nui ≠ vi; 1 ≤ xi ≤ 109).

Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).

It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.

Output

Output a single integer representing the maximum number of the train routes which can be closed.

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

思路:引入一个最短路条数,然后再遍历火车线,如果最短路与火车线长度相等,此时如果最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,如果最短路条数大于1条,说明除了这条火车线外还有别的跟他同样短的路,可以去掉;如果火车线长度比最短路长的话,显然可以去掉。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=900010;
const long long INF=10000000000000;
struct node
{
    int u,next,f;
}edge[maxn];
int n,m,k,num;
int pre[maxn],cnt[maxn];
bool vis[maxn];
long long dis[maxn],res[maxn];
void init()
{
    num=0;
    memset(pre,-1,sizeof(pre));
    memset(cnt,0,sizeof(cnt));
}
void add(int x,int y,int f)
{
    edge[num].u=y;
    edge[num].next=pre[x];
    edge[num].f=f;
    pre[x]=num++;
}


void SPFA()
{
    priority_queue<int> q;
    for(int i=0;i<=n;i++)
    {
        dis[i]=INF;
        vis[i]=false;
    }
    dis[1]=0;
    vis[1]=true;
    cnt[1]=1;
    q.push(1);
    while(!q.empty())
    {
        int t=q.top();q.pop();
        vis[t]=0;
        for(int i=pre[t];i!=-1;i=edge[i].next)
        {
            int v=edge[i].u,w=edge[i].f;
            if(dis[v]>dis[t]+w)
            {
                dis[v]=dis[t]+w;
                cnt[v]=cnt[t];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
            else if(dis[v]==dis[t]+w)
            {
                cnt[v]+=cnt[t];
                if(cnt[v]>=2)cnt[v]=2;
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    init();
    while(m--)
    {
        int u,v,f;
        scanf("%d%d%d",&u,&v,&f);
        add(u,v,f);
        add(v,u,f);
    }
    for(int i=0;i<=n;i++)res[i]=INF;
    for(int i=1;i<=k;i++)
    {
        int s,y;
        scanf("%d%d",&s,&y);
        if(res[s]>y)res[s]=y;
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        if(res[i]!=INF)
        {
            add(1,i,res[i]);
            add(i,1,res[i]);
            sum++;
        }
    }
    SPFA();
    for(int i=1;i<=n;i++)
    {
        if(res[i]!=INF)
        {
            if(dis[i]==res[i]&&cnt[i]==2)sum--;
            else if(dis[i]<res[i])sum--;
        }

    }
    printf("%d\n",k-sum);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值