问题 D: Make a Forest

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

In 1736, Leonhard Euler wrote a paper on the Seven Bridges of Königsberg which is regarded as the first paper in the history of graph theory. Nowadays, the study of graph theory is considered very important as indicated by the fact that most textbooks in discrete mathematics have a chapter on graph theory.

This problem is related to graph theory, especially on tree and forest. Given N tuples (ui,vi,wi), your task is to construct a forest with a minimum number of trees which satisfies the following seven requirements:

  1. Each tree in the forest is a rooted tree;
  2. Each node x in the forest has a value x.A;
  3. Each edge (x, y) in the forest has a value (x, y).B;
  4. Each tuple (ui,vi,wi) appears exactly once in the forest as two nodes with a parent-child relationship (parent node p and child node c) where: ui = p.A, vi = c.A, and wi = (p, c).B;
  5. For any non-root and non-leaf node x in the forest, (p, x).B is smaller than any (x, c).B, where p is x’s parent and c is x’s child;
  6. All nodes in the forest have at most M children.
  7. The forest should contain exactly N edges.

To simplify the problem, it is guaranteed that wi in any tuple is unique, i.e. no two tuples with the same wi .

Output the number of trees in such forest (the forest should have the minimum number of trees).

输入

The first line contains two integers: N M(1 ≤ N,M ≤ 100,000) in a line denoting the number of tuples and the maximum number of children for each node in the forest. The next N following lines, each contains three integers: ui vi wi (1 ≤ ui,vi ≤ 2,000,000,000; 1 ≤ wi ≤ N ) in a line denoting the tuple (ui,vi,wi). It is guaranteed that there will be no two tuples with the same wi .

输出

The output contains an integer denoting the number of trees in a forest with a minimum number of trees which satisfies the given requirements, in a line.

样例输入

5 2

2 4 3

4 4 5

4 7 1

7 2 4

4 8 2

样例输出

2

提示

For the sample, this forest is the only forest which satisfies all the requirements. There are 2 trees in this forest.

