码迷,mamicode.com
首页 > 其他好文 > 详细

暑假练习赛 003 F Mishka and trip

时间:2016-08-09 22:06:20      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

F - Mishka and trip

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

Description

Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a sequence of vertices, such that first one is connected with the second, second is connected with third and so on, while the last one is connected with the first one again. Cycle may consist of a single isolated vertex.

Initially there are k cycles, i-th of them consisting of exactly vi vertices. Players play alternatively. Peter goes first. On each turn a player must choose a cycle with at least 2 vertices (for example, x vertices) among all available cycles and replace it by two cycles with p and x - p vertices where 1 ≤ p < x is chosen by the player. The player who cannot make a move loses the game (and his life!).

Peter wants to test some configurations of initial cycle sets before he actually plays with Dr. Octopus. Initially he has an empty set. In the i-th test he adds a cycle with ai vertices to the set (this is actually a multiset because it can contain two or more identical cycles). After each test, Peter wants to know that if the players begin the game with the current set of cycles, who wins?

Peter is pretty good at math, but now he asks you to help.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of tests Peter is about to make.

The second line contains n space separated integers a1, a2, ..., an (1 ≤ ai ≤ 109), i-th of them stands for the number of vertices in the cycle added before the i-th test.

Output

Print the result of all tests in order they are performed. Print 1 if the player who moves first wins or 2 otherwise.

Sample Input

 
Input
3
1 2 3
Output
2
1
1
Input
5
1 1 5 1 1
Output
2
2
2
2
2

Sample Output

 

Hint

In the first sample test:

In Peter‘s first test, there‘s only one cycle with 1 vertex. First player cannot make a move and loses.

In his second test, there‘s one cycle with 1 vertex and one with 2. No one can make a move on the cycle with 1 vertex. First player can replace the second cycle with two cycles of 1 vertex and second player can‘t make any move and loses.

In his third test, cycles have 1, 2 and 3 vertices. Like last test, no one can make a move on the first cycle. First player can replace the third cycle with one cycle with size 1 and one with size 2. Now cycles have 1, 1, 2, 2 vertices. Second player‘s only move is to replace a cycle of size 2 with 2 cycles of size 1. And cycles are 1, 1, 1, 1, 2. First player replaces the last cycle with 2 cycles with size 1 and wins.

In the second sample test:

Having cycles of size 1 is like not having them (because no one can make a move on them).

In Peter‘s third test: There a cycle of size 5 (others don‘t matter). First player has two options: replace it with cycles of sizes 1 and 4 or 2 and 3.

  • If he replaces it with cycles of sizes 1 and 4: Only second cycle matters. Second player will replace it with 2 cycles of sizes 2. First player‘s only option to replace one of them with two cycles of size 1. Second player does the same thing with the other cycle. First player can‘t make any move and loses.
  • If he replaces it with cycles of sizes 2 and 3: Second player will replace the cycle of size 3 with two of sizes 1 and 2. Now only cycles with more than one vertex are two cycles of size 2. As shown in previous case, with 2 cycles of size 2 second player wins.

So, either way first player loses.

Description

Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.

Here are some interesting facts about XXX:

  1. XXX consists of n cities, k of whose (just imagine!) are capital cities.
  2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
  3. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.
  4. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every 1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i.
  5. There is at most one road between any two cities.
  6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.

Mishka started to gather her things for a trip, but didn‘t still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road between a and b you are to find sum of products ca·cb. Will you help her?

Input

The first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them.

The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities.

The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.

Output

Print the only integer — summary price of passing each of the roads in XXX.

Sample Input

 
Input
4 1
2 3 1 2
3
Output
17
Input
5 2
3 5 2 2 4
1 4
Output
71

Sample Output

 

Hint

This image describes first sample case:

技术分享

It is easy to see that summary price is equal to 17.

This image describes second sample case:

技术分享

It is easy to see that summary price is equal to 71.

/*
下面是自己的代码,在第十组样例T了,虽然没想出来更好的优化方案,但是此次练习赛至少没看题解。下次要更加努力
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#define N 100010
using namespace std;
long long n,k,a[N],b[N],ok[N];
int main()
{
    //freopen("in.txt","r",stdin);
    memset(ok,0,sizeof ok);
    scanf("%lld%lld",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    for(int i=1;i<=k;i++)
    {
        scanf("%lld",&b[i]);
        ok[b[i]]=1;//标记主城
    }
    long long s=0;
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(b[i]==j) continue;
            if(ok[j])
                s+=a[j]*a[b[i]];
            else
                s+=a[j]*a[b[i]]*2;
        }
    }
    for(int i=1;i<n;i++)
    {
        if(ok[i]||ok[i+1])
            continue;
        s+=a[i]*a[i+1]*2;
    }
    if(ok[1]==0&&ok[n]==0)
        s+=a[1]*a[n]*2;
    printf("%lld\n",s/2);
    return 0;
}

下面是CF的题解

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iomanip>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
int dp[100005]={};
LL a[100005]={0};
LL b[100005];
int main()
{
    int n,k;
    LL sum=0,num=0,num2=0;
    scanf("%d %d",&n,&k);
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++)
    {
        scanf("%I64d",&a[i]);
        num+=a[i];
    }
    for(int j=0;j<k;j++)
    {
        scanf("%I64d",&b[j]);
        num2+=a[b[j]-1];
    }
    for(int x=0;x<k;x++)
    {
        dp[b[x]-1]=1;
        sum+=(num-a[b[x]-1])*a[b[x]-1];
        sum-=(num2-a[b[x]-1])*a[b[x]-1];
        num2-=a[b[x]-1];
    }
    for(int q=0;q<n-1;q++)
    {
        if(dp[q]!=1&&dp[q+1]!=1)
        {
            sum+=a[q]*a[q+1];
        }
    }
        if(!dp[n-1]&&!dp[0])
        sum+=a[0]*a[n-1];
    cout<<sum<<endl;
}

 

暑假练习赛 003 F Mishka and trip

标签:

原文地址:http://www.cnblogs.com/wuwangchuxin0924/p/5754621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!