思路:暴力
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+5;
int INF=0x3f3f3f3f;
int a[N];
vector <int> vec;
int main(void){
int n;
cin >> n;
int mmin=INF;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
mmin=min(mmin,a[i]);
}
int index;
for(int i=1;i<=n;i++)
if(a[i]==mmin) vec.push_back(i);
int minn=vec[1]-vec[0];
for(int i=2;i<vec.size();i++) minn=min(minn,vec[i]-vec[i-1]);
cout << minn << endl;
}
It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has bought two cakes and cut them into pieces: the first cake has been cut into a pieces, and the second one — into b pieces.
Ivan knows that there will be n people at the celebration (including himself), so Ivan has set n plates for the cakes. Now he is thinking about how to distribute the cakes between the plates. Ivan wants to do it in such a way that all following conditions are met:
- Each piece of each cake is put on some plate;
- Each plate contains at least one piece of cake;
- No plate contains pieces of both cakes.
To make his guests happy, Ivan wants to distribute the cakes in such a way that the minimum number of pieces on the plate is maximized. Formally, Ivan wants to know the maximum possible number x such that he can distribute the cakes according to the aforementioned conditions, and each plate will contain at least x pieces of cake.
Help Ivan to calculate this number x!
The first line contains three integers n, a and b (1 ≤ a, b ≤ 100, 2 ≤ n ≤ a + b) — the number of plates, the number of pieces of the first cake, and the number of pieces of the second cake, respectively.
Print the maximum possible number x such that Ivan can distribute the cake in such a way that each plate will contain at least x pieces of cake.
5 2 3
1
4 7 10
3
In the first example there is only one way to distribute cakes to plates, all of them will have 1 cake on it.
In the second example you can have two plates with 3 and 4 pieces of the first cake and two plates both with 5 pieces of the second cake. Minimal number of pieces is 3.
思路:暴力枚举所有方案数
#include <bits/stdc++.h>
using namespace std;
int t[300];
vector <int> res;
int main(void){
int n,a,b;
cin >> n>> a>> b;
int ans=-0x3f3f3f3f;
for(int i=1;i<=n-1;i++){
int t1=a/i;
int t2=b/(n-i);
res.push_back(min(t1,t2));
}
for(int i=0;i<res.size();i++) ans=max(ans,res[i]);
cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
int cnt1,cnt2,cnt3,cnt4;
int main(void){
int a[5];
cin >>a[1]>>a[2]>>a[3];
sort(a+1,a+4);
bool flag=false;
if(a[1]==1) flag=true;
else if(a[1]==2&& (a[2]==2||a[3]==2)) flag=true;
else if(a[1]==3&&a[2]==3&&a[3]==3) flag=true;
if(a[1]==2&&a[2]==4&&a[3]==4) flag=true;
if(flag) cout <<"YES"<<endl;
else cout <<"NO"<<endl;
return 0;
}
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.
Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and evenotherwise.
3 1 2 3 2 1 2 2 3
odd even
4 1 2 4 3 4 1 1 1 4 1 4 2 3
odd odd odd even
The first example:
- after the first query a = [2, 1, 3], inversion: (2, 1);
- after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
题意:有n个数字,问把[l,r]反过来后,逆序数是奇数还是偶数
思路:可以求出原始序列的逆序数个数cnt。当部分序列被倒置后,仅影响该序列的顺序和逆序个数。长度为n的序列, tot=顺序+逆序=(n-1)*n/2,那么令n=r-l+1。对于总答案的贡献值为cnt(顺序)-cnt(逆序) 如果tot是偶数,说明顺序个数与逆序奇偶性相同,不影响原来的奇偶性。如果是奇数,那么相反。
考察:n*(n-1)/2个逆序数对=顺序数+逆序数。 对答案贡献为cnt(顺序)-cnt(逆序) 。 根据tot来决定奇偶性的变化
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2000];
ll cnt;
int main(void){
int n,m;
cin >>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
//for(int i=1;i<=n;i++) printf("%d",a[i]);
for(int i=2;i<=n;i++){
for(int j=1;j<i;j++){
if(a[j]>a[i]) cnt++;
}
}
//cout <<"cnt="<<cnt<<endl;
cin >>m;
for(int i=1;i<=m;i++){
int l,r;
scanf("%d%d",&l,&r);
ll t=1LL*(r-l+1)*(r-l)/2;
if(t%2==1){
if(cnt%2==1) cout<<"even"<<endl;
else cout<<"odd"<<endl;
cnt++;
}
else{
if(cnt%2==1) cout<<"odd"<<endl;
else cout<<"even"<<endl;
}
}
}
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty).
You may perform the following operations until both a and s are empty:
- Take the first element of a, push it into s and remove it from a (if a is not empty);
- Take the top element from s, append it to the end of array b and remove it from s (if s is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array b is sorted in non-descending order in the end, then array a is called stack-sortable.
For example, [3, 1, 2] is stack-sortable, because b will be sorted if we perform the following operations:
- Remove 3 from a and push it into s;
- Remove 1 from a and push it into s;
- Remove 1 from s and append it to the end of b;
- Remove 2 from a and push it into s;
- Remove 2 from s and append it to the end of b;
- Remove 3 from s and append it to the end of b.
After all these operations b = [1, 2, 3], so [3, 1, 2] is stack-sortable. [2, 3, 1] is not stack-sortable.
You are given k first elements of some permutation p of size n (recall that a permutation of size n is an array of size n where each integer from 1 to n occurs exactly once). You have to restore the remaining n - k elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that p is lexicographically maximal (an array q is lexicographically greater than an array p iff there exists some integer k such that for every i < k qi = pi, and qk > pk). You may not swap or change any of first kelements of the permutation.
Print the lexicographically maximal permutation p you can obtain.
If there exists no answer then output -1.
The first line contains two integers n and k (2 ≤ n ≤ 200000, 1 ≤ k < n) — the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the first k elements of p. These integers are pairwise distinct.
If it is possible to restore a stack-sortable permutation p of size n such that the first k elements of p are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1.
5 3 3 2 1
3 2 1 5 4
5 3 2 3 1
-1
5 1 3
3 2 1 5 4
5 2 3 4
-1
思路:对于当前的a[]i,有3种情况
1.是需要的数
2.不是需要的数,但比栈中所有元素都要小。 如果比任何一个大,肯定不满足条件,输出-1
若满足,输出前k个元素
栈中可能有残留的元素,那么输出有约束条件的数字(比上一个元素小,比下一个栈元素大)
再逆序从最大输出。但这题被Stack 给TLE了,手动模拟就A了,不清楚原因。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000007;
#define bug1 cout <<"bug1"<<endl
#define bug2 cout <<"bug2"<<endl
#define bug3 cout <<"bug3"<<endl
using namespace std;
typedef long long ll;
const int MAX_N=2e5+5;
int a[MAX_N];
int used[MAX_N];
int s[MAX_N];
int main(void){
int n,k;
cin >> n>>k;
int b=1,top=0,flag=1;
for(int i=1;i<=k;i++) scanf("%d",&a[i]);
for(int i=1;i<=k;i++){
if(a[i]==b) used[a[i]]=1,b++;
else if(top==0||a[i]<s[top-1]) used[a[i]]=1,s[top++]=a[i];
else{
flag=0;
break;
}
while(top&&s[top-1]==b) b++,top--;
}
if(!flag) cout<<"-1"<<endl;
else{
for(int i=1;i<=k;i++) printf("%d ",a[i]);
if(top){
int t=0;
while(top){
for(int i=s[top-1]-1;i>t;i--){
if(!used[i]){
used[i]=1;
printf("%d ",i);
}
}
t=s[top-1];
top--;
}
}
for(int i=n;i>0;i--){
if(!used[i]) printf("%d ",i);
}
}
}