On the other hand, this forest does not satisfy the requirements due to:

  1. Node b violates requirement #5 as (a,b).B = 3 is larger than (b,c).B = 1 and (b,d).B = 2.
  2. Node b violates requirement #6 as it has 3 children (note that M is 2).
  3. There are 6 edges in the forest while ! = 5 (violates requirement #7).

Note that violating even one requirement already makes the forest invalid.

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+10;
struct  node
{
    int u,v,w;
}G[N];
int n,m;
unordered_map<int,int>mp;
bool cmp(node a,node b)
{
    return  a.w<b.w;
}
int main()
{
    scanf ("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        int u,v,w;
        scanf ("%d%d%d",&G[i].u,&G[i].v,&G[i].w);
    }
    sort(G,G+n,cmp);
    int ans=0;
    for(int i=0;i<n;i++)
    {
        int u=G[i].u,v=G[i].v;
        if(!mp[u])
        {
            ans++;
            mp[u]+=m;
        }
        mp[u]--;
        mp[v]+=m;
    }
    printf ("%d\n",ans);
}

问题 C: Non-Interactive Guessing Number

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Romanos: “Can I submit an interactive problem to ACM-ICPC 2017 Jakarta?”

Theodora: “No.”

Romanos: “Aww, man.. That’s not fun.”

Then Romanos decided to submit a non-interactive version of his problem to the contest; and here it is, based on his playing with Theodora.

Romanos and Theodora are playing a game. Initially, Theodora picks a number between 1 to N inclusive. Romanos’ goal is to determine that number. He can make up to ! guesses. For each guess, he will say a number out loud as his guess. Theodora will then say one of the following answers based on the exact condition:

• “My number is smaller than your guess (<)”,

• “My number is greater than your guess (>)”, or

• “Your guess is correct (=)”.

The game will end right after one of the following:

• Romanos guesses the correct number (he wins), or

• All ! guesses are wrong (he loses).

Sadly, Romanos is not playing the game seriously. He knows the best strategy to win this game, but he does not always use it. Nevertheless, he pretends that he plays seriously, hence his guess will never be a dumb one. In other words, his guess will always be a number between 1 to N inclusive and will always be consistent with all of Theodora’s previous answers.

For example, suppose N is 10 and Romanos’ first guess is 4. If Theodora answers “My number is smaller than your guess (<)” then the next Romanos’s guess will always be between 1 to 3, inclusive.

Contrast to Romanos, Theodora is playing the game too seriously. She wants to win (by making Romanos lose) and “cheats” as follows.

Adaptively, Theodora might change her number as far as it is consistent with all of her previous answers. To do that, whenever possible, Theodora will always answer either “My number is smaller than your guess (<)” or “My number is greater than your guess (>)” that maximizes the possible answer range. In the case of tie, she will always answer the first one (<).

For example, suppose N is 10 and Romanos’ first guess is 4. Theodora will always answer “My number is greater than your guess (>)” because the possible answer range will be between 5 to 10 inclusive; it is larger than 1 to 3 inclusive.

Now, this is the actual problem proposed by Romanos. You are given N,K , and Theodora’s answer for each guess. Can you show one of the possible scenario for Romanos’ guesses or state that it is impossible?

输入

The first line contains two integers: N K(1 ≤N≤ 1018; 1 ≤K≤ 50,000) in a line denoting the number range and the number of guesses. The second line contains a string in a line denoting Theodora’s answers. Each character of the string is either ‘<‘, ‘>’, or ‘=’, and either:

• The last character of the string is ‘=’ and each character of the string other than the last character is either ‘<‘ or ‘>’, and the length of the string is not more than K , or

• The length of the string is exactly K and each character of the string is either ‘<‘ or ‘>’.

输出

If there is a possible scenario for Romanos’ guesses,output YES,else output NO.

样例输入

10 5

><>=

样例输出

YES

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn=5e4+10;
ll num;
ll n,k;
char op[maxn];
int judge(ll l,ll r,int p){
    int cnt=0;
    while(l<r){
        //cout<<l<<"   "<<r<<endl;
        if(op[p]=='=') break;
        cnt++;
        ll mid=(l+r)>>1;
        if((r-l+1)&1){
            if(op[p]=='>') l=mid;
            else r=mid-1;
        }
        else{
            if(op[p]=='>') l=mid+1;
            else r=mid;
        }
        p++;
        if(p>num) break;
    }
    //cout<<l<<" "<<r<<endl;
    if(l<r) return inf;
    //cout<<cnt<<endl;
    return cnt;
}
bool check(){
    if(num>n)return false;
    if(k>=n&&op[n]!='=') return false;
    if(op[num]=='='&&num<judge(1ll,n,1)) return false;
    return true;
}
int main()
{
    while(~scanf("%lld %lld",&n,&k)){
        scanf("%s",op+1);
        num=strlen(op+1);
        //cout<<num<<endl;
        if(check()){
            printf("YES\n");
        }
        else{
            printf("NO\n");
        }
    }
}

问题 A: Winning ICPC

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 命题人:admin

题目描述

There are N teams (numbered from 1 to N ) and M problems (numbered from 1 to N ) in this year’s ICPC. The j-th problem has Tj testcases. Surprisingly, every team submitted exactly one solution to every problem. The N -th team managed to solve Sij testcases on the N -th problem.

A team solved a problem only if the team managed to solve ALL testcases on that problem. The winning team is the team with the most number of problems solved. If there are more than one team with the most number of problems solved, then the winning team is the team with the smallest index among those teams.

Determine the index of the winning team.

输入

The first line contains two integers: N M (1 ≤ N ,M ≤ 100) in a line denoting the number of teams and the number of problems. The second line contains M integers: T1 T2…TM(0 ≤ Ti ≤ 100) in a line denoting the number of testcases. The next N following lines, each contains M integers; the j-th integer on the i-th line is Sij (0 ≤ Sij ≤ Tj ) denoting the number of solved testcases by the i-th team for the j-th problem.

输出

The output contains the index of the winning team, in a line.

样例输入

3 2

10 20

0 19

10 0

9 19

样例输出

2

提示

On the first sample, the first and the third team did not solve any problem, and the second team solved the first problem. Therefore, the second team is the winner.

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int N=105;
int n,m;
int stand[N];
struct node
{
    int no,sol;
}ans[N];
bool cmp(node a,node b)
{
    if(a.sol!=b.sol) return a.sol>b.sol;
    return  a.no<b.no;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d",&stand[i]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int temp;
            scanf("%d",&temp);
            if(temp==stand[j]) ans[i].sol++;
        }
        ans[i].no=i;
    }
    sort(ans+1,ans+1+n,cmp);
    printf("%d\n",ans[1].no);
}

