洛谷刷题5道

P1908

#include 
using namespace std;
int tree[500010],ranks[500010],n;
long long ans; 
struct point
{
    int num,val;
}a[500010];
inline bool cmp(point q,point w)
{
    if(q.val==w.val)
        return q.num

P1449

#include
#include
using namespace std;
int main()
{
    char t;
    stack s;
    while(cin>>t&&t!='@')
    {
        if(t>='0'&&t<='9')
        {
            int temp=t-'0';
            while(cin>>t&&t>='0'&&t<='9')
                temp=temp*10+t-'0';
            s.push(temp);
        }
        if(t=='+')
        {
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b+a);
        }
        if(t=='-')
        {
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b-a);
        }
        if(t=='/')
        {
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b/a);
        }
        if(t=='*')
        {
            int a=s.top();
            s.pop();
            int b=s.top();
            s.pop();
            s.push(b*a);
        }
    }
    cout<

P1030

#include
#include
#include
using namespace std;
void beford(string in,string after){
    if (in.size()>0){
        char ch=after[after.size()-1];
        cout<>inord;cin>>aftord;//读入
    beford(inord,aftord);cout<

P1271

#include
using namespace std;
int n;
int a[2000005];
int main(){
	cin>>n;
	int m;
	cin>>m;
	for(int i=1;i<=m;i++)
		cin>>a[i];
	sort(a+1,a+m+1);//排序
	for(int i=1;i<=m;i++)
		cout<

P1966

#include
#include
using namespace std;
const int maxn = 100010;
const int maxm = 99999997;
struct MyStruct
{
    int data;
    int loc;
}a[maxn],b[maxn];
int e[maxn], n, c[maxn];
int inline readint()
{
    int x = 0;
    char c = getchar();
    while (c<'0' || c>'9') c = getchar();
    while (c >= '0'&&c <= '9')
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}
int lowbit(int x)
{
    return x&-x;//树状数组实现 
}
void add(int x,int t)
{
    while (x <= n)
    {
        e[x] += t;
        e[x] %= maxm;
        x += lowbit(x);//每次往后加,可以改变后面对应的和 
    }
}
int sum(int x)
{
    int s = 0;
    while(x)
    {
        s += e[x];
        s %= maxm;
        x -= lowbit(x);//得到所求的和 
    }
    return s;
}
bool cmp(MyStruct x, MyStruct y)
{
    return x.data < y.data;
}
int main()
{
    n = readint();
    for (int i = 1; i <= n; i++)
    {
        a[i].data = readint();
        a[i].loc = i;//记录位置 
    }
    for (int i = 1; i <= n; i++)
    {
        b[i].data = readint();
        b[i].loc = i;
    }
    sort(a + 1, a + n + 1, cmp);
    sort(b + 1, b + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        c[a[i].loc] = b[i].loc;//离散优化 
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        add(c[i], 1);//离散优化后大小就是正确顺序的位置 
        ans += i - sum(c[i]);//当前位置,减去之前比他大的数的个数  
        ans %= maxm;
    }
    printf("%d", ans);
    return 0;
}

希望这些对大家有用,三连必回

你可能感兴趣的:(算法)