水题场。。。
Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.
Before the game Petya will choose an integer and after that Vasya will choose another integer (differentfrom the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.
The game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.
Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair.
The first line contains a single integer n (2 ≤ n ≤ 100) — number of cards. It is guaranteed that n is an even number.
The following n lines contain a sequence of integers a1, a2, ..., an (one integer per line, 1 ≤ ai ≤ 100) — numbers written on the n cards.
If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print "NO" (without quotes) in the first line. In this case you should not print anything more.
In the other case print "YES" (without quotes) in the first line. In the second line print two distinct integers — number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.
4 11 27 27 11
YES 11 27
2 6 6
NO
6 10 20 30 20 10 20
NO
6 1 1 2 2 3 3
NO
In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards — Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.
In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.
In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards — for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards.
A,水
#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>
#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=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn];
int main() {
int n,i,x;
cin >> n;
int k,l;
mem0(a);
for (i=1;i<=n;i++) {
cin >> x;
a[x]++;
}
k=l=-1;
int sum=0;
for (i=1;i<=100;i++) {
if (a[i]) {
sum++;
if (k==-1) k=i; else l=i;
}
}
if (sum==2) {
if (a[k]==a[l]) cout << "YES" << "\n" << k << ' ' << l; else cout << "NO";
} else cout << "NO";
return 0;
}
B,水
#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>
#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=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn];
int p[129];
int main() {
int len,ans,i,cnt;
scanf("%d",&len);
scanf("%s",s+1);
ans=cnt=0;
for (i=1;i<=len;i++) {
if (s[i]>='A'&&s[i]<='Z') {
cnt=0;
mem0(p);
} else {
if (p[s[i]]==0) {
p[s[i]]=1;cnt++;
}
}
ans=max(ans,cnt);
}
cout << ans;
return 0;
}
一个汽车在数轴上来回从0到a走k次,一个来回算两次。油箱有b升油,一升开一公里,加油站位置在f,求最少的充油次数。
数学题,求出每次加油之后最多能经过加油站 f 多少次,来回k次则最多加k次油,模拟K次即可判断。
一定要特判 k=1,2,3 !!!!!!
#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>
#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=20005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
ll a,b,f,k;
ll t[maxn];
int main() {
cin >> a >> b >> f >> k;
ll l,r,mid,p=-1,e;
int i;
l=1;r=1e9;
while (l<=r) {
mid=(l+r)/2;
if (mid%2==1) {
if ((mid-1)*a+f<=b) p=mid,l=mid+1; else r=mid-1;
} else {
if (mid*a-f<=b) p=mid,l=mid+1; else r=mid-1;
}
}
if (b>=a*k) {
cout << 0;
return 0;
}
if (b<f||b<a-f||(b<2*(a-f)&&k>=2)||(b<2*f&&k>=3)) {
cout << -1;
return 0;
}
l=1;r=1e9;e=0;
while (l<=r) {
mid=(l+r)/2;
if (mid*a*2<=b) e=mid,l=mid+1; else r=mid-1;
}
t[1]=p;
if (t[1]>=k) {
cout << 1; return 0;
}
if (t[1]%2==1) {
if ((k-t[1])*a+a-f<=b) {
cout << 1; return 0;
}
} else {
if ((k-t[1])*a+f<=b) {
cout << 1; return 0;
}
}
for (i=2;i<=k;i++) {
if (t[i-1]%2==1) {
if (e*a*2+2*(a-f)<=b) t[i]=t[i-1]+e*2+1; else t[i]=t[i-1]+e*2;
} else {
if (e*a*2+2*f<=b) t[i]=t[i-1]+e*2+1; else t[i]=t[i-1]+e*2;
}
if (t[i]>=k) {
cout << i;return 0;
}
if (t[i]%2==1) {
if ((k-t[i])*a+a-f<=b) {
cout << i; return 0;
}
} else {
if ((k-t[i])*a+f<=b) {
cout << i; return 0;
}
}
}
cout << -1;
return 0;
}
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation(i.e. each of the integers from 1 to n was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations x and y, then x is lexicographically smaller if xi < yi, where i is the first index in which the permutations x and y differ.
Determine the array Ivan will obtain after performing all the changes.
The first line contains an single integer n (2 ≤ n ≤ 200 000) — the number of elements in Ivan's array.
The second line contains a sequence of integers a1, a2, ..., an (1 ≤ ai ≤ n) — the description of Ivan's array.
In the first line print q — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
4 3 2 2 3
2 1 2 4 3
6 4 5 6 3 2 1
0 4 5 6 3 2 1
10 6 8 4 6 7 1 6 3 4 5
3 2 8 4 6 7 1 9 3 10 5
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation.
D题,让你把一个长度为N的序列在最少的次数之内修改成n的全排列,在修改最少的前提下,输出字典序最小的解法。
很明显的一个贪心,出现一次的数字肯定不能改,我们只要把出现多次的改成没有出现过的。
对于这些位置,如果当前没有用过的最小的数字比当前数字小,或者当前数字已经被保留了至少一次,则替换掉,否则不替换。
若某个数字一共出现过n次,而之前n-1次出现都被替换,则最后一次必须不被替换。
复杂度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>
#include <deque>
#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=200005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn],cnt[maxn],ch[maxn],st[maxn];
int main() {
int n,i,s,l;
s=0;
scanf("%d",&n);
mem0(cnt);
for (i=1;i<=n;i++) {
scanf("%d",&a[i]);
if (cnt[a[i]]!=0) s++;
cnt[a[i]]++;
ch[i]=st[i]=0;
}
printf("%d\n",s);
queue<int> q;
for (i=1;i<=n;i++) {
if (cnt[i]==0) q.push(i);
if (cnt[i]>1) ch[i]=1;
}
for (i=1;i<=n;i++) {
if (ch[a[i]]) {
cnt[a[i]]--;
if (cnt[a[i]]==0&&!st[a[i]]) continue;
if (a[i]>q.front()||st[a[i]]) {
a[i]=q.front();
q.pop();
} else {
st[a[i]]=1;
}
}
}
for (i=1;i<=n;i++) {
printf("%d ",a[i]);
}
return 0;
}
Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take ti seconds to save i-th item. In addition, for each item, he estimated the value of di — the moment after which the item i will be completely burned and will no longer be valuable for him at all. In particular, if ti ≥ di, then i-th item cannot be saved.
Given the values pi for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item a first, and then item b, then the item a will be saved in ta seconds, and the item b — in ta + tbseconds after fire started.
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of items in Polycarp's house.
Each of the following n lines contains three integers ti, di, pi (1 ≤ ti ≤ 20, 1 ≤ di ≤ 2 000, 1 ≤ pi ≤ 20) — the time needed to save the item i, the time after which the item i will burn completely and the value of item i.
In the first line print the maximum possible total value of the set of saved items. In the second line print one integer m — the number of items in the desired set. In the third line print m distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.
3 3 7 4 2 6 5 3 7 6
11 2 2 3
2 5 6 1 3 3 5
1 1 1
In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11.
In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.
E题让你求个01背包并输出路径顺序,状态压缩即可。详见 这里
There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.
A path from city s to city t is a sequence of cities p1, p2, ... , pk, where p1 = s, pk = t, and there is a road from city pi to city pi + 1 for each i from 1 to k - 1. The path can pass multiple times through each city except t. It can't pass through t more than once.
A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path q from s to t pi < qi, where i is the minimum integer such that pi ≠ qi.
There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city sjand ends in city tj.
For each pair sj, tj help the agency to study the ideal path from sj to tj. Note that it is possible that there is no ideal path from sj to tj. This is possible due to two reasons:
- there is no path from sj to tj;
- there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.
The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sjto tj).
For each triple sj, tj, kj (1 ≤ j ≤ q) find if there is an ideal path from sj to tj and print the kj-th city in that path, if there is any.
The first line contains three integers n, m and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 3000, 1 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.
Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), denoting that the i-th road goes from city xi to city yi. All roads are one-directional. There can't be more than one road in each direction between two cities.
Each of the next q lines contains three integers sj, tj and kj (1 ≤ sj, tj ≤ n, sj ≠ tj, 1 ≤ kj ≤ 3000).
In the j-th line print the city that is the kj-th in the ideal path from sj to tj. If there is no ideal path from sj to tj, or the integer kj is greater than the length of this path, print the string '-1' (without quotes) in the j-th line.
7 7 5 1 2 2 3 1 3 3 4 4 5 5 3 4 6 1 4 2 2 6 1 1 7 3 1 3 2 1 3 5
2 -1 -1 2 -1
对tarjan进行改造。详见 这里
官方题解用了一个叫做outgoing tree的东西,有时间去学学。