问题 E: Similarity of Subtrees

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Define the depth of a node in a rooted tree by applying the following rules recursively:

·The depth of a root node is 0.

·The depths of child nodes whose parents are with depth d are d+1.

Let S(T,d) be the number of nodes of T with depth d. Two rooted trees T and T′ are similar if and only if S(T,d) equals S(T′,d) for all non-negative integer d.

You are given a rooted tree T with N nodes. The nodes of T are numbered from 1 to N. Node 1 is the root node of T. Let Ti be the rooted subtree of T whose root is node i. Your task is to write a program which calculates the number of pairs (i,j) such that Ti and Tj are similar and i<j.

输入

The input consists of a single test case.

N

a1 b1

a2 b2

aN−1 bN−1

The first line contains an integer N (1≤N≤100,000), which is the number of nodes in a tree. The following N−1 lines give information of branches: the i-th line of them contains ai and bi, which indicates that a node ai is a parent of a node bi. (1≤ai,bi≤N,ai≠bi) The root node is numbered by 1. It is guaranteed that a given graph is a rooted tree, i.e. there is exactly one parent for each node except the node 1, and the graph is connected.

输出

Print the number of the pairs (x,y) of the nodes such that the subtree with the root x and the subtree with the root y are similar and x<y.

样例输入

5

1 2

1 3

1 4

1 5

样例输出

6

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
typedef pair<int,int>P;
const int Hash=131;
const int mod = 1e9+7;
vector<ll>vv;
map<ll,ll>mmap;
ll HASH[maxn];
struct node
{
    int to,index;
}G[maxn];
int n;
int head[maxn],cnt;
void add(int u,int v)
{
    G[++cnt].to=v;
    G[cnt].index=head[u];
    head[u]=cnt;
}
 
void dfs(int x)
{
    HASH[x]=1;
    for(int i=head[x];i;i=G[i].index)
    {
        int v=G[i].to;
        dfs(v);
        HASH[x]+=HASH[v]*Hash;
    }
    if(!mmap[HASH[x]]) vv.push_back(HASH[x]);
    mmap[HASH[x]]++;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    dfs(1);
    ll ans=0,number;
    for(int i=0;i<vv.size();i++){
            //cout<<vv[i]<<endl;
        number=mmap[vv[i]];
        //cout<<number<<endl;
        ans+=(number-1ll)*number/2ll;
    }
    printf("%lld\n",ans);
    return 0;
}

问题 D: Parentheses

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Dave loves strings consisting only of ‘(‘ and ‘)’. Especially, he is interested in balanced strings. Any balanced strings can be constructed using the following rules:

A string “()” is balanced.

Concatenation of two balanced strings are balanced.

