T1 远征
题目
【题目描述】
寒枫将军将要带领他的部队去圣雪山消灭那里的冰龙。部队分成了若干个小队,属于同一个小队的人兵种相同。
寒枫将军有着杰出的指挥能力,在战斗的时候,寒枫将军能够让所有相同兵种的人互相配合,使t个相同兵种的人发挥出t2的战斗力;
寒枫将军还能让不同兵种的人互相配合,使整个部队的战斗力是所有兵种战斗力的和。
例如,部队中有3个小队,分别是5个人的步兵小队,3个人的步兵小队,3个人的骑兵小队。那么步兵战斗力为64,骑兵战斗力为9,部队总战斗力为73。
寒枫将军需要知道他的部队的战斗力是多少。
【输入格式】
第一行一个整数n,表示小队数。接下来n行,第i行有两个整数ai、bi,表示这个小队有ai个人,兵种为bi。
【输出格式】
一行一个整数,部队的战斗力。
【输入样例】
3
5 1
3 1
3 2
【输出样例】
73
【数据规模】
10%的数据,n=1
30%的数据,n≤1000
另有20%的数据,ai=1
另有30%的数据,bi≤1000
100%的数据,1≤n≤100000,1≤ai≤10000,1≤bi≤1,000,000,000
解析
T1依旧水,蒟蒻仍AC。
将a与b用结构体存储,再按b从小到大排序,如果i的b等于i-1的b,那就是相同兵种,令t累加上a,
如果不相等,那就是不同兵种,统计一下答案并把t清零,最后不要忘了再统计一次答案。
Code
#include <algorithm> #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> using namespace std; int read() { int num=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { num=(num<<1)+(num<<3)+ch-'0'; ch=getchar(); } return num*w; } struct rec{ int a,b; }ra[100100]; int n; long long ans,t; bool cmp(rec x,rec y) { return x.b<y.b; } int main() { //freopen("expedition.in","r",stdin); //freopen("expedition.out","w",stdout); n=read(); for(int i=1;i<=n;i++) ra[i].a=read(),ra[i].b=read(); sort(ra+1,ra+n+1,cmp); t=ra[1].a; for(int i=2;i<=n;i++) { if(ra[i].b==ra[i-1].b) t+=ra[i].a; else { ans+=t*t; t=ra[i].a; } } ans+=t*t; cout<<ans; return 0; //fclose(stdin); //fclose(stdout); }