2016 ACM/ICPC Asia Regional Qingdao Online
2017-11-16 21:03
495 查看
5885待搞
5886树型DP,实在懒得写。。。太套路了
5891不会
5878:
I Count Two Three
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2218 Accepted Submission(s): 974
Problem Description
I will show you the most popular board game in the Shanghai Ingress Resistance Team.
It all started several months ago.
We found out the home address of the enlightened agent Icount2three and decided to draw him out.
Millions of missiles were detonated, but some of them failed.
After the event, we analysed the laws of failed attacks.
It's interesting that the i-th
attacks failed if and only if i can
be rewritten as the form of 2a3b5c7d which a,b,c,d are
non-negative integers.
At recent dinner parties, we call the integers with the form 2a3b5c7d "I
Count Two Three Numbers".
A related board game with a given positive integer n from
one agent, asks all participants the smallest "I Count Two Three Number" no smaller than n.
Input
The first line of input contains an integer t (1≤t≤500000),
the number of test cases. t test
cases follow. Each test case provides one integer n (1≤n≤109).
Output
For each test case, output one line with only one integer corresponding to the shortest "I Count Two Three Number" no smaller than n.
Sample Input
10 1 11 13 123 1234 12345 123456 1234567 12345678 123456789
Sample Output
1 12 14 125 1250 12348 123480 1234800 12348000 123480000
Source
2016 ACM/ICPC Asia Regional Qingdao Online
水题,要求输出大于等于n的第一个满足因子只为2,3,5,7的数。
生成所有数,排序即可。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=100005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); ll a[maxn]; int main() { int n,i; a[0]=1; ll d[4]={2,3,5,7}; queue<ll> q; map<ll,int> mp; mp[1]=1; q.push(1); n=1; ll last=0,f; while (!q.empty()) { ll now=q.front(); q.pop(); for (i=0;i<4;i++) { if ((f=now*d[i])<=1e9) { if (!mp[f]) { a[n++]=f; q.push(f); mp[f]=1; } } else last=min(last,f); } } a[n++]=last; sort(a,a+n); int cas; scanf("%d",&cas); while (cas--) { ll x,ans; scanf("%lld",&x); ans=lower_bound(a,a+n,x)-a; printf("%lld\n",a[ans]); } return 0; }
5879:
Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3553 Accepted Submission(s): 892
Problem Description
Given an integer n,
we only want to know the sum of 1/k2 where k from 1 to n.
Input
There are multiple cases.
For each test case, there is a single line, containing a single positive integer n.
The input file is at most 1M.
Output
The required sum, rounded to the fifth digits after the decimal point.
Sample Input
1 2 4 8 15
Sample Output
1.00000 1.25000 1.42361 1.52742 1.58044
Source
2016 ACM/ICPC Asia Regional Qingdao Online
水题。输入n,输出1~n的1/K^2的级数和。
数很大时结果是收敛的,直接输出结果即可。其他情况预处理之后O(1)输出
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <iomanip> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) #define N 1000000 using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=120005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); ld sum[N+5]; int main() { ll i; sum[0]=0L; for (i=1;i<=N;i++) { sum[i]=sum[i-1]+1/(ld)(i*i); } ll n; ld ans=pi*pi/6.0L; string s; cout << setiosflags(ios::fixed) << setprecision(5); while (getline(cin,s)) { if (s.length()>=7) n=N; else { n=0;int len=s.length(); for (i=0;i<len;i++) n=n*10+(s[i]-'0'); if (n>N) n=N; } cout << sum << endl; } return 0; }
5880:
Family View
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2182 Accepted Submission(s): 473
Problem Description
Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and social networking services. A family view can help you to prevent your children access to some content which are not suitable
for them.
Take an MMORPG game as an example, given a sentence T, and a list of forbidden words {P}, your job is to use '*' to subsititute all the characters, which is a part of the substring matched with at least one forbidden word in the list (case-insensitive).
For example, T is: "I love Beijing's Tiananmen, the sun rises over Tiananmen. Our great leader Chairman Mao, he leades us marching on."
And {P} is: {"tiananmen", "eat"}
The result should be: "I love Beijing's *********, the sun rises over *********. Our gr*** leader Chairman Mao, he leades us marching on."
Input
The first line contains the number of test cases. For each test case:
The first line contains an integer n,
represneting the size of the forbidden words list P.
Each line of the next n lines
contains a forbidden words Pi (1≤|Pi|≤1000000,∑|Pi|≤1000000) where Pi only
contains lowercase letters.
The last line contains a string T (|T|≤1000000).
Output
For each case output the sentence in a line.
Sample Input
1 3 trump ri o Donald John Trump (born June 14, 1946) is an American businessman, television personality, author, politician, and the Republican Party nominee for President of the United States in the 2016 election. He is chairman of The Trump Organization, which is the principal holding company for his real estate ventures and other business interests.
Sample Output
D*nald J*hn ***** (b*rn June 14, 1946) is an Ame**can businessman, televisi*n pers*nality, auth*r, p*litician, and the Republican Party n*minee f*r President *f the United States in the 2016 electi*n. He is chairman *f The ***** *rganizati*n, which is the p**ncipal h*lding c*mpany f*r his real estate ventures and *ther business interests.
Source
2016 ACM/ICPC Asia Regional Qingdao Online
给你一段话和一些敏感词,叫你用*隐藏所有敏感词。
很明显的一个AC自动机。匹配之后,用前缀和维护某个字符是否被隐藏,对于每个匹配的词,在词的开头标记。
容易MLE,不能用指针。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=1000005,maxk=26,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int q[maxn],b[maxn]; char t[maxn]; int num; struct node{ int fail,next[maxk]; int cnt,len; void init() { fail=-1; for (int i=0;i<maxk;i++) next[i]=-1; cnt=len=0; } }; node a[maxn]; void insert(int root,int len) { int now=root; int i; for (i=0;i<len;i++) { int pos=t[i]-'a'; if (a[now].next[pos]==-1) { a[now].next[pos]=++num; a[num].init(); } now=a[now].next[pos]; } a[now].cnt++; a[now].len=max(a[now].len,len); } void buildfail(int root) { int p=root; int front=0,tail=0,i; for (i=0;i<maxk;i++) { if (a[p].next[i]!=-1) { a[a[p].next[i]].fail=root; q[tail++]=a[p].next[i]; } else a[p].next[i]=root; } while (front<tail) { p=q[front]; for (i=0;i<maxk;i++) { if (a[p].next[i]==-1) a[p].next[i]=a[a[p].fail].next[i]; else { a[a[p].next[i]].fail=a[a[p].fail].next[i]; q[tail++]=a[p].next[i]; } } front++; } } void search(string s,int root,int len) { int k=0,i; int p=root,now; for (i=0;i<len;i++) { if (s[i]>='a'&&s[i]<='z') p=a[p].next[s[i]-'a']; else { p=root;continue; } now=p; while (now!=root&&a[now].cnt!=-1) { if (a[now].cnt) { b[i-a[now].len+1]++; b[i+1]--; } now=a[now].fail; } } } int main() { num=0; string s,w; int cas; scanf("%d",&cas); while (cas--) { int n,i,j,len; mem0(b); scanf("%d",&n); num=0; a[0].init(); for (i=1;i<=n;i++) { scanf("%s",t); len=strlen(t); insert(0,len); } getchar(); getline(cin,s); len=s.length();w=s; for (i=0;i<len;i++) { if (s[i]>='A'&&s[i]<='Z') s[i]=(char)('a'-'A'+s[i]); } buildfail(0); search(s,0,len); int sum=0; for (i=0;i<len;i++) { sum+=b[i]; if (sum>0) printf("*"); else printf("%c",w[i]); } printf("\n"); } return 0; }
5881:
Tea
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1968 Accepted Submission(s): 572
Problem Description
Tea is good.
Tea is life.
Tea is everything.
The balance of tea is a journey of pursuing balance of the universe.
Alice knows that.
Alice wants to teach you the art of pouring tea.
Alice has a pot of tea.
The exact volume of tea is not important.
The exact volume of tea is at least L.
The exact volume of tea is at most R.
Alice put two empty cups between you and her.
Alice wants the two cups filled by almost equal volume of tea.
Yours cannot be 1 unit
more than hers.
Hers cannot be 1 unit
more than yours.
Alice wants you to pour the tea.
Alice wants you to pour until the pot is almost empty.
Alice wants no more than 1 unit
volume of tea remaining in the pot.
You cannot read the residue volume of tea remaining in the pot.
You can only know the tea status in the pot, empty or not.
Alice does not want you to pour the tea too many times.
You better pour as few times as possible.
Input
There are multiple cases.
For each case, there is one line of two integers L and R,
separated by single space.
Here are some analyses about sample cases.
For the first case, pouring 1 unit
into one cup will satisfy Alice.
For the second case, it is clearly that you cannot only pour once to reach the desired balance, but she can achieve it by pouring twice.
First you pour 1.5 units
into one cup, then you attempt to pour another 1.5 units
into the other cup.
Since the lower bound is 2,
at least 0.5 unit
remains in the pot after the first pouring.
If the initial volume is in range [2,3],
the second cup will have volume in range [0.5,1.5] which
is balanced with 1.5 unit
in the first cup, and at most 1 unit
remain after these two attempts.
About 1000 test
cases, and 0≤L≤R≤1016.
Output
For each case, there should be a single integer in a single line, the least number of pouring attempts.
Sample Input
2 2 2 4
Sample Output
1 2
给你一个体积[L,R]之间的茶壶,要求倒茶到两个杯子里面,使得两个杯子体积绝对值<=1,且最后杯子剩下的茶体积<=1.
贪心,最开始在其中一个杯子里倒L/2+0.5,之后在另一个里面倒L/2+1.5,之后再每个杯子里面依次倒2.最后可以推出公式。有几种特殊情况,需要单独考虑。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int main() { ll l,r; while (scanf("%lld%lld",&l,&r)!=EOF) { if (l<=1&&r<=1) { printf("0\n");continue; } if ((l==0&&r==2)||(l==1&&r==2)||(l==2&&r==2)) { printf("1\n");continue; } if (l==0) { printf("%lld\n",(r+1)/2);continue; } if (r-l<=3) { printf("2\n");continue; } ll ans=(r-l-2)/2+2; printf("%lld\n",ans); } return 0; }
5882:
Balanced Game
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 907 Accepted Submission(s): 704
Problem Description
Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scissors". The game has only three possible outcomes
other than a tie: a player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors") but will lose to one who has played paper ("paper covers rock"); a play of paper will lose to a play of scissors ("scissors cut paper").
If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.
Recently, there is a upgraded edition of this game: rock-paper-scissors-Spock-lizard, in which there are totally five shapes. The rule is simple: scissors cuts paper; paper covers rock; rock crushes lizard; lizard poisons Spock; Spock smashes scissors; scissors
decapitates lizard; lizard eats paper; paper disproves Spock; Spock vaporizes rock; and as it always has, rock crushes scissors.
Both rock-paper-scissors and rock-paper-scissors-Spock-lizard are balanced games. Because there does not exist a strategy which is better than another. In other words, if one chooses shapes randomly, the possibility he or she wins is exactly 50% no
matter how the other one plays (if there is a tie, repeat this game until someone wins). Given an integer N,
representing the count of shapes in a game. You need to find out if there exist a rule to make this game balanced.
Input
The first line of input contains an integer t, the number of test cases. t test cases follow.
For each test case, there is only one line with an integer N (2≤N≤1000),
as described above.
Here is the sample explanation.
In the first case, donate two shapes as A and B. There are only two kind of rules: A defeats B, or B defeats A. Obviously, in both situation, one shapes is better than another. Consequently, this game is not balanced.
In the second case, donate two shapes as A, B and C. If A defeats B, B defeats C, and C defeats A, this game is balanced. This is also the same as rock-paper-scissors.
In the third case, it is easy to set a rule according to that of rock-paper-scissors-Spock-lizard.
Output
For each test cases, output "Balanced" if there exist a rule to make the game balanced, otherwise output "Bad".
Sample Input
3 2 3 5
Sample Output
Bad Balanced Balanced
Source
2016 ACM/ICPC Asia Regional Qingdao Online
找规律猜结论,找出来之后,一句话的事情。
#include <cstdio> using namespace std; int main() { int t,x; scanf("%d",&t); while(t--) { scanf("%d",&x); if (x%2==0) printf("Bad\n"); else printf("Balanced\n"); } }
5883:
The Best Path
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1761 Accepted Submission(s): 721
Problem Description
Alice is planning her travel route in a beautiful valley. In this valley, there are N lakes,
and M rivers
linking these lakes. Alice wants to start her trip from one lake, and enjoys the landscape by boat. That means she need to set up a path which go through every river exactly once. In addition, Alice has a specific number (a1,a2,...,an)
for each lake. If the path she finds is P0→P1→...→Pt,
the lucky number of this trip would be aP0XORaP1XOR...XORaPt.
She want to make this number as large as possible. Can you help her?
Input
The first line of input contains an integer t,
the number of test cases. t test
cases follow.
For each test case, in the first line there are two positive integers N (N≤100000) and M (M≤500000),
as described above. The i-th
line of the next N lines
contains an integer ai(∀i,0≤ai≤10000) representing
the number of the i-th
lake.
The i-th
line of the next M lines
contains two integers ui and vi representing
the i-th
river between the ui-th
lake and vi-th
lake. It is possible that ui=vi.
Output
For each test cases, output the largest lucky number. If it dose not have any path, output "Impossible".
Sample Input
2
3 2
3
4
5
1 2
2 3
4 3
1 23
4
1 2
2 3
2 4
Sample Output
2 Impossible
Source
2016 ACM/ICPC Asia Regional Qingdao Online
要求找一条欧拉路,使得经过的所有点的异或和最大。
利用欧拉路当中度数的关系解题。
要有欧拉路,度数为奇数的节点必须为0或2.对于度数为4*n的节点,经过偶数次,可以忽略,其他点的权值肯定必须被异或计算一次,计算这部分的异或和。对于度数为奇数的节点为2的情况,欧拉路的开头和结尾已经确定,异或和就是答案。否则,枚举起点,此时必定是个环,起点会被多走一次。
总的复杂度O(n)
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=100005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int d[maxn],head[maxn],a[maxn]; int num; int main() { int cas; scanf("%d",&cas); while (cas--) { int n,m,i,x,y,cnt=0; scanf("%d%d",&n,&m); num=0;memset(head,-1,sizeof(head)); mem0(d); for (i=1;i<=n;i++) scanf("%d",&a[i]); for (i=1;i<=m;i++) { scanf("%d%d",&x,&y); d[x]++;d[y]++; } for (i=1;i<=n;i++) { if (d[i]%2) cnt++; } if (cnt==0||cnt==2) { int sum=0; for (i=1;i<=n;i++) { d[i]=(d[i]+1)/2; if (d[i]%2) sum=sum^a[i]; } int ans=-1; if (cnt==0) { for (i=1;i<=n;i++) { ans=max(ans,sum^a[i]); } } else ans=sum; printf("%d\n",ans); } else printf("Impossible\n"); } return 0; }
5884:
Sort
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3415 Accepted Submission(s): 839
Problem Description
Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from Alice.
Alice will give Bob N sorted
sequences, and the i-th
sequence includes ai elements.
Bob need to merge all of these sequences. He can write a program, which can merge no more than k sequences
in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use no more than T cost.
So Bob wants to know the smallest k to
make the program complete in time.
Input
The first line of input contains an integer t0,
the number of test cases. t0 test
cases follow.
For each test case, the first line consists two integers N (2≤N≤100000) and T (∑Ni=1ai<T<231).
In the next line there are N integers a1,a2,a3,...,aN(∀i,0≤ai≤1000).
Output
For each test cases, output the smallest k.
Sample Input
1 5 25 1 2 3 4 5
Sample Output
3
Source
2016 ACM/ICPC Asia Regional Qingdao Online
n叉哈夫曼树,没想到。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=100005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); ll a[maxn]; ll k; int n; bool check(int mid) { int i; int tot=n,p=1; queue<ll> q; ll sum=0,f=0; if ((n-1)%(mid-1)!=0) { int l=(n-1)%(mid-1)+1; for (i=1;i<=l;i++) { f+=a[p++]; } q.push(f); tot-=l-1; sum+=f; } while (tot>1) { f=0; for (i=1;i<=mid;i++) { if (q.empty()) { f+=a[p++]; } else { if (q.front()>a[p]&&p<=n) f+=a[p++]; else f+=q.front(),q.pop(); } } q.push(f); sum+=f; tot-=mid-1; } if (sum>k) return false; return true; } int main() { int cas; scanf("%d",&cas); while (cas--) { int i; scanf("%d%lld",&n,&k); for (i=1;i<=n;i++) { scanf("%lld",&a[i]); } sort(a+1,a+n+1); int l=2,r=n,mid,ans=2; check(2); while (l<=r) { mid=(l+r)/2; if (check(mid)) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); } return 0; }
5885:
XM Reserves
Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 343 Accepted Submission(s): 141
Problem Description
As an eligible Ingress Resistance Agent you should know your power source, the Exotic Matter.
We call it XM, which is the driving force behind all of our actions in Ingress.
XM allows us to construct items through hacking portals, to attack enemy portals, make links and create fields.
We try to collect XM from the ground. XM concentration come from location based services, meaning that areas with a lot of foot traffic have higher amounts versus places that don't.
You can collect XM by moving through those areas.
The XM will be automatically harvested by your Scanner when it is within your interaction circle/range.
Alice decides to select a location such that she can collect XM as much as possible.
To simplify the problem, we consider the city as a grid map with size `n*m' numbered from (0,0) to (n−1,m−1).
The XM concentration inside the block (i,j) is p(i,j).
The radius of your interaction circle is r.
We can assume that XM of the block (i,j) are
located in the centre of this block.
The distance between two blocks is the Euclidean distance between their centres.
Alice stands in the centre of one block and collects the XM.
For each block with the distance d smaller than r to Alice, and whose XM concertation is p(i,j),
Alice's scanner can collects p(i,j)/(1+d) XM
from it.
Help Alice to determine the maximum XM which she can collect once he stands in the centre of one block.
Input
There are multiple cases.
For each case, the first line consists two integers n,m (1≤n,m≤500) and
one float-point number r (0≤r≤300).
Each of the following n line
consists m non-negative
float-point numbers corresponding to the XM concentrations inside each blocks.
Output
For each case, output the maximum XM which Alice can collect in one line.
Your answers should be rounded to three decimal places.
Sample Input
3 3 1 1 3 6 7 9 4 2 8 1 3 3 2 1 3 6 7 9 4 2 8 1 5 5 1.5 4 3 2 9 1 3 4 3 2 8 9 4 3 2 1 2 3 0 1 2 6 3 4 3 1
Sample Output
9.000 24.142 17.956
Source
2016 ACM/ICPC Asia Regional Qingdao Online
待看题
5886:
Tower Defence
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 562 Accepted Submission(s): 194
Problem Description
There was a civil war between two factions in Skyrim, a province of the Empire on the continent of Tamriel. The Stormcloaks, led by Ulfric Stormcloak, are made up of Skyrim's native Nord race. Their goal is an independent Skyrim free from Imperial interference.
The Imperial Legion, led by General Tullius, is the military of the Empire that opposes the Stormcloaks and seeks to reunite and pacify the province.
The current target of General Tullius is to defend Whiterun City. Near by this city there are N towers
under the Empire's control. There are N−1 roads
link these tower, so solders can move from any tower to another one through these roads.
In military affairs, tactical depth means the longest path between two towers of all. Larger the tactical depth is, more stable these towers are.
According to the message sent by spies, General Tullius believe that Stormcloaks is planning to attack one of these roads, and his towers would be divided into two parts. However, Tullius does not know which one, so he supposes the possibility that Stormcloaks
attack these roads are the same. Now, General Tullius ask for your help, to calculate the expectation of tactical depth after this attack.
To avoid the issue of precision, you need to calculate expectationoftacticaldepth×(N−1).
Input
The first line of input contains an integer t,
the number of test cases. t test
cases follow.
For each test case, in the first line there is an integer N(N≤100000).
The i-th
line of the next N−1 lines
describes the i-th
edge. Three integers u,v,w (0≤w≤1000) describe
an edge between u and v of
length w.
Output
For each test cases, output expectationoftacticaldepth×(N−1).
Sample Input
2 3 2 1 2 3 2 5 5 2 1 7 3 1 7 4 2 5 5 2 6
Sample Output
7 63
Source
2016 ACM/ICPC Asia Regional Qingdao Online
要求计算断掉树的任意一条边之后,森林中的最长路径。
树型DP。计算出每个点到子树当中节点的最长路、次长路和到上方节点的最长路,由此再次预处理每个点上方的树当中的最长路。每次断开一条边之后,最长路就是下方节点子树当中的最长路或是上方节点的最长路。
有点烦,不过思路很好想。
懒得打了。
5887:
Herbs Gathering
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1506 Accepted Submission(s): 379
Problem Description
Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them,
how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty
to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal
is to maximize the sum of scores for herbs which we can get within a limited time.
Input
There are at most ten test cases.
For each case, the first line consists two integers, the total number of different herbs and the time limit.
The i-th
line of the following n line
consists two non-negative integers. The first one is the time we need to gather and prepare the i-th
herb, and the second one is its score.
The total number of different herbs should be no more than 100.
All of the other numbers read in are uniform random and should not be more than 109.
Output
For each test case, output an integer as the maximum sum of scores.
Sample Input
3 70 71 100 69 1 1 2
Sample Output
3
Source
2016 ACM/ICPC Asia Regional Qingdao Online
01背包,但容量很大,最大为1e9.
搜索加个剪枝,先选性价比大的。
#include <cstdio> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; typedef long long ll; typedef double db; const int maxn=105; struct Thang{ ll v,w; db ratio; }; Thang a[maxn]; ll sum[maxn]; db ratio[maxn]; ll ans,size,m; bool cmp (Thang x,Thang y) { return x.ratio>y.ratio; } void dfs(int now,ll val,ll weight) { ans=max(ans,val); if (now==m) return; if (val+ceil(a[now+1].ratio*weight)<=ans) return; if (val+sum[now+1]<=ans) return; if (a[now+1].w<=weight) dfs(now+1,val+a[now+1].v,weight-a[now+1].w); dfs(now+1,val,weight); } int main() { int i,j,n; while (scanf("%d%lld",&n,&size)!=EOF) { ll b,c; m=0; for (i=1;i<=n;i++) { scanf("%lld%lld",&b,&c); // b=i;c=i*i; if (b<=size) { a[++m].w=b; a[m].v=c; a[m].ratio=(db)a[m].v/(db)a[m].w; } } sort(a+1,a+m+1,cmp); sum[m+1]=0; for (i=m;i>=1;i--) { sum[i]=sum[i+1]+a[i].v; } ans=0; for (i=1;i<=m;i++) { if (sum[i]>ans) dfs(i,a[i].v,size-a[i].w); } printf("%lld\n",ans); } return 0; }
5889:
Barricade
Time Limit: 3000/1000 MS (Java/Others) MemoryLimit: 65536/65536 K (Java/Others)
Total Submission(s): 1699 Accepted Submission(s): 501
Problem Description
The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as N towns
and M roads,
and each road has the same length and connects two towns. The town numbered 1 is
where general's castle is located, and the town numbered N is
where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he
asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the i-th
road requires wi units
of wood. Because of lacking resources, you need to use as less wood as possible.
Input
The first line of input contains an integer t,
then t test
cases follow.
For each test case, in the first line there are two integers N(N≤1000) and M(M≤10000).
The i-the
line of the next M lines
describes the i-th
edge with three integers u,v and w where 0≤w≤1000 denoting
an edge between u and v of
barricade cost w.
Output
For each test cases, output the minimum wood cost.
Sample Input
1 4 4 1 2 1 2 4 2 3 1 3 4 3 4
Sample Output
4
Source
2016
ACM/ICPC Asia Regional Qingdao Online
一个无向图,给你n个点m条边,每条边的长度相同。让你将图上的最短路封住。封住每条路有一个代价,求最小代价。
先bfs一遍求最短路,标记使用过的路径之后,直接上最小割。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; const int maxn=1005,maxk=10005,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int head[maxn],step[maxn],x[maxk],y[maxk],d[maxk]; int dist[maxn],current[maxn]; bool visit[maxn],mark[maxk*2]; int num; struct Edge { int from,to,flow,pre; }; Edge edge[maxk*2]; void addedge(int from,int to,int flow) { edge[num]=(Edge){from,to,flow,head[from]}; head[from]=num++; edge[num]=(Edge){to,from,0,head[to]}; head[to]=num++; } bool bfs (int n) { queue<int> q; q.push(1); memset(dist,-1,sizeof(dist)); dist[1]=0; while (!q.empty()) { int now=q.front(); q.pop(); for (int i=head[now];i!=-1;i=edge[i].pre) { int to=edge[i].to; if (dist[to]==-1&&edge[i].flow>0) { dist[to]=dist[now]+1; q.push(to); } } } return dist !=-1; } int dfs(int now,int flow,int n) { int f; if (now==n) return flow; for (int i=current[now];i!=-1;i=edge[i].pre) { int to=edge[i].to; current[now]=i; if (dist[now]+1==dist[to]&&edge[i].flow>0&& (f=dfs(to,min(flow,edge[i].flow),n))) { edge[i].flow-=f; edge[i^1].flow+=f; return f; } } return 0; } int dinic(int n) { int sum=0,f; while (bfs(n)) { memcpy(current,head,sizeof(head)); while (f=dfs(1,inf,n)) sum+=f; } return sum; } int main() { int cas; scanf("%d",&cas); while (cas--) { int n,m,i,j; num=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for (i=1;i<=m;i++) { scanf("%d%d%d",&x[i],&y[i],&d[i]); addedge(x[i],y[i],d[i]); } mem0(visit); queue<int> q; q.push(1); step[1]=0;visit[1]=1; while (!q.empty()) { int now=q.front(); q.pop(); for (i=head[now];i!=-1;i=edge[i].pre) { int to=edge[i].to; if (!visit[to]) { visit[to]=1; step[to]=step[now]+1; q.push(to); } } } mem0(mark); for (i=1;i<=n;i++) { for (j=head[i];j!=-1;j=edge[j].pre) { int to=edge[j].to,from=edge[j].from; if (step[to]==step[from]+1) mark[j]=1; } } num=0;memset(head,-1,sizeof(head)); for (i=0;i<2*m;i+=2) { if (mark[i]) addedge(x[i/2+1],y[i/2+1],d[i/2+1]); if (mark[i+1]) addedge(y[i/2+1],x[i/2+1],d[i/2+1]); } int ans=dinic(n); printf("%d\n",ans); } return 0; }
5890:
Eighty seven
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 1391 Accepted Submission(s): 469
Problem Description
Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare Ncards
with numbers. The number on the i-th
card is ai.
In class, each turn he will remove no more than 3 cards
and let students choose any ten cards, the sum of the numbers on which is 87.
After each turn the removed cards will be put back to their position. Now, he wants to know if there is at least one solution of each turn. Can you help him?
Input
The first line of input contains an integer t (t≤5),
the number of test cases. t test
cases follow.
For each test case, the first line consists an integer N(N≤50).
The second line contains N non-negative
integers a1,a2,...,aN.
The i-th
number represents the number on the i-th
card. The third line consists an integer Q(Q≤100000).
Each line of the next Q lines
contains three integers i,j,k,
representing Mr.Fib will remove the i-th, j-th,
and k-th
cards in this turn. A question may degenerate while i=j, i=k or j=k.
Output
For each turn of each case, output 'Yes' if there exists at least one solution, otherwise output 'No'.
Sample Input
1 12 1 2 3 4 5 6 7 8 9 42 21 22 10 1 2 3 3 4 5 2 3 2 10 10 10 10 11 11 10 1 1 1 2 10 1 11 12 1 10 10 11 11 12
Sample Output
No No No Yes No Yes No No Yes Yes
Source
2016 ACM/ICPC Asia Regional Qingdao Online
要求在n个数当中去除1~3个数,问剩下的数选十个能否组成87.
状压优化的二维01背包。
#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map> #include <queue> #include <deque> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <cmath> #include <stack> #include <iomanip> #define mem0(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,0x3f,sizeof(a)) using namespace std; typedef long long ll; typedef long double ld; typedef double db; const int maxn=55,inf=0x3f3f3f3f; const ll llinf=0x3f3f3f3f3f3f3f3f; const ld pi=acos(-1.0L); int a[maxn]; ll dp[105]; bool ans[51][51][51]; int main() { int cas; scanf("%d",&cas); while (cas--) { int n,m=0,x,i,j,k,I,J; scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&a[i]); } mem0(ans); for (i=1;i<=n;i++) { for (j=i;j<=n;j++) { for (k=j;k<=n;k++) { mem0(dp); for (I=1;I<=n;I++) { if (I==i||I==j||I==k) continue; for (J=87;J>=a[I];J--) { dp[J]=dp[J]|(dp[J-a[I]]<<1); } dp[a[I]]=dp[a[I]]|1; } if ((dp[87]>>9)%2==1) ans[i][j][k]=1; } } } int y,z,q; scanf("%d",&q); for (i=1;i<=q;i++) { scanf("%d%d%d",&x,&y,&z); if (y<x) swap(x,y); if (z<x) swap(z,x); if (z<y) swap(z,y); if (ans[x][y][z]) printf("Yes\n"); else printf("No\n"); } } return 0; }
5891:不会,也查不到题解。。。
相关文章推荐
- HDU 5878 I Count Two Three (暴力) 2016 ACM/ICPC Asia Regional Qingdao Online
- 2016 ACM/ICPC Asia Regional Qingdao Online hdu 5889 Barricade (最短路+最小割)
- HDU 5879 Cure(技巧)——2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5882 Balanced Game 2016 ACM/ICPC Asia Regional Qingdao Online 1005
- 2016 ACM/ICPC Asia Regional Qingdao Online hdu 5881 Tea (模拟构造)★
- 2016 ACM/ICPC Asia Regional Qingdao Online 1005 Balanced Game
- HDU 5881 Tea (水题) 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5883 The Best Path(2016 ACM/ICPC Asia Regional Qingdao Online)
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001
- 2016 ACM/ICPC Asia Regional Qingdao Online 1002 Cure
- 【2016 ACM/ICPC Asia Regional Qingdao Online】
- hdu--5878(hdu 5878 I Count Two Three (2016 ACM/ICPC Asia Regional Qingdao Online 1001))
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分
- HDU 5889 Barricade (最小割) 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5883 The Best Path 2016 ACM/ICPC Asia Regional Qingdao Online 1006d
- 2016 ACM/ICPC Asia Regional Qingdao Online比赛笔记
- HDU 5883 The Best Path (欧拉通路) 2016 ACM/ICPC Asia Regional Qingdao Online
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
- HDU5878 Cure 2016 ACM/ICPC Asia Regional Qingdao Online
- 2016 ACM/ICPC Asia Regional Qingdao Online 1001 I Count Two Three(打表+二分搜索)