If T is a balanced string, concatenation of ‘(‘, T, and ‘)’ in this order is balanced. For example, “()()” and “(()())” are balanced strings. “)(” and “)()(()” are not balanced strings.

Dave has a string consisting only of ‘(‘ and ‘)’. It satises the followings:

You can make it balanced by swapping adjacent characters exactly A times.

For any non-negative integer B (B<A), you cannot make it balanced by B swaps of adjacent characters.

It is the shortest of all strings satisfying the above conditions.

Your task is to compute Dave’s string. If there are multiple candidates, output the minimum in lexicographic order. As is the case with ASCII, ‘(‘ is less than ‘)’.

输入

The input consists of a single test case, which contains an integer A (1≤A≤109).

输出

Output Dave’s string in one line. If there are multiple candidates, output the minimum in lexicographic order.

样例输入

1

样例输出

)(

提示

There are infinitely many strings which can be balanced by only one swap. Dave’s string is the shortest of them.

#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int a;
    cin>>a;
    int n=int((sqrt (1ll*8*a)-1)*1.0/2);
    int m=n+1;
    if(a==1) {
        printf (")(\n");
    }
    else if(a==2) {
        printf (")()(\n");
    }
    else {
        int cnt=a-n*(n+1)/2;
        int sum=m*2;
        int cnta=m,cntb=m;
        for(int i=1;i<=cnt;i++) printf (")"),sum--,cnta--;
        printf ("(");
        cntb--;
        while (cnta--) {
            printf (")");
        }
        while (cntb--) {
            printf ("(");
        }
        printf ("\n");
    }
    return 0;
}

问题 C: We don’t wanna work!

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

ACM is an organization of programming contests. The purpose of ACM does not matter to you. The only important thing is that workstyles of ACM members are polarized: each member is either a workhorse or an idle fellow.

Each member of ACM has a motivation level. The members are ranked by their motivation levels: a member who has a higher motivation level is ranked higher. When several members have the same value of motivation levels, the member who joined ACM later have a higher rank. The top 20% highest ranked members work hard, and the other (80%) members never (!) work. Note that if 20% of the number of ACM members is not an integer, its fraction part is rounded down.

You, a manager of ACM, tried to know whether each member is a workhorse or an idle fellow to manage ACM. Finally, you completed to evaluate motivation levels of all the current members. However, your task is not accomplished yet because the members of ACM are dynamically changed from day to day due to incoming and outgoing of members. So, you want to record transitions of members from workhorses to idle fellows, and vice versa.

You are given a list of the current members of ACM and their motivation levels in chronological order of their incoming date to ACM. You are also given a list of incoming/outgoing of members in chronological order.

Your task is to write a program that computes changes of workstyles of ACM members.

输入

The first line of the input contains a single integer N (1≤N≤50,000) that means the number of initial members of ACM. The (i + 1)-th line of the input contains a string si and an integer ai (0≤ai≤105), separated by a single space. si means the name of the i-th initial member and ai means the motivation level of the i-th initial member. Each character of si is an English letter, and 1≤|si|≤20. Note that those N lines are ordered in chronological order of incoming dates to ACM of each member.

The (N + 2)-th line of the input contains a single integer M (1≤M≤20,000) that means the number of changes of ACM members. The (N + 2 + j)-th line of the input contains information of the j-th incoming/outgoing member. When the j-th information represents an incoming of a member, the information is formatted as “+tjbj”, where tj is the name of the incoming member and bj (0≤bj≤105) is his motivation level. On the other hand, when the j-th information represents an outgoing of a member, the information is formatted as “−tj”, where tj means the name of the outgoing member. Each character of tj is an English letter, and 1≤|tj|≤20. Note that uppercase letters and lowercase letters are distinguished. Note that those M lines are ordered in chronological order of dates when each event happens.

No two incoming/outgoing events never happen at the same time. No two members have the same name, but members who left ACM once may join ACM again.

输出

Print the log, a sequence of changes in chronological order. When each of the following four changes happens, you should print a message corresponding to the type of the change as follows:

Member name begins to work hard : “name is working hard now.”

Member name begins to not work : “name is not working now.”

For each incoming/outgoing, changes happen in the following order:

Some member joins/leaves.

When a member joins, the member is added to either workhorses or idle fellows.

Some member may change from a workhorse to an idle fellow and vice versa. Note that there are no cases such that two or more members change their workstyles at the same time.

样例输入

4

Durett 7

Gayles 3

Facenda 6

Daughtery 0

1

+ Mccourtney 2

样例输出

Mccourtney is not working now.

Durett is working hard now.

提示

Initially, no member works because 4×20% <1. When one member joins ACM, Durrett begins to work hard.

set

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6+10;
struct node
{
    string name;
    int id;
    int timee;
}preson[maxn];
bool cmp (node a,node b){
    if(a.id==b.id){
        return a.timee>b.timee;
    }
    return a.id>b.id;
}
struct cmpone{
    bool operator () (const node a,const node b)const{
        if(a.id==b.id)
            return a.timee<b.timee;
        return a.id<b.id;
    }
};
struct cmptwo{
    bool operator () (const node a,const node b)const{
        if(a.id==b.id)
            return a.timee>b.timee;
        return a.id>b.id;
    }
};
set<node,cmpone>setone;
set<node,cmptwo>settwo;
map<string,node>mmap;
char option;
string s;
int power;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        preson[i].timee=i;
        cin>>preson[i].name>>preson[i].id;
        mmap[preson[i].name]=preson[i];
    }
    sort(preson+1,preson+1+n,cmp);
    int hnum=floor(0.2*n);
    for(int i=1;i<=hnum;i++){
        setone.insert(preson[i]);
    }
    for(int i=hnum+1;i<=n;i++){
        settwo.insert(preson[i]);
    }
    int number=n;
    int numbern=n;
    int m;
    node tmp;
    cin>>m;
    while(m--){
        cin>>option;
        numbern++;
        if(option=='-'){
            cin>>s;
            tmp=mmap[s];
            if(setone.erase(tmp)) hnum--;
            settwo.erase(tmp);
            number--;
            if(hnum>floor(0.2*number)){
                hnum--;
                tmp = *setone.begin();
                setone.erase(tmp);
                settwo.insert(tmp);
                cout<<tmp.name<<" is not working now."<<endl;
            }
            if(hnum<floor(0.2*number)){
                hnum++;
                tmp = *settwo.begin();
                setone.insert(tmp);
                settwo.erase(tmp);
                cout<<tmp.name<<" is working hard now."<<endl;
            }
        }
        else{
            cin>>s>>power;
            preson[numbern].name=s;
            preson[numbern].id=power;
            preson[numbern].timee=numbern;
            number++;
            mmap[s]=preson[numbern];
            if(hnum<floor(0.2*number)){
                tmp=*settwo.begin();
                if(preson[numbern].id>tmp.id||(preson[numbern].id==tmp.id&&preson[numbern].timee>tmp.timee)){
                    setone.insert(preson[numbern]);
                    cout<<preson[numbern].name<<" is working hard now."<<endl;
                }
                else{
                    setone.insert(tmp);
                    settwo.erase(tmp);
                    settwo.insert(preson[numbern]);
                    cout<<preson[numbern].name<<" is not working now."<<endl;
                    cout<<tmp.name<<" is working hard now."<<endl;
                }
                hnum++;
            }
            else{
                if(hnum!=0){
                    tmp=*setone.begin();
                    if(preson[numbern].id>tmp.id||(preson[numbern].id==tmp.id&&preson[numbern].timee>tmp.timee)){
                        setone.erase(tmp);
                        setone.insert(preson[numbern]);
                        settwo.insert(tmp);
                        cout<<preson[numbern].name<<" is working hard now."<<endl;
                        cout<<tmp.name<<" is not working now."<<endl;
                    }
                    else{
                        settwo.insert(preson[numbern]);
                        cout<<preson[numbern].name<<" is not working now."<<endl;
                    }
                }else{
                    tmp=*settwo.begin();
                    if(floor(0.2*number)>0){
                        if(preson[numbern].id>tmp.id||(preson[numbern].id==tmp.id&&preson[numbern].timee>tmp.timee)){
                            setone.insert(preson[numbern]);
                            cout<<preson[numbern].name<<" is working hard now."<<endl;
                        }
                        else{
                            settwo.erase(tmp);
                            settwo.insert(preson[numbern]);
                            setone.insert(tmp);
                            cout<<preson[numbern].name<<" is not working now."<<endl;
                            cout<<tmp.name<<" is working hard now."<<endl;
                        }
                    }
                    else{
                        settwo.insert(preson[numbern]);
                        cout<<preson[numbern].name<<" is not working now."<<endl;
                    }
                }
            }
        }
    }
    return 0;
}

问题 B: Help the Princess!

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not.

For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace.

The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace.

If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace?

输入

Each dataset is formatted as follows.

H W

map1

map2

mapH

The first line of a dataset contains two positive integers H and W delimited by a space, where H is the height of the grid and W is the width of the grid (2≤H,W≤200).

The i-th line of the subsequent H lines gives a string mapi, which represents situation in the ground of palace.

mapi is a string of length W, and the j-th character of mapi represents the state of the cell of the i-th row and the j-th column.

‘@’, ‘$’, ‘%’, ‘.’, and ‘#’ represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one ‘@’, only one ‘%’, and more than or equal to zero ‘$’ in the grid.

输出

Output a line containing a word “Yes”, if the princess can escape from the palace. Otherwise, output “No”.

样例输入

2 4

%.@$

..$$

样例输出

Yes

#include <bits/stdc++.h>
 
using namespace std;
const int N=205;
typedef pair<int,int>P;
 
int sodp;
char mp[N][N];
int h,w;
P pr,en,sod[N*N];
int ar[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int dist[N][N];
bool vis[N][N];
void bfs()
{
    vis[en.first][en.second]=true;
    dist[en.first][en.second]=0;
    queue<P>que;
    que.push(en);
    while(que.size())
    {
        P tmp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int ax=tmp.first+ar[i][0],ay=tmp.second+ar[i][1];
            if(!vis[ax][ay] && mp[ax][ay]!='#' && ax>0 && ay>0 && ax<=h && ay<=w)
            {
                vis[ax][ay]=true;
                dist[ax][ay]=dist[tmp.first][tmp.second]+1;
                que.push(P(ax,ay));
            }
        }
    }
}
int main()
{
    scanf ("%d%d",&h,&w);
    for(int i=1;i<=h;i++) scanf ("%s",mp[i]+1);
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            if(mp[i][j]=='@') pr=P(i,j);
            else if(mp[i][j]=='%') en=P(i,j);
            else if(mp[i][j]=='$') sod[sodp++]=P(i,j);
        }
    }
    bfs();
    if(dist[pr.first][pr.second]==0)
    {
        puts("No");
        return 0;
    }
    for(int i=0;i<sodp;i++)
    {
        int x=sod[i].first,y=sod[i].second;
        if(dist[x][y]<=dist[pr.first][pr.second])
        {
            puts("No");
            return 0;
        }
    }
    puts("Yes");
    return 0;
}

问题 A: Best Matched Pair

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

You are working for a worldwide game company as an engineer in Tokyo. This company holds an annual event for all the staff members of the company every summer. This year’s event will take place in Tokyo. You will participate in the event on the side of the organizing staff. And you have been assigned to plan a recreation game which all the participants will play at the same time.

After you had thought out various ideas, you designed the rules of the game as below.

Each player is given a positive integer before the start of the game.

Each player attempts to make a pair with another player in this game, and formed pairs compete with each other by comparing the products of two integers.

Each player can change the partner any number of times before the end of the game, but cannot have two or more partners at the same time.

At the end of the game, the pair with the largest product wins the game.

In addition, regarding the given integers, the next condition must be satisfied for making a pair.

The sequence of digits obtained by considering the product of the two integers of a pair as a string must be increasing and consecutive from left to right. For example, 2, 23, and 56789 meet this condition, but 21, 334, 135 or 89012 do not.

Setting the rules as above, you noticed that multiple pairs may be the winners who have the same product depending on the situation. However, you can find out what is the largest product of two integers when a set of integers is given.

Your task is, given a set of distinct integers which will be assigned to the players, to compute the largest possible product of two integers, satisfying the rules of the game mentioned above.

输入

The input consists of a single test case formatted as follows.

N

a1 a2 … aN

The first line contains a positive integer N which indicates the number of the players of the game. N is an integer between 1 and 1,000. The second line has N positive integers that indicate the numbers given to the players. For i=1,2,…,N−1, there is a space between ai and ai+1. ai is between 1 and 10,000 for i=1,2,…,N, and if i≠j, then ai≠aj.

输出

Print the largest possible product of the two integers satisfying the conditions for making a pair. If any two players cannot make a pair, print -1.

样例输入

2

1 2

样例输出

2

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e3+10;
 
int a[N];
 
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    int ans=-1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) continue;
            int tmp=a[i]*a[j];
            vector<int>t;
            while(tmp)
            {
                t.push_back(tmp%10);
                tmp/=10;
            }
            reverse(t.begin(),t.end());
            bool flag=false;
            for(int k=0;k<t.size()-1;k++)
            {
                if(t[k+1]-t[k]!=1)
                {
                    flag=true;
                    break;
                }
            }
            if(!flag) ans=max(ans,a[i]*a[j]);
        }
    }
    printf("%d\n",ans);
    return 0;
}

问题 D: Daily Division

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Oostende Beach is a very long beach located in the north of Belgium. On this beach, there are n huts located along a straight line. People can rent a room in one of those huts to spend their beach vacations together with the other

tenants.

Every day at lunch time, a food truck rides by to serve fries to the guests. The truck will park in front of one of the huts and people will form two queues. The people staying in huts to the left of the food truck will queue on the left,and the people to the right of the food truck will queue together on the right. The people staying in the hut in front of the food truck will split their group in half, one half going to the left queue and the other half going to the right queue. If this is an odd number of people,the remaining person will go to the queue with fewer people, or choose one randomly if the queues have the same length. The food truck will always position itself so that the difference between the number of people in the left queue and the number of people in the right queue is as small as possible.

Each night the number of guests in exactly one of the huts changes. Can you help the food truck find the best position for each day?

输入

• The first line of the input consists of two integers 1 ≤ n ≤ 105, the number of huts, and 1 ≤ q ≤ 105, the number of days.

• The second line has n integers a0, . . . , an−1 satisfying 1 ≤ ai ≤ 106 for 0 ≤ i < n, where ai is the current number of people in hut i.

• Then follow q lines with two integers 0 ≤ i < n and 1 ≤ x ≤ 106. The jth of these lines indicates that at day j the number of people in hut i changes to x .

输出

Print q lines: the optimal position of the foodtruck after each of the q nights. If there are multiple optimal positions, print the smallest one.

样例输入

5 4

3 1 3 4 2

0 5

0 9

4 5

2 1

样例输出

2

1

2

1

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 1e5+10;

ll a[maxn];
ll sum[maxn*4];

void build(ll l,ll r,ll rt) {
    if(l==r) {
        sum[rt]=a[l];
        return;
    }
    ll mid=(l+r)/2;
    build (l,mid,rt*2);
    build (mid+1,r,rt*2+1);
    sum[rt]=sum[rt*2]+sum[rt*2+1];
}

void update(ll p,ll v,ll l,ll r,ll rt){
    if(l==r) {
        sum[rt]+=ll(v);
        return;
    }
    ll mid=(l+r)/2;
    if(p<=mid) update (p,v,l,mid,rt*2);
    if(p>mid) update (p,v,mid+1,r,rt*2+1);
    sum[rt]=sum[rt*2]+sum[rt*2+1];
}

ll query(ll l1,ll r1,ll l,ll r,ll rt) {
    if(l1<=l&&r1>=r) return sum[rt];
    ll mid=(l+r)/2;
    ll ret=0;
    if(l1<=mid) ret+=query (l1,r1,l,mid,rt*2);
    if(r1>mid) ret+=query (l1,r1,mid+1,r,rt*2+1);
    return ret;
}

int main () {
    ll n,q;
    scanf ("%lld %lld",&n,&q);
    for(ll i=1;i<=n;i++) scanf ("%lld",&a[i]);
    build (1,n,1);
    while (q--) {
        ll id,x;
        scanf ("%lld %lld",&id,&x);
        if(x!=a[id+1])
        {
            ll cc=x-a[id+1];
            update (id+1,cc,1,n,1);
            a[id+1]=x;
        }
        ll l=1,r=n;
        ll ans=n+1;
        ll ss=ll(1e18);
        while (l<r) {
            ll mid=(l+r)/2;
            //if(mid==1) break;
            //if(mid==n) break;
            ll s1,s2;
            if(mid>1) s1=query (1,mid-1,1,n,1);
            else s1=0;
            if(mid<n) s2=query (mid+1,n,1,n,1);
            else s2=0;
            ll s=a[mid];
            s1=s1+ll(s/2),s2=s2+ll(s/2);
            if(s%2==1) {
                if(s1<=s2) s1+=1;
                else s2+=1;
            }
            ll sss=ll(abs(s1-s2));
            if(sss<ss) {
                ans=mid;
                ss=sss;
            }
            else if(sss==ss) ans=min(ans,mid);
            if(s1<s2) l=mid+1;
            else if(s1>=s2) r=mid;
            //cout<<s1<<' '<<s2<<endl;
        }
        for(ll i=ans-1;i<=ans+1;i++) {
            ll s1,s2;
            if(i>1) s1=query (1,i-1,1,n,1);
            else s1=0;
            if(i<n) s2=query (i+1,n,1,n,1);
            else s2=0;
            ll s=a[i];
            s1=s1+ll(s/2),s2=s2+ll(s/2);
            if(s%2==1) {
                if(s1<=s2) s1+=1;
                else s2+=1;
            }
            ll sss=ll(abs(s1-s2));
            if(sss<ss) {
                ans=i;
                ss=sss;
            }
            else if(sss==ss) ans=min(ans,i);
        }
        printf ("%lld\n",ans-1);
    }
    return 0;
}
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>

using namespace std;
typedef  long long ll;
const int N  = 100010;
typedef pair<int,int>P;

ll ar[N];
int n,q;
ll nowp[N];
inline int lowbit(int x)
{
    return x&(-x);
}

inline void add(int i,ll v)
{
    for(;i<N;ar[i]+=v,i+=lowbit(i));
}

inline ll sum(int i)
{
    ll ans=0;
    for(;i>0;ans+=ar[i],i-=lowbit(i));
    return ans;
}

inline bool check(int x)
{
    ll suml=sum(x-1),sumr=sum(n)-suml-nowp[x];
    if(suml-sumr<0) return false;
    return true;
}
int searc()
{
    int l=1,r=n;
    while(r-l>1)
    {
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid;
    }
    //cout<<l<<' '<<r<<endl;
    ll suml1=sum(l-1),sumr1=sum(n)-suml1-nowp[l];
    ll suml2=sum(r-1),sumr2=sum(n)-suml2-nowp[r];
    if(nowp[l]&1)
    {
        if(suml1<sumr1) suml1++;
        else sumr1++;
    }
    if(nowp[r]&1)
    {
        if(suml2<sumr2) suml2++;
        else sumr2++;
    }
    if(abs(suml1-sumr1)<=abs(suml2-sumr2)) return l;
    return r;
}
int main()
{
    //freopen("a.txt","r",stdin);
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&nowp[i]);
        add(i,nowp[i]);
    }
    while(q--)
    {
        int x;ll now;
        scanf("%d%lld",&x,&now);
        x++;
        add(x,now-nowp[x]);
        nowp[x]=now;
        int ans=searc();
        printf("%d\n",ans-1);
    }
    return 0;
}

通过 WordPress.com 设计一个这样的站点
从这里开始